后端项目部署与负载均衡
作者:互联网
-
数据库准备
建立数据库 renren_fast ,然后运行renrenfast项目中的db文件夹内的mysql.sql
-
后端项目部署
1.下载后端源码,并导入到Eclipse当中,具体流程参照开发文档
注意:需要在Ecplice安装spring tools suite插件
2.修改application-dev.yml的数据库配置,连接之前配好的数据库集群的虚拟IP
url: jdbc:mysql://192.168.123.150:3306/renren_fast?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
username: root
password: 123456
3.修改application.yml文件中的redis配置,连接之前配好的Redis集群
redis:
open: false # 是否开启redis缓存 true开启 false关闭
database: 0
#host: localhost
#port: 6379
#password: # 密码(默认为空)
timeout: 6000ms # 连接超时时长(毫秒)
cluster:
nodes:
- 192.168.123.130:5001
- 192.168.123.130:5002
- 192.168.123.130:5003
- 192.168.123.130:5004
- 192.168.123.130:5005
- 192.168.123.130:5006
4.修改application.yml中的项目端口号为6001
将来项目要部署到Docker容器中,而Docker内部的网络是不允许跨网段访问的,所以后端项目容器要连接到Docker的host网络,使用宿主机IP。为了避免端口冲突,需要修改端口号。
# Tomcat
server:
tomcat:
uri-encoding: UTF-8
max-threads: 1000
min-spare-threads: 30
port: 6001
connection-timeout: 5000ms
servlet:
context-path: /renren-fast
5.利用Maven打包工程
renren-fast包含了tomcat.jar文件,所以工程打包成JAR文件就能独立运行
进入renren-fast目录,执行mvn命令
mvn clean install -Dmaven.test.skep=true # 跳过测试代码
# clean :表示清除之前的JAR文件;
# install :意味着打包到本地;
# -Dmaven.test.skip=true :代表跳过测试代码
打包成功后,会在项目根目录的target文件夹下生成renren-fast.jar
6.安装Java镜像
docker pull java
7.创建数据卷j1 ,并将renren-fast.jar复制到该目录下,用来在容器中启动该jar包
docker volume create j1
ls /var/lib/docker/volumes/j1/_data
renren-fast.jar
8.创建j1容器
docker run -it -d --restart=always --name j1 -v j1:/home/soft --net=host java
9.进入j1容器jar包所在目录,执行如下命令使后端项目在关闭终端时不挂断(nohup)并且在后台运行
#进入j1容器
docker exec -it j1 bash
#启动Java项目
nohup java -jar /home/soft/renren-fast.jar
项目启动成功后可以通过 http://192.168.123.130:6001/renren-fast/swagger/index.html 来测试后端接口
打不开需要开放6001端口
[root@localhost ~]# firewall-cmd --permanent --add-port=6001-6003/tcp
success
[root@localhost ~]# firewall-cmd --reload
10.创建负载站点
按照上述方法再在Docker中部署j2(端口6002),j3(端口6003)两个后台项目容器
注意:修改tomcat配置端口,打包
#创建数据卷,上传JAR文件
docker volume create j2
ls /var/lib/docker/volumes/j2/_data
renren-fast.jar
#启动容器
docker run -it -d --restart=always --name j2 -v j2:/home/soft --net=host java
#进入j1容器
docker exec -it j2 bash
#启动Java项目
nohup java -jar /home/soft/renren-fast.jar
#创建数据卷,上传JAR文件
docker volume create j3
ls /var/lib/docker/volumes/j3/_data
renren-fast.jar
#启动容器
docker run -it -d --restart=always --name j3 -v j3:/home/soft --net=host java
#进入j1容器
docker exec -it j3 bash
#启动Java项目
nohup java -jar /home/soft/renren-fast.jar
-
后台程序负载均衡
Nginx是性能非常出色的反向代理服务器,最大可以支持8万/秒的并发访问
1.安装Nginx镜像
docker pull nginx
2.创建n1目录
mkdir /home/n1
宿主机上/home/n1/nginx.conf配置文件内容如下:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 5s;
proxy_send_timeout 5s;
proxy_read_timeout 5s;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
upstream tomcat {
server 192.168.123.130:6001;
server 192.168.123.130:6002;
server 192.168.123.130:6003;
}
server {
listen 6101;
server_name 192.168.123.130;
location / {
proxy_pass http://tomcat;
index index.html index.htm;
}
}
}
3.创建第1个Nginx节点
docker run -it -d --restart=always --name n1 -v /home/n1/nginx.conf:/etc/nginx/nginx.conf --net=host --privileged nginx
4.按照上述操作再创建一个nginx节点6102端口
mkdir /home/n2
docker run -it -d --restart=always --name n2 -v /home/n2/nginx.conf:/etc/nginx/nginx.conf --net=host --privileged nginx
-
Nginx双机热备
参考之前Haproxy双机热备的方案,仍然采用Keepalived和虚拟IP技术。建立两个Nginx容器并各自安装Keepalived,映射到同一宿主机虚拟IP上
1.在Nginx容器安装Keepalived
#进入n1节点
docker exec -it n1 bash
#更新软件包
apt-get update
#安装VIM
apt-get install vim
#安装Keepalived
apt-get install keepalived
#编辑Keepalived配置文件(如下)
vim /etc/keepalived/keepalived.conf
#启动Keepalived
service keepalived start
keepalived配置文件:
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.123.151
}
}
virtual_server 192.168.123.151 6201 {
delay_loop 3
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.123.130 6101 {
weight 1
}
}
#进入n2节点
docker exec -it n2 bash
#更新软件包
apt-get update
#安装VIM
apt-get install vim
#安装Keepalived
apt-get install keepalived
#编辑Keepalived配置文件(如下)
vim /etc/keepalived/keepalived.conf
#启动Keepalived
service keepalived start
keepalived配置文件:
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.123.151
}
}
virtual_server 192.168.123.151 6201 {
delay_loop 3
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.123.130 6102 {
weight 1
}
}
成功访问http://192.168.123.151:6201/renren-fast/swagger/index.html代表成功
标签:负载,部署,jar,192.168,nginx,fast,均衡,docker,renren 来源: https://blog.csdn.net/Gushiyuta/article/details/94429540