實驗系統:四臺CentOS7.4,其中一臺Nginx反向代理服務器,兩臺Tomcat服務器,一臺Redis
實驗目的:爲了可以讓客戶端訪問時,無論反向代理服務器代理到哪一個服務器上均可以上客戶端獲得相同的數據,因此就應該有專用於存放session的服務器,Redis就能夠充當次服務器
實現拓撲圖:
實驗步驟:html
[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
[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>
測試頁面建立java
[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>
[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>
測試頁面建立nginx
[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/目錄下
最後重啓服務git
[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服務器查詢github
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是能夠讓其餘服務器的訪問web