使用Spring RMI呼叫遠端方法
阿新 • • 發佈:2019-01-30
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高階程式設計》
客戶端: