Nacos with Dubbo fusion become registry

Nacos as Dubbo ecosystems important registry implementation, including dubbo-registry-nacos is Dubbo fusion Nacos registry implementation.

Preparatory work

When you put dubbo-registry-nacos integrated into your dubbo project before, please make sure the background nacos service has started. If you are still not familiar with the basic use of Nacos, reference Quick Start for Nacos.

Quick Start

Nacos Dubbo fusion become registry procedure is very simple, general steps can be divided into “increasing Maven dependency” and “the registry”.

Increasing Maven dependency

First, you need to dubbo-registry-nacosMaven dependent on added to your project pom.xml file, and strongly recommend that you use the dubbo 2.6.5:

  1. <dependencies>
  2. ...
  3. <!-- Dubbo Nacos registry dependency -->
  4. <dependency>
  5. <groupId>com.alibaba</groupId>
  6. <artifactId>dubbo-registry-nacos</artifactId>
  7. <version>0.0.1</version>
  8. </dependency>
  9. <!-- Dubbo dependency -->
  10. <dependency>
  11. <groupId>com.alibaba</groupId>
  12. <artifactId>dubbo</artifactId>
  13. <version>2.6.5</version>
  14. </dependency>
  15. <!-- Alibaba Spring Context extension -->
  16. <dependency>
  17. <groupId>com.alibaba.spring</groupId>
  18. <artifactId>spring-context-support</artifactId>
  19. <version>1.0.2</version>
  20. </dependency>
  21. ...
  22. </dependencies>

When a project to adddubbo-registry-nacos, you don’t need to explicitly logic programming to realize service discovery and registration, actual implementation is provided by the third party and then configure Naocs registry.

Configuration registry

Suppose you Dubbo application using the Spring Framework assembly, there will be two kinds of optional configuration method, respectively: Dubbo Spring externalized configuration, and the Spring XML configuration files, and, I strongly recommend the former.

Dubbo Spring externalized configuration

Dubbo Spring externalized configuration consists of Dubbo 2.5.8 introduced new features, through the Spring Environment attribute automatically generate and bind the Dubbo configuration Bean, implement configuration to simplify, and lower the threshold of service development.

Suppose you Dubbo applications using Zookeeper as registry, and the server IP address is:10.20.153.10 at the same time, the registered address as Dubbo externalized configuration properties are stored in dubbo-config.properties file, as shown below:

  1. ## application
  2. dubbo.application.name = your-dubbo-application
  3. ## Zookeeper registry address
  4. dubbo.registry.address = zookeeper://10.20.153.10:2181
  5. ...

Suppose you Nacos Server running on the Server also 10.20.153.10 , and use the default Nacos service port 8848, you only need to dubbo.registry.address property adjustment is as follows:

  1. ## 其他属性保持不变
  2. ## Nacos registry address
  3. dubbo.registry.address = nacos://10.20.153.10:8848
  4. ##If you want to use your own namespace, you can use the following two methods:
  5. #dubbo.registry.address = nacos://10.20.153.10:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932
  6. #dubbo.registry.parameters.namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932
  7. ...

Then, restart your application Dubbo, Dubbo information services and consumption in Nacos console can show:

image-20181213103845976-4668726.png | left | 747x284

As shown, the service name prefix for providers: metainfo for the service provider’s information, consumers: represents the service consumer metainfo. Click”details“ can check the service status details:

image-20181213104145998-4668906.png | left | 747x437

If you are using a Spring XML configuration file assembly Dubbo registry, please refer to the next section.

Spring XML configuration files

Similarly, suppose you Dubbo applications using Zookeeper as registry, and the server IP address is:10.20.153.10, and assembling Spring Bean in the XML file, as shown below:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
  6. <!-- 提供方应用信息,用于计算依赖关系 -->
  7. <dubbo:application name="dubbo-provider-xml-demo" />
  8. <!-- 使用 Zookeeper 注册中心 -->
  9. <dubbo:registry address="zookeeper://10.20.153.10:2181" />
  10. ...
  11. </beans>

Like Dubbo Spring externalized configuration configuration, only need to adjust the address attribute configuration can be:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
  6. <!-- 提供方应用信息,用于计算依赖关系 -->
  7. <dubbo:application name="dubbo-provider-xml-demo" />
  8. <!-- 使用 Nacos 注册中心 -->
  9. <dubbo:registry address="nacos://10.20.153.10:8848" />
  10. <!-- If you want to use your own namespace, you can use the following configuration -->
  11. <!-- <dubbo:registry address="nacos://10.20.153.10:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932" /> -->
  12. ...
  13. </beans>

After restart the Dubbo application, you can also find the provider and consumer registered metainfo is presented in Nacos console:

image-20181213113049185-4671849.png | left | 747x274

Whether you absolute configuration or switch Nacos registry super Easy? If you are still wanting more or less understand, may refer to the following the complete example.

Complete sample

Above pictures of metadata from Dubbo Spring annotations driver sample and Dubbo Spring XML configuration driven example, the following will introduce both, you can choose your preference programming model.Before the formal discussion, first to introduce the preparation work, because they are dependent on the Java service interface and implementation.At the same time, please make sure that the local (127.0.0.1) environment has launched Nacos service.

Sample interface and implementation

First define the sample interface, as shown below:

  1. package com.alibaba.dubbo.demo.service;
  2. /**
  3. * DemoService
  4. *
  5. * @since 2.6.5
  6. */
  7. public interface DemoService {
  8. String sayName(String name);
  9. }

Provide the above interface implementation class:

  1. package com.alibaba.dubbo.demo.service;
  2. import com.alibaba.dubbo.config.annotation.Service;
  3. import com.alibaba.dubbo.rpc.RpcContext;
  4. import org.springframework.beans.factory.annotation.Value;
  5. /**
  6. * Default {@link DemoService}
  7. *
  8. * @since 2.6.5
  9. */
  10. @Service(version = "${demo.service.version}")
  11. public class DefaultService implements DemoService {
  12. @Value("${demo.service.name}")
  13. private String serviceName;
  14. public String sayName(String name) {
  15. RpcContext rpcContext = RpcContext.getContext();
  16. return String.format("Service [name :%s , port : %d] %s(\"%s\") : Hello,%s",
  17. serviceName,
  18. rpcContext.getLocalPort(),
  19. rpcContext.getMethodName(),
  20. name,
  21. name);
  22. }
  23. }

Interface and implementation after ready, the following will be driven by annotations and XML configuration driven their implementation.

Spring annotations driver sample

Dubbo 2.5.7 reconstructed the Spring annotations driver programming model.

Provider annotation driven implementation

  • Definition of Dubbo provider externalized configuration properties - provider-config.properties
  1. ## application
  2. dubbo.application.name = dubbo-provider-demo
  3. ## Nacos registry address
  4. dubbo.registry.address = nacos://127.0.0.1:8848
  5. ##If you want to use your own namespace, you can use the following two methods:
  6. #dubbo.registry.address = nacos://127.0.0.1:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932
  7. #dubbo.registry.parameters.namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932
  8. ## Dubbo Protocol
  9. dubbo.protocol.name = dubbo
  10. dubbo.protocol.port = -1
  11. # Provider @Service version
  12. demo.service.version=1.0.0
  13. demo.service.name = demoService
  • Implement the provider to guide class - DemoServiceProviderBootstrap
  1. package com.alibaba.dubbo.demo.provider;
  2. import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
  3. import com.alibaba.dubbo.demo.service.DemoService;
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  5. import org.springframework.context.annotation.PropertySource;
  6. import java.io.IOException;
  7. /**
  8. * {@link DemoService} provider demo
  9. */
  10. @EnableDubbo(scanBasePackages = "com.alibaba.dubbo.demo.service")
  11. @PropertySource(value = "classpath:/provider-config.properties")
  12. public class DemoServiceProviderBootstrap {
  13. public static void main(String[] args) throws IOException {
  14. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
  15. context.register(DemoServiceProviderBootstrap.class);
  16. context.refresh();
  17. System.out.println("DemoService provider is starting...");
  18. System.in.read();
  19. }
  20. }

The annotation @EnableDubbo activation Dubbo annotation driven and externalized configuration, its scanBasePackages properties scanning to specify the Java package, all marked @Service Service interface implementation class exposure for Spring Bean, then be exported Dubbo Service.

@PropertySource is Spring Framework 3.1 introduced the standard import properties annotation configuration resources, it will provide Dubbo externalized configuration.

Service consumer comments drive implementation

  • Definition of Dubbo consumer externalized configuration properties - consumer-config.properties
  1. ## Dubbo Application info
  2. dubbo.application.name = dubbo-consumer-demo
  3. ## Nacos registry address
  4. dubbo.registry.address = nacos://127.0.0.1:8848
  5. ##If you want to use your own namespace, you can use the following two methods:
  6. #dubbo.registry.address = nacos://127.0.0.1:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932
  7. #dubbo.registry.parameters.namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932
  8. # @Reference version
  9. demo.service.version= 1.0.0

Similarly, dubbo.registry.address attribute points to Nacos registry, other dubbo service relevant meta information through Nacos registry access.

  • Implementation services consumer guide - DemoServiceConsumerBootstrap
  1. package com.alibaba.dubbo.demo.consumer;
  2. import com.alibaba.dubbo.config.annotation.Reference;
  3. import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
  4. import com.alibaba.dubbo.demo.service.DemoService;
  5. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  6. import org.springframework.context.annotation.PropertySource;
  7. import javax.annotation.PostConstruct;
  8. import java.io.IOException;
  9. /**
  10. * {@link DemoService} consumer demo
  11. */
  12. @EnableDubbo
  13. @PropertySource(value = "classpath:/consumer-config.properties")
  14. public class DemoServiceConsumerBootstrap {
  15. @Reference(version = "${demo.service.version}")
  16. private DemoService demoService;
  17. @PostConstruct
  18. public void init() {
  19. for (int i = 0; i < 10; i++) {
  20. System.out.println(demoService.sayName("小马哥(mercyblitz)"));
  21. }
  22. }
  23. public static void main(String[] args) throws IOException {
  24. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
  25. context.register(DemoServiceConsumerBootstrap.class);
  26. context.refresh();
  27. context.close();
  28. }
  29. }

Similarly, @EnableDubbo annotations to activate Dubbo annotation driven and externalized configuration, but the current belong to the Service consumers, without having to specify the Java package name scan label @Service Service implementation.

@Reference is Dubbo remote service dependency injection annotations, need service providers and consumers agreed interface (interface), version (version) and group (group) information.Example, in the current service consumption DemoService service version from the configuration properties file consumer-config.properties.

@PostConstruct code shows when DemoServiceConsumerBootstrap Bean initialization, execution Dubbo ten times remote method invocation.

Run annotation driver sample

Twice in the local boot DemoServiceProviderBootstrap, the registry will appear two health services:

image-20181213123909636-4675949.png | left | 747x38

Run again DemoServiceConsumerBootstrap, run results as follows:

  1. Service [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz
  2. Service [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz
  3. Service [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz
  4. Service [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz
  5. Service [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz
  6. Service [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz
  7. Service [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz
  8. Service [name :demoService , port : 20880] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz
  9. Service [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz
  10. Service [name :demoService , port : 20881] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz

Operate, and service consumer using the load balancing strategy, RPC calls ten times the average contribution to two Dubbo provider instance.

Spring XML configuration driver sample

The Spring XML configuration driven programming model is a traditional Spring assembly components.

Provider XML configuration

  • Define the service provider XML context configuration file - /META-INF/spring/dubbo-provider-context.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
  6. <!-- 提供方应用信息,用于计算依赖关系 -->
  7. <dubbo:application name="dubbo-provider-xml-demo"/>
  8. <!-- 使用 Nacos 注册中心 -->
  9. <dubbo:registry address="nacos://127.0.0.1:8848"/>
  10. <!-- If you want to use your own namespace, you can use the following configuration -->
  11. <!-- <dubbo:registry address="nacos://127.0.0.1:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932" /> -->
  12. <!-- 用dubbo协议在随机端口暴露服务 -->
  13. <dubbo:protocol name="dubbo" port="-1"/>
  14. <!-- 声明需要暴露的服务接口 -->
  15. <dubbo:service interface="com.alibaba.dubbo.demo.service.DemoService" ref="demoService" version="2.0.0"/>
  16. <!-- 和本地bean一样实现服务 -->
  17. <bean id="demoService" class="com.alibaba.dubbo.demo.service.DefaultService"/>
  18. </beans>
  • Implement the provider to guide class - DemoServiceProviderXmlBootstrap
  1. package com.alibaba.dubbo.demo.provider;
  2. import com.alibaba.dubbo.demo.service.DemoService;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import java.io.IOException;
  5. /**
  6. * {@link DemoService} provider demo XML bootstrap
  7. */
  8. public class DemoServiceProviderXmlBootstrap {
  9. public static void main(String[] args) throws IOException {
  10. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
  11. context.setConfigLocation("/META-INF/spring/dubbo-provider-context.xml");
  12. context.refresh();
  13. System.out.println("DemoService provider (XML) is starting...");
  14. System.in.read();
  15. }
  16. }

Service consumer driven XML configuration

  • Define the service consumer context XML configuration files - /META-INF/spring/dubbo-consumer-context.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
  6. <!-- 提供方应用信息,用于计算依赖关系 -->
  7. <dubbo:application name="dubbo-consumer-xml-demo"/>
  8. <!-- 使用 Nacos 注册中心 -->
  9. <dubbo:registry address="nacos://127.0.0.1:8848"/>
  10. <!-- If you want to use your own namespace, you can use the following configuration -->
  11. <!-- <dubbo:registry address="nacos://127.0.0.1:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932" /> -->
  12. <!-- 引用服务接口 -->
  13. <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.service.DemoService" version="2.0.0"/>
  14. </beans>
  • Implementation services consumer guide - DemoServiceConsumerXmlBootstrap
  1. package com.alibaba.dubbo.demo.consumer;
  2. import com.alibaba.dubbo.demo.service.DemoService;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import java.io.IOException;
  5. /**
  6. * {@link DemoService} consumer demo XML bootstrap
  7. */
  8. public class DemoServiceConsumerXmlBootstrap {
  9. public static void main(String[] args) throws IOException {
  10. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
  11. context.setConfigLocation("/META-INF/spring/dubbo-consumer-context.xml");
  12. context.refresh();
  13. System.out.println("DemoService consumer (XML) is starting...");
  14. DemoService demoService = context.getBean("demoService", DemoService.class);
  15. for (int i = 0; i < 10; i++) {
  16. System.out.println(demoService.sayName("小马哥(mercyblitz)"));
  17. }
  18. context.close();
  19. }
  20. }

Run the XML configuration driven example

Similarly, to start the first two DemoServiceProviderXmlBootstrap bootstrap class, observe Nacos registry service provider changes:

image-20181213125527201-4676927.png | left | 747x33

XML configuration driven service version for 2.0.0, therefore the registration service and correct.

Again run service consumers leading class DemoServiceConsumerXmlBootstrap, watch the console output:

  1. Service [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz
  2. Service [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz
  3. Service [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz
  4. Service [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz
  5. Service [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz
  6. Service [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz
  7. Service [name :null , port : 20882] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz
  8. Service [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz
  9. Service [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz
  10. Service [name :null , port : 20883] sayName("小马哥(mercyblitz)") : Hello,小马哥(mercyblitz

Results also runs and load balancing is normal, but because the current sample has yet to add attributes demo.service.name of therefore, “name” part of the information output null.

If your attention or love Dubbo and Nacos open source project, as well as for their points of “star”, related links: