Nacos集群部署

330 阅读5分钟

Nacos集群部署

准备环境

  1. 64 bit OS Linux/Unix/Mac,推荐使用Linux系统。
  2. 64 bit JDK 1.8+; JDK1.8下载以及安装配置
  3. Maven 3.2x+;
  4. 3个或3个以上Nacos节点才能构成集群
  5. nacos-server-1.4.0.tar.gz下载

安装Mysql5.7数据库

请根据Mysql5.7.14安装教程进行安装。

Nacos数据库配置


1.上传tar包并解压
tar -zxvf nacos-server-1.4.0.tar.gz

解压之后nacos的目录结构

2.创建nacos数据库
  • 查看nacos数据库sql脚本
cd /home/downloads/nacos/conf
  • 创建nacos数据库
-- 创建数据库
create database nacos;
-- use nacos
use nacos;
-- 执行数据库脚本
source /home/downloads/nacos/conf/nacos-mysql.sql;
-- 查看数据库表
show tables;
3.修改nacos数据库信息

将Nacos内置型数据库切换为Mysql数据库

  • 备份配置文件
cp application.properties application.properties.bak
  • 修改application.properties配置文件中mysql配置
### If use MySQL as datasource:
spring.datasource.platform=mysql

Count of DB:



db.num=1



Connect URL of DB:



db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user=root
db.password=root

db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC db.user=root db.password=root

Nacos集群部署

在Linux服务器上配置的nacos的集群配置文件为cluster.conf

1.复制配置文件
cp /home/downloads/nacos/conf/cluster.conf.example cluster.conf
2.修改集群配置文件
#查看本机网卡IP
[root@centos conf]# hostname -I
192.168.15.100 172.17.0.1 
#配置集群节点ip和端口
[root@centos conf]# vim cluster.conf
#it is ip
#example
#192.168.16.101:8847
#192.168.16.102
#192.168.16.103

#添加本机ip+集群节点端口
192.168.15.100:8818
192.168.15.100:8828
192.168.15.100:8838
3.修改Nacos集群的启动脚本

首先备份下原始启动脚本

cp /home/downloads/nacos/bin/startup.sh /home/downloads/nacos/bin/startup.sh.bak

修改启动脚本

[root@centos bin]# vim startup.sh
# 142行
142 nohup $JAVA ${JAVA_OPT} nacos.nacos >> ${BASE_DIR}/logs/start.out 2>&1 &
#修改如下,添加-Dserver.port=${PORT}`
142 nohup $JAVA -Dserver.port=${PORT} ${JAVA_OPT} nacos.nacos >> ${BASE_DIR}/logs/start.out 2>&1 &
4.查看集群各节点是否启动
[root@centos bin]# ps -ef|grep nacos|grep -v grep|wc -l

Nginx负载均衡部署

1.下载Nginx

Nginx下载地址:nginx.org/download/ng…

2.安装nginx依赖库
  • 安装gcc环境
#nginx 编译时依赖gcc环境
[root@centos software]# yum -y install gcc gcc-c++
  • 安装pcre
# 让nginx支持重写功能
[root@centos software]# yum -y install pcre pcre-devel
  • 安装zlib
# zlib库提供了很多压缩和解压缩的方式,nginx使用zlib对http包内容进行gzip压缩
[root@centos software]# yum -y install zlib zlib-devel
  • 安装openssl
# 安全套接字层密码库,用于通信加密
[root@centos software]# yum -y install openssl openssl-devel
3.解压nginx压缩包
tar -zxvf nginx-1.18.0.tar.gz
4.检查编译环境
# 进入压缩目录
[root@centos software]# cd nginx-1.18.0
# --prefix=/usr/local/nginx  是 nginx 编译安装的目录(默认),安装完后会在此目录下生成相关文件
[root@centos nginx-1.18.0]# ./configure --prefix=/usr/local/nginx
5.源码编译安装
# 编译
[root@centos nginx-1.18.0]# make
# 安装
[root@centos nginx-1.18.0]# make install
6.nginx设置软连接
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
7.Nginx启动
  • 启动nginx服务
[root@centos nginx-1.18.0]# nginx
  • Web访问nginx,默认端口是80
  • 查看nginx进程
[root@centos nginx-1.18.0]# ps -ef |grep nginx
root      75855      1  0 16:04 ?        00:00:00 nginx: master process nginx
nobody    75856  75855  0 16:04 ?        00:00:00 nginx: worker process
root      75863  61143  0 16:06 pts/1    00:00:00 grep --color=auto nginx
  • 重新加载nginx服务
[root@centos nginx-1.18.0]# ps -ef |grep nginx
root      75855      1  0 16:04 ?        00:00:00 nginx: master process nginx
nobody    75866  75855  0 16:06 ?        00:00:00 nginx: worker process
root      75868  61143  0 16:06 pts/1    00:00:00 grep --color=auto nginx

两次的进程号不一样,master进程没有变化

  • 停止nginx服务
[root@centos nginx-1.18.0]# nginx -s stop
[root@centos nginx-1.18.0]# ps -ef |grep nginx
root      75871  61143  0 16:08 pts/1    00:00:00 grep --color=auto nginx

Nginx整合Nacos

1.备份nginx配置文件
cp /usr/local/nginx/conf/nginx.conf  /usr/local/nginx/conf/nginx.conf.bak
2.配置nginx配置文件
[root@centos conf]# vim nginx.conf

    #gzip  on;
    
    upstream cluster{
        server 127.0.0.1:8818;
        server 127.0.0.1:8828;
        server 127.0.0.1:8838;
    }


    server {
        listen       8888;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            # root   html;
            # index  index.html index.htm;
            proxy_pass http://cluster;
        }

修改处:

  1. 增加 upstream cluster
  2. server的监听端口 80 --> 8888
  3. location注释掉原有的,增加 proxy_pass http://cluster
3.启动Nacos集群
$ cd /opt/nacos/bin
$ ./startup.sh -p 8818
$ ./startup.sh -p 8828
$ ./startup.sh -p 8838
4.web访问测试
# 虚拟机IP:8888/nacos
# 账号: nacos
# 密码: nacos

完结补充说明:

本次Nacos集群并未搭建成功,原因是由于本次学习使用的是本地虚拟机,由于nacos集群模式启动,所需内存过大无法启动,即使改小也不行,因为当前目录内存太小。