javascript
Spring远程调用技术1-RMI
在java中,我們有多種可以使用的遠程調用技術
1.遠程方法調用(remote method invocation, RMI)?
適用場景:不考慮網絡限制時(例如防火墻),訪問/發布基于java的服務
?2.Caucho的Hession和Burlap
適用場景:考慮網絡限制時,通過http訪問/發布基于java的服務。Hession是二進制協議,Burlap是基于XML的
?3.Spring基于HTTP的遠程服務(HTTP invoke)
適用場景:考慮網絡限制,并希望使用基于xml或專有的序列化機制實現java序列化時,訪問/發布基于Spring的服務
?4.使用JAX-RPC和JAX-WS的Web Server
適用場景:訪問/發布平臺獨立的、基于SOAP的Web服務
?5.還有EJB技術
參考http://www.cnblogs.com/wwzyy/p/5655600.html
?
?這里遠程方法的調用,方法的執行是在服務端執行的,客戶端只是獲取服務端處理完返回的結果。
?
一、首先開始配置服務端,建立java普通項目就行
先導入spring的jar包,這里使用的是spring4.1.1
1.配置遠程調用的接口(MyServer.java)
package com.spring.rmiServer;public interface MyServer {public void getMsg();public String getMsgById(Integer id);public Integer add(Integer a, Integer b);}2.實現類(MyServerImpl.java)
package com.spring.rmiServer;public class MyServerImpl implements MyServer {@Overridepublic void getMsg() {System.out.println("call getMsg..");}@Overridepublic String getMsgById(Integer id) {System.out.println("call getMsgById "+ id);return "id is "+id;}@Overridepublic Integer add(Integer a, Integer b) {System.out.println("call add ");return a+b;}}3.配置RMI服務(RMIConfig.java),使用java配置的方式
package com.spring.rmiServer;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.remoting.rmi.RmiServiceExporter;@Configuration public class RMIConfig {//@Bean()的name默認是方法名@Bean(name="rmiExporter")public RmiServiceExporter rmiExporter(MyServer myServer){RmiServiceExporter rmiExporter = new RmiServiceExporter();rmiExporter.setService(myServer);rmiExporter.setServiceName("My_server");rmiExporter.setServiceInterface(MyServer.class);//rmiExporter.setRegistryHost("localhost");rmiExporter.setRegistryPort(1199);//遠程的端口return rmiExporter;}@Bean(name="myServer")public MyServer myServer(){return new MyServerImpl();} }4.啟動服務
public static void main(String[] args) {//當出現java.net.ConnectException: Connection refused: connect,可以這樣解決,指定ip地址
System.setProperty("java.rmi.server.hostname" , "192.168.23.128" );
ApplicationContext ac = new AnnotationConfigApplicationContext(com.spring.rmiServer.RMIConfig.class); System.out.println("start rmi server..");
}
?
二、配置客戶端
?
1.配置要訪問的遠程接口,和服務端的那個接口一模一樣,實現類不用寫,因為服務端已經實現了,客戶端調用就行
(MyServer.java)
package com.spring.rmiServer;public interface MyServer {public void getMsg();public String getMsgById(Integer id);public Integer add(Integer a, Integer b);}2.配置文件 ?(ClientContext.xml)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"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 class="org.springframework.remoting.rmi.RmiProxyFactoryBean"><!-- 要調用的接口 --><property name="serviceInterface" value="com.spring.rmiServer.MyServer"></property><!-- url --><property name="serviceUrl" value="rmi://192.168.23.128:1199/My_server"></property></bean> </beans>3.啟動客戶端(Client.java)
public static void main(String[] args) {ApplicationContext app = new ClassPathXmlApplicationContext("com/spring/rmiClient/ClientContext.xml");MyServer myServer = app.getBean(MyServer.class);System.out.println(myServer.getMsgById(03));}?
注意:有時候會由于主機ip的原因出現拒絕訪問異常
?
?
3種解決方法:
1. ?服務器端添加代碼:?System.setProperty("java.rmi.server.hostname"?,?"192.168.23.128"?);上面代碼使用的就是這種方法。
2.??在 RMI 服務器上 root 身份登錄,輸入 Vi /etc/hosts ,在第一行添加?192.168.39.11 ? ? ieie
3.? 若是用 spring, 則在?RmiServiceExporter?中添加屬性?<property?name="registryHost"??value="192.168.23.128"?/>
?
缺點:
但是它存在某種限制,RMI很難穿越防火墻,這是RMI使用任意端口來交互(這是防火墻通常不允許的)。在企業內部網絡幻境中,我們通常不需要擔心。但如果在互聯網運行,可能會有麻煩。
還有就是RMI是基于java的,也就是說客戶端和服務端必須都是由java開發,這樣接口才能對得上。因為RMI使用了java的序列化機制,所以通過網絡傳輸的對象類型必須要保證在調用兩端的java運行中是完全相同的版本
?
轉載于:https://www.cnblogs.com/wwzyy/p/6083090.html
總結
以上是生活随笔為你收集整理的Spring远程调用技术1-RMI的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: quantum theory
- 下一篇: 个人学习进度条------第八周