Why is a registry needed

In the previous singleton services, we did not need this component because the calls between businesses were local calls and did not involve network communication.

But now usually adopt micro service distributed architecture, the service will be a big monomer split into multiple services, a micro service and multiple instances, and many of the service node is usually run in k8s, node start at any time, at any time to kill, IP change frequently, so the service between the calls are unlikely to be configured in advance, but a dynamic change, At this point, a component is needed to solve the problem of changing communication addresses between microservices.

That’s why registries exist.

Second, the principle of the registry

Service discovery, when it comes down to it, is really simple. We have three roles: 1 service provider, 2 service consumer, and 3 registry. When the service is started, the service provider registers the IP + port and other information in the registry, and then the service consumer reads the registered information in the registry to obtain the way of remote communication.

However, because it is a key component, all of our call relationships between microservices depend on it, so it needs high availability. Then high availability involves a data consistency problem. Nacos supports both AP and CP to meet the requirements of users for data consistency.


Three, the four core points of the registry

  • 1. The Service provider registers its Service address with the registry
  • 2, The Service consumer needs to check the address of the Service provider from the registry.
  • 3. The registry needs to be aware of the up-down of the Service Provider
  • 4. The Service Consumer needs to be “dynamically aware” of changes to the Service address on the registry side

Iv. Current problems of Eureka?

  • Eureka1.0 is no longer maintained
  • When a high level vulnerability occurs, it can be quite troublesome to resolve
  • An unhealthy node takes a long time to go offline, causing traffic to enter the unhealthy node

What are Nacos?

Nacos is dedicated to helping you discover, configure, and manage microservices. Nacos provides an easy-to-use feature set that helps you quickly implement dynamic service discovery, service configuration, service metadata, and traffic management.

From the above description, we can see that NACOS actually integrates service discovery, service configuration, and service management, but we are investigating it primarily as a registry.

6. Architecture and concept of NACOS

Basic structure and concept

7. Single node deployment and integration into Spring Cloud

Implementation steps

Start nacOS Server

1. Download the latest stable version

The first thing you need to do is download the latest stable version, because the stable version has undergone extensive testing and production applications and hasn’t had a lot of bugs so far.

Because the current latest stable version is 1.4.2, we used it as the test target address: nacos-server-1.4.2.zip75.4 MB

2. Decompress and configure

In nacOS, you need to rely on mysql as the storage. Of course, Derby has an embedded database by default, but it’s not very convenient to view the data, so we unzip it to the conf directory, open the application.properties file, and edit the following:

The db. The num = 1 db. Url. 0 = JDBC: mysql: / / 127.0.0.1:3306 / nacos_config? characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTime zone=UTC db.user.0=root db.password.0=xxxxxxxCopy the code

The nacos-mysql.sql file in the directory is then executed.

The problem

Do I need to configure the database if I’m not using nacOS’s configuration center? Answer: You have to use a database, because while nacOS is not used as a configuration hub, it still has permissions, user management, and so on that require the database.

3. Start the service

Go to the bin directory and run the following command:

> sh startup.sh -m standalone
Copy the code

As we are testing locally, the standalone mode is used here. If in production, the -m standalone mode is removed to enter the cluster mode and the address of the cluster node is configured in./conf/cluster.conf.

4. Access the Web management page
  • Control interface directory: http://127.0.0.1:8848/nacos/index.html
  • The default user name and password are nacos

2. Integrate with Spring Boot and Spring Cloud

1. Add dependencies
<dependency> 
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
	<version>1.5.1. RELEASE</version>
</dependency>
Copy the code

Since we are using Spring Boot 1.5.4, we need to use Spring-cloud-starter-Alibaba -nacos-discovery 1.5.x

2. Configure the IP address of the nacOS Server

We need to add nacos during all the service configuration server address, similar to the use of eureka configuration of eureka. Client. ServiceUrl. DefaultZone.

// Set the nacos server address
spring.cloud.nacos.discovery.server-addr=127.0. 01.:8848
// Enable NACOS as a registry for microservices; if false, disable nacOS
spring.cloud.nacos.discovery.enabled=true
// Service group - Sets the service group
spring.cloud.nacos.discovery.group=DEFAULT_GROUP
.....
Copy the code

There are some additional configurations as well, which can be found in the official documentation Nacos Discovery

3. Enable service discovery

This is basically the same as using Eureka, just add an annotation to the startup class (@enableDiscoveryClient annotation), and the default load balancing strategy when using Feign as a remote call is polling

Viii. Deployment type

Nacos is defined as an IDC internal application component and is not designed for public networks. You are advised to deploy Nacos on an isolated internal network, but not on a public network.

Nacos supports three deployment modes

  • Single-machine mode – Used for testing and single-machine trial.
  • Cluster mode – Used in production environments to ensure high availability.
  • Multi-cluster mode – Used in multi-DATA center scenarios.

Ix. Problems encountered

Nohup: /Library/Internet: No such file or directory

Cause: The macBig Sur system was built with Java virtual machines, and there were Spaces in the directory, which caused NACOS to fail parsing

Solution: Specify JAVA_HOME as the JDK directory for your installation, and source reference: #4227

Ten, nacOS some functions of the source code implementation

1. How does NACOS implement health checks?

Health checks are divided into two types in NACOS:

  • The default heartbeat rate is 5 seconds. The Nacos server will set the instance as unhealthy after receiving no heartbeat for 15 seconds and remove the temporary instance after receiving no heartbeat for 30 seconds.
  • Persistent instance: persistent instances arenacos serverActive heartbeat detection, most commonly usedTCP Port DetectionandHTTP interface return code detectionThese two types of probes are sufficient for the vast majority of cases, but there are some cases that cannot be solved by these two methods, such as database availability probes, which may require an SQL execution to determine.

TCP check

Concrete classes and methods:TcpSuperSenseProcessor -> TaskProcessor -> call()

Initiate a TCP connection through IP and port.

Check the HTTP

Location:HttpHealthCheckProcessor -> process()Make an HTTP request and set the class to handle the callback

The callback processing

Check if the response code for this HTTP request is 200, otherwise try later.

Mysql check

Class: MysqlHealthCheckProcessor – > MysqlCheckTask – > run ()

Create a connection to execute the SQL. If it succeeds, the node is healthy. Otherwise, the node is unhealthy. In order to ensure the health of the database, we need to carefully design the SQL that is executed. Generally speaking, the health of the database is more effective when performing inserts and updates in the database than when performing queries.

2. Client registration and data structure?

3. Nacos consistency protocol

Among several open source implementations of the registry, Zookeeper and Eureka are representative. Their consensus protocols are CP and AP respectively. Zookeeper adopts paxOS protocol to achieve strong consistency, while Eureka adopts its own customized Renew protocol to ensure final consistency.

The following figure shows two conformance protocols supported by NACOS.

conclusion

This essay first introduces the internal reasons for the emergence of the registry, as well as the four main problems that need to be solved by the registry, and some shortcomings of Eureka. After also recorded the single node deployment of NACOS and integration with Spring Cloud, the last simple display of NACOS to achieve heartbeat, client-initiated registration function of the source code, I hope to help you.

reference

  • Detailed explanation of Nacos registry design principles
  • A bit hard and a bit fierce, worthy of the most in-depth analysis of Nacos principles
  • Nacos 1.3.0 features and functionality usage documentation