Hessian

来源:互联网 发布:网络诈骗后该如何处理 编辑:程序博客网 时间:2024/04/19 05:55

Hessian 分为服务端和客户端 遵循http协议

        客户端:远程调用接口 通过序列化流传输到服务器端

服务端:提供服务的实现和对外的接口 调用相关服务反序列化流返回结果给客户端

客户端核心类 HessianProxy 处理客户端请求,采用proxy模式调用远程服务接口输出序列化流到服务器端

服务器端核心类 HessianSkeleton 通过反序列化流读取调用的方法和参数 通过反射调用并返回序列化流.

服务端配置 普通实现配置
<servlet>
     <servlet-name>hessianServer</servlet-name>
     <servlet-class>
         com.caucho.hessian.server.HessianServlet
     </servlet-class>
     <init-param>            
         <param-name>home-class</param-name>            
         <param-value>
             <!-- 服务端实现类 -->
             com.hessian.impl.HessianTestServiceImpl
         </param-value>
     </init-param>
     <init-param>            
         <param-name>home-api</param-name>
         <!-- 服务端接口 -->
         <param-value>com.hessian.inte.HessianTestService</param-value>
     </init-param>
 </servlet>

<servlet-mapping>

<servlet-name>hessianServer></servlet-name>

<url-pattern>/hs</url-pattern>

</servlet-mapping>

客户端调用

服务端:提供服务的实现和对外的接口

String url = "http://localhost:8080/hs/hs";HessianProxyFactory factory = new HessianProxyFactory();HessianTestServicehelloService = (HessianTestService) factory.create(HessianTestService.class, url);
hessian+spring 配置
服务端:

@Service("hessianTestService")
public class HessianTestServiceImpl implements HessianTestService {


@Override
public Person sayHelloPerson(String id, String name) {
return new Person(id,name);
}


@Override
public String sayHello(String id, String name) {
return "say hello "+name + " id == "+id;
}
}

 <bean name="/hs" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="hessianTestService"/> <property name="serviceInterface" value="com.hessian.inte.HessianTestService"/> </bean>

客户端:

  <bean id="testHessianService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
    <property name="serviceUrl" value="http://localhost:8080/hs/hessian/hs"/>  
    <property name="serviceInterface" value="com.hessian.HessianTestService"/>  
</bean>  



如果需要扩展  可以 继承 HessianServiceExporter的 handleRequest 做请求认证
HessianProxyFactory,HessianProxy,HessianOutput 等根据源码 扩展自己需求.



0 0
原创粉丝点击