SpringBoot整合dubbo

1、添加依赖


        com.alibaba.boot
        dubbo-spring-boot-starter
        0.2.0
    
    
        com.101tec
        zkclient
        0.2
    

2、生产者配置文件

    spring:
      application:
          name: dubbo-provider
    demo:
      service:
          version: 1.0.0
    dubbo:
       scan:
          basePackages: xxx.xxx.xxx #DemoServiceImpl所在的包路径
       application:
          id: dubbo-provider
          name: dubbo-provider
       protocol:
           id: dubbo
           name: dubbo
           port: 20880
           status: server
       registry:
           id: my-registry
           address: zookeeper://xxx.xxx.xx.xx:2181
           timeout: 100000

3、消费者配置文件

spring:
  application:
    name: dubbo-consumer
demo:
  service:
      version: 1.0.0
dubbo:
   application:
      id: dubbo-consumer
      name: dubbo-consumer
   protocol:
       id: dubbo
       name: dubbo
       port: 20880
   registry:
       address: zookeeper://xxx.xx.xxx.xx:2181
       timeout: 100000

4、定义service接口

public interface DemoService {

    String sayHello(String name);

}

5、生产者实现service接口

@Service(version = "${demo.service.version}",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)
public class DemoServiceImpl implements DemoService {

    @Override
    public String sayHello(String name) {
        return "Hello, " + name + " (from Spring Boot)";
    }
}

6 、消费者调用service

@RestController
public class DemoConsumerController {

    @Reference(version = "${demo.service.version}",
            application = "${dubbo.application.id}",
            url = "dubbo://localhost:20880")
    private DemoService demoService;

    @RequestMapping("/sayHello/{name}")
    public String sayHello(@PathVariable("name") String name) {
        return demoService.sayHello(name);
    }

}

7、zookeeper集群时

配置改为

dubbo:
  registry:
    protocol: zookeeper
    address: ip:2181,ip:2182,ip:2183

8、源码地址

https://gitee.com/hsshy/beam-parent

参考链接:

1.https://github.com/apache/incubator-dubbo-spring-boot-project

加入Java互助群

SpringBoot整合dubbo_第1张图片
image.png

你可能感兴趣的:(SpringBoot整合dubbo)