SpringBoot整合Hessian
Hessian是一个轻量级的Binary RPC(二进制远程调用协议)协议的remoting on http框架(远程调用框架),这个跟我们以前常用的webservice比较类似,但是Hessian比较轻量级。
需要的maven依赖
<!--引入hessian-->
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>
1、服务端代码
1.1、业务接口
package com.msiwang.webservicedemo.service;
/**
* @author : wangm
* create at: 2020/9/18 9:18 AM
* @description: 测试hessian demo接口
*/
public interface HelloHessianService {
/**
* Discreption: 接口测试方法
* @param: [name]
* @return: java.lang.String
* @author: wangm
* @date: 2020/9/18 1:51 PM
*/
String hello(String name);
}
1.2、服务端业务代码接口实现类
package com.msiwang.webservicedemo.service;
import org.springframework.stereotype.Service;
/**
* @author : wangm
* create at: 2020/9/18 9:19 AM
* @description: 测试hessian demo接口
*/
@Service
public class HelloHessianServiceImpl implements HelloHessianService {
/**
* Discreption: 接口测试方法
* @param: [name]
* @return: java.lang.String
* @author: wangm
* @date: 2020/9/18 1:51 PM
*/
@Override
public String hello(String name) {
return "Hello Hessian "+name;
}
}
1.3、服务端接口发布配置类,通过spirng的Configuration注解配置
package com.msiwang.webservicedemo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.caucho.HessianServiceExporter;
/**
* @author : wangm
* create at: 2020/9/18 1:47 PM
* @description: hessian服务发布配置类
*/
@Configuration
public class HessianConfig {
@Autowired
private HelloHessianService helloHessianService;
//发布服务
@Bean(name = "/hessian")
public HessianServiceExporter hessianServer() {
HessianServiceExporter exporter = new HessianServiceExporter();
exporter.setService(helloHessianService);
exporter.setServiceInterface(HelloHessianService.class);
return exporter;
}
}
2、客户端调用代码
2.2、Application启动类
package com.msiwang.webserviceclient;
import com.msiwang.webserviceclient.service.HelloHessianService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.remoting.caucho.HessianProxyFactoryBean;
@SpringBootApplication
public class WebserviceClientApplication {
@Bean
public HessianProxyFactoryBean hessianClient() {
HessianProxyFactoryBean hessianProxyFactoryBean = new HessianProxyFactoryBean();
hessianProxyFactoryBean.setServiceUrl("http://localhost:8978/hessian");
hessianProxyFactoryBean.setServiceInterface(HelloHessianService.class);
return hessianProxyFactoryBean;
}
public static void main(String[] args) {
SpringApplication.run(WebserviceClientApplication.class, args);
}
}
2.2、客户端业务接口,这里跟服务端业务接口一致
package com.msiwang.webserviceclient.service;
/**
* @author : wangm
* create at: 2020/9/18 9:18 AM
* @description: 测试hessian client接口
*/
public interface HelloHessianService {
/**
* Discreption: 接口测试方法
* @param: [name]
* @return: java.lang.String
* @author: wangm
* @date: 2020/9/18 1:51 PM
*/
String hello(String name);
}