看了其余的文章发现,大多数都是只写了关键的部分,对于一个初学者来讲只能明白用了什么东西,但实际动手发现,项目还存在一些问题,经过本篇文章,能够避免一些问题,节省一些时间成本。java
Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,由于采用的是二进制协议,因此它很适合于发送二进制数据。
可是它的参数和返回值都须要实现Serializable接口。git
因为Hessian是一个远程调用的工具,那么咱们须要建立2个springboot项目来模拟,服务端项目:springboot-hessian-server,客户端项目:springboot-hessian-client.web
application.propertiesspring
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,这会引发客户端找不到接口中的方法的问题。springboot
建立service接口mvc
public interface HelloWorldService { String sayHello(String name); }
建立service实现类app
@Service public class HelloWorldServiceImpl implements HelloWorldService { @Override public String sayHello(String name) { return "Hello world! "+ name; } }
Application类maven
@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; }
application.propertieside
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 便可。
server端
https://gitee.com/BAKERSTREET...
client端
https://gitee.com/BAKERSTREET...