"Spring Cloud Alibaba Microservice Architecture" topic (8)-Spring Cloud Alibaba's Nacos high-availability cluster environment construction

1 Introduction

In the previous study, in the microservice project built on SpringCloud, Eurekaserved as the registration center to realize the registration and discovery of services. Such a combination was still used a lot in the first generation of SpringCloud microservices. Later, a domestic Bat company was based on SpringCloudthe expansion and extension With some of its own microservice components, the second-generation microservice Spring Cloud Alibaba emerged. Some of these components are still relatively high. For example nacos, the nacosregistration center and the configuration center are integrated, which greatly eliminates the need for In the first generation of microservices , the configuration file needs to Spring Cloud Configbe combined with the Spring Cloud Busmessage bus to dynamically refresh the configuration file, and the configuration file needs to be stored in the Git version control library, which is too cumbersome, and Nacos comes with a console interface, which can dynamically configure and log in. To the background interface, it is very convenient to manage.

2. The difference between Eureka and Nacos cluster

As we all know, Eurekathe core is decentralization, that is, when Eureka Server services need to be clustered, they Eureka Serverregister with each other. If there are several servers, register several with each other. You can use eight words: "You have me in you, I have you in me "To understand, then centralization means that there is a boss in multiple cluster nodes, and there is no boss in decentralization. Everyone is the boss, everyone is equal, and multiple nodes are equal. nacosWhen multiple nodes are used as a cluster, a master node is required, so there is an election process.

3. Case

Assuming that our project environment is deployed as follows, the client first accesses Nginx, then Nginxpasses nacos, then to the following specific sub-services and then to the cache and finally arrives DB. NginxHere assumes that there is only one. Then think about nacoshow to cluster the next three nodes?

Insert picture description here

4. Nacos cluster deployment and construction

prompt:

NacosThe cluster deployment is cluster.confto configure the IP+Port
address of the cluster node in the configuration file provided by nacos-server , and multiple nodes can write multiple IP+Portaddresses. It itself cluster.confprovides scenarios that require cluster deployment. It can be said to be very simple. Here It needs to be emphasized that the IP address of the cluster node must be the IP of the intranet.

4.1. Modify the name of cluster.conf

Here we first find nacos-serverthe location of the installation package, find the cluster.config.examplefile in the directory , and modify the name to cluster.conf:
Insert picture description here

4.2. Edit cluster node configuration

Open the cluster.conffile, the default content is as follows:

#it is ip
#example
10.10.109.214
11.16.128.34
11.16.128.36

It is assumed that the 3 units nacosare: 192.168.1.6:8848, 192.168.1.6:8849, and 192.168.1.6:8850. Replace the above default configuration with the following configuration:

192.168.1.6:8848
192.168.1.6:8849
192.168.1.6:8850

Here: 192.168.1.6 is the intranet IP, and the address of the local cluster environment cannot be written as 127.0.0.1, remember

Insert picture description here
Then nacos-servercopy the parts 3, respectively, wherein modifying application.propertiesthe port number of the file to 8848, 8849, 8850:
Insert picture description here

5. Modify the local Hosts file

Find the directory: add the C:\Windows\System32\drivers\etcfollowing hostsfiles:
Insert picture description here

6. Configure Nginx

Find the Nginx installation directory, configure the nginx.config file, configure the access domain name: www.brucenacos.comautomatically poll to 3 nacos nodes,
Insert picture description here
modify the configuration nginx.configconfiguration as follows


#user  nobody;
worker_processes  1;


events {
    
    
    worker_connections  1024;
}


http {
    
    
    include       mime.types;
    default_type  application/octet-stream;


    sendfile        on;

    keepalive_timeout  65;
	
    upstream brucenacos{
    
    
		server 192.168.1.6:8848;
		server 192.168.1.6:8849;
		server 192.168.1.6:8850;
	} 

    server {
    
    
        listen       80;
        server_name  www.brucenacos.com;

        location /nacos/ {
    
    
           proxy_pass   http://brucenacos/nacos/;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
    
    
            root   html;
        }
    }
}

Double-click to start nginx.exe, and the http://www.brucenacos.com/following interface appears, indicating that the nginx configuration is OK, and the startup is successful.
Insert picture description here

7. Start nacos in cluster mode

Note : In the Windwos environment, the default startup is singleton. If nacos has configured the cluster node, you cannot double-click startup.cmd in the default startup method. This method is singleton startup by default, not in the cluster node mode. Start, so start the three nodes nacos-8848, nacos-8849, nacos-8850, start with the following command:

startup.cmd -m cluster

It can be seen from the console that it is started in cluster mode. nacos-8848
Insert picture description here
It can be seen from the console that it is started in cluster mode. nacos-8849
Insert picture description here
It can be seen from the console that it is started in cluster mode. nacos-8850
Insert picture description here
Access Nginx:http://www.brucenacos.com/nacos/
Insert picture description here

8. Summary

Summary 1 : It is found that there are one LEADERor two FOLLOWERnodes, which means that when one of them is down, there will be a re-election process to select a suitable LEADERnode;

Summary 2 : In a cluster environment, in a SpringCloudmicroservice project, the service registration does not need to be changed, just register a single nacosone. When a service is registered on a certain node, it will be automatically synchronized to other nodes, and its bottom layer is through the Raft algorithm. Realize the problem of maintaining data consistency.

Summary 3:

Nacos runs in stand-alone mode by default under the windows version, and the cluster operation needs to specify the execution command:startup.cmd -m cluster

Nacos runs in cluster mode by default under the Linux version. If you want to start as a stand-alone machine, you need to specify the execution command: startup.cmd –m standalone

Note : There is no need to separate multiple registered addresses with commas, just register one;
Insert picture description here

Guess you like

Origin blog.csdn.net/BruceLiu_code/article/details/113863722