一、什么是Hessian
Hessian 是一个基于 binary-RPC 实现的远程通讯 library。使用二进制传输数据。
Hessian通常通过Web应用来提供服务,通过接口暴露。
Servlet和Spring的DispatcherServlet都可以把请求转发给Hessian服务。
由以下两种方式提供,分别为:
com.caucho.hessian.server.HessianServlet、org.springframework.web.servlet.DispatcherServlet。
服务端开发:
服务发布的两种方式:
1 通过web.xml的servelet进行服务的发布。
2 通过spring集成进行服务的发布
第一步:创建一个web项目,并导入hessian的jar包
第二步:创建一个接口
public interface HelloService {
public String sayHello(String name);
public List<User> findAllUser();
}
第三步:提供上面接口的实现类
public class HelloServiceImpl implements HelloService{
public String sayHello(String name) {
System.out.println("sayHello方法被调用了");
return "hello " + name;
}
public List<User> findAllUser() {
List<User> list = new ArrayList<User>();
list.add(new User(1, "小王"));
list.add(new User(2,"小白"));
return list;
}
}
第四步:在web.xml中配置服务
<servlet>
<servlet-name>hessian</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>home-class</param-name>
<param-value>cn.xiao.service.HelloServiceImpl</param-value>
</init-param>
<init-param>
<param-name>home-api</param-name>
<param-value>cn.xiao.service.HelloService</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>hessian</servlet-name>
<url-pattern>/hessian</url-pattern>
</servlet-mapping>
客户端开发:
第一步:创建一个客户端项目,并导入hessian的jar包
第二步:创建一个接口(和服务端接口对应)
public interface HelloService {
public String sayHello(String name);
public Object findAllUser();
}
第三步:使用hessian提供的方式创建代理对象调用服务
HessianProxyFactory factory = new HessianProxyFactory();
HelloService proxy =
(HelloService) factory.create(HelloService.class, "http://localhost:8080/hessian_server/hessian");
String ret = proxy.sayHello("test");
System.out.println(ret);
Object users = proxy.findAllUser();
System.out.println(users);
==============================================================================================================================
方式二:通过spring集成发布服务,服务端开发
<?xml version="1.0" encoding="UTF-8"?>
-<beans xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">
<!-- 业务类 -->
<bean class="cn.xiao.crm.service.impl.CustomerServiceImpl" id="customerService"/>
<!-- 注册hessian服务 -->
- <bean class="org.springframework.remoting.caucho.HessianServiceExporter" id="/customer">
<!-- 业务接口实现类 -->
<property ref="customerService" name="service"/>
<!-- 业务接口 -->
<property name="serviceInterface" value="cn.xiao.crm.service.CustomerService"/>
</bean>
</beans>
客户端开发:通过在客户端创建代理对象调用服务
<!-- 配置远程服务的代理对象 -->
-<bean class="org.springframework.remoting.caucho.HessianProxyFactoryBean" id="customerService">
<!-- 注入接口类型 -->
<property value="com.xiao.bos.crm.CustomerService" name="serviceInterface"/>
<!-- 服务访问路径 -->
<property value="http://localhost:8080/crm/remoting/customer" name="serviceUrl"/>
</bean>
客户端接口:
package com.itheima.bos.crm;
import java.util.List;
import cn.xiao.crm.domain.Customer;
// 客户服务接口
public interface CustomerService {
// 未关联定区客户
public List<Customer> findnoassociationCustomers();
// 查询已经关联指定定区的客户
public List<Customer> findhasassociationCustomers(String decidedZoneId);
// 将未关联定区客户关联到定区上
public void assignCustomersToDecidedZone(Integer[] customerIds, String decidedZoneId);
}
按类型匹配,注入
@Autowired
protected CustomerService customerService;