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);
}
启动类
/**
* @author alen
* @create 2018-12-03 20:53
**/
@SpringBootApplication
public class HessianClientApplication {
@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);
}
}
测试Controller
@RestController
public class TestController {
@Autowired
private HelloWorldService helloWorldService;
@RequestMapping("/test")
public String test() {
return helloWorldService.sayHello("Spring boot with Hessian.");
}
}