Install the software first. Official website: https://nacos.io/en/
You can change the port in the conf/application.properties file; the default is 8848.
Service Registration
Add the management dependency in the parent project:
| |
Comment out the Eureka dependency and add the Nacos client dependency:
| |
Modify application.yml to add the Nacos address and comment out the Eureka address:
| |
Start Nacos:
| |
Access
http://localhost:8848/nacos/
. The default username and password are both nacos.
Tiered Storage Model
Services are divided into multiple clusters, each containing multiple instances. Clusters can be assigned by region to improve access speed.
In contrast, Eureka only has a Service-Instance model without clusters.
Service calls should prioritize the local cluster to avoid high latency. If the local cluster is unavailable, Nacos will fail over to other clusters.
You can set the cluster in application.yml:
| |
Then set the Ribbon strategy to Nacos’s strategy:
| |
You can also set the instance weight (0-1) in the Nacos console. Higher weight means higher access frequency.
Environment Isolation
The outermost layer for service and data storage in Nacos is the namespace, which is used for top-level isolation.
You can create a new namespace in Nacos, such as dev, and then update application.yml:
| |
Services in different namespaces are invisible to each other and cannot be accessed.
Ephemeral vs. Non-ephemeral Instances
For ephemeral instances, Nacos handles them the same way as Eureka (heartbeats).
For non-ephemeral instances, Nacos actively probes the service for health. The service is not deleted if it becomes unavailable, and Nacos will actively notify consumers about the failure.
Set an instance as non-ephemeral:
| |
Nacos Configuration Management
As the number of microservice instances grows, changing configurations one by one becomes tedious. Nacos allows for centralized configuration management with hot updates.
Basic Usage
In the Nacos console’s Configuration Management, create a new configuration. The Data ID is the config file ID, formatted as [service-name]-[profile].[extension], e.g., userService-dev.yaml.
Then write the configuration content:
| |
Add the Nacos configuration management client dependency:
| |
Configuration retrieval steps:
App Start -> bootstrap.yml -> Nacos config file -> Local application.yml -> Create Spring Container -> Load beans
Add bootstrap.yml:
| |
Testing:
| |
Check the page to see the effect.
Hot Updates
This can be achieved in two ways:
Method 1: Add the @RefreshScope annotation to the class where the @Value is injected.
| |
Method 2: Use @ConfigurationProperties.
Create a configuration class:
| |
Controller class:
| |
Shared Configuration
When a microservice starts, it reads multiple configuration files from Nacos:
[spring.application.name]-[spring.profiles.active].yaml, e.g.,userService-dev.yaml[spring.application.name].yaml, e.g.,userService.yaml
The second one ([spring.application.name].yaml) does not include the environment profile and can be shared across multiple environments.
Configuration priority: [spring.application.name]-[spring.profiles.active].yaml > [spring.application.name].yaml > Local configuration.