In Depth with Alibaba’s Nacos: Exploring Registry Implementation in Dubbo

A technical guide to implementing Nacos’ registry function in Dubbo

Image for post
Image for post

Preparatory Work

Quick Start

Adding Maven dependencies

...

<!-- Dubbo Nacos registry dependency -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>0.0.2</version>
</dependency>
<!-- Keep latest Nacos client version -->
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>[0.6.1,)</version>
</dependency>
<!-- Dubbo dependency -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.5</version>
</dependency>
<!-- Alibaba Spring Context extension -->
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>1.0.2</version>
</dependency>
...

Configuring the registry

Dubbo Spring externalization configuration

Image for post
Image for post
Image for post
Image for post

Spring XML configuration file

<!-- Provider application information for calculating dependencies -->
<dubbo:application name="dubbo-provider-xml-demo" />
<!-- Use the Zookeeper Registry -->
<dubbo:registry address="zookeeper://10.20.153.10:2181" />
...
<!-- Provider application information for calculating dependencies -->
<dubbo:application name="dubbo-provider-xml-demo" />
<!-- Use the Nacos Registry -->
<dubbo:registry address="nacos://10.20.153.10:8848" />
...
Image for post
Image for post

Complete Implementation Explained

Interface and implementation

Spring annotation driver

Service consumer annotation driver implementation

Running the annotation driver

Image for post
Image for post

Spring XML configuration driver

Service provider XML configuration driver

<!-- Provider application information for calculating dependencies -->
<dubbo:application name="dubbo-provider-xml-demo"/>
<!-- Use the Nacos registry -->
<dubbo:registry address="nacos://127.0.0.1:8848"/>
<!-- Exposing services on random ports using the dubbo protocol -->
<dubbo:protocol name="dubbo" port="-1"/>
<!-- Declare the service interface that needs to be exposed -->
<dubbo:service interface="com.alibaba.dubbo.demo.service.DemoService" ref="demoService" version="2.0.0"/>
<!-- Implement the same service as a local bean -->
<bean id="demoService" class="com.alibaba.dubbo.demo.service.DefaultService"/>

Service consumer XML configuration driver

<!-- Provider application information for calculating dependencies -->
<dubbo:application name="dubbo-consumer-xml-demo"/>
<!-- Use the Nacos registry -->
<dubbo:registry address="nacos://127.0.0.1:8848"/>
<!-- Refer service interfaces -->
<dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.service.DemoService" version="2.0.0"/>

Running the XML configuration driver

Image for post
Image for post

Alibaba Tech

First-hand & in-depth information about Alibaba's tech innovation in Artificial Intelligence, Big Data & Computer Engineering. Follow us on Facebook!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store