Dubbo:Nacos作为注册中心

雨点打透心脏的1/2处 2022-01-23 10:25 438阅读 0赞
前言

本文介绍如何通过Nacos作为配置中心,实现Dubbo服务的注册与消费

以HelloService服务为例

  1. public interface HelloService {
  2. String sayHello(String name);
  3. }

服务提供方和消费方都引入以下jar

  1. <dependencys>
  2. <!-- Spring Boot dependencies -->
  3. <dependency>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-starter</artifactId>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.dubbo</groupId>
  9. <artifactId>dubbo-spring-boot-starter</artifactId>
  10. </dependency>
  11. <!-- Dubbo -->
  12. <dependency>
  13. <groupId>org.apache.dubbo</groupId>
  14. <artifactId>dubbo</artifactId>
  15. </dependency>
  16. <!-- Dubbo Registry Nacos -->
  17. <dependency>
  18. <groupId>org.apache.dubbo</groupId>
  19. <artifactId>dubbo-registry-nacos</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>com.alibaba.nacos</groupId>
  23. <artifactId>nacos-client</artifactId>
  24. </dependency>
  25. </dependencys>

需要搭建一个Nacos服务,这里直接使用程序员DD提供的公益性的Nacos服务,
详细地址

服务提供方
  1. 实现接口

    @Service(version = “${dubbo.service.version}”)
    public class HelloServiceImpl implements HelloService {

    1. @Value("${spring.application.name}")
    2. private String serviceName;
    3. public String sayHello(String name) {
    4. return String.format("[%s] : Hello , %s", serviceName, name);
    5. }

    }

  2. 配置服务

    spring.application.name = nacos-dubbo-provider

    dubbo.scan.base-packages= com.github.ly641921791.dubbo.example.nacos.provider.service

    dubbo.protocol.name = dubbo
    dubbo.protocol.port = -1

    dubbo.registry.address = nacos://127.0.0.1:8848

    dubbo.registry.address = nacos://nacos.didispace.com:80

    dubbo.service.version = 1.0.0

  3. 引导程序

    @SpringBootApplication
    public class NacosProviderApplication {

    1. public static void main(String[] args) {
    2. SpringApplication.run(NacosProviderApplication.class, args);
    3. }

    }

  4. 启动应用

启动后在Nacos控制台发现应用注册成功,服务名为providers:com.github.ly641921791.dubbo.example.common.service.HelloService:1.0.0

服务消费方
  1. 配置服务

    spring.application.name = nacos-dubbo-consumer

    dubbo.registry.address = nacos://127.0.0.1:8848

    dubbo.registry.address = nacos://nacos.didispace.com:80

    dubbo.service.version = 1.0.0

  2. 引导程序 & 服务消费

    @SpringBootApplication
    public class NacosConsumerApplication {

    1. @Reference(version = "${dubbo.service.version}")
    2. private HelloService helloService;
    3. public static void main(String[] args) {
    4. SpringApplication.run(NacosConsumerApplication.class, args).close();
    5. }
    6. @Bean
    7. public ApplicationRunner runner() {
    8. return new ApplicationRunner() {
    9. public void run(ApplicationArguments args) throws Exception {
    10. System.out.println(helloService.sayHello("consumer"));
    11. }
    12. };
    13. }

    }

  3. 启动程序

控制台打印出[nacos-provider] : Hello , consumer,说明消费成功

源码及参考

源码地址(可运行) :https://github.com/ly641921791/knowledge-examples/tree/master/dubbo-example

  • 服务提供模块 :nacos-dubbo-provider
  • 服务消费模块 :nacos-dubbo-consumer

参考文档(Nacos官网) :http://dubbo.apache.org/zh-cn/docs/user/references/registry/nacos.html

参考案例(Nacos示例) :https://github.com/apache/dubbo-spring-boot-project/tree/master/dubbo-spring-boot-samples/dubbo-registry-nacos-samples

发表评论

表情:
评论列表 (有 0 条评论,438人围观)

还没有评论,来说两句吧...

相关阅读