I. Introduction to Nacos

Nacos is a platform for microservice management. Its core functions are service registration and discovery and service configuration management.

  • As a service registration discovery component, Nacos can replace the traditional service registration discovery component in Spring Cloud applications, such as Eureka, Consul, and support the health check of services.
  • Nacos acts as a service configuration hub, replacing Spring Cloud Config.

Of course, as a micro-service management platform, Nacos not only faces Spring Cloud, but also supports many other micro-service infrastructures, such as Docker, Dubbo, Kubernetes, etc. In addition to the core service registration and discovery and configuration management functions, it also provides various service management functions, such as dynamic DNS and service metadata management.

Second, SINGLE point deployment of Nacos

Nacos supports a single point of deployment mode and the standalone mode is very simple to set up. In fact, Nacos has no installation process, just download and launch. However, there is no high availability support for this case, so it is only suitable for testing or learning. First go to the Github address of Nacos to download the Release installation package. Nacos is developed in Java and compiled and packaged using Maven. So instead of packaging it ourselves, we’re going to use the Release installation package. The download address is github.com/alibaba/nac… . In Linux, you can run the following command to download and decompress files.

# download nacos wget HTTP: / / https://github.com/alibaba/nacos/releases/download/1.1.4/nacos-server-1.1.4.tar.gz; Unzip nacos tar-xvf nacos-server-1.1.4.tar.gzCopy the code

Go to the nacos/bin directory. The startup command is used to start nacOS and the shutdown command is used to stop nacOS.

  • If you are running Linux/Unix, usesh startup.sh -m standaloneScript startup mode.
  • If you’re running Windows, double-click startup. CMD to start nacOS.

The default service port for Nacos is 8848. After starting up, you can access Nacos from a browser: http://ip:8848/nacos/. When you see the following interface, you need to log in. The default user name and password are all nacOS. After logging in, you will see the following interface:

If you cannot access the above screen, check the firewall Settings of your host operating system. The following command is used to open port 8848 for the firewall of CentOS7.

firewall-cmd --zone=public --add-port=8848/tcp --permanent    
firewall-cmd --reloadCopy the code

3. View the host NIC name

Run the IP addr command to view the network adapter of the Linux host.

  • The first LO network IP is the loop IP, 127.0.0.1, which is standard
  • The IP address of the second ENP0S3 network device is 10.0.2.15. This is because my server network uses dual network cards: bridge +HostOnly mode. A normal server does not exist.
  • The third enP0s8 is the real network IP address used by the server to provide services, such as 192.168.161.4.
  • The fourth network device is a Docker0 network device because I have installed Docker on this VIRTUAL machine.

Due to a large number of network devices, I had problems in the subsequent installation process. Here’s the secret.

4. Configure the NACOS cluster

We have prepared three servers (virtual machines) : 192.168.161.3, 192.168.161.4, and 192.168.161.5. Download and decompress NACOS on the three servers, and open port 8848. Refer to the standalone deployment mode for operation. Then add the IP addresses of the three servers to conf/cluster.conf

# the IP: port 192.168.161.3:8848 192.168.161.4:8848 192.168.161.5:8848Copy the code

  • To initialize the MySQL database, the SQL source file is the nacos-mysql.sql file in the nacos/conf unzip directory. SQL statement source file. And in nacos/conf/application. The properties of increasing mysql configuration
Spring. The datasource. Platform = mysql db. Num = 1 db. Url. 0 = JDBC: mysql: / / 192.168.161.3:3306 / testdb? characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true db.user=test db.password=Copy the code

  • Spring. The datasource. The platform that supports data persistence database type, known currently only supports mysql
  • Num = 1; db = 1; db = 1
  • Db.url. 0 represents the JDBC URL connection for the first mysql database. If there is a second or a third, add n in db.url.n.

Problem: Solve the problem that the nic is not the one we want to bind: After the configuration is complete, use startup.sh to start. No record was found in the cluster node list. In addition, an error is reported in the background service log as follows:

Analysis: From the log, we can see that the NACOS program automatically obtains the IP address 10.0.2.15, but we configured the IP address 192.168.161.x. The two are inconsistent, so an error is reported. Let’s take a look at the source code to obtain the host IP, as follows:

private static String getHostAddress() { String address = System.getProperty("nacos.server.ip"); if (StringUtils.isNotEmpty(address)) { return address; } else {address = "127.0.0.1"; }... }Copy the code

Solution: After reading the source code, the IP is obtained via System.getProperty, so we can specify the IP by JVM arguments. Add nacos.server. IP to the startup script of startup.sh.

#================================================================================= # JVM Configuration # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = # stand-alone MODE corresponding to the launch parameters if the [[= = "${MODE}" "standalone" ]]; Then JAVA_OPT="${JAVA_OPT} -xMS512m -xmx512m -xmn256m "JAVA_OPT="${JAVA_OPT} -dnacos. standalone=true" else # Startup parameter corresponding to cluster mode  JAVA_OPT="${JAVA_OPT} -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m" JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -xx :HeapDumpPath=${BASE_DIR}/logs/ javA_heapdump. hprof" JAVA_OPT="${JAVA_OPT} -xx: -uselargePages "# Add the following parameters to set the local IP address JAVA_OPT="${JAVA_OPT} -dnacos.server. IP = IP address of your server "fiCopy the code

When all the above work is done, we access the NacOS service through the browser and see the following interface. In the cluster management node list, there are three nodes whose IP addresses are 192.168.161.3:8848, 192.168.161.4:8848, and 192.168.161.5:8848.

5. Nacos cluster architecture

After configuring the NACOS cluster, we can access nacOS services in the cluster through three portals, so the next problem is how to convert the three portals into one. Many individual developers write blogs or tutorials today by adding a load balancer to the front of the three NACOS services, such as Nginx and HaProxy. Then it’s called a product-level scaffolding approach, but that’s never going to work for production because your Nginx and HaProxy are single points, and once Nginx goes down, the whole service goes down.

The method recommended by nacOS official website is to use virtual IP, as follows:

  • Initially, the virtual IP 192.168.161.6 May be bound to the host 192.168.161.3, and the nacOS service of the host 192.168.161.3 can be accessed through both IP addresses.
  • If the host 192.168.161.3 breaks down or other network faults occur, the host 192.168.161.6 is automatically bound to the host 192.168.161.4 or 192.168.161.5. This process is called virtual IP drift.

This virtual IP approach does not use load balancing and still accesses the NACOS service of a certain node, but forms a master/slave backup and provides high availability. That can provide both high availability, and can provide a load balancing method may have some friends have thought of, as shown in the following figure:

  • Add nginx or HaProxy load balancer in front of nacOS service
  • Then the load balancer uses virtual IP and keepalived to implement the drift of virtual IP
  • The user accesses the load balancer to achieve access to the NacOS service, the master Nginx hangs, and the virtual IP drifts to the slave Nginx load balancer to provide the service

Vi. Nacos Cluster (Virtual IP Drift)

Let’s take an example of how to access a NACOS cluster using a virtual IP as recommended in the official website. Why not the third one? Generally speaking, people who have reached the level of system architecture will know how to do the third way when they understand this way, while people who have not reached the level of system architecture will still not understand the third way when they hear it.

6.1. Install and configure Keepalived

Install Keepalived on all three servers

yum install -y keepalivedCopy the code

In the three servers are modify/etc/keepalived/keepalived conf

vrrp_instance VI_1 { state MASTER interface enp0s8 virtual_router_id 51 priority 100 advert_int 1 authentication { Auth_type PASS auth_pass 123456} virtual_ipAddress {192.168.161.6}}Copy the code
  • One server is MASTER, and the other servers are BACKUP
  • Interface is configured as the name of the network card we just looked at
  • virtualrouterThe ids must be the same, indicating that the three servers use the same virtual IP address.
  • Change the priority of the three servers, for example, 100, 101, and 102. The server with the highest priority uses the virtual IP address first. The MASTER must have a higher priority than the BACKUP host
  • Advert_int is the synchronization check time between several servers, 1 second
  • The authentication Settings must be consistent so that the servers can communicate
  • Example Change virtual_ipAddress to the unoccupied IP addresses in the network segment where the three servers reside, for example, 192.168.161.6

6.2. Modifying the Firewall CentOS7 The firewall configuration must be enabled. Otherwise, the three hosts cannot communicate with each other on the priority of the virtual IP address.

firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0  --protocol vrrp -j ACCEPT;
firewall-cmd --reload;Copy the code

6.3. Start the Keepalived service

sudo systemctl restart keepalived.serviceCopy the code

We look forward to your attention

  • I recommend the blogger’s series of documents: “Hand on Hand to teach you to learn SpringBoot Series – Chapter 16, Section 97”.
  • This article is reprinted with a credit (must be accompanied by a link, not only the text) : Antetokounmpo blog.