day048-系统负载高排查流程与前后端分离项目
文章目录
- 0. 老男孩思想
- 1. 系统负载高排查流程
- 1.1 进程/线程相关命令
- 1.1.1 jps
- 1.1.2 jstack
- 1.1.3 jmap
- 1.1.4 top -Hp pid
- 1.2 排查流程图
- 2. 前后端分离项目
- 2.1 项目说明
- 2.2 负载均衡
- 2.3 数据库配置
- 2.3.1 安装数据库服务
- 2.3.2 配置数据库环境
- 2.4 后端配置
- 2.5 四层负载均衡配置
- 2.6 前端配置
- 2.6.1 nginx子配置文件
- 2.6.2 配置站点目录
- 2.6.3 配置本地hosts解析并测试
- 2.7 七层负载均衡配置
- 3. 思维导图
0. 老男孩思想
1. 系统负载高排查流程
1.1 进程/线程相关命令
1.1.1 jps
- jps:java版的ps命令,查看java进程
- -lvm:显示详细信息
1.1.2 jstack
- jstack:显示java进程的线程信息
- jstack pid |grep ‘Thread.State’ |awk ‘{print $2}’ |sort |uniq -c:统计指定进程的线程状态数量信息
1.1.3 jmap
- jmap:可以导出jvm信息,用jvm分析工具分析
- jmap -dump:format=b,file=/root/java.hprof pid
- .hprof:jvm数据文件
- java内存分析工具:mat;本地需要安装jdk
【MAT-jvm内存镜像分析工具】MemoryAnalyzer-1.8.0.20180604-win32.win32.x86_64.zip 链接: https://pan.baidu.com/s/1ef-sfwRyBxAaGr50a5e_6g?pwd=8ksr 提取码: 8ksr
[root@web03 ~]# jps
1716 Jps
1288 Bootstrap
[root@web03 ~]# jmap -dump:format=b,file=/root/1288.hprof 1288
Dumping heap to /root/1288.hprof ...
Heap dump file created [24942256 bytes in 0.102 secs]
[root@web03 ~]# ll 1288.hprof -h
-rw------- 1 root root 24M 7月 6 20:50 1288.hprof
[root@web03 ~]# file 1288.hprof
1288.hprof: Java HPROF dump, created Sun Jul 6 12:50:38 2025
# 下载到本地
[root@web03 ~]# sz 1288.hprof
- 捕捉开发人员一起分析jvm情况
1.1.4 top -Hp pid
- top -Hp pid:显示指定进程的线程信息
1.2 排查流程图
2. 前后端分离项目
2.1 项目说明
- 业务:考试系统
- 前端:exam-web-前端.zip,直接用nginx部署
- 后端:jar包+yaml配置文件
- 数据库:mysql8.0,导入sql文件
老男孩教育-前后端分离项目 链接: https://pan.baidu.com/s/1NDG6kCEXkWtyrBP0Mse9QQ?pwd=knvj 提取码: knvj
2.2 负载均衡
负载均衡 | 说明 | 举例 |
---|---|---|
七层负载均衡 | 解析http/https等协议,转发请求到API服务器 | 根据url分发请求 |
四层负载均衡 | 对端口负载均衡 | nginx根据请求的端口和轮询算法选择后端的服务器 |
2.3 数据库配置
- 服务器:db02
- 服务:mysql8.0
2.3.1 安装数据库服务
# 上传mysql压缩包
[root@db02 ~]# rz
# 解压到指定目录
[root@db02 ~]# tar xf mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz -C /app/tools/
# 配置软链接
[root@db02 ~]# cd /app/tools/
[root@db02 /app/tools]# ln -s mysql-8.0.28-linux-glibc2.12-x86_64 mysql
# 安装依赖
[root@db02 /app/tools]# yum install ncurses ncurses-devel libaio-devel openssl openssl-devel -y
……
# 添加mysql虚拟用户
[root@db02 /app/tools]# useradd -s /sbin/nologin -M mysql
# 添加mysql配置文件
[root@db02 /app/tools]# cat /etc/my.cnf
#by oldboy weixin:oldboy0102
[mysqld]
##用户
user=mysql
##安装目录
basedir=/app/tools/mysql/
##数据目录
datadir=/app/data/3306/
port=3306
socket=/tmp/mysql.sock [client]
socket=/tmp/mysql.sock# 修改数据目录和配置文件的所有者
[root@db02 /app/tools]# chown -R mysql:mysql /app/data/3306 /etc/my.cnf
[root@db02 /app/tools]# chown -R root:root /app/tools/mysql
# 添加系统环境变量
[root@db02 /app/tools]# export PATH=/app/tools/mysql/bin/:$PATH
[root@db02 /app/tools]# mysql --version
mysql Ver 8.0.28 for Linux on x86_64 (MySQL Community Server - GPL)
[root@db02 /app/tools]# echo "PATH=/app/tools/mysql/bin/:$PATH" >> /etc/profile
[root@db02 /app/tools]# source /etc/profile
- 初始化数据库
mysqld --initialize-insecure --user=mysql --basedir=/app/tools/mysql/ --datadir=/app/data/3306/
echo $?
#########################
--initialize-insecure 以不安全方式初始化,root密码为空,不加这个选项就是安全,创建root随机密码.
--user=mysql 指定mysql的虚拟用户
--basedir=/app/tools/mysql/ 安装目录
--datadir=/app/data/3306/ 指定数据目录
与配置文件中一致.
- 启动服务
# 复制启动脚本
[root@db02 ~]# cp /app/tools/mysql/support-files/mysql.server /etc/init.d/mysqld
# 给予执行权限
[root@db02 ~]# chmod +x /etc/init.d/mysqld
# 修改脚本中的mysql目录和数据目录
[root@db02 ~]# sed -i '/^basedir=/s#basedir=#basedir=/app/tools/mysql/#g' /etc/init.d/mysqld
[root@db02 ~]# sed -i '/^datadir=/s#datadir=#datadir=/app/data/3306/#g' /etc/init.d/mysqld
# 启动服务
[root@db02 ~]# systemctl start mysqld.service
# 查看进程和端口
[root@db02 ~]# ps -ef |grep [m]ysql
root 2036 1 0 07:58 ? 00:00:00 /bin/sh /app/tools/mysql//bin/mysqld_safe --datadir=/app/data/3306/ --pid-file=/app/data/3306//db02.pid
mysql 2202 2036 8 07:58 ? 00:00:00 /app/tools/mysql/bin/mysqld --basedir=/app/tools/mysql/ --datadir=/app/data/3306 --plugin-dir=/app/tools/mysql//lib/plugin --user=mysql --log-error=db02.err --pid-file=/app/data/3306//db02.pid --socket=/tmp/mysql.sock --port=3306
[root@db02 ~]# ss -lntup |grep mysql
tcp LISTEN 0 128 *:3306 *:* users:(("mysqld",pid=2202,fd=26))
tcp LISTEN 0 70 *:33060 *:* users:(("mysqld",pid=2202,fd=24))
2.3.2 配置数据库环境
# 创建数据库,并指定字符集
create database exam charset utf8mb4;
# 创建用户
# with mysql_native_password 指定密码加密插件. 与之前旧的版本兼容
create user exam@'172.16.1.%' identified with mysql_native_password by '1';
# 授权
grant all on exam.* to exam@'172.16.1.%';
# 导入sql文件
[root@db02 ~]# unzip xzs-sql-v3.9.0.zip
Archive: xzs-sql-v3.9.0.zipinflating: xzs-mysql.sql inflating: xzs-postgresql.sql
[root@db02 ~]# mysql exam <xzs-mysql.sql
2.4 后端配置
- 服务器:web03、web04
- 服务:jdk、jar包
# 创建目录
[root@web03 ~]# mkdir -p /app/code/exam/backend/
# 上传jar包和配置文件
[root@web03 /app/code/exam/backend]# rz
[root@web03 /app/code/exam/backend]# ll
总用量 39844
-rw-r--r-- 1 root root 658 3月 20 2024 application-prod.yml
-rw-r--r-- 1 root root 40541350 2月 22 2023 xzs-3.9.0.jar
# 启动jar包
[root@web03 /app/code/exam/backend]# java -Duser.timezone=Asia/Shanghai -jar -Dspring.profiles.active=prod xzs-3.9.0.jar
……
- 浏览器访问
2.5 四层负载均衡配置
- 服务器:lb01、lb02
- 服务:nginx
# 查看nginx是否有四层负载均衡的模块
[root@lb01 /etc/nginx]# nginx -V |& grep stream
……
# 配置四层负载均衡
[root@lb01 /etc/nginx]# vim nginx.conf
[root@lb01 /etc/nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 /etc/nginx]# cat nginx.conf
……
# 四层负载均衡配置
stream {upstream exam_pools {server 10.0.0.9:8000;#server web04:8000;hash $remote_addr consistent;}log_format basic '$remote_addr [$time_local]''$protocol $status $bytes_sent $bytes_received''$session_time';access_log /var/log/nginx-l4.log basic;server {listen 8000;proxy_pass exam_pools;}
}
# 查看nginx服务监听的端口,有8000端口
[root@lb01 /etc/nginx]# ss -lntup |grep nginx
tcp LISTEN 0 128 0.0.0.0:443 0.0.0.0:* users:(("nginx",pid=1404,fd=20),("nginx",pid=1403,fd=20),("nginx",pid=1402,fd=20))
tcp LISTEN 0 128 0.0.0.0:8000 0.0.0.0:* users:(("nginx",pid=1404,fd=21),("nginx",pid=1403,fd=21),("nginx",pid=1402,fd=21))
tcp LISTEN 0 128 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=1404,fd=19),("nginx",pid=1403,fd=19),("nginx",pid=1402,fd=19))
- 浏览器访问
2.6 前端配置
- 服务器:web01、web02
- 服务:nginx、代码
2.6.1 nginx子配置文件
[root@web02 /etc/nginx/conf.d]# cat exam.conf
server {listen 80;server_name admin.oldboy.cn; root /app/code/exam/front/admin/;location / {index index.html;}location /api/ {proxy_pass http://10.0.0.5:8000;}
}server {listen 80;server_name stu.oldboy.cn; root /app/code/exam/front/student/;location / {index index.html;}location /api/ {proxy_pass http://10.0.0.5:8000;}
}
2.6.2 配置站点目录
[root@web02 ~]# mkdir -p /app/code/exam/front/
# 上传前端压缩包
[root@web02 /app/code/exam/front]# ll
总用量 8852
-rw-r--r-- 1 root root 9063890 7月 7 13:29 exam-web-前端.zip
[root@web02 /app/code/exam/front]# unzip exam-web-前端.zip
[root@web02 /app/code/exam/front]# rm exam-web-前端.zip
[root@web02 /app/code/exam/front]# ll
总用量 0
drwxr-xr-x 4 root root 34 2月 22 2023 exam-web-前端
[root@web02 /app/code/exam/front]# mv exam-web-前端/* ./
[root@web02 /app/code/exam/front]# ll
总用量 0
drwxr-xr-x 4 root root 70 2月 22 2023 admin
drwxr-xr-x 2 root root 6 7月 7 13:30 exam-web-前端
drwxr-xr-x 3 root root 57 2月 22 2023 student
[root@web02 /app/code/exam/front]# rm exam-web-前端/
文件,目录已经移动到回收站:/recyle/tmp.WyRMLDyDr0
[root@web02 /app/code/exam/front]# ll
总用量 0
drwxr-xr-x 4 root root 70 2月 22 2023 admin
drwxr-xr-x 3 root root 57 2月 22 2023 student
2.6.3 配置本地hosts解析并测试
2.7 七层负载均衡配置
- 服务器:lb01、lb02
- 服务:nginx
[root@lb01 /etc/nginx/conf.d]# cat exam_lb.conf
upstream exam_l7_pools {#server web:80;server 10.0.0.8:80;
}
server {listen 80;server_name admin.oldboy.cn;location / {proxy_pass http://exam_l7_pools;proxy_set_header Host $http_host;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Real-Ip $remote_addr;}
}
server {listen 80;server_name stu.oldboy.cn;location / {proxy_pass http://exam_l7_pools;proxy_set_header Host $http_host;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Real-Ip $remote_addr;}
}
- 测试
- 复制到lb02
[root@lb02 /etc/nginx/conf.d]# scp lb01:$PWD/e* ./
……
- 启动keepalived,再测试
[root@lb01 ~]# systemctl start keepalived.service
[root@lb01 ~]# hostname -I
10.0.0.5 10.0.0.3 172.16.1.5
##########################
[root@lb02 /etc/nginx/conf.d]# systemctl start keepalived.service
[root@lb02 /etc/nginx/conf.d]# hostname -I
10.0.0.6 172.16.1.6
3. 思维导图
https://kdocs.cn/join/gpuxq6r?f=101\r\n邀请你加入共享群「老男孩教育Linux运维99期-孙克旭」一起进行文档协作