RMI需要服務端和客戶端
我們先從服務器開始
我的代碼結構
- package rmi;
- public interface ServerRmiI {
- public String sayHi(String name);
- }
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;
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");
- }
- }
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>
<?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;
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"));
- }
- }
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>
<?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高級程序設計》