spring boot整合hessian(十二)
阿新 • • 發佈:2018-12-26
1.首先新增hessian依賴
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>
2.服務端:HessianServer,埠號:8090
public interface HelloWorldService {
String sayHello(String name);
}
@Service ("HelloWorldService")
public class HelloWorldServiceImpl implements HelloWorldService {
@Override
public String sayHello(String name) {
return "Hello World! " + name;
}
}
@SpringBootApplication
public class HessianServerApplication {
@Autowired
private HelloWorldService helloWorldService;
public static void main(String[] args) {
SpringApplication.run(HessianServerApplication.class, args);
}
//釋出服務
@Bean(name = "/HelloWorldService")
public HessianServiceExporter accountService() {
HessianServiceExporter exporter = new HessianServiceExporter();
exporter.setService(helloWorldService);
exporter.setServiceInterface(HelloWorldService.class);
return exporter;
}
}
3.客戶端程式碼:HessianClient,同服務端一樣引入hessian依賴,埠號:8092
public interface HelloWorldService {
String sayHello(String name);
}
@SpringBootApplication
public class HessianClientApplication {
//想呼叫多個hessian服務就多些幾個下面類似的方法
@Bean public HessianProxyFactoryBean helloClient() { HessianProxyFactoryBean factory = new HessianProxyFactoryBean(); factory.setServiceUrl("http://localhost:8090/HelloWorldService"); factory.setServiceInterface(HelloWorldService.class); return factory; } public static void main(String[] args) { SpringApplication.run(HessianClientApplication.class, args); }}@RestControllerpublic class TestController { @Autowired private HelloWorldService helloWorldService; @RequestMapping("/test") public String test() { return helloWorldService.sayHello("Spring boot with Hessian."); }}