RPC远程调用及常用框架之Hessian

说明

在大量实例和分布式应用的架构下,肯定存在这不同实例之间的服务调用和依赖,那么这就是我们常说的远程服务调用简称rpc,常用的有以下框架
从语言兼容上的rpc框架有 thrift zeroC-ICE protbuf
从服务治理角度的rpc架构有 dubbo、dubbox、 RMI、Hessian、 spring Cloud,gRPC
Thrift:thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 这些编程语言间无缝结合的、高效的服务。

Dubbo:Dubbo是一个分布式服务框架,以及SOA治理方案。其功能主要包括:高性能NIO通讯及多协议集成,服务动态寻址与路由,软负载均衡与容错,依赖分析与降级等。 Dubbo是阿里巴巴内部的SOA服务化治理方案的核心框架,Dubbo自2011年开源后,已被许多非阿里系公司使用。

Spring Cloud:Spring Cloud由众多子项目组成,如Spring Cloud Config、Spring Cloud Netflix、Spring Cloud Consul 等,提供了搭建分布式系统及微服务常用的工具,如配置管理、服务发现、断路器、智能路由、微代理、控制总线、一次性token、全局锁、选主、分布式会话和集群状态等,满足了构建微服务所需的所有解决方案。Spring Cloud基于Spring Boot, 使得开发部署极其简单。

gRPC: 一开始由 google 开发,是一款语言中立、平台中立、开源的远程过程调用(RPC)系统。
Dubbox 是一个分布式服务框架,其前身是阿里巴巴开源项目 Dubbo ,被国内电商及互联网项目中使用,后期阿里巴巴停止了该项目的维护,当当网便在 Dubbo 基础上进行优化,并继续维护,为了与原有的 Dubbo 区分,故将其命名为 Dubbox。
Dubbox致力于提高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案,简单的说,dubbox就是个服务框架,如果没有服务式的需求,其实是不需要用的,只有在分布式的时候,才有dubbox这样的分布式服务框架的需求.
Hessian是一个轻量级的remoting on http框架(远程调用框架),采用的是Binary RPC协议(二进制远程调用协议),和我们在web工程中常用的webservice比较类似,不过是个比较轻量级的框架,还有一点不一样的是webserce服务端和客户端何意用不同的框架,例如服务端用CXF,客户端用Axis。Hessian不行,调用方和被调用方必须都是Hessian。Hessian可通过Servlet提供远程服务,需要将请求映射到Hessian服务。也可Spring框架整合。

springboot中使用Hessian

依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<modules>
		<module>hessianserver</module>
		<module>hessianclient</module>
	</modules>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.7.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>hessian</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>hessian</name>
	<description>Demo project for Spring Boot</description>
    <packaging>pom</packaging>
	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--hessian-->
		<dependency>
			<groupId>com.caucho</groupId>
			<artifactId>hessian</artifactId>
			<version>4.0.38</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

服务端代码

服务端在springBoot的启动类进行配置(@SpringBootApplication),或者在标记为配置类的代码中进行配置(@Configuration)。如果启动类中已经配置了很多东西,推荐做单独的配置类,也方便管理。

## 实现类
@Service("sayHelloHessian")
public class SayHelloHessianImpl implements SayHelloHessian {
    private Logger log = Logger.getLogger(SayHelloHessianImpl.class);

    @Override
    public String SayHello(String msg) {
        log.info("-----------进入hessian方法,客户端参数:"+ msg +"--------------");
        return "服务端返回:hello hessian";
    }
}

## 配置暴露
@Configuration //标记为spring 配置类
public class HessionServiceConfig {

    @Resource
    private SayHelloHessian sayHelloHessian;

    /**
     * 1. HessianServiceExporter是由Spring.web框架提供的Hessian工具类,能够将bean转化为Hessian服务
     * 2. @Bean(name = "/helloHessian")加斜杠方式会被spring暴露服务路径,发布服务。
     * @return
     */
    @Bean("/helloHessian")
    public HessianServiceExporter exportHelloHessian()
    {
        HessianServiceExporter exporter = new HessianServiceExporter();
        exporter.setService(sayHelloHessian);
        exporter.setServiceInterface(SayHelloHessian.class);
        return exporter;
    }
}
到此为止一个简单的基于Hessian的远程调用服务端就完成了,比webservice简单多了

客户端

客户端同样需要服务端的jar。除了jar依赖,客户端还需要两个东西,第一个是和业务端一样的接口代码(这里依赖父模块把接口提出去了),还有一个就是Hessian的连接对象。客户端可以做成spring管理的模式,一个接口配置一个,也可以做成我下面写的这种,一个工具类,每次调用。


客户端获取连接对象工具类   

public class HessianProxyFactoryUtil {

    private Logger log = Logger.getLogger(HessianProxyFactoryUtil.class);

    /**
     *  获取调用端对象
     * @param clazz 实体对象泛型
     * @param url 客户端url地址
     * @param <T>
     * @return 业务对象
     */
    public static <T> T getHessianClientBean(Class<T> clazz,String url) throws Exception
    {
        // 客户端连接工厂,这里只是做了最简单的实例化,还可以设置超时时间,密码等安全参数
        HessianProxyFactory factory = new HessianProxyFactory();

        return (T)factory.create(clazz,url);
    }

    //
    public static void main(String[] args) {

        // 服务器暴露出的地址
        String url = "http://localhost:8080/SpringBootDemo/helloHessian.do";

        // 客户端接口,需与服务端对象一样
        try {
            SayHelloHessian helloHessian = HessianProxyFactoryUtil.getHessianClientBean(SayHelloHessian.class,url);
            String msg =  helloHessian.SayHello("你好");

            System.out.println(msg);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
这里直接在utils中写了个main方法,我们启动服务端后运行下man方法
当然也可以直接注入
@Configuration
public class ClientConfig {
    @Bean
    public HessianProxyFactoryBean helloClient() {
        HessianProxyFactoryBean factory = new HessianProxyFactoryBean();
        factory.setServiceUrl("http://localhost:8080/helloHessian");
        factory.setServiceInterface(SayHelloHessian.class);
        return factory;
    }
}
@RestController
public class TestController {
    @Autowired
    SayHelloHessian sayHelloHessian;
    @RequestMapping("/test")
    public String test(){
        return sayHelloHessian.SayHello("Spring boot with Hessian.");
    }

}

测试

服务端控制台输出
在这里插入图片描述
客户端访问浏览器
在这里插入图片描述

参考:参考1
参考: 参考2

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值