看了其餘的文章發現,大多數都是隻寫了關鍵的部分,對於一個初學者來講只能明白用了什麼東西,但實際動手發現,項目還存在一些問題,經過本篇文章,能夠避免一些問題,節省一些時間成本。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...