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 服務模式

HTTP invoker 服務模式

說明:

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

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

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

配置伺服器端和客戶端的步驟

配置伺服器端

1. 新增 springJAR 檔案

建議使用 spring2+.jar 版本

2. 建立服務介面

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

4. 公開服務

配置客戶端

1. 新增 springJAR 檔案

建議使用 spring2+.jar 版本

2. 建立服務介面

3. 訪問服務

例項講解

伺服器端

1. 服務介面: UcService.java

它提供兩項服務,查詢使用者資訊和記錄日誌,如下:

public interface UcService {

public UserInfo getUserInfobyName(String userName);

public int recordLog(String username, String point, String operate, String desc);

}

說明:舉這個列子是因為其比較有代表性,它將展示普通資料型別( int,long 等)和複雜資料型別( DTO 等)的遠端呼叫方式。 UserInfo 是一個普通的 DTO ,程式碼如下:

public class UserInfo implements Serializable {

private static final long serialVersionUID = -6970967506712260305L;

/** 使用者名稱 */

private String userName ;

/** 電子郵箱 */

private String email ;

/** 註冊日期 */

private Date registDate ;

public String getUserName() {

return userName ;

}

public void setUserName(String userName) {

this userName = userName;

}

public String getEmail() {

return email ;

}

public void setEmail(String email) {

this email = email;

}

public Date getRegistDate() {

return registDate ;

}

public void setRegistDate(Date registDate) {

this registDate = registDate;

}

}

注意:因為是在網路間傳輸物件,所以需要將 UserInfo 實現 Serializable 介面,並指定一個 serialVersionUID (任意值即可,同時客戶端也要有這個類,否則在客戶端接收物件時會因為 serialVersionUID 不匹配而出現異常)

回到UcService.java ,它提供了兩個服務(在這裡一個方法代表一個服務功能),我們需要具體的實現類來實現真正的服務

2. 實現類是 UCServiceImpl.java

public class UCServiceImpl implements UcService {

private static Logger pointrecordlog = Logger.getLogger ( "pointrecordlog" );

private static Logger logger = Logger.getLogger (UCServiceImpl. class );

private UcFacade ucFacade ;

public void setUcFacade(UcFacade ucFacade) {

this ucFacade = ucFacade;

}

public UserInfo getUserInfobyName(String userName) {

UserInfo user = null ;

try {

user = ucFacade .getUserInfoDetail(userName);

logger .debug( "get userinfo success by username:" + userName);

catch (Throwable t) {

logger .error( "get userinfo fail by username:" + userName, t);

}

return user;

}

public int recordLog(String username, String point, String operate, String desc) {

int result = 0;

try {

pointrecordlog .info(username + " - " + point + " - " + operate + " - " + desc);

catch (Throwable t) {

result = -1;

logger .error(t);

}

return result;

}

}

說明: ucFacade 是通過 spring 注入的一個數據查詢類,因為它與 http invoker 沒有直接關係,所以不進行介紹。

3. 公開服務 UcService.java

² WEB-INF/application-context.xml :將介面宣告為 HTTP invoker 服務

< bean id = "httpService"

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

< property name = "service" >

< ref bean = "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 >

² WEB-INF/web.xml :配置 spring 監聽及 DispatcherServlet

< context-param >

< param-name > contextConfigLocation </ param-name >

< param-value >

/WEB-INF/application-context.xml

</ param-value >

</ context-param >

< listener >

< listener-class >

org.springframework.web.context.ContextLoaderListener

</ listener-class >

</ listener >

< servlet >

< servlet-name > service</ servlet-name >

< servlet-class >

            org.springframework.web.servlet.DispatcherServlet

        </ servlet-class >

        < load-on-startup > 1 </ load-on-startup >

    </ servlet >

    < servlet-mapping >

        < servlet-name > service </ servlet-name >

        < url-pattern > /service/* </ url-pattern >

    </ servlet-mapping >

說明:不瞭解為什麼這麼配置的可以去看看 spring mvc 方面的資料。

好了,經過以上配置,一個基於 spring HTTP invoker 的遠端服務就完成了,服務的地址為:

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

客戶端

1.        建立服務介面及網路間傳輸的 DTO 類

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

2.        配置訪問服務

²       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 為實際的伺服器地址

²         WEB-INF/web.xml :配置 spring 監聽

如果專案沒有 spring 環境,則需要在 web.xml 中加入對 spring 的支援

< context-param >

        < param-name > contextConfigLocation </ param-name >

        < param-value >

            /WEB-INF/application-context.xml

        </ param-value >

    </ context-param >

    < listener >

        < listener-class >

            org.springframework.web.context.ContextLoaderListener

        </ listener-class >

</ listener >

3.        訪問服務方法

u         讀取 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" );

u         依賴注入,遠端呼叫 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;

}

關於伺服器端配置的補充說明:

 有一個誤區:有些關於springMVC的書 上說,如果沒有明確宣告一個處理介面卡,預設會使用 org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,這個介面卡 專門負責處理所有實現了

org.springframework.web.servlet.mvc.Controller 介面的處理器,我就是受其影響,認為 org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter實現的是 org.springframework.web.HttpRequestHandler介面,所以按理說應該使用的處理介面卡是 org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,但實際上並不會出現異 常。

其實,原因是因為spring預設會使用四個處理介面卡(參看DispatcherServlet.properties,spring2.5,spring2.0只預設三個,2.5增加註解方式):

org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,/
 org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,/
 org.springframework.web.servlet.mvc.throwaway.ThrowawayControllerHandlerAdapter,/
 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

 關於DispatcherServlet.properties的詳細資訊可以參看:

但是,如果明確聲明瞭其它的處理介面卡,比如 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter, 等等,則預設規則則會覆蓋,需要明確宣告 org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter這個處理介面卡,否則系 統會拋異常:

javax.servlet.ServletException: No adapter for handler [org.[email protected]179bd14]: Does your handler implement a supported interface like Controller?

所以,建議在使用spring invoker時,最好明確宣告org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter這個處理介面卡

補充:

預設情況下,客戶端的HttpInvokerProxy使用J2SE的HTTP Client來建立連線,即org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor,可以通過設定httpInvokerRequestExecutor屬性來改變預設配置,spring提供了另外一種HttpClient,org.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