SpringBoot整合dubbo

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值