Centos7部署Nginx负载均衡Tomcat服务器及session共享架构

共 2345字,需浏览 5分钟

 ·

2020-07-12 19:20

往日精选文章最中肯的Redis规范全在这了Redis 高级面试题 学会这些还怕进不了大厂?
Redis中缓存雪崩、缓存穿透等问题的解决方案
掌握Redis主从复制、哨兵、Cluster三种集群模式
2020 年最新版 68 道Redis面试题,收藏起来备用!

精选文章正文

负载均衡
负载均衡建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。
负载均衡(Load Balance)其意思就是分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务
session共享
Session 是客户端与服务器通讯会话跟踪技术,服务器与客户端保持整个通讯的会话基本信息
由于负载均衡会把任务分摊到不同的机器上,因此当用户进行页面刷新时会从机器A跳转到机器B,此时用户所进行的所有操作数据可能会丢失,因此需要通过集群的session共享机制来实现数据跟踪

前期准备

准备Centos7服务器,同步系统时间,配置IP地址和hostname,关闭防火墙和selinux
安装openjdk

[root@localhost ~]# yum install java -y

#查看安装情况
[root@localhost ~]# java -version
openjdk version "1.8.0_232"
OpenJDK Runtime Environment (build 1.8.0_232-b09)
OpenJDK 64-Bit Server VM (build 25.232-b09, mixed mode)

部署Nginx
从官网下载yum源并安装Nginx

[root@localhost ~]# yum install nginx -y

部署redis

#yum安装redis
[root@localhost ~]# yum install redis -y

#配置
#设定为后台守护进程方式运行
daemonize yes
#设定登录密码(可选)
masterauth "your_password"

#启动服务
[root@localhost ~]# systemctl start redis

部署Tomcat集群

部署tomcat1

从Tomcat官网下载Tomcat7压缩包并解压

[root@localhost ~]# tar -zxvf apache-tomcat-7.0.104.tar.gz -C /usr/local/tomcat-7

在/usr/local/tomcat-7/lib文件夹中添加三个jar包实现连接redis和session共享

commons-pool-1.6.jar
jedis-2.0.0.jar
tomcat-redis-session-manager-1.2-tomcat-7-java-7.jar

配置服务启动文件

[root@localhost ~]# vi /usr/lib/systemd/system/tomcat7.service
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment=CATALINA_PID=/usr/local/tomcat-7/temp/tomcat.pid
Environment=CATALINA_HOME=/usr/local/tomcat-7
Environment=CATALINA_BASE=/usr/local/tomcat-7
ExecStart=/usr/local/tomcat-7/bin/startup.sh
ExecStop=/usr/local/tomcat-7/bin/shutdown.sh
User=root
Group=root
[Install]
WantedBy=multi-user.target

配置redis缓存

[root@localhost ~]# vi /usr/local/tomcat-7/conf/context.xml

WEB-INF/web.xml
WEB-INF/tomcat-web.xml
${catalina.base}/conf/web.xml


host="127.0.0.1"
port="6379"
#redis有设置密码登录则需要填写密码
password="your_password"
database="0"
maxInactiveInterval="60" />

编写读取redis缓存文件

[root@localhost ~]# vi /usr/local/tomcat-7/webapps/ROOT/session.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>




shared session



session id=<%=session.getId()%>

tomcat 1

启动服务

[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start tomcat7

部署tomcat2

#复制tomcat1文件夹
[root@localhost ~]# cp -r /usr/local/tomcat-7/ /usr/local/tomcat-7-8888/

修改配置文件

[root@localhost ~]# vi /usr/local/tomcat-7-8888/conf/server.xml
#修改三处端口号

address="::1"
port="8019"
redirectPort="8443" />

配置服务启动文件

[root@localhost ~]# vi /usr/lib/systemd/system/tomcat7-8888.service
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment=CATALINA_PID=/usr/local/tomcat-7-8888/temp/tomcat.pid
Environment=CATALINA_HOME=/usr/local/tomcat-7-8888
Environment=CATALINA_BASE=/usr/local/tomcat-7-8888
ExecStart=/usr/local/tomcat-7-8888/bin/startup.sh
ExecStop=/usr/local/tomcat-7-8888/bin/shutdown.sh
User=root
Group=root
[Install]
WantedBy=multi-user.target

修改读取redis缓存的文件

[root@localhost ~]# vi /usr/local/tomcat-7-8888/webapps/ROOT/session.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>




shared session



session id=<%=session.getId()%>

tomcat 2

修改主页显示以示区分

[root@localhost ~]# vi /usr/local/tomcat-7-8888/webapps/ROOT/index.jsp
Home-7-8888

启动服务

[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start tomcat7-8888

部署tomcat3

#复制tomcat1文件夹
[root@localhost ~]# cp -r /usr/local/tomcat-7/ /usr/local/tomcat-7-9999/

修改配置文件

[root@localhost ~]# vi /usr/local/tomcat-7-9999/conf/server.xml
#修改三处端口号

address="::1"
port="8029"
redirectPort="8443" />

配置服务启动文件

[root@localhost ~]# vi /usr/lib/systemd/system/tomcat7-9999.service
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment=CATALINA_PID=/usr/local/tomcat-7-9999/temp/tomcat.pid
Environment=CATALINA_HOME=/usr/local/tomcat-7-9999
Environment=CATALINA_BASE=/usr/local/tomcat-7-9999
ExecStart=/usr/local/tomcat-7-9999/bin/startup.sh
ExecStop=/usr/local/tomcat-7-9999/bin/shutdown.sh
User=root
Group=root
[Install]
WantedBy=multi-user.target

修改读取redis缓存的文件

[root@localhost ~]# vi /usr/local/tomcat-7-9999/webapps/ROOT/session.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>




shared session



session id=<%=session.getId()%>

tomcat 3

修改主页显示以示区分

[root@localhost ~]# vi /usr/local/tomcat-7-9999/webapps/ROOT/index.jsp
Home-7-9999

启动服务

[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start tomcat7-9999

配置Nginx负载均衡

[root@localhost ~]# vi /etc/nginx/conf.d/default.conf
#配置tomcat集群
upstream serverpool{
#默认情况下通过轮询方式来调用tomcat集群
server localhost:8080;
server localhost:8888;
server localhost:9999;
}
#配置服务
server{
listen 80;
server_name localhost;
location / {
proxy_pass http://serverpool/;
}
}

启动服务

[root@localhost ~]# systemctl start nginx

测试验证

验证负载均衡
浏览器访问http://ip
125c13d2c8e367d9d1a8df277c2a9da3.webp
点击刷新
febd81358208e166f937ef6674d5b9e3.webp
再次点击刷新
1bc50a4b98debce7b2ddc24a38fa99a7.webp
测试session共享
浏览器访问http://ip/session.jsp
8d6b93b45b0e508886c50c30c31a954a.webp
点击刷新
241ea3fdbca1e711cb20b98d9d72d1ad.webp
再次点击刷新
3a1345a5d70e26fab1c79c4e7946fced.webp
查看redis数据库记录

[root@localhost ~]# redis-cli
127.0.0.1:6379> auth "your_password"
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> keys *
1) "C13930F70F4B33537E99140CFEDAC637"

浏览 28
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报