SpringCloudAlibaba-microservice-Nacos service configuration
Nacos installation and deployment: https://blog.csdn.net/Cey_Tao/article/details/127611559
1. Service Discovery
Nacos service discovery has three main areas, from large to small: namespace (namespace), group (group), cluster (cluster)
The defaults are public namespace, DEFAULT_GROUP grouping, and DEFAULT cluster
Services between different clusters can access each other, but services in different groups or namespaces cannot be discovered
Service discovery is configured in spring.cloud.nacos.discovery, the example is as follows:
spring:
application:
name: user-service # 服务名称
cloud:
nacos:
server-addr: @nacos.server-addr@ # nacos 注册中心地址
discovery:
namespace: namespace-dev # namespace 的 ip
group: g1 # 分组名称
cluster-name: ShenZhen # 集群名称
The group and cluster-name can be configured directly, and the namespace needs to be created in the nacos console first, and the id of the namespace needs to be obtained
The namespace id can be filled in by yourself or automatically generated
As shown below:
2. Load balancing strategy
The Ribbon-related dependencies are also included in the Nacos dependencies, and the load balancing strategy can be set through the Ribbon
The default policy is polling access
An example is as follows, setting a load balancing policy for access to the user-service service:
user-service: # 服务名称,为改服务设置负载均衡策略
ribbon:
# NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule # 随机访问策略
NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule # nacos 优先选择策略
If it is set as the NacosRule policy, nacos will select the optimal service based on the relevant information discovered by the service:
- Prefer services within the same cluster
- Random selection based on weight within the same cluster
The greater the weight, the greater the possibility of being selected
The weight of the service can be set in the Nacos service details, as shown in the figure below:
3. Naocs management configuration
new configuration
Configuration files are isolated based on namespaces
Select the namespace in the Nacos configuration list to add configuration
The following example adds a default configuration for user-service, and the configuration content is the log level of the service
Nacos looks for the corresponding Data ID based on the service name, so the Data ID should be prefixed with the service name
If multi-environment configuration is required, the environment name suffix should be added to the Data ID, as follows:
pull configuration
Add dependencies for nacos service discovery and service configuration:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
The relevant properties of pulling nacos configuration should be configured in the bootstrap configuration file
The necessary attributes are service name, nacos address, configuration format
Create a new bootstrap.yml under the project resources directory, the configuration example is as follows:
spring:
application:
name: user-service # 服务名称,要与 Data ID 前缀对应
profiles:
active: dev # 启用环境
cloud:
nacos:
server-addr: @nacos.server-addr@ # nacos 注册中心地址
discovery:
namespace: 6c1d9771-2afb-4a48-9ced-4bc21b2781c8 # 服务发现的命名空间 id
config:
file-extension: yaml # 配置格式
namespace: 6c1d9771-2afb-4a48-9ced-4bc21b2781c8 # 服务配置的命名空间 id
# group: DEFAULT_GROUP # 默认为 DEFAULT_GROUP
Note that the namespace for service discovery and the configured namespace are configured separately, and both are in the public space by default
@@ in bootstrap configuration may not be replaced by Maven property, just add the following configuration in pom.xml:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
Priority of different configurations
The order of priority of different configurations from high to low is as follows:
nacos environment configuration > nacos default configuration > command line parameters > bootstrap configuration > application configuration
Between different priorities, different parts are complementary, and the same conflicting part adopts the configuration with higher priority
But there are exceptions. According to my test, the service name (spring.application.name) and service port (server.port) have the highest priority of command line parameters.
In addition, although the service port can be set by pulling the nacos configuration, the port number cannot be changed after the service is started, so the service port will not be affected by the hot update
Configure Hot Update
In the nacos configuration list, you can edit the existing configuration
If the configuration is being used by the service, the service will be updated immediately after the configuration is changed, which is hot update