Hessian是一种常用的远程调用方式,当第三方应用使用hessian协议提供服务时,就不用常用的http测试工具进行mock测试了,因为hessian协议只能用hessian协议访问。这里记录了hessian如何在Spring Boot里调用,进行mock测试模拟。
1、项目结构 1.1 server 1.2 client 2、pom文件 2.1 hessian server2.2 hessian client4.0.0 com.example performanceserver0.0.1-SNAPSHOT springboot_demo Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent2.1.8.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-webcom.caucho hessian4.0.38 org.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-configuration-processortrue org.springframework.boot spring-boot-maven-plugin
3、application.propertities 3.1 hessian server4.0.0 com.example performanceserver0.0.1-SNAPSHOT springboot_demo Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent2.1.8.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-webcom.caucho hessian4.0.38 org.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-configuration-processortrue org.slf4j slf4j-log4j12org.projectlombok lomboktrue org.springframework.boot spring-boot-maven-plugin
server.servlet.context-path=/hessian-server server.port=82803.2 hessian client
server.servlet.context-path=/hessian-client server.port=81804、代码实现 4.1 hessian-interface的统一接口
package springboot_demo.service;
public interface HessianServiceInterface {
String appleUpload(String methodName, String json);
String test(String name);
}
4.2 hessian-service的服务注册
4.2.1.注入service,实现hessian-interface中的接口
package springboot_demo.serviceimpl;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;
import springboot_demo.controller.BatchResponese;
import springboot_demo.service.HessianServiceInterface;
@Service
public class HessianTestService implements HessianServiceInterface {
@Override
public String appleUpload(String methodName, String json){
// System.out.println(methodName);
// System.out.println(json);
String result = null;
BatchResponese response = new BatchResponese();
response.setApiErrorMsg("");
response.setApiResponseID("00017E333AD82D3FEFC7FF06F625B63F");
response.setApiResultCode("A1000");
response.setApiResultData("{"Response":{"Result":"1","Note":"SUCCESS","payloadDetails":[{"customResponse":{"SNmetaData":{"serialNumbersDetails":[{"serialNumberStatus":"30","serialNumber":"201065HHHG","siteCode":"QTBG","configCode":"0FM2","countryOfOrigin":"","weekCode":"21-09"}],"totalCountSN":"10","partnerID":"3P151AHUB"}},"seqNo":"0001","statusCode":"0000"}],"controlNumber":"000032376"}}");
Gson gson = new Gson();
result = gson.toJson(response);
System.out.println(result);
return result;
}
@Override
public String test(String str) {
System.out.println("接口接收:"+str);
return "aaa-"+str;
}
}
4.2.2.注入service的服务发现
package springboot_demo.component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.remoting.caucho.HessianServiceExporter;
import org.springframework.stereotype.Component;
import springboot_demo.service.HessianServiceInterface;
import springboot_demo.serviceimpl.HessianTestService;
@Component
public class HessianUtil {
@Autowired
private HessianTestService hessianTestService;
//发布服务,此处的name必须以斜杠开头,是客户端的访问地址
@Bean(name = "/hessian/apple")
public HessianServiceExporter appleUpload_string_string() {
//HessianServiceExporter 位于spring-web内
HessianServiceExporter exporter = new HessianServiceExporter();
//hessianTest已由Spring注入为hessianTestImpl(实现类)对象
exporter.setService(hessianTestService);
//注意:此处的HessianTest是接口的类类型,不是实现类的
exporter.setServiceInterface(HessianServiceInterface.class);
return exporter;
}
}
4.3.hessian-client的客户端发现服务
package springboot_demo.component;
import org.springframework.context.annotation.Bean;
import org.springframework.remoting.caucho.HessianProxyFactoryBean;
import org.springframework.stereotype.Component;
import springboot_demo.service.HessianServiceInterface;
@Component
public class HessianClientComponent {
@Bean
public HessianProxyFactoryBean helloClient() {
HessianProxyFactoryBean factory = new HessianProxyFactoryBean();
//该接口的路径,需要和4.2.2中注册的bean名称一致
factory.setServiceUrl("http://localhost:8280/hessian-server/hessian/apple");
//HessianServiceInterface为接口名
factory.setServiceInterface(HessianServiceInterface.class);
return factory;
}
}
4.4 服务端调用测试
package springboot_demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springboot_demo.service.HessianServiceInterface;
@RestController
public class TestController {
@Autowired
private HessianServiceInterface hessianService;
@RequestMapping(value="/test/{id}")
public String test(@PathVariable("id")String id) {
String sayTest = hessianService.test("aa"+id);
System.out.println("请求ID:"+id+ ",接口返回:..............."+sayTest);
return sayTest;
}
@RequestMapping(value = "/apple", method = RequestMethod.GET)
public String apple(){
String res = hessianService.appleUpload("","");
System.out.println(res);
return res;
}
}
4.5 客户端调用测试
package springboot_demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springboot_demo.service.HessianServiceInterface;
@RestController
public class TestController {
//客户端需要引入相应的接口,客户端调用服务端代码
@Autowired
private HessianServiceInterface hessianService;
@RequestMapping(value="/test/{id}")
public String test(@PathVariable("id")String id) {
String sayTest = hessianService.test("aa"+id);
System.out.println("请求ID:"+id+ ",服务端返回:..............."+sayTest);
return sayTest;
}
@RequestMapping(value = "/apple", method = RequestMethod.GET)
public String apple(){
String res = hessianService.appleUpload("","");
System.out.println(res);
return res;
}
}
5.服务启动5.1 服务端启动
package springboot_demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("springboot_demo")
public class HessianServerApplication {
public static void main(String[] args) {
SpringApplication.run(HessianServerApplication.class, args);
}
}
5.2 客户端启动
package springboot_demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("springboot_demo")
public class HessianClientApplication {
public static void main(String[] args) {
SpringApplication.run(HessianClientApplication.class, args);
}
}
6 测试
服务端、客户端均调用成功,至此模拟完成,不用再等客户联调了,自己构造数据先行测试。
http://100.118.36.37:8180//hessian-client/apple
http://100.118.36.37:8280//hessian-server/apple
7 脚本编写过程中遇到的报错 7.1 ComponentScan配置Field hessianTestService in springboot_demo.component.HessianUtil required a bean of type 'springboot_demo.serviceimpl.HessianTestService' that could not be found.
@SpringBootApplication
@ComponentScan("springboot_demo.component")
public class HessianServerApplication {
public static void main(String[] args) {
SpringApplication.run(HessianServerApplication.class, args);
}
}
ComponentScan("springboot_demo.component")扫描的包路径为springboot_demo.component,而服务暴露调用的HessianTestService在springboot_demo.serviceimpl路径下



