Spring+RMI集成實現遠程訪問分佈式應用

下面是個人感覺比較清晰易懂的spring實現RMI的網絡帖子:


使用Spring對RMI的支持,可以非常容易地構建你的分佈式應用。在服務端,可以通過Spring的org.springframework.remoting.rmi.RmiServiceExporter可以暴露你的服務;在客戶端,通過org.springframework.remoting.rmi.RmiProxyFactoryBean可以使用服務端暴露的服務,非常方便。這種C/S模型的訪問方式,可以屏蔽掉RMI本身的複雜性,如服務端Skeleton和客戶端Stub等的處理細節,這些對於服務開發和服務使用的人員來說,都是透明的,無需過度關注,而集中精力開發你的商業邏輯。

下面通過一個例子,說明如何通過Spring集成RMI。

服務端發佈服務

我們定義了服務接口,服務端實現該服務接口來完成其複雜的邏輯,客戶端可以通過該接口調用服務端暴露的服務,如下所示:


[java] view plaincopy

  1. package org.shirdrn.spring.remote.rmi;  

  2.   

  3. public interface AccountService {  

  4.     int queryBalance(String mobileNo);  

  5.     String shoopingPayment(String mobileNo, byte protocol);  

  6. }  


服務實現,示例如下所示:


[java] view plaincopy

  1. package org.shirdrn.spring.remote.rmi;  

  2.   

  3. import org.apache.log4j.Logger;  

  4.   

  5. public class MobileAccountServiceImpl implements AccountService {  

  6.   

  7.     private static final Logger LOG = Logger.getLogger(MobileAccountServiceImpl.class);  

  8.     public int queryBalance(String mobileNo) {  

  9.         if (mobileNo != null)  

  10.             return 100;  

  11.         return 0;  

  12.     }  

  13.   

  14.     public String shoopingPayment(String mobileNo, byte protocol) {  

  15.         StringBuffer sb = new StringBuffer().append("Your mobile number is /"").append(  

  16.                 mobileNo).append("/", protocol type is /"").append(protocol)  

  17.                 .append("/".");  

  18.         LOG.info("Message is: " + sb.toString());  

  19.         return sb.toString();  

  20.     }  

  21. }  


服務端發佈服務,供客戶端進行(遠程方法)調用,Spring配置server.xml如下所示:


[xhtml] view plaincopy

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <beans xmlns="http://www.springframework.org/schema/beans"  

  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  

  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  

  5.   

  6.     <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">  

  7.         <property name="serviceName" value="MobileAccountService" />  

  8.         <property name="service" ref="accountService" />  

  9.         <property name="serviceInterface"  

  10.             value="org.shirdrn.spring.remote.rmi.AccountService" />  

  11.         <property name="registryPort" value="8080" />  

  12.         <property name="servicePort" value="8088" />  

  13.     </bean>  

  14.   

  15.     <bean id="accountService" class="org.shirdrn.spring.remote.rmi.MobileAccountServiceImpl" />  

  16.   

  17. </beans>  


上面配置,指定了暴露的服務的名稱,通過serviceName屬性注入到RmiServiceExporter中,服務名稱爲MobileAccountService,客戶端通過該服務名稱就能夠進行調用。

下面啓動服務端,發佈服務,如下所示:


[java] view plaincopy

  1. package org.shirdrn.spring.remote.rmi;  

  2.   

  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  

  4.   

  5. public class RmiServer {  

  6.   

  7.     public static void main(String[] args) throws InterruptedException {  

  8.         new ClassPathXmlApplicationContext("org/shirdrn/spring/remote/rmi/server.xml");  

  9.           

  10.         Object lock = new Object();  

  11.         synchronized (lock) {  

  12.             lock.wait();  

  13.         }  

  14.     }  

  15. }  


 

客戶端調用服務

客戶端配置client.xml如下所示:


[xhtml] view plaincopy

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <beans xmlns="http://www.springframework.org/schema/beans"  

  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  

  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  

  5.   

  6.     <bean id="mobileAccountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">  

  7.         <property name="serviceUrl" value="rmi://192.168.1.103:8080/MobileAccountService" />  

  8.         <property name="serviceInterface"  

  9.             value="org.shirdrn.spring.remote.rmi.AccountService" />  

  10.     </bean>  

  11.   

  12. </beans>  


配置中,將一個serviceUrl和serviceInterface注入給RmiProxyFactoryBean,即可進行遠程方法調用。調用示例如下所示:


[java] view plaincopy

  1. package org.shirdrn.spring.remote.rmi;  

  2.   

  3. import org.apache.log4j.Logger;  

  4. import org.springframework.context.ApplicationContext;  

  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  

  6.   

  7. public class RmiClient {  

  8.   

  9.     private static final Logger LOG = Logger.getLogger(RmiClient.class);  

  10.       

  11.     public static void main(String[] args) {  

  12.         ApplicationContext ctx = new ClassPathXmlApplicationContext(  

  13.                 "org/shirdrn/spring/remote/rmi/client.xml");  

  14.         AccountService accountService = (AccountService) ctx  

  15.                 .getBean("mobileAccountService");  

  16.         String result = accountService.shoopingPayment("13800138000", (byte5);  

  17.         LOG.info(result);  

  18.     }  

  19.   

  20. }  


可見,實現遠程訪問變得非常容易。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章