preface

Nacos is gaining momentum in the area of service registration and discovery in microservices. In microservice system, the registration and discovery of service is the existence of a soul. Without the existence of a registry, the complexity of calls between hundreds of services is unimaginable.

If you plan to use Nacos or are already using it, but only at the use level, this article is worth reading.

In this article, we will start with the service discovery mechanism, and then explain the basic introduction, implementation principle, architecture, etc., to truly understand Nacos in a simple way.

Service registration and discovery

When it comes to Nacos, it’s important to talk about service discovery in microservices architecture. In fact, about service discovery has been fully explained in the article “To learn the service discovery of micro-service? Let’s understand some popular science knowledge first”. Let’s go through this briefly.

In traditional applications, one service A accesses another service B, and we only need to configure the service address and port of service B in the static configuration file of service A.

However, in the architecture of microservices, this situation changes, as shown in the following figure:

In the figure above, the IP address of the service instance is dynamically assigned. At the same time, it also faces the change of service increase or decrease, failure, upgrade and so on. In this case, a more precise service discovery mechanism is required for the client program.

To solve this problem, middleware for service registration such as ETCD, Consul, Apache Zookeeper, Nacos, etc.

Nacos profile

Nacos is commonly pronounced /nɑ:kəʊs/, derived from the name “Dynamic Naming and Configuration Service”. Na is named in the first two letters, CO is named in the first two letters of Configuration, and S is named in the first letter of Service.

Nacos’s features are described in one sentence: “A dynamic service discovery, configuration management, and service management platform that makes it easier to build cloud-native applications.” That is, Nacos provides not only service registration and discovery, but also configuration management, as well as a visual management platform.

The official documentation also mentions that “Service is a first-class citizen of the Nacos world.” That is, Nacos revolves around services.

If you look at the source code, you will find that the Nacos core API defines two interfaces, NamingService and ConfigService. Service registration and discovery revolves around NamingService, and configuration management revolves around ConfigService.

The official website lists four core features of Nacos: service discovery and service health monitoring, dynamic configuration services, dynamic DNS services, and service and its metadata management. We’ll focus on service discovery.

Nacos Server and Client

Nacos registry is divided into Server and Client. Nacos provides SDK and openApi. If there is no SDK, the openApi can also be used to manually write service registration, discovery and configuration of pull logic.

The Server is written in Java and based on the Spring Boot framework, providing registration and discovery services and configuration services for the Client.

Client support includes currently known Nacos multilingual clients and related clients of the Spring ecosystem. Clients and microservices are nested together.

The DNS implementation of Nacos relies on CoreDNS and its project is nacOS-CoreDNS-Plugin. The plug-in provides a DNS-F client based on CoreDNS, developed in go.

Interaction flow in Nacos registration

As a function of the registry, Nacos provides functions similar to other mainstream frameworks, which are basically implemented around the three cores of service instance registration, instance health check and service instance acquisition.

Taking the Java version of Nacos client as an example, the basic process of service registration is as follows:

  • The service instance starts registering itself with the Nacos registry and then maintains a heartbeat with the registry;
  • The heartbeat maintenance policy is to send a heartbeat to the Nacos Server every five seconds, carrying instance information (service name, instance IP address, port, etc.).
  • Nacos Server also initiates health checks to clients and supports TCP/Http.
  • If there is no heartbeat within 15 seconds and the health check fails, the instance is considered unhealthy. If the health check fails within 30 seconds, the instance is removed.
  • The service consumer obtains the instance through the registry and initiates the invocation;

Service discovery supports two scenarios: first, a service consumer directly sends a request to the registry to obtain a service instance, and the registry returns all available instances, but this method is generally not recommended; Second, a service consumer subscribes to a service in the registry and submits a listener. When a service in the registry changes, the listener is notified and the consumer updates the list of local service instances to ensure that all services are available.

Nacos data model

Nacos data model keys are uniquely determined by triples, Namespace defaults to empty strings, Namespace defaults to public, and group defaults to DEFAULT_GROUP.

The above picture is provided by the government. We can further break it down and have a look:

If that doesn’t make sense, we can look directly at how namespaces, groups, and services are stored at the code level:

/**
 * Map(namespace, Map(group::serviceName, Service)).
 */
private final Map<String, Map<String, Service>> serviceMap = new ConcurrentHashMap<>();
Copy the code

That is, the Nacos Service registry structure is: Map<namespace, Map<group::serviceName, Service>>.

Nacos is namespace-based in design for multi-environment and multi-tenant data (configuration and service) isolation. If you have multiple environments (development, test, production, etc.), you can create three different namespaces, such as dev-namespace and prod-Namespace in the figure above.

Nacos service domain model

In the above data schema, we can locate a Service. What is the model of the Service? The official website provides the following image:

From hierarchical storage model in this picture you can see, in the level of service, save the health check switch, metadata, the routing mechanism, and protection of the threshold Settings, save the health examination and cluster model, metadata, data synchronization mechanism, instance holds the IP, port, the weight of the instance, state of health examination, offline, metadata, the response time.

At this point, we ignore the one-to-many situation, and the relationship of data storage in the whole Nacos is shown as follows:

It can be seen that Namespace contains multiple groups, Group contains multiple services, Service contains multiple clusters, and Cluster contains Instance sets.

The corresponding source code is as follows:

/ / ServiceManager class, Map(namespace, Map(group::serviceName, Service)) private final Map<String, Map<String, Service>> serviceMap = new ConcurrentHashMap<>(); // Service class, Map(cluster, cluster) private Map<String, cluster > clusterMap = new HashMap<>(); // Cluster class private Set<Instance> persistentInstances = new HashSet<>(); private Set<Instance> ephemeralInstances = new HashSet<>(); // Instance class private String instanceId; private String ip; private int port; Private double weight = 1.0d; / /...Copy the code

Instances are divided into temporary instances and persistent instances. The key difference is the way the health check is done. Temporary instances use client-side reporting mode, while persistent instances use server-side reverse probing mode.

Temporary instances need to be able to automatically remove unhealthy instances without persisting the instance. Persistent instances use server-side health checks because clients do not report heartbeats and cannot automatically remove offline instances.

summary

We start with why service discovery is used in microservices, and then introduce Nacos, the implementation mechanism of Nacos, the underlying data model, and part of the source code implementation.

In addition to Service registration and discovery and health check, Namespace, Group, Service and Instance in the Service data model also need to be paid attention to.

Once you understand how this works, the integration and configuration of the upper layer applications can be easily applied.

Nacos series

Why is the String. Intern method used in Nacos source code? “The soul ferryman of micro services — Nacos, to a complete overview of the principle” \


Program new horizon

\

The public account “program new vision”, a platform for simultaneous improvement of soft power and hard technology, provides massive information

\