Use Nacos as a Registry for Automatic Service Discovery

Demonstrates how to use Nacos as a registry for automatic service discovery through examples.

This example demonstrates automatic service discovery using Nacos as a registry based on a Spring Boot application. You can view the complete example code.

1 Basic Configuration

1.1 Add Dependencies

For Spring Boot applications, 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>

Non-Spring Boot users can add dubbo and nacos-client dependencies:

  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>

1.2 Nacos Version Mapping

DubboRecommended Nacos VersionNacos Compatibility Range
3.3.02.3.02.x
3.2.212.1.02.x
3.1.112.0.92.x
3.0.102.0.92.x
2.7.21Latest 1.x Version1.x
2.6.0Latest 1.x Version1.x

1.3 Configure and Enable Nacos

  1. # application.yml (Spring Boot)
  2. dubbo
  3. registry
  4. address: nacos://localhost:8848

or

  1. # dubbo.properties
  2. dubbo.registry.address=nacos://localhost:8848

or

  1. <dubbo:registry address="nacos://localhost:8848" />

2 Advanced Configuration

2.1 Authentication

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

or

  1. # dubbo.properties
  2. dubbo.registry.address: nacos://nacos:nacos@localhost:8848

2.2 Custom Namespace

  1. # application.yml (Spring Boot)
  2. dubbo:
  3. registry:
  4. address: nacos://localhost:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932

or

  1. # application.yml (Spring Boot)
  2. dubbo:
  3. registry:
  4. address: nacos://localhost:8848
  5. parameters.namespace: 5cbb70a5-xxx-xxx-xxx-d43479ae0932

2.3 Custom Group

  1. # application.yml
  2. dubbo:
  3. registry:
  4. address: nacos://localhost:8848
  5. group: dubbo

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

2.4 Register Interface-level Consumers

Starting from Dubbo 3.0.0, a parameter was added to indicate whether to register consumers. To register the consumer in the Nacos registry, set the parameter (register-consumer-url) to true; the default is false.

  1. # application.yml
  2. dubbo:
  3. registry:
  4. address: nacos://localhost:8848?register-consumer-url=true

or

  1. # application.yml
  2. dubbo:
  3. registry:
  4. address: nacos://localhost:8848
  5. parameters.register-consumer-url: true

2.5 More Configurations

Parameter NameDescriptionDefault Value
usernameUsername for connecting to Nacos Servernacos
passwordPassword for connecting to Nacos Servernacos
backupBackup addressempty
namespaceID of the namespacepublic
groupGroup nameDEFAULT_GROUP
register-consumer-urlWhether to register the consumerfalse
com.alibaba.nacos.naming.log.filenameLog filename during initializationnaming.log
endpointSpecified connection point to connect to Nacos Server, can refer to documentationempty
endpointPortPort of the specified connection point to Nacos Server, can 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 on startuptrue
namingCacheRegistryDirSpecify cache subdirectory, location is …/nacos/{SUB_DIR}/namingempty
namingClientBeatThreadCountThread pool size for client heartbeatshalf of the machine’s CPU count
namingPollingThreadCountThread pool size for client timed polling data updateshalf of the machine’s CPU count
namingRequestDomainMaxRetryCountNumber of retries for client requests to Nacos Server via HTTP3
namingPushEmptyProtectionWhether to enable protection when there are no valid (healthy) instances; if enabled, it will use old service instancesfalse
push.receiver.udp.portClient UDP portempty

After version 1.0.0 of nacos-server, clients can report instances with specific metadata to the server to control instance behaviors.

Parameter NameDescriptionDefault Value
preserved.heart.beat.timeoutTime (in milliseconds) for an instance to go from healthy to unhealthy without sending heartbeats15000
preserved.ip.delete.timeoutTime (in milliseconds) for the server to remove the instance after it stops sending heartbeats30000
preserved.heart.beat.intervalInterval time (in milliseconds) for the instance to report heartbeats5000
preserved.instance.id.generatorInstance ID generation strategy; when the value is snowflake, it starts increasing from 0simple
preserved.register.sourceThe type of service framework during instance registration (e.g., Dubbo, Spring Cloud, etc.)empty

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

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

3 Working Principle

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

3.1 Dubbo2 Registration Data

Then, restart your Dubbo application, and the service provider and consumer information in Dubbo can be displayed in the Nacos console:

dubbo-registry-nacos-1.png

As shown in the figure, the information with the service name prefix providers: is the metadata of the service provider, while consumers: represents the metadata of the service consumer. Click “Details” to view service status details:

image-dubbo-registry-nacos-2.png

3.2 Dubbo3 Registration Data

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

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

3.3 Client Caching

3.4 Heartbeat Detection

3.5

Feedback

Was this page helpful?

Yes No

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