rpc之hessian闲聊

2018-08-26  本文已影响44人  木易爽

参考链接

参考链接

前言

   在开源世界中有很多优秀的RPC框架,如gRpc,Dubbo,Motan等等,但在某些场景下,此类框架就显得"太重",Spring全家桶是java程序猿的最爱,在rpc方面对多种模型提供了支持,如Hessisan,Burlap以及Http invoker。今天想聊一聊的是基于Spring的轻量级的RPC实现---Hessian。

  Hessian是一种基于Http的轻量级远程服务解决方案,利用二进制消息进行客户端和服务端的交互,采用Servlet暴露服务。因它的二进制消息可以移植到其他非java语言中,具有跨语言特性。

Spring对RPC的支持及调用模型

Hessian服务发布与调用

模块架构

QQ截图20180826110043.png

服务接口定义


public interface HelloService {

    String sayHello(String word);

}

服务实现与暴露


> /**
> 
>  * 服务实现
> 
>  */
> 
> @Component("helloService")
> 
> public class HelloServiceImpl implements HelloService,Serializable {
> 
>     @Override
> 
>     public String sayHello(String word) {
> 
>         return "hessian" + word;
> 
>     }
> 
> }


   /**

     * 服务暴露

     * @param helloService

     * @return

     */

    @Bean(name = "/helloService.service")

    public HessianServiceExporter exportService(@Autowired HelloService helloService) {

        HessianServiceExporter exporter = new HessianServiceExporter();

        exporter.setService(helloService);

        exporter.setServiceInterface(HelloService.class);

        return exporter;

    }


    /**

     * 外部依赖服务

     *

     * @return

     */

    @Bean(name = "helloService")

    public HessianProxyFactoryBean getHelloService() {

        HessianProxyFactoryBean proxy = new HessianProxyFactoryBean();

        proxy.setServiceUrl("http://localhost:8083/helloService.service");

        proxy.setServiceInterface(HelloService.class);

        return proxy;

    }

注意

setServiceUrl("http://localhost:8083/helloService.service");


 注意这里需要指定的服务提供端的端口

> 项目示例链接

[链接](https://gitee.com/lingluojun/sb-rpc-parent.git)
上一篇 下一篇

猜你喜欢

热点阅读