在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中的拦截器不重名就不会出现问题了。