1. 程式人生 > >Spring HTTP Invoker-遠端呼叫模式

Spring HTTP Invoker-遠端呼叫模式

一,Spring HTTP Invoker-遠端呼叫模式 

Spring HTTP invoker  spring 框架中的一個遠端呼叫模型,執行基於 HTTP 的遠端呼叫(意味著可以通過防火牆),並使用 java 的序列化機制在網路間傳遞物件。客戶端可以很輕鬆的像呼叫本地物件一樣呼叫遠端伺服器上的物件,這有點類似於 webservice,但又不同於 webservice ,區別如下:

webservice

HTTP invoker

跨平臺,跨語言

只支援 java 語言

支援 SOAP ,提供 wsdl

不支援

結構龐大,依賴特定的 webservice 實現,如

 xfire

結構簡單,只依賴於 spring 框架本身

  HTTP invoker 服務模式



說明:

1. 伺服器端:通過 HTTP invoker 服務將服務介面的某個實現類提供為遠端服務

2. 客戶端:通過 HTTP invoker 代理向伺服器端傳送請求,遠端呼叫服務介面的方法

3. 伺服器端與客戶端通訊的資料需要序列化

Spring HTTP Invoker有兩種實現方式 

1基於Url對映方式,遠端系統處理請求的方式同SpringMVCcontroller類似,所有的請求通過在web.xml中的 org.springframework.web.servlet.DispatcherServlet

統一處理,根據url對映,去對應的 【servlet名稱-servlet.xml】檔案中,查詢跟請求的url匹配的bean配置

基於Servlet方式,

2org.springframework.web.context.support.HttpRequestHandlerServlet去攔截url- pattern匹配的請求,如果匹配成功,去ApplicationContext中查詢nameservlet-name一致的bean,完成遠端方法呼叫。

注意:如果涉及java物件因為是在網路間傳輸物件,所以需要將物件實現 Serializable 介面,並指定一個serialVersionUID

 (任意值即可,同時客戶端也要有這個類,否則在客戶端接收物件時會因為serialVersionUID 不匹配而出現異常)

配置伺服器端

1. 新增 springJAR 檔案

建議使用 spring2+.jar 版本

2. 建立服務介面

3. 建立服務介面的具體實現類

4. 公開服務

(

將介面宣告為 HTTP invoker 服務

(如:

< bean id = "httpService"

class = "org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter" >

< property name = "service" >

< ref= "ucService" />

</ property >

< property name = "serviceInterface"

value = "com.netqin.baike.service.UcService" >

</ property >

</ bean >

< bean id = "ucService" class = "com.netqin.baike.service.impl.UCServiceImpl" />

)

說明: HttpInvokerServiceExporter 實際上是一個 spring mvc 控制器,它處理客戶端的請求並呼叫服務實現。

WEB-INF/service-servlet.xml  

HttpInvokerServiceExporter 實際上是一個 spring mvc 控制器,所以需要為其 提供 spring URL 處理器,這裡我們使用 SimpleUrlHandlerMapping

(如:

< bean

class = "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >

< property name = "mappings" >

< props >

< prop key = "/httpService" > httpService </ prop >

</ props >

</ property >

</ bean >

)

一個基於 spring HTTP invoker 的遠端服務就完成了,服務的地址為:

http://${serviceName}:${port}/${contextPath}/service/httpService

)

配置客戶端

1. 新增 springJAR 檔案

建議使用 spring2+.jar 版本

2. 建立服務介面

(

 為了方便,可以將伺服器端建立好的的 UcService.java  UserInfo.java 拷貝到客戶端 , 或打個 jar 包放到 lib 下。

)

3.配置訪問服務

(

 WEB-INF/application-context.xml :如果專案中已經存在 spring 配置檔案,則不需要建立該檔案,需要配置 HTTP invoker 的代理

< bean id = "httpService"

class = "org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean" >

< property name = "serviceUrl" >

< value > http://${serviceName}:${port}/${contextPath}/service/httpService

</ value >

 </ property >

 < property name = "serviceInterface"

      value = "com.netqin.baike.service.UcService" >

  </ property >

</ bean >

說明:客戶端使用 HttpInvokerProxyFactoryBean 代理客戶端向伺服器端傳送請求,請求介面為 UcService 的服務 注意:需要修改 serviceUrl 為實際的伺服器地址

)

4. 訪問服務

(

讀取 spring 上下文,以遠端呼叫 getUserInfobyName 方法為例

在 jsp,servlet,action 等等檔案中

UcService service = (UcService) WebApplicationContextUtils

        .getRequiredWebApplicationContext(

            request.getSession().getServletContext()).getBean(

            "httpService" );

UserInfo user = service .getUserInfobyName( "hanqunfeng" );

如果不想配置 spring 執行環境,可以使用如下方式:

ApplicationContext applicationContext

= new FileSystemXmlApplicationContext( "classpath:application-context.xml" );

service = (UcService) applicationContext.getBean( "httpService" );

依賴注入,遠端呼叫 recordLog 方法為例

在 WEB-INF/application-context.xml 中加入如下配置:

< bean id = "abc" class = "com.netqin.test.abc" >

        < property name = "service" >

            < ref bean = "httpService" />

        </ property >

</ bean >

為 com.netqin.test.abc 中加入對 service 的 set 方法:

private UcService service ;

    public void setService(UcService service){

        this . service = service;

    }

    public String recordUserLog(String username,String point,String operate,String desc){

        String result = service .recordLog(username, point, operate, desc);

        return result;

}

)

二,Spring HTTP Invoker高階篇

 預設情況下,客戶端的HttpInvokerProxy使用J2SEHTTP Client來建立連線,org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor,可以通過設定httpInvokerRequestExecutor屬性來改變預設配置,spring提供了另外一種HttpClientorg.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor

修改配置如下:

<bean id="httpService"

  class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">

  <property name="serviceUrl">

   <value>http://vm.netqin.com:4080/ucs/service/httpService</value>

  </property>

  <property name="serviceInterface" value="com.netqin.baike.service.UcService">

  </property>

  <property name="httpInvokerRequestExecutor">

   <bean

    class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor" />

  </property>

 </bean>

需要在專案中引入兩個jar包:

commons-codec-x.x.jar

commons-httpclient-x.x.x.jar

CommonsHttpInvokerRequestExecutor具有HTTP connection pooling,不過通過使用jmeter進行壓力測試發現,SimpleHttpInvokerRequestExecutor效能高於CommonsHttpInvokerRequestExecutor