Build Nacos cluster + nginx for load balancing
-
Download Nacos: Visit the official website of Nacos (https://nacos.io), select your operating system and version on the download page, and download the installation package of Nacos.
-
Unzip the installation package: Unzip the downloaded installation package to the specified directory.
tar -zxvf nacos-server-2.0.3.tar.gz
-
Configure cluster nodes: enter the decompressed Nacos directory, enter
conf
the folder, and editcluster.conf
the file. In this file, add the IP address and port number of each node in the cluster, one node per line. (The new version of nacos does not have cluster.conf, you can copy the file cluster.conf.example and rename it)# 复制文件重命名 cp cluster.conf.example cluster.conf # 编辑节点信息 vim cluster.conf ##############编辑内容如下############### #it is ip #example 节点的ip + 端口 192.163.45.44:8848 192.163.45.45:8848 192.163.45.46:8848
-
Use MySQL as the data source to create a new database, modify the data source configuration, there is a sql script nacos-mysql.sql in the conf directory, just create a new database and execute it
-
Add configuration in conf/application.properties
spring.datasource.platform=mysql db.num=1 db.url.0=jdbc:mysql://192.163.45.44:3306/nacos_config_llh?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC db.user.0=root db.password.0=root
-
Similarly, other nodes also do the above modification operations
-
Start the nacos cluster (start the nacos nodes separately)
./startup.sh
-
Add nginx configuration to forward requests, nginx configuration reference
-
Add the following content (inside http) in the nginx configuration file, the content is as follows
upstream cluster{ server 192.163.45.44:8848; server 192.163.45.45:8848; server 192.163.45.46:8848; } server { listen 7847; server_name localhost; charset utf-8; location / { proxy_pass http://cluster/; } #access_log /data/nginx/logs/nacos.log; #error_log /data/nginx/logs/nacos.log; }
-
Add the grpc protocol configuration of nacos in the nginx configuration file, note that the stream needs to be outside of http
stream { upstream lb-nocos-tcp{ server 192.163.45.44:9848 weight=1; server 192.163.45.45:9848 weight=1; server 192.163.45.46:9848 weight=1; } server { listen 8847; proxy_pass lb-nocos-tcp; } } http{ ... }
-
-
start nginx