在SSH中使用Hessian
2010-11-16 17:10:13| 分類:遠程調用
| 標籤:ssh hessian 攔截器 |字型大小大中小 訂閱
個人感覺使用hessian可以解決一些需要遠程調用的商務邏輯相對簡單,節點數量不是太多的網路應用。但是當數量增加,邏輯負責,特別是各個節點間又可以相互調用的時候就有些麻煩了。
在SSH結構下使用的Hessian版本最好是spring內建的版本,通常在lib/caucho目錄下。 首先,假設我們要遠程調用的service是Hello,位於org.dreamfly.core.test.Hello.它的實作類別是HelloImpl,位於org.dreamfly.core.test.HelloImpl. 在web.xml 中加入 <servlet> <servlet-name>remote</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>remote</servlet-name> <url-pattern>/remote/*</url-pattern> </servlet-mapping>這段的意思是攔截到任何/remote/ 以下的URL 都交由remote servlet處理。(這個地方有需要注意的,稍後會寫)然後再在WEB-INF目錄加入remote-servlet.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="hessianService" class="org.dreamfly.core.test.HelloImpl"/> <bean name="/hessian" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" > <ref bean="hessianService"></ref> </property> <property name="serviceInterface" > <value>org.dreamfly.core.test.Hello</value> </property> </bean></beans>接下來在任意一個spring的上下文中加入<bean id="hessianProxy" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <property name="serviceUrl"> <value>http://localhost:8080/shuzi/remote/hessian</value> </property> <property name="serviceInterface" value="org.dreamfly.core.test.Hessian"></property> </bean>註:http://localhost:8080/shuzi/remote/hessian中的shuzi是項目名。這樣就可以通過向任意一個bean中注入hessianProxy這個bean來實現遠程調用了。值得一提的是由於struts2也是通過攔截器作用的,我最開始的時候為了省事把struts2的攔截器配成了/*,這樣遠程調用的請求被struts2攔截了,就會提示找不到http://localhost:8080/shuzi/remote/hessian,解決的方法可以有把struts2的攔截器細化,具體到struts2 的namespace,這樣只要struts2 的namespace和web.xml中的攔截器不重名就不會出現問題了。