Hessian将网络传输的对象转换为二进制流通过Http进行传递,不过它是使用自己的序列化机制实现的编组与反编组,其支持的数据类型是有限制的,不支持复杂的对象。Hessian的优点是可以透过防火墙。
Hessian也是轻量级的 ws服务,好处是不需要关心过程,调用时就像调用本地一样,毕竟是RMI,http的话,需要自己做好对象的解析 --------方便
以spring boot整合hessian为例
首先
服务端 和 客户端
添加hessian依赖
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>
服务端
public interface HelloService {
public String sayHello();
}
@Service("helloService")
public class HelloServiceImpl implements HelloService{
@Override
public String sayHello() {
return "Hello Service!!!";
}
}
@Component
public class HelloServicePublic {
@Autowired
private HelloService helloService;
//发布服务
@Bean(name = "/helloService")
public HessianServiceExporter accountService() {
HessianServiceExporter exporter = new HessianServiceExporter();
exporter.setService(helloService);
exporter.setServiceInterface(HelloService.class);
return exporter;
}
}
客户端
public interface HelloService {
public String sayHello();
}
@Component
public class HelloServiceClient {
@Bean
public HessianProxyFactoryBean helloClient() {
HessianProxyFactoryBean factory = new HessianProxyFactoryBean();
factory.setServiceUrl("http://localhost:8081/taskServer/helloService");
factory.setServiceInterface(HelloService.class);
return factory;
}
}
@Controller
public class UserController {
@RequestMapping(value="/getHelloRpc",method= RequestMethod.GET)
@ResponseBody
public String getHelloRpc(HttpServletRequest request ,ShopTaskUser user) {
return helloService.sayHello();
}
}
简单,再也不需要自己做对象的解析了……