SpringBoot整合hessian示例

1 定义公共接口

public interface IHelloService {
    String sayHello(String msg);
}

2 Server端实现

2.1 maven依赖

        
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

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

2.2 springboot配置

server.port=8080

2.3 接口实现

@Service("helloService")
public class HelloServiceImpl implements IHelloService {
    @Override
    public String sayHello(String msg) {
        return "hello, " + msg;
    }
}

2.4 Application类

@SpringBootApplication
public class DemoStudyApplication {

    @Autowired
    private IHelloService helloService;

    @Bean(name = "/hessian")
    public HessianServiceExporter hessianServiceExporter() {
        // 使用Spring的HessianServie做代理
        HessianServiceExporter hessianServiceExporter = new HessianServiceExporter();
        hessianServiceExporter.setService(helloService);
        hessianServiceExporter.setServiceInterface(IHelloService.class);
        return hessianServiceExporter;
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoStudyApplication.class,args);
    }
}

3 Client端实现

3.1 maven依赖

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

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

3.2 springboot配置

server.port=9080

3.3 Application类

@SpringBootApplication
public class DemoSpringbootApplication {

    @Bean
    public HessianProxyFactoryBean hessianClient() {
        HessianProxyFactoryBean hessianProxyFactoryBean = new HessianProxyFactoryBean();
        hessianProxyFactoryBean.setServiceUrl("http://localhost:8080/hessian");
        hessianProxyFactoryBean.setServiceInterface(IHelloService.class);
        return hessianProxyFactoryBean;
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoSpringbootApplication.class, args);
    }

}

3.4 Controller类

@RestController
public class HessianController {

    @Autowired
    private IHelloService helloService;

    @GetMapping("/hessianClient")
    public String helloHessian() {
        return helloService.sayHello("jtzen9");
    }
}

3.5 启动测试

 http://localhost:9080/hessianClient

你可能感兴趣的:(Java开源组件库)