服务器有两个网卡,一个内网,一个外网
Spring xml配置如下:
<context:property-placeholder location="classpath:/config/app.properties" /> <!-- 客户端RMI服务 --> <bean id="appPush" class="cn.com.xxx.ebusiness.messagePush.service.server.support.RMIServiceImpl"/> <bean id="appPushMsg2Client" class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="service" ref="appPush" /> <!-- 定义服务名 --> <property name="serviceName" value="appPushMsg2Client" /> <property name="serviceInterface" value="cn.com.xxx.ebusiness.messagePush.service.server.RMIService" /> <property name="registryPort" value="${ServicePort}" /> <property name="registryHost" value="${ServiceRegistryIp}" /> </bean>
在app.properties强制spring发布rmi服务注册IP为内网的IP(10.120.20.197),结果发布的时候出现下面异常:
2014-04-11 16:50:05 INFO ClassPathXmlApplicationContext:456 Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6aaea944: startup date [Fri Apr 11 16:50:05 CST 2014]; root of context hierarchy 2014-04-11 16:50:05 INFO XmlBeanDefinitionReader:315 Loading XML bean definitions from class path resource [config/spring/applicationContext-messagePush.xml] 2014-04-11 16:50:06 INFO PropertyPlaceholderConfigurer:177 Loading properties file from class path resource [config/app.properties] 2014-04-11 16:50:06 INFO DefaultListableBeanFactory:555 Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@44b89233: defining beans [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,org.springframework.aop.config.internalAutoProxyCreator,appPush,appPushMsg2Client]; root of factory hierarchy 2014-04-11 16:50:06 INFO RmiServiceExporter:328 Looking for RMI registry at port '1099' of host [10.120.20.197] 2014-04-11 16:50:07 INFO DefaultListableBeanFactory:422 Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@44b89233: defining beans [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,org.springframework.aop.config.internalAutoProxyCreator,appPush,appPushMsg2Client]; root of factory hierarchy Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appPushMsg2Client' defined in class path resource [config/spring/applicationContext-messagePush.xml]: Invocation of init method failed; nested exception is java.rmi.ConnectException: Connection refused to host: 10.120.20.197; nested exception is: java.net.ConnectException: Connection refused: connect at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) at cn.com.sinosoft.ebusiness.messagePush.App.main(App.java:20) Caused by: java.rmi.ConnectException: Connection refused to host: 10.120.20.197; nested exception is: java.net.ConnectException: Connection refused: connect at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619) at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216) at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202) at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:341) at sun.rmi.registry.RegistryImpl_Stub.list(Unknown Source) at org.springframework.remoting.rmi.RmiServiceExporter.testRegistry(RmiServiceExporter.java:420) at org.springframework.remoting.rmi.RmiServiceExporter.getRegistry(RmiServiceExporter.java:331) at org.springframework.remoting.rmi.RmiServiceExporter.prepare(RmiServiceExporter.java:268) at org.springframework.remoting.rmi.RmiServiceExporter.afterPropertiesSet(RmiServiceExporter.java:229) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417) ... 12 more Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:579) at java.net.Socket.connect(Socket.java:528) at java.net.Socket.<init>(Socket.java:425) at java.net.Socket.<init>(Socket.java:208) at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40) at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:147) at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613) ... 22 more
如果注释掉这一行
<property name="registryHost" value="${ServiceRegistryIp}" />
发布的时候从日志上看是外网的IP,现在发布服务的时候要强制绑定到内网的IP上。求解决方法,谢谢!