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:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.apache.dubbo</groupId>
  4. <artifactId>dubbo</artifactId>
  5. <version>3.3.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.alibaba.nacos</groupId>
  9. <artifactId>nacos-client</artifactId>
  10. <version>2.1.0</version>
  11. </dependency>
  12. </dependencies>

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

  1. <dependency>
  2. <groupId>org.apache.dubbo</groupId>
  3. <artifactId>dubbo-spring-boot-starter</artifactId>
  4. <version>3.3.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.dubbo</groupId>
  8. <artifactId>dubbo-nacos-spring-boot-starter</artifactId>
  9. <version>3.3.0</version>
  10. </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

  1. # application.yml (Spring Boot)
  2. dubbo
  3. registry
  4. address: nacos://localhost:8848
  5. 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

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

or

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

2 Advanced Configuration

2.1 Authentication

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

or

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

2.2 Custom Namespace

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

or

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

2.3 Custom Group

  1. # application.yml
  2. dubbo:
  3. registry:
  4. address: nacos://localhost:8848
  5. register-mode: instance # New users should set this value, indicating the enablement of application-level service discovery. Options: interface, instance, all
  6. 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.

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

or

  1. # application.yml
  2. dubbo:
  3. registry:
  4. address: nacos://localhost:8848
  5. register-mode: instance # New users should set this value, indicating the enablement of application-level service discovery. Options: interface, instance, all
  6. 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

  1. 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

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)