Build a nacos cluster under Linux (CentOS7), super detailed

Recently, I took over the work of adjusting the project framework. I was assigned to build the nacos registration center; the nacos registration center is an open source component of Alibaba, which provides the registration center and configuration center functions of the SpringCloud Alibaba microservice architecture, and provides A more intuitive visual interface; this article will introduce how to build a nacos cluster

1. Environment preparation

Linux system : CentOS7-2009

三台主机:
192.168.64.70
192.168.64.71
192.168.64.72

nacos : nacos-2.0.2
MySQL : MySQL8.0.15;
address: 192.168.64.71:3306

Nacos download address: https://github.com/alibaba/nacos/tags
insert image description here
After downloading, upload it to the Linux server. Here, take the /usr/local/ path as an example

2. Nacos cluster construction

Upload the nacos installation package and perform the following operations

# 进入nacos上传目录
cd /usr/local/	
# 解压安装包
tar -zxvf nacos-server-2.0.2.tar.gz
# 解压好后进入nacos配置目录
cd nacos/conf/
# 复制一份cluster.conf.example并命名为cluster.conf
mv cluster.conf.example cluster.conf

insert image description here
insert image description here
The job of the nacos cluster mode is to store all registration or configuration information in the database, and each nacos node also obtains each user registration and configuration information from the database; therefore, we need to prepare a database server separately, here is mysql as an example
; There is a nacos-mysql.sql script file under the conf folder of the nacos installation directory. This script is a nacos storage database table structure script. We need to import it to the prepared mysql server

insert image description here
After the mysql server is ready, modify the cluster.conf and application.properties configurations in the conf directory.
Each server must be modified and kept consistent.

# 编辑
vim cluster.conf
# 添加以下配置
192.168.64.70:8848		#这几个地址为安装nacos的所有服务器地址以及设置的端口号
192.168.64.71:8848
192.168.64.72:8848
# 编辑
vim application.properties
# 修改以下几处配置
# 配置nacos访问uri(一般按默认就行了)
server.servlet.contextPath=/nacos
# 配置nacos访问端口(一般按默认8848就可以了)
server.port=8848
# 指定数据库类型(这里指定数据库为mysql)
spring.datasource.platform=mysql
# 数据库实例数量
db.num=1
# 数据库访问地址(即准备好的数据库服务器地址)
db.url.0=jdbc:mysql://192.168.64.71:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
# 数据库访问用户名
db.user.0=root
# 数据库访问密码
db.password.0=xiang@2021

insert image description here
At this point, the installation of nacos is complete, and each machine is started according to the following command

# 进入
cd /usr/local/nacos/bin/
# 启动nacos
sh startup.sh 

The common command to start nacos is as follows:

  • sh startup.sh -m mode: the mode has two options standalone / cluster, which represent stand-alone and cluster mode startup; stand-alone mode does not need to rely on the database to be used alone, cluster mode needs to rely on the database, and the default is cluster mode

After the startup is successful, access nacos through ip: port number/nacos ( note that the firewall of the physical machine and the virtual machine should be closed first ), and the following interface will appear to indicate that the startup is successfulinsert image description here

3. nacos set boot automatically

After nacos is installed, it needs to be turned on and off manually. You can set it to start automatically after booting. Execute the following command

# 编辑开机自启脚本
vim /lib/systemd/system/nacos.service
# 添加以下内容
[Unit]
Description=nacos
After=network.target

[Service]
Type=forking
Environment="JAVA_HOME=/usr/local/jdk1.8"
# 启动后面可以加上 -m standalone/cluster 表示以单机/集群方式启动,默认为集群
ExecStart=/usr/local/nacos/bin/startup.sh
ExecReload=/usr/local/nacos/bin/shutdown.sh
ExecStop=/usr/local/nacos/bin/shutdown.sh
PrivateTmp=true

[Install]
WantedBy=multi-user.target

insert image description here
After the configuration is complete, start the service and execute the following command

#重新加载服务
systemctl daemon-reload
#启用服务
systemctl enable nacos.service
#启动服务
systemctl start nacos.service

Guess you like

Origin blog.csdn.net/weixin_44947701/article/details/122636546