使用spring对RMI的支持,可以非常容易地构建你的分布式应用。在服务端,可以通过Spring的org.springframework.remoting.rmi.RmiServiceExporter可以暴露你的服务;在客户端,通过org.springframework.remoting.rmi.RmiProxyFactoryBean可以使用服务端暴露的服务,非常方便。这种C/S模型的访问方式,可以屏蔽掉RMI本身的复杂性,如服务端Skeleton和客户端Stub等的处理细节,这些对于服务开发和服务使用的人员来说,都是透明的,无需过度关注,而集中精力开发你的商业逻辑。
Spring提供了非常方便的RMI(远程方法调用)调用方式,本文介绍下如何在Spring中集成RMI。
定义接口MessageProvider及接口中供调用的方法(MessageProvider.java)
package org.thera.rmi.service; public interface MessageProvider { public String queryForMessage(String name); }
实现MessageProvider接口(MessageProviderImpl.java)
package org.thera.rmi.service; public class MessageProviderImpl implements MessageProvider { @Override public String queryForMessage(String name) { return "Hello, " + name; } }
服务端发布服务,供客户端进行(远程方法)调用,Spring配置server.xml如下所示
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 注入要发布的RMI服务类 --> <bean id="messageService" class="org.thera.rmi.service.MessageProviderImpl"></bean> <bean class="org.springframework.remoting.rmi.RmiServiceExporter"> <!-- RMI服务名称,可自定义服务名称 --> <property name="serviceName" value="MessageService" /> <!-- 导出实体 --> <property name="service" ref="messageService" /> <!-- 导出接口 --> <property name="serviceInterface" value="org.thera.rmi.service.MessageProvider" /> <!-- spring默认使用1099端口 --> <property name="registryPort" value="1199" /> </bean> </beans>
加载Spring容器,发布RMI服务(Main.java)
package org.thera.rmi.service.main; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/context.xml"); System.out.println("已成功发布RMI服务类"); } }
到这里,RMI的服务端已经发布成功。
先移植服务端服务接口文件MessageProvider.java,再Spring配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="messageService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <property name="serviceUrl" value="rmi://192.168.1.100:1199/MessageService" /> <property name="serviceInterface" value="org.thera.rmi.service.MessageProvider" /> </bean> </beans>
加载Spring容器,调用RMI服务端(Main.java)
package org.thera.rmi.service.main; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.thera.rmi.service.MessageProvider; public class Main { public static void main(String[] args) { ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/context.xml"); System.out.println("加载Spring容器,并初始化RMI客户端"); MessageProvider client = (MessageProvider)ctx.getBean("messageService"); String temp = client.queryForMessage("LvSantorini"); System.out.println("返回结果: " + temp); } }
运行效果