Spring Boot整合hessian入门

前言

看了其他的文章发现,大多数都是只写了关键的部分,对于一个初学者来说只能明白用了什么东西,但实际动手发现,项目还存在一些问题,通过本篇文章,可以避免一些问题,节省一些时间成本。

Hessian简介

Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。
但是它的参数和返回值都需要实现Serializable接口。

代码实现

由于Hessian是一个远程调用的工具,那么我们需要创建2个springboot项目来模拟,服务端项目:springboot-hessian-server,客户端项目:springboot-hessian-client.

springboot-hessian-server端

application.properties

server.port=8081

server端pom.xml

<!-- hessian -->
        <dependency>
            <groupId>com.caucho</groupId>
            <artifactId>hessian</artifactId>
            <version>4.0.38</version>
        </dependency>

        <!-- springboot-mvc-->
        <!-- 添加的原因是 HessianServiceExporter 在这个jar包中 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <!-- 创建Spring Boot项目时会出现这个插件 -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!-- 修改一下 -->
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

需要注意上面那个插件,默认的时候创建,但是没有下面<configuration>中的内容,打包之后,反编译会发现根路径是BOOT-INF,这会引起客户端找不到接口中的方法的问题。

创建service接口

public interface HelloWorldService {
    String sayHello(String name);
}

创建service实现类

@Service
public class HelloWorldServiceImpl implements HelloWorldService {
    @Override
    public String sayHello(String name) {
        return "Hello world! "+ name;
    }
}

Application类

@SpringBootApplication
public class TestSpringbootHessianApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestSpringbootHessianApplication.class, args);
    }

    @Autowired
    private HelloWorldService helloWorldService;

    @Bean(name = "/hello/world/service")
    public HessianServiceExporter accountService(){
        HessianServiceExporter exporter = new HessianServiceExporter();
        exporter.setService(helloWorldService);
        exporter.setServiceInterface(HelloWorldService.class);
        return exporter;
    }

client端

application.properties

    server.port=8082

pom.xml

    <!-- hessian -->
        <dependency>
            <groupId>com.caucho</groupId>
            <artifactId>hessian</artifactId>
            <version>4.0.38</version>
        </dependency>

        <!-- HessianProxyFactoryBean在这个包下 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
       <!-- 服务端jar包 -->
        <dependency>
            <groupId>com.test.springboot.hessian</groupId>
            <artifactId>test-hessian</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

Application类

@SpringBootApplication
public class TestSpringbootHessianClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestSpringbootHessianClientApplication.class, args);
    }

    @Bean
    public HessianProxyFactoryBean helloClient(){
        HessianProxyFactoryBean factoryBean = new HessianProxyFactoryBean();
        factoryBean.setServiceUrl("http://localhost:8081/hello/world/service");
        factoryBean.setServiceInterface(HelloWorldService.class);
        return factoryBean;
    }

}

controller

@RestController
public class HelloWorldController {

    @Autowired
    HelloWorldService helloWorldService;

    @RequestMapping("/test")
    public String test(){
        return helloWorldService.sayHello("zzz");
    }
}

将服务端的代码打包安装到本地仓库,打开浏览器输入 http://localhost:8082/test 即可。

附上本人的git地址:

server端
https://gitee.com/BAKERSTREET...
client端
https://gitee.com/BAKERSTREET...

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值