查阅网上很多资料,最后还是解决了,网上的远程基本是局域网内操作
我的项目是springboot 项目,其他项目也是类似,
贴出自动配置
@Configuration
public class ConfigureRMI {
//这里我配置外网ip会报错,所以这里先配置127,启动就带参启动解决这个问题
private String rmiHost="127.0.0.1";
private Integer rmiPort=28080;
@Bean
public RmiRegistryFactoryBean rmiRegistry() {
final RmiRegistryFactoryBean rmiRegistryFactoryBean = new RmiRegistryFactoryBean();
rmiRegistryFactoryBean.setPort(rmiPort);
rmiRegistryFactoryBean.setAlwaysCreate(true);
return rmiRegistryFactoryBean;
}
@Bean
@DependsOn("rmiRegistry")
public ConnectorServerFactoryBean connectorServerFactoryBean() throws Exception {
final ConnectorServerFactoryBean connectorServerFactoryBean = new ConnectorServerFactoryBean();
connectorServerFactoryBean.setObjectName("connector:name=rmi");
connectorServerFactoryBean.setServiceUrl(String.format("service:jmx:rmi://%s:%s/jndi/rmi://%s:%s/jmxrmi", rmiHost, rmiPort, rmiHost, rmiPort));
return connectorServerFactoryBean;
}
}
然后就是掏出你的命令运行
java -jar -Djava.rmi.server.hostname=39.106.xx.xx spider-0.0.1-SNAPSHOT.jar
#注意这个hostname 必须是外网IP