Nacos There are many parameters in , Such as : Namespace 、 Group name 、 service name 、 Protection threshold 、 Service routing type 、 Temporary examples, etc , What do these parameters mean ? And how to set ? Next, let's play it together .
1. Namespace
stay Nacos Through namespace (Namespace)+ grouping (Group)+ service name (Name) You can locate a unique service instance .
Namespace (Namespace):Nacos At the top of the service 、 It is also the most inclusive concept , It is used to forcibly isolate similar environments or tenants .Nacos Services also need to use namespaces for isolation .
Namespace in Nacos You can find it in the first level directory of the console , As shown in the figure below :
You can also see the namespace in the service list , As shown in the figure below :
1.1 Namespace usage
The namespace defaults to public, In project development , If you don't specify a namespace , Then the default value will be used public. The official recommendation is to use the runtime environment to define the namespace , If the production version can be used public, The development version can be defined as private.
In project development , configurable “spring.cloud.nacos.discovery.namespace” To define the namespace , As shown in the figure below :
1.2 matters needing attention
Namespace before use , You must first create a new namespace in the console , As shown in the figure below :
If there is no new namespace in the console , If used directly in the project , The service cannot be successfully registered to Nacos Medium , An un created... Is configured in the project as follows dev Namespace , As shown in the figure below :
Then start the project , You will find , stay Nacos No service instance can be refreshed in the service list of the console , As shown in the figure below :
2. Group name
Group name (Group):Nacos Second to namespace ⼀ A concept of isolation , Mandatory isolation attribute different from namespace , The group belongs to ⼀ A concept of weak isolation , Mainly used to logically distinguish ⼀ Some service usage scenarios or services with the same name in different applications , The most common situation is mainly the same as ⼀ Test group and production group of services 、 Or group the application names to prevent duplicate service names provided by different applications .
The group name is in Nacos In the service list of the console, you can see , As shown in the figure below :
The group name defaults to DEFAULT_GROUP, In the project, you can use “spring.cloud.nacos.discovery.group” To set up , As shown in the figure below :
This can be omitted , The default value when omitted is DEFAULT_GROUP.
The group name can be used directly in the project , You don't need to be like a namespace , Before use, you should also create a new... In the console , After setting the group name , Refresh the service list to see the new group name , As shown in the figure below :
3. service name
service name (Name): The actual name of the service ,⼀ Used to describe a function or capability provided by the service .
It is generally recommended to use the running environment as the namespace 、 Apply name as grouping , The service function is used as a combination of service names to ensure the natural uniqueness of the service ⼀ sex , Of course, users can ignore namespaces and groupings , Use only the service name as the service name ⼀ Mark , This requires users to add their own rules when defining the service name to ensure that the service name is unique in use ⼀ Locate the service without finding the wrong service .
The service name in the project can be through “spring.application.name” To specify the , As shown in the figure below :
4. Protection threshold
Health protection threshold (ProtectThreshold): To prevent failures due to too many instances , Cause all traffic to flow into the remaining instances , In turn, the flow pressure will collapse the remaining instances, forming an avalanche effect . The health protection threshold should be defined as ⼀ individual 0 To 1 The floating point number between . When the proportion of domain name health instances to the total number of service instances is less than this value , Whether the instance is healthy or not , Will return this instance to the client . Although it costs ⼀ Partial flow , However, it ensures that the remaining healthy instances in the cluster can work normally .
Simply speaking , The protection threshold is a 0-1 The floating-point value of , The protection threshold is the minimum value that allows the proportion of healthy instances in the cluster , If the proportion of actual health instances is less than or equal to the set protection threshold , Threshold protection will be triggered , As shown in the figure below , Set the protection threshold to 0.75:
Stop the only instance of health , The proportion of healthy instances in clusters has decreased to 0%, Less than the set protection threshold 0.75(75%), The threshold protection is triggered , As shown in the figure below :
5. Service routing type
The setting of service routing type is shown in the figure below :
It is used to set the routing policy of the service , The default value is none. If Set this value to label( label ) Pattern , You need to set the corresponding label expression to match the instance selector (Selector), Through the instance selector, you can customize the load balancing policy , For example, we can customize the instance selector , Realize the load balancing strategy of nearby access , In this way, when the consumer calls , Will give priority to those close to you IP node , So as to realize more efficient service invocation .
6. The weight
The weight (Weight): Instance level configuration . The weight is floating point number , The scope is 0-10000. The greater the weight , The more traffic is allocated to the instance .
It is set for service instances , As shown in the figure below :
7. Temporary instance
stay Nacos There are two kinds of service instances in ( type ): Persistent and temporary instances ( Also known as non persistent instances ). When in the console “ Temporary instance ” by true when , Indicates that this service is a temporary instance , As shown in the figure below :
7.1 Temporary instance VS Persistent instances
There are two main differences between temporary instances and persistent instances :
- Temporary instances will be automatically rejected in an unhealthy state , Persistent instances are not automatically rejected .
The health status of the temporary instance is Nacos Clients at a fixed frequency (5s once ) Report to Nacos Server side , The persistence instance is Nacos Server side active detection .
7.2 Instance type settings
In project development , Can be set by “spring.cloud.nacos.discovery.ephemeral” To specify the instance type of the service , The default is temporary instance , That's the default “spring.cloud.nacos.discovery.ephemeral=true”. If you want to set a persistent instance , Need to set up “spring.cloud.nacos.discovery.ephemeral” Set to false, As shown in the figure below :
7.3 matters needing attention
Once the instance type of the service is determined , It is not allowed to be modified in the whole life cycle , If you try to modify the instance type, you will be prompted with the following error :
summary
Nacos Through namespace + Group name + The service name can be located to a unique instance , It is generally recommended to use the running environment as the namespace 、 Apply name as grouping , Service functions define services as a combination of service names . The protection threshold is sacrifice ⼀ Partial flow , A means to ensure that the remaining healthy instances in the cluster can work normally . Service routing types and weights are used to define Nacos Routing rule , Temporary instances and persistent instances are Nacos Two instance types in .
Reference resources & Acknowledgement
《Nacos Framework and principle 》
It's up to you to judge right and wrong , Disdain is to listen to people , Gain or loss is more important than number .
official account :Java Chinese community
Java Interview collection :https://gitee.com/mydb/interview








