spring boot整合hessian

原博地址:http://www.jianshu.com/p/71df169bcf5e

首先添加hessian依赖

<dependency>    
      <groupId>com.cauchogroupId>    
       <artifactId>hessianartifactId>    
        <version>4.0.38version>
dependency>

服务端: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;
    }
}

客户端代码:HessianClient,同服务端一样引入hessian依赖,端口号:8092

public interface HelloWorldService {

    String sayHello(String name);
}

@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);
    }
}

@RestController
public class TestController {

    @Autowired
    private HelloWorldService helloWorldService;

    @RequestMapping("/test")
    public String test() {
        return helloWorldService.sayHello("Spring boot with Hessian.");
    }

}

访问地址即可:http://localhost:8092/test
网上很多spring整合hessian的小例子可以对比着看使用方式。

你可能感兴趣的:(spring)