“This is the 14th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”
Nacos has many parameters, such as namespace, group name, service name, protection threshold, service routing type, temporary instance, etc. What do these parameters mean? How do you set it up? Let’s plate it together.
1. Namespace
In Nacos, you can locate a unique service instance by Namespace + Group + service Name. Namespace: The top-level and broadest concept in the Nacos service, used to enforce isolation of scenarios such as similar environments or tenants. Nacos services also need to use namespaces for isolation.Namespaces can be found in the first level directory of the Nacos console, as shown below:You can also see the namespace in the service list, as shown below:
1.1 Namespace Usage
The default namespace is public. In project development, if no namespace is specified, the default value public is used.The official recommendation is to use the runtime environment to define namespacesIf the production version can use public, the development version can be defined as private. In the project development, can through configuration “spring. Cloud. Nacos. Discovery. The namespace” to define the namespace, as shown in the figure below:
1.2 Precautions
Before using a namespace, you must create a namespace on the console, as shown in the figure below:If the dev namespace is not created on the console and used directly in the project, the service cannot be successfully registered with Nacos.Then start the project and you will find that no service instances have been refreshed in the service list of the Nacos console, as shown below:
2. The group name
Group name: Nacos a concept of isolation secondary to namespaces, distinguished from the mandatory isolation attribute of namespaces, grouping is a concept of weak isolation, mainly used to logically distinguish service usage scenarios or services of the same name from different applications. The most common cases are test and production groups for the same service, or application names are grouped to prevent services provided by different applications from having the same name.The group name is visible in the list of services in the Nacos console, as shown below:Group name defaults to DEFAULT_GROUP, in the project can be “spring. Cloud. Nacos. Discovery. Group” to set up, as shown in the figure below:This item can be omitted. The default value is DEFAULT_GROUP.Group names can be used directly within a projectAfter setting the group name, refresh the service list to see the new group name, as shown in the following figure:
3. The service name
Service Name: The actual Name of the service, usually used to describe the function or capability that the service provides.It is generally recommended to use the running environment as the namespace, the application name as the group, and the service function as the combination of the service name to ensure the natural uniqueness of the service. Of course, users can ignore the namespace and the group and only use the service name as the unique identification of the service. This requires the user to add their own rules when defining the service name to ensure that the service can be uniquely located in use and the wrong service will not be found. The service name can be specified in the project by “spring.application.name”, as shown below:
4. Protection threshold
ProtectThreshold: Prevents all traffic from flowing into the remaining instances due to the faults of too many instances. As a result, the remaining instances are overwhelmed by the traffic pressure, resulting in avalanche effect. The health protection threshold should be defined as a floating point number between 0 and 1. When the ratio of healthy domain name instances to total service instances is smaller than the value, the domain name is returned to the client regardless of whether the instance is healthy. Doing so loses some traffic, but keeps the remaining healthy instances in the cluster working.In simple terms, the protection threshold is a floating point value of 0-1. The protection threshold is the minimum allowed proportion of healthy instances in the cluster. If the proportion of actual healthy instances is less than or equal to the set protection threshold, threshold protection will be triggered, as shown in the following figure.When the unique health instance is stopped, the proportion of health instances in the cluster decreases to 0%, which is less than the protection threshold 0.75 (75%), and threshold protection is triggered, as shown in the following figure:
5. Service route type
The service route type is set as follows:It is used to set the routing policy for the service and defaults to None. ifIf the value is set to label mode, the corresponding label expression needs to be set to match the instance Selector, through which you can customize the load balancing policyFor example, we can customize the instance selector to realize the load balancing strategy of nearby access. In this way, consumers will preferentially call the IP node close to them when invoking, so as to realize more efficient service invocation.
6. The weight
Weight: Specifies the level of the instance. The weight is a floating point number ranging from 0 to 10000. The greater the weight, the greater the traffic allocated to the instance.It is set for the service instance, as shown below:
7. Temporary instances
There are two types of service instances in Nacos: persistent instances and temporary instances (also called nonpersistent instances). When “Temporary Instance” in the console is true, it indicates that the service is a temporary instance, as shown in the following figure:
7.1 Temporary instances VS Persistent Instances
There are two main differences between temporary and persistent instances:
- Temporary instances are automatically culled when they are not healthy, while persistent instances are not.
- The health of temporary instances is reported to the Nacos server by the Nacos client at a fixed frequency of 5 seconds, while the Nacos server proactively probes persistent instances.
7.2 Setting the Instance Type
In the project development, you can set “spring. Cloud. Nacos. Discovery. Ephemeral” to specify the instance type of service, the default for temporary instance, Which is the default “spring. Cloud. Nacos. Discovery. Ephemeral = true”. If you want to set persistent instance, you need to set up “spring. Cloud. Nacos. Discovery. Ephemeral” set to false, as shown in the figure below:
7.3 Precautions
Once the instance type of a service is determined, it cannot be changed during the entire service life cycle. If you try to change the instance type, the following error will be displayed:
conclusion
In Nacos, a unique instance can be located by namespace + group name + service name. It is generally recommended to define a service as a combination of running environment as namespace, application name as group, and service function as service name. A protection threshold is a way to sacrifice some traffic to ensure the normal operation of the remaining healthy instances in the cluster. Service routing types and weights are used to define Nacos routing rules, while temporary instances and persistent instances are the two instance types in Nacos.
Reference & acknowledgements
Nacos Framework and Principles
Judge right and wrong from yourself, praise to listen to others, gain and loss in the number.
Public account: Java Chinese Community
Java Interview Collection: gitee.com/mydb/interv…