负载均衡
负载均衡建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。
负载均衡(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官网下载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
编写读取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
#复制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
#修改三处端口号
配置服务启动文件
[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
启动服务
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start tomcat7-8888
#复制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
#修改三处端口号
配置服务启动文件
[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
启动服务
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start tomcat7-9999
[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
点击刷新
再次点击刷新
测试session共享
浏览器访问http://ip/session.jsp
点击刷新
再次点击刷新
查看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"