2

我已经导出了一些 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]]]]

在哪里可以手动配置?

4

1 回答 1

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>

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

<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 解析为环回地址。

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

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

于 2013-07-16T11:31:13.797 回答