“This is the 12th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

A few days ago, I encountered a problem that the permanent service in Nacos could not be deleted. The following is the cause analysis and solution. It is recommended to collect them for a rainy day.

Temporary and persistent instances are a new feature in Nacos 1.0.0. The biggest difference between a temporary instance and a persistent instance is the mode of health check: the temporary instance uses the health check mode proactively reported by the client, while the persistent instance uses the mode reversely detected by the server. That is, if it is a temporary instance, the client needs to proactively report its health, while a persistent instance requires the Nacos server to reverse detect the health of the instance.

In these two instances, temporary instances can automatically delete unhealthy instances, and when all temporary instances are deleted, the services in Nacos will also be automatically deleted, which is the process of deleting temporary services. However, for persistent instances, this is not so simple, so persistent instances do not automatically delete instances and services, even if they are not healthy. In this case, we need to manually delete services.

PS: Persistent instances are also called persistent instances.

It is important to note that before Nacos 2.0, instances in a service could be either temporary or persistent, but with Nacos 2.0 there are some minor tweaks. Prior to Nacos 2.0, instances in a service could be either temporary or permanent, which caused great confusion and complexity for operations personnel.

At the same time, a scenario in which both persistent and nonpersistent instances of a service exist is somewhat contradictory from an architectural point of view. As a result, this capability is not actually widely used. In order to simplify the service data model of Nacos, reduce the complexity of operation and maintenance personnel, and improve the ease of use of Nacos, in Nacos 2.0, the data whether persistent or not is abstracted to the service level, and a service is no longer allowed to have persistent instances and non-persistent instances. After Nacos 2.0, A temporary instance becomes a temporary service, a persistent instance becomes a persistent service, and there can only be one instance type for the entire life of a service.

Why do you need two service types?

Taking Taobao as an example, during the Double Eleven Rush, the traffic will be much higher than usual. At this time, the service definitely needs to add more instances to cope with the high concurrency, and these instances do not need to be used after the double Eleven. It is more appropriate to use temporary instances. For some standing instances of the service, a permanent instance is more appropriate.

Problem reproduction

However, the persistent service will report an error when it is manually deleted, as shown in the following figure:When we click the “Remove” button on the service in the Nacos console, the prompt “Caused: Service DEFAULT_GROUP @@xxx is not empty, can’t be delete. Please unregister instance first; Please deregister the instance under the service first. Then we enter the service instance list, as shown in the picture below:There is no logout button in the service instance, only the “offline” button, is it in the service “edit” page? So we click the Edit button again and see the following message:There is still no logout button on the service edit page. Should I take all instances “offline”? So we try to “take all instances offline” as shown below:Then return to the service list page and click the “Delete” button to find the same prompt message:How can this be done, a meal operation or delete?

The solution

Knowing that besides the console, Nacos can be operated using either the Nacos SDK or OpenAPI, which has the lowest operating cost, we quickly checked out the Official Nacos OpenAPI documentation to see how to log out of a service instance using the API. Sure enough, the effort paid off. The API for cancellation was successfully found in the official document, as shown in the picture below:OpenAPI address:Nacos. IO/useful – cn/docs /…

PS: Thank you @ershi brother for your ideas.

The OpenAPI content is as follows:Build the delete command from the API documentation:

curl -X DELETE 'http://127.0.0.1:8848/nacos/v1/ns/instance? ServiceName = spring - the cloud - nacos - producer&groupName = DEFAULT_GROUP & namespaceId = public&ip = 10.0.24.8 & clusterName = DEFAULT&port =8081&ephemeral=false'
Copy the code

The result of executing the above commands on the Nacos server is as follows:The server returns the result “OK” and opens the Nacos service instance list to see if the instance is logged out properly:As a result, the persistent instance is unregistered successfully, so use the same method to unregister instance 2, as shown below:When I unregistered all instances of the service, I went to the Nacos console and found that the service had also disappeared, as shown below:Friends will find that a bit more careful service will not disappear immediately, before it became empty service, manual switch to the “hide empty service” to display, but have it and don’t have the effect of it is the same, we can create and its temporary instance of the same name, this just and remove the effect of the same, if not removed is to create a temporary instance, So logically, we can assume that it has been removed.

conclusion

There are two types of instances in Nacos: temporary instances and persistent instances. After Nacos 2.0, only one type of instance can be stored in each service, that is, the instance type has been upgraded to the service type. The temporary service does not need to be deleted. After all instances of the temporary service are deleted, the temporary service is automatically deleted. The permanent service requires that all instances of the service be unregistered through OpenAPI. When all instances are unregistered, the service is deleted.

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…