centos 6.6 搭建nginx1.80+redis-3.0.2.tar + tomcat8做負載平衡

來源:互聯網
上載者:User

標籤:

本文記錄nginx+redis+tomcat實現session共用的過程

nginx安裝:http://blog.csdn.net/grhlove123/article/details/47834673

redis安裝:http://blog.csdn.net/grhlove123/article/details/47783471

準備兩個tomcat,修改相應的連接埠 8080,9090

修改nginx.conf加上:

upstream backend {
server 10.10.49.23:8080 max_fails=1 fail_timeout=10s;
server 10.10.49.15:8081 max_fails=1 fail_timeout=10s;
}

修改nginx.conf的location成

location / {
root html;
index index.html index.htm;
proxy_pass http://backend;
}

啟動nginx。

 

下載tomcat-redis-session-manager相應的jar包,主要有三個:

wget https://github.com/downloads/jcoleman/tomcat-redis-session-manager/tomcat-redis-session-manager-1.2-tomcat-7-java-7.jar
wget http://central.maven.org/maven2/redis/clients/jedis/2.5.2/jedis-2.5.2.jar
wget http://central.maven.org/maven2/org/apache/commons/commons-pool2/2.0/commons-pool2-2.0.jar

下載完成後拷貝到$TOMCAT_HOME/lib中

修改兩tomcat的context.xml:

<Context>

<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>

<!-- 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" />
-->

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="10.10.49.20"
port="6379"
database="0"
maxInactiveInterval="60" />
</Context>

在tomcat/webapps/test放一個index.jsp

<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>

<table align="centre" border="1">
<tr>
<td>Session ID</td>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
sessionID:<%=session.getId()%>
<br>
SessionIP:<%=request.getServerName()%>
<br>
SessionPort:<%=request.getServerPort()%>
<%
//為了區分,第二個可以是222
out.println("This is Tomcat Server 1111");
%>

啟動tomcat,發現有異常:com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve 類找不到

分別開啟三個jar包,確實沒有這個類,解決可以參考:

http://blog.csdn.net/qinxcb/article/details/42041023

 

通過訪問http://10.10.49.20/test/


重新整理:

可以看到雖然Server從1111變為2222,但session的建立時間沒有變化,這就完成了session共用。

centos 6.6 搭建nginx1.80+redis-3.0.2.tar + tomcat8做負載平衡

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.