Loading

前后端分离集群部署案例

1. 部署环境说明

部署项目:Ruo-Yi

介绍:基于SpringBoot、Spring Security、Jwt、Vue的前后端分离的后台管理系统

前端:Vue

后端:Java SpringBoot(jar包)

项目代码地址:https://gitee.com/y_project/RuoYi-Vue


环境要求 版本 说明
MySQL >= 5.7 存储数据信息
Redis >= 3 提供缓存服务
JDK >= 1.8 提供java环境,用于运行jar包、Maven编译打包
nginx 1.20.1 提供web,用于运行前端代码
Maven >= 3.0 编译打包后端代码
nodejs >= 12 编译打包前端代码
NFS 提供共享存储

主机说明

主机 IP 说明
lb01 外网:10.0.0.5
内网:172.16.1.5
七层负载+四层负载+keepalived高可用,VIP:10.0.0.150
lb02 外网:10.0.0.6
内网:172.16.1.5
七层负载+四层负载+keepalived高可用,VIP:10.0.0.150
web01 172.16.1.7 前端运行
web02 172.16.1.8 前端运行
web03 172.16.1.9 后端运行
web04 172.16.1.10 后端运行
db01 172.16.1.51 MySQL数据库、Redis缓存
nfs01 172.16.1.31 NFS共享存储
devops 172.16.1.71 编译打包、测试环境

目录说明

目录 说明
/app/code 存放前后端项目代码包
/server/soft 存放JDK、nodejs、maven软件包
/app/tools 软件运行目录(JDK、nodejs、maven)
/app/build devops主机代码构建编译目录

2. 部署MySQL

2.1 配置mysql 5.7版本的源

在db01上进行书写repo文件

cat > /etc/yum.repos.d/mysql5.7.repo << 'EOF'
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-connectors-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

[mysql-tools-community]
name=MySQL Tools Community
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-tools-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

[mysql-5.7-community]
name=MySQL 5.7 Community Server
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el7-$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

2.2 安装mysql 5.7

yum makecache
yum install -y mysql-community-server

2.3 初始化mysql

#启动mysql设置开机自启
systemctl enable mysqld
systemctl start mysqld

#获取默认生成的mysql密码
grep -i 'tempor.*password' /var/log/mysqld.log

#Mysql安全启动配置向导
[root@db01 ~]# mysql_secure_installation  

Securing the MySQL server deployment.

Enter password for user root:  #输入刚从获取的密码

#用户现有密码已过期。请设置一个新密码。
The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password:
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100   #密码强度已经100
#按y重新设置密码,按任意键为不重新修改。
Change the password for root ? ((Press y|Y for Yes, any other key for No) :

 ... skipping.(任意键跳过)
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

#是否删除匿名用户
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

#是否不允许root远程登录
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

#是否删除测试数据库
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

#正在删除对测试数据库的权限
 - Removing privileges on test database...
Success.

#重新加载权限表将确保所有更改生效
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

#是否立即重新加载权限表
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

2.4 创建数据库、授权用户

[root@db01 ~]# mysql -u root -p
Enter password:

#执行
create database ruoyi charset utf8mb4 collate utf8mb4_general_ci;
grant all on ruoyi.* to 'ruoyi'@'172.16.1.%' identified by 'Huawei@123';

2.5 导入数据库

拉取项目,导入sql目录中两个sql文件。

#安装git
yum install -y git

#进入到项目sql目录
git clone https://gitee.com/y_project/RuoYi-Vue.git

#导入
cd RuoYi-Vue/sql/
mysql -u root -p ruoyi < ry_20230706.sql
mysql -u root -p ruoyi < quartz.sql

#查看
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| ruoyi              |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> select user,host from mysql.user;
+---------------+------------+
| user          | host       |
+---------------+------------+
| ruoyi         | 172.16.1.% |
| mysql.session | localhost  |
| mysql.sys     | localhost  |
| root          | localhost  |
+---------------+------------+
4 rows in set (0.00 sec)

mysql> use ruoyi;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+--------------------------+
| Tables_in_ruoyi          |
+--------------------------+
| QRTZ_BLOB_TRIGGERS       |
| QRTZ_CALENDARS           |
| QRTZ_CRON_TRIGGERS       |
| QRTZ_FIRED_TRIGGERS      |
| QRTZ_JOB_DETAILS         |
| QRTZ_LOCKS               |
| QRTZ_PAUSED_TRIGGER_GRPS |
| QRTZ_SCHEDULER_STATE     |
| QRTZ_SIMPLE_TRIGGERS     |
| QRTZ_SIMPROP_TRIGGERS    |
| QRTZ_TRIGGERS            |
| gen_table                |
| gen_table_column         |
| sys_config               |
| sys_dept                 |
| sys_dict_data            |
| sys_dict_type            |
| sys_job                  |
| sys_job_log              |
| sys_logininfor           |
| sys_menu                 |
| sys_notice               |
| sys_oper_log             |
| sys_post                 |
| sys_role                 |
| sys_role_dept            |
| sys_role_menu            |
| sys_user                 |
| sys_user_post            |
| sys_user_role            |
+--------------------------+
30 rows in set (0.00 sec)

3. 部署Redis

db01下进行

yum install -y redis

更改redis服务监听地址和密码

#更改服务监听地址
sed -i '61s/127.0.0.1/172.16.1.51/' /etc/redis.conf

#更改密码
sed -i 's/# requirepass foobared/requirepass Huawei@123/g' /etc/redis.conf

启动redis服务并设置开机自启

systemctl start redis
systemctl enable redis

检查服务状态

[root@db01 ~]# ps -ef |grep redis
redis     51491      1  0 09:15 ?        00:00:00 /usr/bin/redis-server 172.16.1.51:6379
root      51560   1984  0 09:17 pts/0    00:00:00 grep --color=auto redis

测试可用性

[root@db01 ~]# redis-cli -h 172.16.1.51
172.16.1.51:6379> KEYS *
(error) NOAUTH Authentication required.
172.16.1.51:6379> AUTH Huawei@123
OK
172.16.1.51:6379> KEYS *
(empty list or set)
172.16.1.51:6379>

4. 部署代码编译环境

4.1 配置nodejs环境

下载nodejs软件包

#创建软件包存放目录
mkdir -p /server/soft
wget --no-check-certificate -P /server/soft/ https://mirrors.tuna.tsinghua.edu.cn/nodejs-release/v14.19.3/node-v14.19.3-linux-x64.tar.gz

解压、创建软链接并追加PATH环境变量。

#创建软件运行目录
mkdir -p /app/tools
tar xf /server/soft/node-v14.19.3-linux-x64.tar.gz -C /app/tools/
ln -s /app/tools/node-v14.19.3-linux-x64/ /app/tools/node

#追加PATH环境变量
echo 'export PATH=/app/tools/node/bin/:$PATH' >> /etc/profile
source /etc/profile

检查是否可用

[root@devops ~]# node -v
v14.19.3

修改node源

#修改
npm config set registry https://registry.npmmirror.com

#查看
npm config get registry https://registry.npmmirror.com

4.2 配置Maven环境

4.2.1 配置JDK

下面通过二进制包进行部署,先创建软件包存放目录、软件运行目录、创建软链接方便升级。(JDK下载点这

#下载软件包到soft目录,进行解压到指定目录
wget --no-check-certificate -P /server/soft/ https://repo.huaweicloud.com/java/jdk/8u192-b12/jdk-8u192-linux-x64.tar.gz
tar xf /server/soft/jdk-8u192-linux-x64.tar.gz -C /app/tools

#创建软链接
ln -s /app/tools/jdk1.8.0_192 /app/tools/jdk

#查看
[root@devops ~]# ll /app/tools/
total 0
lrwxrwxrwx 1 root root  23 Sep  9 09:28 jdk -> /app/tools/jdk1.8.0_192
drwxr-xr-x 7   10  143 245 Oct  6  2018 jdk1.8.0_192
lrwxrwxrwx 1 root root  35 Sep  9 09:22 node -> /app/tools/node-v14.19.3-linux-x64/
drwxr-xr-x 6 1001 1001 108 May 18  2022 node-v14.19.3-linux-x64

配置JAVA环境变量,一般是二进制安装需要配置,yum安装不需要。

cat >>/etc/profile<<'EOF'
export JAVA_HOME=/app/tools/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
EOF

Tips:'EOF'是对下面的$不进行解析!


使环境变量生效并检查Java版本

#使环境变量生效
source /etc/profile

[root@devops ~]# java -version
java version "1.8.0_192"
Java(TM) SE Runtime Environment (build 1.8.0_192-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.192-b12, mixed mode)

4.2.2 配置maven

下载maven软件包,解压、建立软链接并检查可用。

#下载maven软件包
wget https://archive.apache.org/dist/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz -P /server/soft/

#解压到指定目录
tar xf /server/soft/apache-maven-3.6.3-bin.tar.gz -C /app/tools/

#建立软链接
ln -s /app/tools/apache-maven-3.6.3/ /app/tools/maven

#通过输出版本信息检查可用
[root@devops ~]# /app/tools/maven/bin/mvn --version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /app/tools/maven
Java version: 1.8.0_192, vendor: Oracle Corporation, runtime: /app/tools/jdk1.8.0_192/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.el7.x86_64", arch: "amd64", family: "unix"

Tips:Maven前提是需要依赖JDK,所以JDK环境先配置。


配置maven环境变量

#配置环境变量
echo 'export PATH=/app/tools/maven/bin/:$PATH' >> /etc/profile
source /etc/profile

#检查环境变量生效
[root@devops ~]# mvn --version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /app/tools/maven
Java version: 1.8.0_192, vendor: Oracle Corporation, runtime: /app/tools/jdk1.8.0_192/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.el7.x86_64", arch: "amd64", family: "unix"

配置maven依赖下载源,在配置文件中找到下列内容,将不同的地方进行替换即可。

[root@devops ~]# vim /app/tools/maven/conf/settings.xml
  <mirrors>
    <mirror>
      <id>aliyun maven</id>
      <mirrorOf>*</mirrorOf>
      <name>aliyun的maven公共仓库</name>
      <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
  </mirrors>

image-20230909093237302


4.3 后端代码编译

4.3.1 拉取项目代码

先拉取项目后端代码到本地

#安装git
yum install -y git

#创建代码构建编译目录
mkdir -p /app/build

#拉取项目到指定目录
git clone https://gitee.com/y_project/RuoYi-Vue.git /app/build/

#进入到项目主目录
cd /app/build/

4.3.2 修改后端配置文件
tree -L 1 /app/build/ruoyi-admin/src/main/resources/

image-20230909095707262


4.3.2.1 修改日志配置

修改该文件,修改日志存放目录。

vim /app/build/ruoyi-admin/src/main/resources/logback.xml

image-20230909100152318


修改日志输出编码格式,使用UTF-8。在该文件下的每一个<pattern>${log.pattern}</pattern>下添加<charset>UTF-8</charset>

image-20230909100239389


4.3.2.2 修改数据源配置

修改该文件

vim /app/build/ruoyi-admin/src/main/resources/application-druid.xml

image-20230909100656242


4.3.2.3 修改程序配置

修改数据存储目录

vim /app/build/ruoyi-admin/src/main/resources/application.xml

image-20230909111625694


修改redis配置

image-20230909101050544


修改后端程序端口(可选,保持默认也可)

image-20230909101200576


4.3.3 maven构建编译打包

在项目主目录进行

[root@devops build]# pwd
/app/build
[root@devops build]# mvn clean install -DskipTests

clean:清理项目目录下的target文件夹,删除之前编译生成的文件。

install:将项目构建并安装到本地仓库中。这意味着项目将被编译、打包成jar或war文件,并被复制到本地Maven仓库供其他项目使用。

-DskipTests:跳过运行测试。通常在构建过程中运行测试,但使用该参数可以跳过测试阶段,加快构建速度。


编译完成,构建的jar包在/app/build/ruoyi-admin/target目录下。

image-20230909102055232

image-20230909102121841


4.4 前端代码打包

4.4.1 修改前端配置文件

进入前端项目目录,修改网页标题,用于区分代码更新迭代。

vim /app/build/ruoyi-ui/vue.config.js

image-20230909102406354


4.4.2 npm构建打包

在项目下ruoyi-ui目录进行

[root@devops ruoyi-ui]# pwd
/app/build/ruoyi-ui
[root@devops ruoyi-ui]# npm install

警告信息可忽略,不影响构建打包。

image-20230909103105392


打包代码,打包完成后在/app/build/ruoyi-ui/dist目录下。

[root@devops ruoyi-ui]# npm run build:prod

image-20230909103612224


5. 部署测试环境

5.1 NFS存储部署

5.1.1 安装并启动NFS
yum install -y nfs-utils rpcbind
systemctl start nfs
systemctl enable nfs

5.1.2 修改NFS配置并重新加载
#修改nfs配置
cat > /etc/exports << EOF
/nfs_data 172.16.1.0/24(rw)
EOF

#重新加载配置文件
systemctl reload nfs

5.1.3 创建存储目录并修改权限
mkdir -p /nfs_data
chown nfsnobody.nfsnobody /nfs_data

[root@nfs01 ~]# showmount -e
Export list for nfs01:
/nfs_data 172.16.1.0/24

5.1.4 共享存储挂载到后端主机

web03创建文件存储目录

mkdir -p /data/ruoyi_data

挂载存储

[root@web03 ~]# mount -t nfs 172.16.1.31:/nfs_data /data/ruoyi_data
[root@web03 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 894M     0  894M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M   11M  900M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root   36G  4.5G   31G  13% /
/dev/nvme0n1p1          1014M  185M  830M  19% /boot
tmpfs                    182M   12K  182M   1% /run/user/42
tmpfs                    182M     0  182M   0% /run/user/0
172.16.1.31:/nfs_data     36G  4.4G   31G  13% /data/ruoyi_data

测试写入

#web03上进行
echo test > /data/ruoyi_data/test.txt

#nfs01上查看
[root@nfs01 ~]# ll /nfs_data/
total 4
-rw-r--r-- 1 nfsnobody nfsnobody 5 Sep  9 11:02 test.txt

5.2 前端部署

5.2.1 拉取代码

将前端代码部署在web01上/app/code/目录下

#创建代码运行目录
mkdir -p /app/code

#拉取打包好的前端代码
scp -r root@172.16.1.71:/app/build/ruoyi-ui/dist/* /app/code/

5.2.2 配置nginx
#安装nginx
yum install nginx -y

#启动nginx并设置开机自启
systemctl start nginx
systemctl enable nginx

#修改代码目录所有者
chown -R nginx:nginx /app/code/

#新增一个配置文件
[root@web01 ~]# cat /etc/nginx/conf.d/ruoyi.yinjay.com.conf
server {
        listen       80;
        server_name  ruoyi.yinjay.com;

       location / {
           # 静态文件地址
           root /app/code/;
           index  index.html index.htm index;
           try_files $uri $uri/ /index.html;
        }


       location /prod-api/ {
           proxy_set_header Host $http_host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header REMOTE-HOST $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_pass http://172.16.1.9:8080/;
       }
}

#重新加载配置文件
systemctl reload nginx

5.3 后端部署

5.3.1 拉取jar包

将后端jar包部署在web03上/app/code/目录下

#创建代码运行目录
mkdir -p /app/code

#拉取打包好的前端代码
scp -r root@172.16.1.71:/app/build/ruoyi-admin/target/ruoyi-admin.jar /app/code/

5.3.2 启动后端jar包
java -jar /app/code/ruoyi-admin.jar

5.4 联调测试

在宿主机上添加一条host记录10.0.0.7 ruoyi.yinjay.com并进行浏览器访问,能够显示验证码,说明前后端连接没问题。默认账号密码:admin/admin123

image-20230909110758914


数据访问无问题

image-20230909110954455


新增用户测试,登录该用户,同时修改头像。

image-20230909111134085

image-20230909111228641


查看存储

image-20230909111916906


5.5 清理测试环境

#web01
rm -rf /app/
rm -rf /etc/nginx/conf.d/ruoyi.yinjay.com.conf

#web03
rm -rf /app/
umount /data/ruoyi_data
rm -rf /data/

#nfs01
rm -rf /nfs_data/*

6. 接入高可用集群负载环境

6.1 部署高可用keepalived

6.1.1 环境准备
主机 IP 角色
lb01 10.0.0.5 Master
lb02 10.0.0.6 Backup

6.1.2 安装服务
#lb01、lb02安装keepalived
yum install keepalived -y

#启动服务并设置开机自启
systemctl start keepalived
systemctl enable keepalived

6.1.3 修改Keepalived配置文件

lb01修改

[root@lb01 ~]# cat /etc/keepalived/keepalived.conf
global_defs {

     router_id keepalived-01

}

vrrp_instance lb_test {

     state MASTER
     nopreempt
     interface eth0
     virtual_router_id 10
     priority 100
     advert_int 1

     authentication {
           auth_type PASS
           auth_pass 123456
     }

     virtual_ipaddress {
           10.0.0.150/24 dev eth0 label eth0:1
     }

}

lb02修改

[root@lb02 ~]# cat /etc/keepalived/keepalived.conf
global_defs {

     router_id keepalived-02

}

vrrp_instance lb_test {

     state BACKUP
     nopreempt
     interface eth0
     virtual_router_id 10
     priority 50
     advert_int 1

     authentication {
           auth_type PASS
           auth_pass 123456
     }

     virtual_ipaddress {
           10.0.0.150/24 dev eth0 label eth0:1
     }

}

lb01、lb02重新加载服务

systemctl reload keepalived

6.1.4 验证vip漂移

查看lb01的ip配置情况,已经有了。

image-20230909113805454


现在模拟lb01上eth0网卡down了,查看vip是否漂移到lb02,成功漂移。

[root@lb01 ~]# ifdown eth0

image-20230909114021925

Tips:建议用虚拟机的断开网络连接进行模拟,否则重新ifup网卡也不会起vip。


6.1.5 keepalived监控nginx

需要书写脚本,监控nginx端口/进程,如果端口或进程不存在,nginx挂了同时关闭keepalived,实现vip漂移即可。

#以下均在lb01、lb02进行

#创建脚本存放目录
mkdir -p /server/scripts

#书写脚本
cat > /server/scripts/check_nginx.sh << 'EOF'
#!/bin/bash

#检查端口是否存在,通过个数判断
count=`ss -lntup | grep nginx | wc -l`

#如果端口数量为0,则关闭Keepalived
if [ $count -eq 0 ];then
    systemctl stop keepalived
fi
EOF

#赋予权限
chmod +x /server/scripts/check_nginx.sh

修改keepalived配置文件

示例

#放在vrrp_instance 实例上面
vrrp_script check_nginx {                  #check_nginx名字给脚本起个命令(keepalived使用)
   script /server/scripts/check_nginx.sh   #指定脚本路径,要有执行权限。
   interval 1                              #执行脚本的间隔,默认1秒
   timeout 30                              #脚本执行的超时时间,一般用于curl/wget等操作
   weight 1                                #权重(优先级) 如果仅仅1个脚本,可以忽略。一般用于keepalived中有多个监控脚本。
} 

#放在与在vrrp_instance实例里面并列
track_script {
   check_nginx
}

lb01的keepalived配置

[root@lb01 ~]# cat /etc/keepalived/keepalived.conf
global_defs {

     router_id keepalived-01

}

vrrp_script check_nginx {
     script /server/scripts/check_nginx.sh
     interval 1
     timeout 30
     weight 1

}

vrrp_instance lb_test {

     state MASTER
     nopreempt
     interface eth0
     virtual_router_id 10
     priority 100
     advert_int 1

     authentication {
           auth_type PASS
           auth_pass 123456
     }

     virtual_ipaddress {
           10.0.0.150/24 dev eth0 label eth0:1
     }


     track_script {
           check_nginx
     }

}

lb02的keepalived配置

[root@lb02 ~]# cat /etc/keepalived/keepalived.conf
global_defs {

     router_id keepalived-02

}

vrrp_script check_nginx {
     script /server/scripts/check_nginx.sh
     interval 1
     timeout 30
     weight 1

}

vrrp_instance lb_test {

     state BACKUP
     nopreempt
     interface eth0
     virtual_router_id 10
     priority 50
     advert_int 1

     authentication {
           auth_type PASS
           auth_pass 123456
     }

     virtual_ipaddress {
           10.0.0.150/24 dev eth0 label eth0:1
     }


     track_script {
           check_nginx
     }

}

重新加载配置文件

#lb01、lb02
systemctl reload keepalived

6.2 前端接入七层负载

lb01、lb02下进行,书写一个配置文件。

cat > /etc/nginx/conf.d/ruoyi.yinjay.com.conf << 'EOF'
upstream front_pools {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
}


server {
        listen       80;
        server_name  ruoyi.yinjay.com;

       location / {
          # 静态文件地址
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://front_pools/;
        }


       location /prod-api/ {
           proxy_set_header Host $http_host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header REMOTE-HOST $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_pass http://localhost:8080/;
       }
}
EOF

6.3 后端接入四层负载

安装四层负载的插件

yum install -y nginx-mod-stream

在nginx.conf配置文件下与http区块同级,配置以下内容,下面直接用追加方式进行。

cat >> /etc/nginx/nginx.conf << EOF
stream {

    upstream backend_pools {
         server 172.16.1.9:8080;
         server 172.16.1.10:8080;
    }

    server {
        listen 8080;
        proxy_pass backend_pools;
   }
}
EOF

加载七层负载和四层负载的配置

#lb01、lb02
systemctl reload nginx

image-20230909142224122

image-20230909142237449


6.4 前端部署

6.4.1 拉取代码

将前端代码部署在web01、web02上/app/code/目录下

#创建代码运行目录
mkdir -p /app/code

#拉取打包好的前端代码
scp -r root@172.16.1.71:/app/build/ruoyi-ui/dist/* /app/code/

6.4.2 配置nginx

web01、web02上进行

#安装nginx
yum install nginx -y

#启动nginx并设置开机自启
systemctl start nginx
systemctl enable nginx

#修改代码目录所有者
chown -R nginx:nginx /app/code/

#新增一个配置文件
cat > /etc/nginx/conf.d/ruoyi.yinjay.com.conf << 'EOF'
server {
        listen       80;
        server_name  ruoyi.yinjay.com;

       location / {
          # 静态文件地址
            root /app/code/;
            index  index.html index.htm index;
            try_files $uri $uri/ /index.html;
        }

}
EOF

#重新加载配置文件
systemctl reload nginx

6.5 后端部署

6.5.1 拉取jar包

将后端jar包部署在web03、web04上/app/code/目录下

#创建代码运行目录
mkdir -p /app/code

#拉取打包好的前端代码
scp -r root@172.16.1.71:/app/build/ruoyi-admin/target/ruoyi-admin.jar /app/code/

6.5.2 启动后端jar包

web03、web04上进行

java -jar /app/code/ruoyi-admin.jar >/dev/null &

可以通过以下命令进行查看info日志

tail -f /var/log/ruoyi/sys-info.log

6.6 接入共享存储

web04创建文件存储目录

mkdir -p /data/ruoyi_data

挂载共享存储

mount -t nfs 172.16.1.31:/nfs_data /data/ruoyi_data

6.7 集群环境联调测试

在宿主机上添加一条host记录10.0.0.150 ruoyi.yinjay.com(使用vip)并进行浏览器访问,能够显示验证码,说明前后端连接没问题。默认账号密码:admin/admin123


在web03、web04上进行监控日志的情况,假装输错验证码,输错一次刷新一下页面重新输入,可以看到负载的情况。

image-20230909144754225


用wireshark进行抓包,然后再系统里面多点几个面板,看lb主机的静态资源请求到前端主机是否进行负载,下面抓包很明显是已经负载!

image-20230909145054903


lb主机的nginx配置就是通过匹配到请求后端的七层请求,然后代理到lb主机本地的8080端口,本地8080端口由lb主机的nginx四层负载所监听,实现四层转发。

image-20230909145506976


模拟lb01的nginx故障,看看业务系统是否正常使用。

[root@lb01 ~]# systemctl stop nginx

查看lb02主机的ip情况,已漂移!

image-20230909145802300


用wireshark进行抓包,然后再系统里面多点几个面板,看lb主机的静态资源请求和动态请求是否进行负载,下面抓包很明显是已经负载!

image-20230909150039131


重新启动lb01的nginx,查看lb02的ip信息。vip依然存在,因为之前的配置中,配置了vip不抢占。

[root@lb01 ~]# systemctl start nginx

image-20230909150220686


查看redis,也有相关的session信息。

image-20230909150341388


用户上传头像,查看共享存储,没问题!

image-20230909151525671

posted @ 2023-09-18 13:20  YinJayChen  阅读(452)  评论(0编辑  收藏  举报