美文网首页springboot
springboot整合hessian

springboot整合hessian

作者: 黄浦 | 来源:发表于2016-11-24 17:23 被阅读1204次

    1.新建两个springboot项目 HessianServer和HessianClient

    2.添加hessian maven引用

    <dependency>    
          <groupId>com.caucho</groupId>    
           <artifactId>hessian</artifactId>    
            <version>4.0.38</version>
    </dependency>
    
    

    3.HessianServer中添加服务并实现服务

    HelloWorldService

    public interface HelloWorldService {    
        String sayHello(String name);
    }
    

    HelloWorldServiceImpl

    public class HelloWorldServiceImpl implements HelloWorldService {
        @Override    
        public String sayHello(String name) {
            return "Hello World! " + name;    
        }
    }
    

    4.发布服务,在application中添加

    @Autowired
    private HelloWorldService helloWorldService;
    @Bean(name = "/HelloWorldService")
    public HessianServiceExporter accountService() {   
        HessianServiceExporter exporter = new HessianServiceExporter();      
        exporter.setService(helloWorldService);   
        exporter.setServiceInterface(HelloWorldService.class);   
        return exporter;
    }
    

    5. HessianClient添加HelloWorldService接口

    6.注解配置远程接口信息

    @Bean
    public HessianProxyFactoryBean helloClient() {              
          HessianProxyFactoryBean factory = new HessianProxyFactoryBean();   
          factory.setServiceUrl("http://localhost:8090/HelloWorldService");   
          factory.setServiceInterface(HelloWorldService.class);   
          return factory;
    }
    

    7.调用测试

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

    8.测试结果,如下显示成功

    屏幕快照 2016-11-24 下午5.22.20.png

    demo地址: https://github.com/HuangPugang/spring-boot-hessian-demo/tree/master

    相关文章

      网友评论

        本文标题:springboot整合hessian

        本文链接:https://www.haomeiwen.com/subject/honbpttx.html