Hessian通常用来作RPC接口,经过http传输二进制文件,用于程序和程序之间的通讯。 在这个例子中,有两个项目,客户端(hessianClient)和主项目(asset)java
导入依赖为web
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.33</version> </dependency>
MyHessianService.javaspring
package com.ucardemo.asset.api; import com.ucardemo.asset.model.User; public interface MyHessianService { public String justHadEnoughParties(); public boolean checkLogin(User user); }
MyHessianServiceImpl.javaapi
@Service("myHessianService") public class MyHessianServiceImpl implements MyHessianService { [@Override](https://my.oschina.net/u/1162528) public String justHadEnoughParties() { System.out.println("task--------------->"); return "Please save me.."; } [@Override](https://my.oschina.net/u/1162528) public boolean checkLogin(User user) { String username=user.getUsername(); String password=user.getPassword(); if(username.equals("tdw") && password.equals("123456")){ System.out.println("登陆成功"); return true; } System.out.println("登陆失败"); return false; } }
其余的服务器经过访问 http://***/myHessianService 便可调用其接口Service服务器
@Autowired MyHessianService myHessianService; @Bean(name = "/myHessianService") public HessianServiceExporter exportHelloService() { HessianServiceExporter exporter = new HessianServiceExporter(); exporter.setService(myHessianService); exporter.setServiceInterface(MyHessianService.class); return exporter; }
关键点:app
客户端须要调用服务端的服务,须要将服务端项目打包成jar框架
客户端引用jar文件,才能调用接口ide
客户端核心代码:spring-boot
package com.useapi.hessionclient.Controller; import com.caucho.hessian.client.HessianProxyFactory; import com.ucardemo.asset.api.MyHessianService; import com.ucardemo.asset.model.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.net.MalformedURLException; @RestController public class ClientController { @RequestMapping("/xxxx") @ResponseBody public void test() throws MalformedURLException { String url = "http://localhost:8081/myHessianService"; HessianProxyFactory factory = new HessianProxyFactory(); MyHessianService myHessianService = (MyHessianService) factory.create(MyHessianService.class, url); User user=new User(); user.setPassword("123"); user.setUsername("123"); Boolean b= myHessianService.checkLogin(user); System.out.println(b); } }