Using Nacos as Registration Center for Automatic Service Discovery

This example demonstrates how to use Nacos as a registration center to achieve automatic service discovery.

This example demonstrates Nacos as a registration center for automatic service discovery, based on a Spring Boot application. You can view the complete sample code here.

1 Basic Configuration

1.1 Add Dependency

Add the dependencies for dubbo and nacos-client:

<dependencies>
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo</artifactId>
        <version>3.3.0</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba.nacos</groupId>
      <artifactId>nacos-client</artifactId>
      <version>2.1.0</version>
    </dependency>
</dependencies>

For Spring Boot applications, you can use the following spring-boot-starter:

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

1.2 Nacos Version

Nacos version mapping:

DubboRecommended Nacos VersionNacos Compatibility Range
3.3.02.2.32.x
3.2.212.1.02.x
3.1.112.0.92.x
3.0.102.0.92.x
2.7.211.x1.x
2.6.01.x1.x

1.3 Configure and Enable Nacos

# application.yml (Spring Boot)
dubbo
 registry
   address: nacos://localhost:8848
   register-mode: instance # New users should set this value, indicating the enablement of application-level service discovery. Options: interface, instance, all. Default is all; future versions will change default to instance.

or

# dubbo.properties
dubbo.registry.address=nacos://localhost:8848
dubbo.registry.register-mode=instance

or

<dubbo:registry address="nacos://localhost:8848" register-mode="instance"/>

2 Advanced Configuration

2.1 Authentication

# application.yml (Spring Boot)
dubbo
 registry
   address: nacos://localhost:8848?username=nacos&password=nacos
   register-mode: instance

or

# dubbo.properties
dubbo.registry.address: nacos://nacos:nacos@localhost:8848
dubbo.registry.register-mode=instance

2.2 Custom Namespace

# application.yml (Spring Boot)
dubbo:
 registry:
   address: nacos://localhost:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932
   register-mode: instance # New users should set this value, indicating the enablement of application-level service discovery. Options: interface, instance, all

or

# application.yml (Spring Boot)
dubbo:
 registry:
   address: nacos://localhost:8848
   register-mode: instance # New users should set this value, indicating the enablement of application-level service discovery. Options: interface, instance, all
   parameters.namespace: 5cbb70a5-xxx-xxx-xxx-d43479ae0932

2.3 Custom Group

# application.yml
dubbo:
 registry:
   address: nacos://localhost:8848
   register-mode: instance # New users should set this value, indicating the enablement of application-level service discovery. Options: interface, instance, all
   group: dubbo

If not configured, the group is specified by Nacos by default. The group and namespace represent different isolation levels in Nacos. Generally, the namespace is used to isolate different users or environments, while the group is used to further organize data within the same environment.

2.4 Register Interface-Level Consumer

Since Dubbo version 3.0.0, a parameter has been added to indicate whether to register consumers. If you need to register consumers in the Nacos registration center, set the parameter (register-consumer-url) to true; the default is false.

# application.yml
dubbo:
  registry:
    register-mode: instance # New users should set this value, indicating the enablement of application-level service discovery. Options: interface, instance, all
    address: nacos://localhost:8848?register-consumer-url=true

or

# application.yml
dubbo:
  registry:
    address: nacos://localhost:8848
    register-mode: instance # New users should set this value, indicating the enablement of application-level service discovery. Options: interface, instance, all
    parameters.register-consumer-url: true

2.5 More Configuration

Parameter NameDescriptionDefault Value
usernameUsername to connect to Nacos Servernacos
passwordPassword to connect to Nacos Servernacos
backupBackup addressempty
namespaceNamespace IDpublic
groupGroup nameDEFAULT_GROUP
register-consumer-urlWhether to register the consumerfalse
com.alibaba.nacos.naming.log.filenameInitialization log filenamenaming.log
endpointSpecified connection point for connecting to Nacos Server, refer to documentationempty
endpointPortPort for the specified connection point to Nacos Server, refer to documentationempty
endpointQueryParamsQuery parameters for endpointempty
isUseCloudNamespaceParsingWhether to parse the namespace parameter in a cloud environmenttrue
isUseEndpointParsingRuleWhether to enable endpoint parameter rule parsingtrue
namingLoadCacheAtStartWhether to prioritize reading local cache at startuptrue
namingCacheRegistryDirSpecify cache subdirectory, located at …/nacos/{SUB_DIR}/namingempty
namingClientBeatThreadCountSize of the thread pool for client heartbeathalf the number of CPU cores
namingPollingThreadCountSize of the thread pool for regular data polling updateshalf the number of CPU cores
namingRequestDomainMaxRetryCountNumber of retries when client requests Nacos Server via HTTP3
namingPushEmptyProtectionWhether to enable protection when no valid (healthy) instance exists, after enabling it will use the old service instancefalse
push.receiver.udp.portClient UDP portempty

In nacos-server@1.0.0, clients can report instances with specific metadata to control instance behavior.

Parameter NameDescriptionDefault Value
preserved.heart.beat.timeoutTime (ms) for the instance to become unhealthy after not sending heartbeat15000
preserved.ip.delete.timeoutTime (ms) for the instance to be removed by the server after not sending heartbeat30000
preserved.heart.beat.intervalInterval (ms) for the instance to report heartbeat5000
preserved.instance.id.generatorID generation strategy for the instance. When the value is snowflake, it increases from 0simple
preserved.register.sourceService framework type during instance registration (e.g., Dubbo, Spring Cloud, etc.)empty

These parameters can be configured in Nacos similarly to namespace, such as

dubbo.registry.parameters.preserved.heart.beat.timeout=5000

3 Working Principle

In the previous section, we explained the difference between application-level service discovery and interface-level service discovery. Below are the specific storage structures for both modes in Nacos.

3.1 Dubbo2 Registration Data

Then restart your Dubbo application. The service provision and consumption information of Dubbo can be displayed in the Nacos console:

dubbo-registry-nacos-1.png

As shown, the service prefix providers: contains metadata for service providers, while consumers: represents metadata for service consumers. Click on “Details” to view service status details:

image-dubbo-registry-nacos-2.png

3.2 Dubbo3 Registration Data

The “service name” for application-level service discovery is the application name.

Dubbo3 adopts a “dual registration model” of “application-level service discovery + interface-level service discovery” by default, so you will find both application-level service (application name) and interface-level service (interface name) appearing in the Nacos console. You can change the registration behavior by configuring dubbo.registry.register-mode=instance/interface/all.

3.3 Client Cache

3.4 Heartbeat Detection

3.5