spring MVC rmi配置

本文详细介绍了如何使用Spring框架配置远程方法调用(RMI)服务,包括生产者和服务消费者的XML配置示例。阐述了如何设置服务端口、服务名及接口,以及客户端如何连接并调用服务。此外,还探讨了RMI stub管理和刷新策略,以应对服务重启后的连接失败问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.生产者配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 
<bean id="testSpringRMI" class="com.rmi.server.ServerImpl"/>
 
<!-- 服务端  配置rmi服务发布 -->
<bean id="rmiServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
  <!-- 配置service -->
  <property name="service" ref="testSpringRMI"></property>
  <!-- 客户端使用的serviceName -->
  <property name="serviceName" value="testrmiSpring"/>
  <!-- 服务接口 -->
  <property name="serviceInterface" value="com.rmi.server.Server"/>
  
  <!-- 注册服务端口号,默认是1099 -->
  <property name="registryPort" value="1099"/>
 
</bean>
 
</beans>

2.消费者配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 
 
 
<!-- 客户端   调用服务端rmi服务配置-->
<bean id="testrmiSpring" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
  <!--调用服务端url以及serviceName -->
  <property name="serviceUrl" value="rmi://:1099/testrmiSpring"/>
  <!-- 调用服务端的接口 -->
 <property name="serviceInterface" value="com.rmi.server.Server"/>
    
 <property name="refreshStubOnConnectFailure" value="true" />
 <property name="lookupStubOnStartup" value="false" />
 
</bean>
 
</beans>

因为RMI stub被连接到特定的端点,不仅仅是为每个调用打开一个给定的目标地址的连接,所以如果重新启动RMI端点主机的服务器,那么就需要重新注册这些stub,并且客户端需要再次查询它们。

     虽然目标服务的重新注册在重新启动时通常会自动发生,不过此时客户端保持的stub将会变的陈旧,且客户端不会注意这些,除非他们再次尝试调用stub上的方法,而这也将throw一个连接失败的异常。

      为了避免这种情形,Spring的RmiProxyFactoryBean提供了一个refreshStubOnConnectFailure的bean属性,如果调用失败,并且连接异常的话,将它设定为true来强制重新自动查询stub。

 

stub查询的另一个问题是,目标RMI服务器和RMI注册项在查询时要为可用的。如果客户端在服务器启动之前,尝试查询和缓存该服务stub,那么客户端的启动将会失败(即使还不需要该服务)。

     为了能够惰性查询服务stub,设定RmiProxyFactoryBean的lookupStubOnStarup标志为false。然后在第一次访问时查询该stub,也就是说,当代理上的第一个方法被调用的时候去主动查询stub,同时被缓存。这也有一个缺点,就是直到第一次调用,否则无法确认目标服务是否实际存在。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值