Spring有多种调用远程的方式,今天学习了一下远程方法调用(RMI)。
RMI需要服务端和客户端
我们先从服务器开始
我的代码结构
package rmi; public interface ServerRmiI { public String sayHi(String name); }
package rmi; public class ServerRmiImpl implements ServerRmiI{ public String sayHi(String name) { return "Hi,"+name; } }
package rmi; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Testrun { /** * <p> * </p> * @author zhangjunshuai * @date 2014-5-28 下午5:06:06 * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-bean.xml"); context.getBean("serverTest"); } }
配置:
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-autowire="byName" default-lazy-init="true"> <bean name="rmiserver" class="rmi.ServerRmiImpl"/> <bean name="serverTest" class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="service" ref="rmiserver"/> <property name="serviceName" value="serverRmiTest"/> <property name="serviceInterface" value="rmi.ServerRmiI"/> <property name="registryPort" value="1021"/> </bean> </beans>
说明:service本地实现,serviceName对外提供的名称对外提供的名称,registyPort开启端口
客户端:
代码结构
首先要定义对应接口
package client; public interface ServerRmiI { public String sayHi(String name); }
测试方法
package client; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestRun { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/config/applicationContext-rmi-client.xml"); ServerRmiI rmiI = (ServerRmiI) context.getBean("clentrmi"); System.out.println(rmiI.sayHi("rmi")); } }
配置文件
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-autowire="byName" default-lazy-init="true"> <bean name="clentrmi" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <property name="serviceUrl" value="rmi://127.0.0.1:1021/serverRmiTest"/> <property name="serviceInterface" value="client.ServerRmiI"/> </bean> </beans>
后台成功输出“hi”说明执行成功
需要注意:
1、RMI是很难穿越防火墙的;
2、RMI是基于Java的,这意味着客户端和服务端必须都是采用Java开发的;
3、因为RMI使用了Java的序列化机制,所以通过网络传输的对象类型必须保证在调用的两端是相同的版本。
-------------------------------------------------------------------------------------------------------------------------------------------------------------
参考:
《Spring事件(第3版)》
《Spring高级程序设计》
客户端:
使用Spring RMI调用远程方法
时间: 2024-10-24 14:44:42