Nginx+Tomcat+Redis (負載均衡+session共享)完整案例
今天整合了一些資源,做了一個Nginx+Tomcat+Redis的案例,使部署的web專案能夠承載較大的訪問壓力,Nginx實現負載均衡,並使用Redis實現session共享;
如下拓撲圖:
各版本如圖所示
========================================
開啟專案說明:
作業系統:win7_64
反向代理:Nginx_1.4.7
Tomcat:apache-tomcat-7.0.42
複製三份:
apache-tomcat-7.0.42_A;
apache-tomcat-7.0.42_B;
apache-tomcat-7.0.42_C
Redis:redis-64.3.0.503
JDK:1.8
重要:
Redis實現session共享時,需要的三個重要jar包:
tomcat-redis-session-manager1.2.jar
commons-pool2-2.2.jar
jedis-2.6.1.jar
==========================================
步驟一:
安裝Nginx:
我使用的Nginx為綠色版本,雙擊即可開啟
(命令啟動,重新載入等請參考:http://blog.csdn.net/qq_16216221/article/details/72597388)
修改 nginx.conf 配置檔案,多個Tomcat伺服器都是在這裡配置:
upstream中的localhost前面不能加:http://, location中proxy_pass裡的:http:// 不能省略
weight為訪問權重,數值越高,訪問的機會也越高。
以上Nginx配置只是簡單配置,請根據自己的專案情況,做詳細配置。
步驟二:
部署Tomcat伺服器:
apache-tomcat-7.0.42_A;
apache-tomcat-7.0.42_B;
apache-tomcat-7.0.42_C
1:apache-tomcat-7.0.42_A 部署:
server.xml注意三個地方,本伺服器使用預設配置:
<Server port="8005" shutdown="SHUTDOWN">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
context.xml新增session共享的配置,去註釋,全貼出來,方便觀看和黏貼:
其中的password="123456"是redis的訪問密碼,很多帖子都沒有提到,讓我耽誤很多時間,怎麼設定redis的訪問密碼呢?請繼續往下看。。。
當然,訪問路徑和埠號也需要配置正確。
<?xml version='1.0' encoding='utf-8'?>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- tomcat-redis-session共享配置 -->
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="localhost"
port="6379"
password="123456"
database="0"
maxInactiveInterval="60" />
</Context>
2:apache-tomcat-7.0.42_B 部署:
server.xml修改三個地方的埠號:
<Server port="8105" shutdown="SHUTDOWN">
<Connector port="8180" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8109" protocol="AJP/1.3" redirectPort="8443" />
context.xml新增session共享的配置同apache-tomcat-7.0.42_A伺服器的配置:
3:apache-tomcat-7.0.42_C 部署:
server.xml修改三個地方的埠號:
<Server port="8205" shutdown="SHUTDOWN">
<Connector port="8280" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8209" protocol="AJP/1.3" redirectPort="8443" />
context.xml新增session共享的配置同apache-tomcat-7.0.42_A伺服器的配置:
步驟三:
部署測試web專案,編寫首頁測試程式碼:index.jsp
apache-tomcat-7.0.42_A伺服器中web專案的程式碼:
相信大家都能明白程式碼中意思,第一次訪問,不管是訪問哪一個Tomcat,都會輸出:new session:AAA111
重新整理請求,理論上就應該輸出:old session:AAA111,如果不是,那就是你的會話不是一個session。
為了有所區分,能夠知道我們訪問了哪一臺伺服器,我們新增標註資訊。當然,儲存和訪問的session資訊是一致的。
<body>
<%
HttpSession mySession = request.getSession(false);
if(mySession.getAttribute("appname") == null){
mySession = request.getSession(true);
mySession.setAttribute("appname", "AAA111");
out.println("new session:"+mySession.getAttribute("appname"));
}else{
out.println("old session:"+mySession.getAttribute("appname"));
}
%>
<br>
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA標註是:A tomcat<br>
</body>
apache-tomcat-7.0.42_B伺服器中web專案的程式碼:
<body>
<%
HttpSession mySession = request.getSession(false);
if(mySession.getAttribute("appname") == null){
mySession = request.getSession(true);
mySession.setAttribute("appname", "AAA111");
out.println("new session:"+mySession.getAttribute("appname"));
}else{
out.println("old session:"+mySession.getAttribute("appname"));
}
%>
<br>
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB標註是:B tomcat<br>
</body>
apache-tomcat-7.0.42_C伺服器中web專案的程式碼:
<body>
<%
HttpSession mySession = request.getSession(false);
if(mySession.getAttribute("appname") == null){
mySession = request.getSession(true);
mySession.setAttribute("appname", "AAA111");
out.println("new session:"+mySession.getAttribute("appname"));
}else{
out.println("old session:"+mySession.getAttribute("appname"));
}
%>
<br>
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC標註是:C tomcat <br>
</body>
將web專案打成war包,放到tomcat的webapps目錄下即可。
步驟四:
redis安裝和配置:
下載:redis-64.3.0.503版本的解壓包,我的是綠色版,解壓即可用,如下所示:
修改redis.windows.conf配置檔案:
新增訪問密碼:行前不能有空格,否則報錯。
# requirepass foobared
requirepass 123456
啟動Redis即可,session的儲存不需要我們處理。
ok,準備工作到此結束,下面就是見證奇蹟的時刻:
啟動Nginx,三個tomcat,redis
阿彌陀佛保佑。。。
開啟瀏覽器:輸入:http://localhost/TestDemo/;localhost 訪問的是我們的Nginx,它會隨機分配到具體的tomcat上 ;TestDemo為我的web專案名稱。
第一次訪問:Nginx為我分配到apache-tomcat-7.0.42_C伺服器,並輸出:new ...,說明之前沒有這個session,建立後並輸出。
再重新整理幾次瀏覽器,看效果:
效果大家也看到了,這就是負載均衡和session共享。
涉及到的資源統一放到,歡迎下載:
稍後會貼出連線
==============完==================
---------------------
作者:二十畝魚
來源:CSDN
原文:https://blog.csdn.net/qq_16216221/article/details/72599016
版權宣告:本文為博主原創文章,轉載請附上博文連結!