基於 Spring 的遠端過程呼叫(RPC):RMI、Hessian/Burlap、Spring HttpInvoker 四種實現方式詳解

zifangsky發表於2019-03-04

一 簡介

在Java開發中,我們可以使用多種遠端呼叫技術,如:

  • 遠端方法呼叫(Remote Method Invocation,RMI)
  • Caucho的Hessian和Burlap
  • Spring基於HTTP的遠端服務
  • SOAP和RESTful風格的Web Service

注:關於Web Service可以自行參考我之前寫過的相關文章(PS:www.zifangsky.cn/webservice)。在本篇文章中我主要介紹前面幾種方式的具體程式碼實現

(1)遠端呼叫與本地呼叫:

遠端呼叫是客戶端應用和服務端之間的會話。在客戶端,它所需要的一些功能並不在該應用的實現範圍之內,所以應用要向能提供這些功能的其他系統尋求幫助。同樣,遠端應用通過釋出服務將這些功能暴露給其他系統使用

i)二者之間的相似點:

從表面上看,RPC呼叫(遠端過程呼叫)類似於呼叫一個本地物件的某個方法。和本地方法呼叫相比兩者都是同步操作,會阻塞呼叫程式碼的執行,直到被呼叫的過程執行完畢

ii)二者之間的不同點:

本地方法呼叫是指同一個應用中的兩個程式碼塊之間的執行流交換;RPC呼叫則是執行流從一個應用傳遞給另一個應用的過程,理論上另一個應用可以部署在跨網路的任意一臺其他遠端機器上

(2)Spring對多種遠端呼叫的支援:

Spring支援多種不同的RPC模型,包括RMI、Hessian/Burlap以及Spring自帶的Http Invoker。下面我將簡單介紹一下它們之間的異同點:

  • RMI:不考慮網路限制時使用(PS:因為RMI使用任意埠來互動,有時無法穿越防火牆)
  • Hessian/Burlap:考慮網路限制時,通過HTTP訪問/釋出基於Java的服務。Hessian是基於二進位制的遠端呼叫技術;而Burlap是基於XML的遠端呼叫技術
  • Spring的HttpInvoker:跟Hessian/Burlap實現的呼叫技術類似,但是不同的是Hessian/Burlap使用了私有的物件序列化機制,而Spring的Http Invoker則使用的是Java的序列化機制

但是,不管選擇哪種遠端呼叫模型我們都會發現Spring提供了風格一致的支援。這意味著一旦理解了如何在Spring中配置和使用其中一種模型。那麼當我們想要使用另外一種模型的話,將會變得非常容易

在所有的模型中,服務都作為Spring所管理的bean配置到我們的應用中。這是通過一個代理工廠bean實現的,這個bean能夠把遠端服務像本地物件一樣裝配到其他bean的屬性中去。客戶端向代理髮起呼叫,就像代理提供了這些服務一樣。代理代表客戶端與遠端服務進行通訊,由它負責處理連線的細節並向遠端服務發起呼叫。最後代理再返回遠端服務執行完成之後的結果,至此整個呼叫過程完成

無論我們開發的是使用遠端服務的程式碼,還是實現這些服務的程式碼,或者兩者兼而有之。在Spring中,使用遠端服務純粹是一個配置問題。我們不需要編寫任何Java程式碼就可以支援遠端呼叫。我們的服務bean也不需要關心它們是否參與了一個RPC(PS:任何傳遞給遠端呼叫的bean或從遠端呼叫返回的bean可能需要實現java.io.Serializable 介面)

二 遠端方法呼叫(RMI)

(1)定義一個暴露出來給其他系統呼叫的服務:

i)它的介面:

package cn.zifangsky.rmi.service;

import java.util.Date;

public interface TestRMIService {

    /**
     * 時間格式轉化服務
     * @param date 時間
     * @return String型別的時間字串
     */
    public String formatDateService(Date date);
}複製程式碼

ii)它的實現類:

package cn.zifangsky.rmi.service.impl;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Component;

import cn.zifangsky.rmi.service.TestRMIService;

@Component("testRMIServiceImpl")
public class TestRMIServiceImpl implements TestRMIService {

    @Override
    public String formatDateService(Date date) {
        Format format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if(date != null){
            return format.format(date);
        }else{
            return "";
        }
    }

}複製程式碼

從上面的程式碼可以看出,這些都是簡單的POJO,並沒有依賴其他服務

(2)新建一個context_rmi.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:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/jee 
            http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="cn.zifangsky.rmi.service.impl" />

    <bean id="testRMIService" class="org.springframework.remoting.rmi.RmiServiceExporter">
        <!-- RMI服務名 -->
        <property name="serviceName" value="testRMIService"/>
        <!-- RMI具體服務實現類 -->
        <property name="service" ref="testRMIServiceImpl" />
        <!-- 服務呼叫介面 -->
        <property name="serviceInterface" value="cn.zifangsky.rmi.service.TestRMIService" />
        <!-- 註冊埠 -->
        <property name="registryPort" value="1099" />
    </bean>

</beans>複製程式碼

可以看出,通過上面的XML配置匯出了一個遠端服務,其地址是:rmi://127.0.0.1:1099/testRMIService

注:上面的service的值“testRMIServiceImpl”是上面的TestRMIServiceImpl類的一個例項,通過@Component註解生成並注入到這裡

(3)將TestRMIService介面等類匯出成jar包供客戶端使用:

上面定義好一個服務之後,我們要想在客戶端(另一個Web專案)中使用這個服務,那麼我們肯定是需要知道這個遠端方法所在的類、方法名等資訊才能夠正常呼叫的。因此,接下來還需要將這些公共類打包成jar包放到客戶端的lib目錄才行

這一步可以手動選擇匯出的類,也可以使用一個Ant指令碼來自動化完成這一操作。我這裡使用的Ant指令碼如下:

build_service.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project name="common_build" default="build_service" basedir=".">

    <property name="targetdir" value="target"/>
    <property name="classbase" value="WebContent/WEB-INF/classes/"/>
    <property name="xpf.copy.info" value="true"/>
    <property name="model.name" value="test"/>
    <property name="env.name" value="dev"/>

    <target name="build_service">
        <jar destfile="${basedir}/target/zifangsky_${model.name}_RMIService_api.jar">
            <fileset dir="${classbase}">
                <include name="cn/zifangsky/**/model/*.class" />
                <include name="cn/zifangsky/**/model/**/*.class" />
                <include name="cn/zifangsky/**/service/*.class" />
                <include name="cn/zifangsky/**/common/*.class" />
                <include name="cn/zifangsky/**/common/**/*.class" />
            </fileset>
        </jar>
    </target>

</project>複製程式碼

最後將生成的zifangsky_test_RMIService_api.jar放到客戶端的lib目錄下即可

(4)客戶端的XML配置:

context_rmi_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:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/jee 
            http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="cn.zifangsky.rmi.service" />

    <bean id="testRMIServiceClient" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
        <!-- 服務地址 -->
        <property name="serviceUrl" value="rmi://127.0.0.1:1099/testRMIService" />
        <!-- 服務呼叫介面 -->
        <property name="serviceInterface" value="cn.zifangsky.rmi.service.TestRMIService" />
    </bean>

</beans>複製程式碼

這裡的程式碼很簡單,自己看註釋就明白了

(5)測試:

package cn.zifangsky.test.rmi;

import java.util.Date;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.zifangsky.rmi.service.TestRMIService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/context/context.xml","classpath:/context/context_rmi_client.xml"})
public class TestRMI {

    @Resource(name="testRMIServiceClient")
    private TestRMIService testRMIService;

    @Test
    public void testFormatDate(){
        Date date = new Date();
        System.out.println("格式化後的時間是: " + testRMIService.formatDateService(date));
    }
}複製程式碼

最後輸出如下:

格式化後的時間是: 2017-01-08 15:55:49複製程式碼

從控制檯的輸出結果來看,這裡的客戶端程式碼的確呼叫了遠端服務實現了時間格式的轉化。到此,關於RMI的配置和使用就結束了,下面介紹的其他的幾種遠端呼叫的配置和使用跟RMI類似,因此我就簡單敘述了

三 使用Hessian釋出遠端服務

Hessian和Burlap是Caucho Technology提供的兩種基於HTTP的輕量級遠端服務解決方案。藉助於儘可能簡單的API和通訊協議,它們都致力於簡化Web服務。

關於如何選擇使用Hessian還是Burlap釋出服務,很大程度上,它們是一樣的。唯一的區別在於Hessian的訊息是二進位制的,而Burlap的訊息是XML格式。因為Hessian的訊息是二進位制的,所以它在頻寬上更具優勢。但是如果我們更注重可讀性(如出於除錯的目的)或者我們的應用需要與沒有Hessian實現的語言互動,那麼Burlap的XML訊息將會是更好的選擇

(1)下載Hessian相關的jar包:

下載地址:hessian.caucho.com

(2)定義一個測試服務:

i)它的介面TestHessianService.java:

package cn.zifangsky.hessian.service;

public interface TestHessianService {

    /**
     * 測試介面
     * @return
     */
    public String sayHello();
}複製程式碼

ii)它的實現類TestHessianServiceImpl.java:

package cn.zifangsky.hessian.service.impl;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Component;

import cn.zifangsky.hessian.service.TestHessianService;

@Component("testHessianServiceImpl")
public class TestHessianServiceImpl implements TestHessianService{

    @Override
    public String sayHello() {
        Date date = new Date();
        Format format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return "Hello,this`s TestHessianService ---Time: " + format.format(date);
    }

}複製程式碼

(3)匯出成服務context_hessian.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:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/jee 
            http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="cn.zifangsky.hessian.service.impl" />

    <bean id="/testHessianService" class="org.springframework.remoting.caucho.HessianServiceExporter">
        <!-- Hessian具體服務實現類 -->
        <property name="service" ref="testHessianServiceImpl" />
        <!-- 服務呼叫介面 -->
        <property name="serviceInterface" value="cn.zifangsky.hessian.service.TestHessianService" />
    </bean>

</beans>複製程式碼

配置web.xml將所有的Hessian服務單獨釋出到一個目錄:

    <servlet>
        <servlet-name>hessian</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:context/context_hessian.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>hessian</servlet-name>
        <url-pattern>/hessian/*</url-pattern>
    </servlet-mapping>複製程式碼

因此,上面的那個測試服務的地址是:http://localhost:9180/RMIServerDemo/hessian/testHessianService

(4)將服務介面匯出成jar包供客戶端使用:

操作步驟同上面的RMI的操作,略

(5)客戶端的XML配置:

context_hessian_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:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/jee 
            http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <bean id="testHessianServiceClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <!-- 服務地址 -->
        <property name="serviceUrl" value="http://localhost:9180/RMIServerDemo/hessian/testHessianService" />
        <!-- 服務呼叫介面 -->
        <property name="serviceInterface" value="cn.zifangsky.hessian.service.TestHessianService" />
    </bean>

</beans>複製程式碼

(6)測試:

package cn.zifangsky.test.hessian;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.zifangsky.hessian.service.TestHessianService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/context/context.xml","classpath:/context/context_hessian_client.xml"})
public class TestHessian {

    @Resource(name="testHessianServiceClient")
    TestHessianService testHessianService;

    @Test
    public void sayHello(){
        System.out.println(testHessianService.sayHello());
    }

}複製程式碼

最後輸出如下:

Hello,this`s TestHessianService ---Time: 2017-01-08 16:48:25複製程式碼

四 使用Burlap釋出遠端服務

(1)下載Burlap相關的jar包:

下載地址:repo1.maven.org/maven2/edu/…

(2)定義一個測試服務:

介面TestBurlapService.java:

package cn.zifangsky.burlap.service;

public interface TestBurlapService {
    /**
     * 測試介面
     * @return
     */
    public String sayHello();
}複製程式碼

其實現類TestBurlapServiceImpl.java:

package cn.zifangsky.burlap.service.impl;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Component;

import cn.zifangsky.burlap.service.TestBurlapService;

@Component("testBurlapServiceImpl")
public class TestBurlapServiceImpl implements TestBurlapService{

    @Override
    public String sayHello() {
        Date date = new Date();
        Format format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return "Hello,this`s TestBurlapService ---Time: " + format.format(date);
    }

}複製程式碼

(3)匯出成服務context_burlap.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:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/jee 
            http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="cn.zifangsky.burlap.service.impl" />

    <bean id="/testBurlapService" class="org.springframework.remoting.caucho.BurlapServiceExporter">
        <!-- Burlap具體服務實現類 -->
        <property name="service" ref="testBurlapServiceImpl" />
        <!-- 服務呼叫介面 -->
        <property name="serviceInterface" value="cn.zifangsky.burlap.service.TestBurlapService" />
    </bean>

</beans>複製程式碼

對應的web.xml配置:

    <servlet>
        <servlet-name>burlap</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:context/context_burlap.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>burlap</servlet-name>
        <url-pattern>/burlap/*</url-pattern>
    </servlet-mapping>複製程式碼

因此,上面的那個測試服務的地址是:http://localhost:9180/RMIServerDemo/burlap/testBurlapService

(4)將服務介面匯出成jar包供客戶端使用:

操作步驟同上面的RMI的操作,略

(5)客戶端的XML配置:

context_burlap_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:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/jee 
            http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <bean id="testBurlapServiceClient" class="org.springframework.remoting.caucho.BurlapProxyFactoryBean">
        <!-- 服務地址 -->
        <property name="serviceUrl" value="http://localhost:9180/RMIServerDemo/burlap/testBurlapService" />
        <!-- 服務呼叫介面 -->
        <property name="serviceInterface" value="cn.zifangsky.burlap.service.TestBurlapService" />
    </bean>

</beans>複製程式碼

(6)測試:

package cn.zifangsky.test.burlap;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.zifangsky.burlap.service.TestBurlapService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/context/context.xml","classpath:/context/context_burlap_client.xml"})
public class TestBurlap {

    @Resource(name="testBurlapServiceClient")
    private TestBurlapService testBurlapService;

    @Test
    public void sayHello(){
        System.out.println(testBurlapService.sayHello());
    }


}複製程式碼

最後輸出如下:

Hello,this`s TestBurlapService ---Time: 2017-01-08 17:03:47複製程式碼

五 使用Spring的Http Invoker釋出遠端服務

(1)定義一個測試服務:

介面TestHttpInvokerService.java:

package cn.zifangsky.httpinvoker.service;

public interface TestHttpInvokerService {
    /**
     * 測試介面
     * @return
     */
    public String sayHello();
}複製程式碼

其實現類TestHttpInvokerServiceImpl.java:

package cn.zifangsky.httpinvoker.service.impl;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Component;

import cn.zifangsky.httpinvoker.service.TestHttpInvokerService;

@Component("testHttpInvokerServiceImpl")
public class TestHttpInvokerServiceImpl implements TestHttpInvokerService {

    @Override
    public String sayHello() {
        Date date = new Date();
        Format format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return "Hello,this`s TestHttpInvokerService ---Time: " + format.format(date);
    }

}複製程式碼

(2)匯出成服務context_invoker.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:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/jee 
            http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="cn.zifangsky.httpinvoker.service.impl" />

    <bean id="/testHttpInvokerService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
        <!-- Http Invoker具體服務實現類 -->
        <property name="service" ref="testHttpInvokerServiceImpl" />
        <!-- 服務呼叫介面 -->
        <property name="serviceInterface" value="cn.zifangsky.httpinvoker.service.TestHttpInvokerService" />
    </bean>

</beans>複製程式碼

對應的web.xml配置:

    <servlet>
        <servlet-name>invoker</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:context/context_invoker.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>invoker</servlet-name>
        <url-pattern>/invoker/*</url-pattern>
    </servlet-mapping>複製程式碼

因此,上面的那個測試服務的地址是:http://localhost:9180/RMIServerDemo/invoker/testHttpInvokerService

(3)將服務介面匯出成jar包供客戶端使用:

操作步驟同上面的RMI的操作,略

(4)客戶端的XML配置:

context_httpinvoker_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:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/jee 
            http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <bean id="testHttpInvokerServiceClient" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
        <!-- 服務地址 -->
        <property name="serviceUrl" value="http://localhost:9180/RMIServerDemo/invoker/testHttpInvokerService" />
        <!-- 服務呼叫介面 -->
        <property name="serviceInterface" value="cn.zifangsky.httpinvoker.service.TestHttpInvokerService" />
    </bean>

</beans>複製程式碼

(5)測試:

package cn.zifangsky.test.httpinvoker;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.zifangsky.httpinvoker.service.TestHttpInvokerService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/context/context.xml","classpath:/context/context_httpinvoker_client.xml"})
public class TestHttpInvoker {

    @Resource(name="testHttpInvokerServiceClient")
    private TestHttpInvokerService testHttpInvokerService;

    @Test
    public void sayHello(){
        System.out.println(testHttpInvokerService.sayHello());
    }

}複製程式碼

最後輸出如下:

Hello,this`s TestHttpInvokerService ---Time: 2017-01-08 17:09:51複製程式碼

至此,關於基於Spring的遠端過程呼叫(RPC)的四種實現方式的介紹就全部結束了。從上面的程式碼可以看出,由於Spring的封裝,這幾種服務的釋出和實現步驟都是很類似的,並且我們實際需要做的工作也變得非常簡單,我們很大程度上只需要關注我們的具體業務邏輯就行了

相關文章