table of Contents
1 Introduction
In the previous study, in the microservice project built on SpringCloud, Eureka
served 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 SpringCloud
the 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 nacos
registration 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 Config
be combined with the Spring Cloud Bus
message 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, Eureka
the core is decentralization, that is, when Eureka Server services need to be clustered, they Eureka Server
register 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. nacos
When 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 Nginx
passes nacos
, then to the following specific sub-services and then to the cache and finally arrives DB
. Nginx
Here assumes that there is only one. Then think about nacos
how to cluster the next three nodes?
4. Nacos cluster deployment and construction
prompt:
Nacos
The cluster deployment iscluster.conf
to configure the IP+Port
address of the cluster node in the configuration file provided by nacos-server , and multiple nodes can write multipleIP+Port
addresses. It itselfcluster.conf
provides 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-server
the location of the installation package, find the cluster.config.example
file in the directory , and modify the name to cluster.conf
:
4.2. Edit cluster node configuration
Open the cluster.conf
file, 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 nacos
are: 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
Then nacos-server
copy the parts 3, respectively, wherein modifying application.properties
the port number of the file to 8848
, 8849
, 8850
:
5. Modify the local Hosts file
Find the directory: add the C:\Windows\System32\drivers\etc
following hosts
files:
6. Configure Nginx
Find the Nginx installation directory, configure the nginx.config file, configure the access domain name: www.brucenacos.com
automatically poll to 3 nacos nodes,
modify the configuration nginx.config
configuration 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.
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
It can be seen from the console that it is started in cluster mode. nacos-8849
It can be seen from the console that it is started in cluster mode. nacos-8850
Access Nginx:http://www.brucenacos.com/nacos/
8. Summary
Summary 1 : It is found that there are one LEADER
or two FOLLOWER
nodes, which means that when one of them is down, there will be a re-election process to select a suitable LEADER
node;
Summary 2 : In a cluster environment, in a SpringCloud
microservice project, the service registration does not need to be changed, just register a single nacos
one. 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;