rpc之hessian闲聊

参考链接

参考链接

前言

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

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

Spring对RPC的支持及调用模型

  • 不管选择哪种远程调用模型,在spring中都提供了风格一致的支持(通常是由一个代理工厂bean实现),如下图。

Hessian服务发布与调用

模块架构

QQ截图20180826110043.png
  • sb-hessian-client 服务接口定义

  • sb-hessian-consumer 服务调用方

  • sb-hessian-provider 服务提供方

服务接口定义


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;

    }

注意

  • hessian是基于二进制的消息,在服务实现必须实现序列化接口。

  • 关于HessianServiceExporter

    HessianServiceExporter本质上是一个SpringMVC控制器。它通过接收Hessian请求,并把其转换成对应的bean方法调用。

    由setService()和setServiceInterface()暴露服务。

  • 关于HessianProxyFactoryBean

    通过setServiceUrl()和setServiceInterface()获取服务调用

    
    

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


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

> 项目示例链接

[链接](https://gitee.com/lingluojun/sb-rpc-parent.git)
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,332评论 19 139
  • 1.1 spring IoC容器和beans的简介 Spring 框架的最核心基础的功能是IoC(控制反转)容器,...
    simoscode阅读 6,834评论 2 22
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,229评论 6 342
  • 1.1 Spring IoC容器和bean简介 本章介绍了Spring Framework实现的控制反转(IoC)...
    起名真是难阅读 2,663评论 0 8
  • 昨天从格尔木出发,奔唐古拉山镇。后半段开始出现高反,总觉得眼珠子要滚出来,实在没有精力再写,更新中断了,也挺好,为...
    尘羊阅读 275评论 0 1

友情链接更多精彩内容