SpringBoot整合dubbo

作者: yellow_han | 来源:发表于2018-08-15 09:52 被阅读172次

    1、添加依赖

    <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.2</version>
        </dependency>
    

    2、生产者配置文件

        spring:
          application:
              name: dubbo-provider
        demo:
          service:
              version: 1.0.0
        dubbo:
           scan:
              basePackages: xxx.xxx.xxx
           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);
        }
    
    }
    

    完。
    参考链接:

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

    相关文章

      网友评论

        本文标题:SpringBoot整合dubbo

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