Java之——Spring與RMI整合實現遠端存取(插曲)

來源:互聯網
上載者:User

標籤:rmi   網路   發布   分布式應用   

    轉載請註明出處:http://blog.csdn.net/l1028386804/article/details/45972095

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

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

服務端發布服務

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

package org.shirdrn.spring.remote.rmi;/** * @author liuyazhuang */public interface AccountService {int queryBalance(String mobileNo);String shoopingPayment(String mobileNo, byte protocol);}
服務實現,樣本如下所示:

package org.shirdrn.spring.remote.rmi;import org.apache.log4j.Logger;/** * @author liuyazhuang */public class MobileAccountServiceImpl implements AccountService {private static final Logger LOG = Logger.getLogger(MobileAccountServiceImpl.class);public int queryBalance(String mobileNo) {if (mobileNo != null)return 100;return 0;}public String shoopingPayment(String mobileNo, byte protocol) {StringBuffer sb = new StringBuffer().append("Your mobile number is /"").append(mobileNo).append("/", protocol type is /"").append(protocol).append("/".");LOG.info("Message is: " + sb.toString());return sb.toString();}}
服務端發布服務,供用戶端進行(遠程方法)調用,Spring配置server.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" xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"><property name="serviceName" value="MobileAccountService" /><property name="service" ref="accountService" /><property name="serviceInterface"value="org.shirdrn.spring.remote.rmi.AccountService" /><property name="registryPort" value="8080" /><property name="servicePort" value="8088" /></bean><bean id="accountService" class="org.shirdrn.spring.remote.rmi.MobileAccountServiceImpl" /></beans>

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

下面啟動服務端,發布服務,如下所示:

package org.shirdrn.spring.remote.rmi;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * @author liuyazhuang */public class RmiServer {public static void main(String[] args) throws InterruptedException {new ClassPathXmlApplicationContext("org/shirdrn/spring/remote/rmi/server.xml");Object lock = new Object();synchronized (lock) {lock.wait();}}}

用戶端調用服務

用戶端配置client.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" xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="mobileAccountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"><property name="serviceUrl" value="rmi://192.168.1.103:8080/MobileAccountService" /><property name="serviceInterface"value="org.shirdrn.spring.remote.rmi.AccountService" /></bean></beans>
配置中,將一個serviceUrl和serviceInterface注入給RmiProxyFactoryBean,即可進行遠程方法調用。調用樣本如下所示:

package org.shirdrn.spring.remote.rmi;import org.apache.log4j.Logger;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * @author liuyazhuang */public class RmiClient {private static final Logger LOG = Logger.getLogger(RmiClient.class);public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("org/shirdrn/spring/remote/rmi/client.xml");AccountService accountService = (AccountService) ctx.getBean("mobileAccountService");String result = accountService.shoopingPayment("13800138000", (byte) 5);LOG.info(result);}}
可見,實現遠端存取變得非常容易



Java之——Spring與RMI整合實現遠端存取(插曲)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.