负载均衡 负载均衡建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。 负载均衡(Load Balance)其意思就是分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务 session共享 Session 是客户端与服务器通讯会话跟踪技术,服务器与客户端保持整个通讯的会话基本信息 由于负载均衡会把任务分摊到不同的机器上,因此当用户进行页面刷新时会从机器A跳转到机器B,此时用户所进行的所有操作数据可能会丢失,因此需要通过集群的session共享机制来实现数据跟踪
前期准备
准备Centos7服务器,同步系统时间,配置IP地址和hostname,关闭防火墙和selinux 安装openjdk
代码语言:javascript复制[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
代码语言:javascript复制[root@localhost ~]# yum install nginx -y
部署redis
代码语言:javascript复制#yum安装redis
[root@localhost ~]# yum install redis -y
#配置
#设定为后台守护进程方式运行
daemonize yes
#设定登录密码(可选)
masterauth "your_password"
#启动服务
[root@localhost ~]# systemctl start redis
部署Tomcat集群
部署tomcat1
从Tomcat官网下载Tomcat7压缩包并解压
代码语言:javascript复制[root@localhost ~]# tar -zxvf apache-tomcat-7.0.104.tar.gz -C /usr/local/tomcat-7
在/usr/local/tomcat-7/lib文件夹中添加三个jar包实现连接redis和session共享
代码语言:javascript复制commons-pool-1.6.jar
jedis-2.0.0.jar
tomcat-redis-session-manager-1.2-tomcat-7-java-7.jar
配置服务启动文件
代码语言:javascript复制[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缓存
代码语言:javascript复制[root@localhost ~]# vi /usr/local/tomcat-7/conf/context.xml
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!--配置Redis缓存-->
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"
host="127.0.0.1"
port="6379"
#redis有设置密码登录则需要填写密码
password="your_password"
database="0"
maxInactiveInterval="60" />
</Context>
编写读取redis缓存文件
代码语言:javascript复制[root@localhost ~]# vi /usr/local/tomcat-7/webapps/ROOT/session.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>shared session</title>
</head>
<body>
<br>session id=<%=session.getId()%>
<br>tomcat 1
</body>
</html>
启动服务
代码语言:javascript复制[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start tomcat7
部署tomcat2
代码语言:javascript复制#复制tomcat1文件夹
[root@localhost ~]# cp -r /usr/local/tomcat-7/ /usr/local/tomcat-7-8888/
修改配置文件
代码语言:javascript复制[root@localhost ~]# vi /usr/local/tomcat-7-8888/conf/server.xml
#修改三处端口号
<Server port="8015" shutdown="SHUTDOWN">
<Connector port="8888" protocol="HTTP/1.1"
<Connector protocol="AJP/1.3"
address="::1"
port="8019"
redirectPort="8443" />
配置服务启动文件
代码语言:javascript复制[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缓存的文件
代码语言:javascript复制[root@localhost ~]# vi /usr/local/tomcat-7-8888/webapps/ROOT/session.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>shared session</title>
</head>
<body>
<br>session id=<%=session.getId()%>
<br>tomcat 2
</body>
</html>
修改主页显示以示区分
代码语言:javascript复制[root@localhost ~]# vi /usr/local/tomcat-7-8888/webapps/ROOT/index.jsp
<span id="nav-home"><a href="${tomcatUrl}">Home-7-8888</a></span>
启动服务
代码语言:javascript复制[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start tomcat7-8888
部署tomcat3
代码语言:javascript复制#复制tomcat1文件夹
[root@localhost ~]# cp -r /usr/local/tomcat-7/ /usr/local/tomcat-7-9999/
修改配置文件
代码语言:javascript复制[root@localhost ~]# vi /usr/local/tomcat-7-9999/conf/server.xml
#修改三处端口号
<Server port="8025" shutdown="SHUTDOWN">
<Connector port="9999" protocol="HTTP/1.1"
<Connector protocol="AJP/1.3"
address="::1"
port="8029"
redirectPort="8443" />
配置服务启动文件
代码语言:javascript复制[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缓存的文件
代码语言:javascript复制[root@localhost ~]# vi /usr/local/tomcat-7-9999/webapps/ROOT/session.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>shared session</title>
</head>
<body>
<br>session id=<%=session.getId()%>
<br>tomcat 3
</body>
</html>
修改主页显示以示区分
代码语言:javascript复制[root@localhost ~]# vi /usr/local/tomcat-7-9999/webapps/ROOT/index.jsp
<span id="nav-home"><a href="${tomcatUrl}">Home-7-9999</a></span>
启动服务
代码语言:javascript复制[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start tomcat7-9999
配置Nginx负载均衡
代码语言:javascript复制[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/;
}
}
启动服务
代码语言:javascript复制[root@localhost ~]# systemctl start nginx
测试验证
验证负载均衡 浏览器访问http://ip
点击刷新
再次点击刷新
测试session共享 浏览器访问http://ip/session.jsp
点击刷新
再次点击刷新
查看redis数据库记录
代码语言:javascript复制[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"