如何配置rmi服务导出端点地址?

时间:2013-07-16 11:23:26

标签: java spring service rmi

我已经出口了一些rmi服务。

 <bean id="entityRmiServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
    <property name="serviceName" value="entityService"/>
    <property name="service" ref="entityServiceImpl"/>
    <property name="serviceInterface" value="IEntityService"/>
    <property name="registryPort" value="1099"/>
</bean>

在我的机器上运行时,端点 127.0.0.1:1099 但在VM上,它是 10.0.2.15:1099 ,ip地址。

RmiServiceExporter:276 - Binding service 'entityService' to RMI registry: RegistryImpl[UnicastServerRef [liveRef: [endpoint:[127.0.0.1:1099](local),objID:[0:0:0, 0]]]]

我可以在哪里手动配置它?

1 个答案:

答案 0 :(得分:1)

您可以在Spring配置中使用占位符,并将特定值移动到属性文件中。要做到这一点,首先需要一个可以解析文件属性的bean:

<!-- Read file that contains properties -->
<bean id="properties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:app.properties" />
</bean>

接下来,您可以修改entityRmiServiceExporter bean以使用该文件中的值:

<bean id="entityRmiServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
    <property name="serviceName" value="entityService"/>
    <property name="service" ref="entityServiceImpl"/>
    <property name="serviceInterface" value="IEntityService"/>
    <property name="registryPort" value="1099"/>

    <property name="registryHost" value="${rmi.endpoint}"/>
</bean>

你需要一个带有这样一行的app.properties文件:

rmi.endpoint=10.0.2.15

替代方法

根据RmiServiceExporter Javadoc,可能有另一种方法。这个Javadoc说:

  

注意:RMI尽最大努力获取完全限定的主机名。如果无法确定,它将退回并使用IP地址。根据您的网络配置,在某些情况下,它会将IP解析为环回地址。

您可以通过{J}启动时将-Djava.rmi.server.hostname=server.mycompany.com传递给JVM来告诉RMI机器主机名是什么。

这意味着您不必配置Spring bean - 而是将JVM配置为在不同的接口上公开RMI接口。如果你的机器直接暴露在互联网上(即没有防火墙或介于两者之间),我就不会这样做。如果机器在公司网络内部,那么以这种方式解决它可能是可接受的,甚至更可取。

相关问题