Nacos Registry Center
Nacos is a very important registry center for Dubbo ecosystem, dubbo-registry-nacos
is the implementation of Nacos integration to Nacos.
Prepare Work
When you integrate dubbo-registry-nacos
into your Dubbo project, please Make sure the Nacos service is started in the background. If you are not familiar with the basic use of Nacos, you can refer to Nacos Quick Start. It is recommended to use the version equal or above Nacos 1.0.0
.
Quick Start
The steps for Dubbo to integrate Nacos as a registry center are very simple. The general steps can be divided into “add Maven dependencies” and “configure registry center”.
Add Maven Dependencies
First, you need to add the dubbo-registry-nacos
Maven dependency to your project’s pom.xml
file, and we strongly recommend that you use Dubbo 2.6.5
:
<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>
...
</dependencies>
When you add dubbo-registry-nacos
to your project, you don’t need to explicitly program the service discovery and registration logic. The actual implementation is provided by the three-party package.
Configure Registry Center
Assuming your Dubbo app uses the Spring Framework assembly, there are two configuration options available: Dubbo Spring Externalization Configuration and the Spring XML configuration file.
Dubbo Spring Externalization Configuration
The Dubbo Spring externalization configuration is a new feature introduced by Dubbo 2.5.8
that automatically generates and binds Dubbo configuration beans through the Spring Environment
property, simplifying configuration and lowering the microservice development threshold.
Assuming your Nacos Server is also running on server 10.20.153.10
and using the default Nacos service port 8848
, you only need to adjust the dubbo.registry.address
property as follows:
## Other properties remain unchanged
## Nacos registry address
dubbo.registry.address = nacos://10.20.153.10:8848
...
Then, restart your Dubbo app, Dubbo’s service provider and consumer information can be displayed on the Nacos console:
As shown in the figure, the information whose service name prefix is providers:
is the meta information of the service provider, and the consumers:
represents the meta information of the service consumer. Click on “Details” to view service status details:
If you are using the Spring XML configuration file to assemble the Dubbo registry, please refer to the next section.
Spring XML Configuration File
Similar to Dubbo Spring Externalization Configuration, just adjust the address
attribute to configure:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
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">
<!-- Provider application information for calculating dependencies -->
<dubbo:application name="dubbo-provider-xml-demo" />
<!-- Use Nacos as Registry Center -->
<dubbo:registry address="nacos://10.20.153.10:8848" />
...
</beans>
After restarting the Dubbo app, you can also find that the registration meta-information of the service provider and consumer is presented on the Nacos console:
Additional information: since nacos-server@1.0.0, support client report instance info which contains particular key in metadata to control some behavior.
Such as:preserved.heart.beat.timeout
: The time of the instance from healthy to unhealthy after heartbeat is not send.(unit:millisecond)preserved.ip.delete.timeout
: The time of the instance is dropped by server after the heartbeat is not send.(unit:millisecond)preserved.heart.beat.interval
: The interval of the instance to send heartbeat (unit:millisecond)preserved.instance.id.generator
: The id generator strategy, value set as snowflake
means the id will be increment from 0.preserved.register.source
: Reserved key, not used at now.
This feature will be support since Dubbo@2.7.10
, you can use this feature by append param in nacos address.
Such as: nacos://10.20.153.10:8848?preserved.heart.beat.timeout=15000&preserved.ip.delete.timeout=30000&preserved.heart.beat.interval=10000