Implementation of a small Demo with separation of front and back ends [multi-node cluster deployment]

Preface

1. The backend of this demo mainly uses SpringBoot to integrate WeChat authorization and WeChat website scan code payment, and the front-end page mainly uses Bootstrap and JQuery. Website: http://wechatpay.ysqorz.top/ (long-term validity is not guaranteed)

2. The following is [theoretically] the architecture diagram of project deployment. In fact, I am relatively poor and can't afford so many ECSs, so I actually only used two ECSs for deployment. This blog will use Linux commands to implement multi-node cluster deployment according to the following architecture diagram. It seems quite simple, but I encountered a lot of weird errors during actual deployment.

 3. The consuming crowd of this blog has knowledge of SpringBoot and Linux, and knowledge of cloud servers (for example: security groups...), and they don’t need to be in-depth. In addition, I use Docker for MySQL, Nginx and other installations. The reason is that the image is pulled and used at any time, and the container configuration is broken and deleted and reinstalled at any time. Consumers need to understand Dokcer. It's almost after reading this blog. https://blog.csdn.net/qq_43290318/article/details/107743188

table of Contents

Environmental installation

1. First install Docker on ECS_1 and ECS_4

2. Install JDK on ECS_2 and ECS_3

3. Install Nginx via Docker on ECS_1

4. Install MySQL via Docker on ECS_4

Project deployment

1. Type the SpringBoot project into a jar package and push it to ECS_2 and ECS_3 and run

2. Push static resources to ECS_1, configure Nginx

3. Remotely connect to MySQL on ECS_4, create a new database and import data


Environmental installation

1. First install Docker on ECS_1 and ECS_4

Reference blog: https://blog.csdn.net/qq_43290318/article/details/107743188

2. Install JDK on ECS_2 and ECS_3

# 检查当前系统是否已安装JDK
yum list installed | grep java

# 查看当前所使用的JDK版本
java -verision

# 如果想卸载当前系统的所有JDK
yum -y remove java-*-openjdk*
yum -y remove tzdata-java.noarch

# 安装JDK8
yum -y list java*
yum install java-1.8.0-openjdk  java-1.8.0-openjdk-devel

# 如果存在多个JDK,想切换jdk版本
alternatives --config java

3. Install Nginx via Docker on ECS_1

# 拉取最新的Nginx镜像
docker pull nginx

# 启动容器
# 我不建议启动容器时挂载,因为挂载会以宿主机为基准,如果容器内目录有文件,而宿主机目录是空的,那么挂在后容器内目录的文件就没了
docker run --name nginx -p 80:80 -d nginx

# 进入容器,并返回一个终端
docker exec -it nginx /bin/bash

# 容器内没有vim,在后面无法操作配置文件,所有需要安装vim
apt-get update
apt-get install vim

# 下面是Nginx容器内部几个主要的路径,后面配置Nginx时需要知道
静态资源文件夹:/usr/share/nginx/html
日志:/var/log/nginx 
主配置文件:/etc/nginx/nginx.conf 
虚拟主机的配置文件:/etc/nginx/conf.d/*.conf 

# 从宿主机复制文件或目录到容器内
# 这条命令也可以直接复制目录,不需要加其他命令参数
docker cp 宿主机路径 容器id或容器别名:容器内部路径 

4. Install MySQL via Docker on ECS_4

# 拉取最新版的MySQL镜像
docker pull mysql

# 启动容器
docker run -p 3306:3306 --name mysql8 -e MYSQL_ROOT_PASSWORD=123456 -d mysql

# 进入容器内部,并返回终端
docker exec -it mysql8 /bin/bash

# 在容器内登进mysql
mysql -u root -p
# 输入密码...

# 查看所有账户
# 注意,有些MySQL,root账户默认并支持远程连接,所以我们要么打开权限,要么新增账户。
select user, host from mysql.user;

# 创建远程登录账户ysq,具有所有权限
create user 'ysq'@'%' identified by '密码';
grant all privileges on *.* to 'ysq'@'%' with grant option;
# 不要忘了刷新权限
flush privileges;

# 修改root密码
alter user 'root'@'localhost' identified by '新密码';
alter user 'root'@'%' identified by '新密码';

# 查看root的权限
show grants for 'root'@'%' \G;

Project deployment

Use tools: xFTP and xshell

1. Type the SpringBoot project into a jar package and push it to ECS_2 and ECS_3 and run

# 在idea的终端,输入以下命令
# 跳过test模块,将项目达成jar包。默认会声称在target目录下
mvn package -Dmaven.test.skip=true

Note that we don't need to modify the configuration file application.yml specially before opening the jar package. We can make another copy, change the related configuration to the one used online, and push it to the cloud server together with the jar package. Then when you run the jar package on the cloud server, you can specify a new configuration file.

# 指定另外的配置文件application.yml,运行jar包
# 以下命令基于application.yml和jar包处于同一目录下
java -jar ***.jar --Dspring.config.location=application.yml

上述命令是在前台运行jar包,会占用终端。按 ctrl + c 停止进程退出。
在测试时,可以这样做,可以看控制台输出。

# 在确保运行正常之后,以守护进程的方式后台启动并运行
nohup java -jar ***.jar  --Dspring.config.location=application.yml &
# 查看守护进程的日志输出(实时的)
tail -f nohup.out

# 查看对应端口号占用情况
lsof -i:端口号

# 如果没有lsof工具,直接通过以下命令安装
yum install lsof

# 通过pid强制杀死进程
kill -9 pid

2. Push static resources to ECS_1 and configure Nginx

(1) Copy static resources from the host to the Nginx container

# /data/WechatPay/static 是静态资源在宿主机的路径
docker cp /data/WechatPay/static 容器id或容器别名:/usr/share/nginx/html 

(2) Configure multi-node and request distribution strategies in the Nginx main configuration file

# 进入容器内部
# nginx为容器名称,也可以替换为容器id
docker exec -it nginx /bin/bash

# 修改主配置文件
vim /etc/nginx/nginx.conf

In the http closure, add the following configuration:

# 配置多节点集群
# lbs是自定义的集群名称,后面会用到
# 端口是jar包(项目)运行后占用的端口
upstream lbs {
    server ECS_2的公网ip:端口;   
    server ECS_3的公网ip:端口;		
}

(3) Modify the configuration file of the virtual host and configure the request distribution strategy

Here I directly use the default port 80 of Nginx, without additional virtual host configuration.

# 建议做好备份,方便以后配置其他虚拟主机
vim /etc/nginx/conf.d/default.conf

# 配置请求分发策略
# 凡是请求的路径为/api/的,都会负载均衡地分配到集群lbs的任一节点
# 转发策略可以配置,下面采用默认策略
location /api/ {
    proxy_pass http://lbs;
    proxy_redirect default;
}

# 注意修改配置后需要重启Nginx容器,让配置生效
# 从容器内部退出
exit
docker restart 容器id或容器名称

(4) Modify the path of the related interface request in the static resource to point to Nginx

In my deployment above, since Nginx is on ECS_1 and uses port 80, there is no need to bring the port

// 本地前后端联调时,使用localhost,微信那边会返回终端ip无效
//var host = "http://127.0.0.1:8080";

// 上线时改成下面
var host = "Nginx所在ECS的公网ip:端口号";

3. Remotely connect to MySQL on ECS_4, create a new database and import data

The Navicat operation is mainly used here.

 

Guess you like

Origin blog.csdn.net/qq_43290318/article/details/107974938