Redis+Tomcat實現session綁定
阿新 • • 發佈:2018-06-18
AR web tar vid 分享圖片 支持 nginx created com 1.實驗環境
實驗系統:四臺CentOS7.4,其中一臺Nginx反向代理服務器,兩臺Tomcat服務器,一臺Redis
實驗目的:為了能夠讓客戶端訪問時,不管反向代理服務器代理到哪個服務器上都可以上客戶端得到相同的數據,所以就應該有專用於存放session的服務器,Redis就可以充當次服務器
實現拓撲圖:
實驗步驟:
- Nginx代理配置
[root@nginx ~]# yum install -y nginx [root@nginx ~]# vim /etc/nginx/conf.d/nginx.conf upstream web { server 192.168.1.162:8080; server 192.168.1.161:8080; } server { listen 80; server_name nginx.lin.com; root /app/web/; index index.jsp index.html; location / { proxy_pass http://web; } } [root@nginx ~]# systemctl start nginx
- Tomcat1
[root@tomcat1 ~]# yum install -y tomcat tomcat-admin-webapps.noarch tomcat-webapps.noarch tomcat-docs-webapp.noarch java-1.8.0-openjdk-devel [root@tomcat1 ~]# vim /etc/tomcat/server.xml <Engine name="Catalina" defaultHost="tomcat2.lin.com" jvmRoute="jvm2"> <Host name="tomcat2.lin.com" appBase="/app/web/" unpackWARs="true" autoDeploy="true"> </Host> [root@tomcat1 ~]# mkdir /app/web/ROOT/META-INF/ -p [root@tomcat1 ~]# cp /etc/tomcat/context.xml /app/web/ROOT/META-INF/ [root@tomcat1 ~]# vim /app/web/ROOT/META-INF/context.xml <Context> <!-- Default set of monitored resources --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="192.168.1.149" port="6379" database="0" password="123456" maxInactiveInterval="60" /> <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> --> <!-- Uncomment this to enable Comet connection tacking (provides events on session expiration as well as webapp lifecycle) --> <!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> --> </Context>
測試頁面創建
[root@tomcat1 ROOT]# vim /app/web/ROOT/index.jsp <%@ page language="java" %> <html> <head><title>TomcatA</title></head> <body> <h1><font color="red">TomcatA.magedu.com</font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <% session.setAttribute("magedu.com","magedu.com"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html>
- Tomcat2
[root@tomcat2 ~]# yum install -y tomcat tomcat-admin-webapps.noarch tomcat-webapps.noarch tomcat-docs-webapp.noarch java-1.8.0-openjdk-devel
[root@tomcat2 ~]# vim /etc/tomcat/server.xml
<Engine name="Catalina" defaultHost="tomcat2.lin.com" jvmRoute="jvm2">
<Host name="tomcat2.lin.com" appBase="/app/web/"
unpackWARs="true" autoDeploy="true">
</Host>
[root@tomcat2 ~]# mkdir /app/web/ROOT/META-INF/ -p
[root@tomcat2 ~]# cp /etc/tomcat/context.xml /app/web/ROOT/META-INF
[root@tomcat2 ~]# vim /app/web/ROOT/META-INF/context.xml
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="192.168.1.149"
port="6379"
database="0"
password="123456"
maxInactiveInterval="60" />
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
</Context>
測試頁面創建
[root@tomcat1 ROOT]# vim /app/web/ROOT/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="red">TomcatB.magedu.com</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("magedu.com","magedu.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
tomcat兩臺服務器上需要的jar包有:tomcat-redis-session-manager-2.0.0.jar、jedis-2.9.0.jar、commons-pool2-2.2.jar,可以從github中下載
將以上的三個jar包復制到/usr/share/tomcat/lib/目錄下
最後重啟服務
- Redis緩存
[root@redis ~]# yum install -y redis
[root@redis ~]# vim /etc/redis.conf
bind 0.0.0.0
requirepass centos
[root@tomcat2 ~]# systemctl start redis
測試結果
redis服務器查詢
127.0.0.1:6379> KEYS *
1) "2089214D59A2D2B1A76EE031C4D73FA8.jvm1.jvm1"
2) "8B8BA3B5715A5F0AC08E7223800E356C.jvm1.jvm1"
3) "9A9631C00441246A879E52A09295846C.jvm1.jvm1"
4) "61B59A937639E0CE8629A82A239BEE1B.jvm1.jvm1"
5) "mykey"
6) "905A1924350D75D599A4723F5D2D6CC9.jvm1.jvm1"
註意項說明:
1.註意三個jar包的版本,有些版本可能會不支持
2.註意redis是可以讓其他服務器的訪問
Redis+Tomcat實現session綁定