Dubbo:Nacos作為註冊中心

東京易冷發表於2019-06-03
前言

本文介紹如何通過Nacos作為配置中心,實現Dubbo服務的註冊與消費

以HelloService服務為例

public interface HelloService {
    String sayHello(String name);
}

服務提供方和消費方都引入以下jar

<dependencys>

    <!-- Spring Boot dependencies -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
    </dependency>

    <!-- Dubbo -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo</artifactId>
    </dependency>

    <!-- Dubbo Registry Nacos -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-registry-nacos</artifactId>
    </dependency>

    <dependency>
        <groupId>com.alibaba.nacos</groupId>
        <artifactId>nacos-client</artifactId>
    </dependency>
</dependencys>

需要搭建一個Nacos服務,這裡直接使用程式設計師DD提供的公益性的Nacos服務,
詳細地址

服務提供方
  1. 實現介面
@Service(version = "${dubbo.service.version}")
public class HelloServiceImpl implements HelloService {

    @Value("${spring.application.name}")
    private String serviceName;

    public String sayHello(String name) {
        return String.format("[%s] : Hello , %s", serviceName, name);
    }

}
  1. 配置服務
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
  1. 載入程式
@SpringBootApplication
public class NacosProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosProviderApplication.class, args);
    }
}
  1. 啟動應用

啟動後在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
  1. 載入程式 & 服務消費
@SpringBootApplication
public class NacosConsumerApplication {

    @Reference(version = "${dubbo.service.version}")
    private HelloService helloService;

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

    @Bean
    public ApplicationRunner runner() {
        return new ApplicationRunner() {
            public void run(ApplicationArguments args) throws Exception {
                System.out.println(helloService.sayHello("consumer"));
            }
        };
    }
}
  1. 啟動程式

控制檯列印出[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

相關文章