源博客http://my.oschina.net/gccr/blog/321083
當用戶量大、應用服務器使用集群來布署時,使用Tomcat默認自帶的Session就不能滿足需求了。當然解決方法有很多,本文提供了一個解決方案,就是使用Redis來保存Session,好處就是使用Session的代碼沒有任何變化,Tomcat默認把Session保存到Redis上面了。
使用 Redis 服務器來存儲Session非常有優勢。首先它是一個NOSQL數據,第二它很容易擴展使用。
下面這種安裝方式非常清晰明白的引導你把Redis緩存作為一個Session的存儲系統。步驟如下:
1. 下載Redis並且使用下面的命令編譯安裝:
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make
2. 使用如下命令啟動Redis
cd RedisDirectory/src
./redis-server --port 6379
3. 下載最新的Tomcat 7
4. 下載最新的Jedis(一個Redis 的Java客戶端),Tomcat Redis Session Manager 和 Apache Commons Pool
5. 將上面所有的Jar包都拷到Tomcat7安裝目錄下面的Lib目錄下
6. 在Tomcat 的conf/context.xml 文件里增加如下內容(或者在server.xml的context塊中添加):
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" /> <Manager className="com.radiadesign.catalina.session.RedisSessionManager" host="localhost" <!-- 可選,默認是"localhost" --> port="6379" <!-- 可選,默認是 "6379" --> database="0" <!-- 可選,默認是 "0" --> maxInactiveInterval="60" <!-- 可選,默認是 "60" (單位:秒)--> />
context.xml后把我的備注 復制到
<!-- 可選,默認是"localhost" --> 這些注釋去掉 有可能會影響讀入xml文件
7. 重啟Tomcat7,你現你可以看到,Session的內容開始在Redis中創建了。
現在,Tomcat7的Session就保存到Redis中了,而且它也維護着Session的不同方面。
各個組件的下載地址:
Redis:http://redis.io/
JRedis: https://github.com/xetorthio/jedis
Tomcat Redis Session Manager :https://github.com/jcoleman/tomcat-redis-session-manager/downloads
Apache Commons Pool :http://commons.apache.org/proper/commons-pool/download_pool.cgi
jedis用的jedis-2.2.0.jar
tomcat-redis-session-manager-1.2-tomcat-7.jar
commons-pool-1.3.jar
commons-pool2-2.2.jar
jdk要用1.7
測試所用jsp 放到D:\apache-tomcat-7.0.56\webapps\ROOT目錄下
<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false"%> SessionID:<%=session.getId()%> <BR> SessionIP:<%=request.getServerName()%> <BR> SessionPort:<%=request.getServerPort()%> <% out.println("This is Tomcat Server 111111!"); %>
訪問
執行過程遇到的問題
tomcat報錯java版本問題 需要配置jdk
jedis版本過高 應改為2.2.2
嚴重: Error deploying web application directory D:\apache-tomcat-7.0.56\webapps\ROOT
java.lang.VerifyError: Bad type on operand stack
Exception Details:
Location:
com/radiadesign/catalina/session/RedisSessionManager.initializeDatabaseConnection()V @28: invokespecial
Reason:
Type 'redis/clients/jedis/JedisPoolConfig' (current frame, stack[3]) is not assignable to 'org/apache/commons/pool/impl/GenericObjectPool$Config'
Current Frame:
bci: @28
flags: { }
locals: { 'com/radiadesign/catalina/session/RedisSessionManager' }
stack: { 'com/radiadesign/catalina/session/RedisSessionManager', uninitialized 1, uninitialized 1, 'redis/clients/jedis/JedisPoolConfig', 'java/lang/String', integer, integer, 'java/lang/String' }
Bytecode:
0000000: 2abb 009d 59bb 009e 59b7 009f 2ab6 00a0
0000010: 2ab6 00a1 2ab6 00a2 2ab6 00a3 b700 a4b5
0000020: 0018 a700 134c 2bb6 00a5 bb00 3259 12a6
0000030: 2bb7 00a7 bfb1
Exception Handler Table:
bci [0, 34] => handler: 37
Stackmap Table:
same_locals_1_stack_item_frame(@37,Object[#276])
same_frame(@53)
更新 基於nginx tomcat redis分布式web應用的session共享配置 源博客 http://www.cnblogs.com/lengfo/p/4260363.html
用nginx實現tomcat服務器的負載均衡
基於nginx tomcat redis分布式web應用的session共享配置
一、前言
nginx 作為目前最流行的開源反向代理HTTP Server,用於實現資源緩存、web server負載均衡等功能,由於其輕量級、高性能、高可靠等特點在互聯網項目中有着非常普遍的應用,相關概念網上有豐富的介紹。分布式web server集群部署后需要實現session共享,針對 tomcat 服務器的實現方案多種多樣,比如 tomcat cluster session 廣播、nginx IP hash策略、nginx sticky module等方案,本文主要介紹了使用 redis 服務器進行 session 統一存儲管理的共享方案。
相關應用結構參照下圖:
二、環境配置
測試環境基於 Linux CentOS 6.5,請先安裝 tomcat、redis、nginx 相關環境,不作詳細描述,本文測試配置如下:
Version | IP_Port | |
nginx | 1.6.2 | 10.129.221.70:80 |
tomcat_1 | 7.0.54 | 10.129.221.70:8080 |
tomcat_2 | 7.0.54 | 10.129.221.70:9090 |
redis | 2.8.19 | 10.129.221.70:6379 |
三、構建 tomcat-redis-session-manager-master
1、由於源碼構建基於 gradle,請先配置 gradle 環境。
2、從 github 獲取 tomcat-redis-session-manager-master 源碼,地址如下:
https://github.com/jcoleman/tomcat-redis-session-manager
3、找到源碼中的 build.gradle 文件,由於作者使用了第三方倉庫(sonatype),需要注冊帳號,太麻煩,注釋后直接使用maven中央倉庫,同時注釋簽名相關腳本並增加依賴包的輸出腳本 copyJars(dist目錄),修改后的 build.gradle 文件如下:

4、執行gradle命令構建源碼,編譯輸出tomcat-redis-session-manager-master 及依賴jar包
gradle build -x test copyJars
所有輸出列表文件如下:
四、tomcat 配置
安裝配置兩台 tomcat web服務器,分別修改 Connector 端口號為8080和9090,並確保都能正常工作,當然如果分布在不同的主機則可以使用相同端口號。
五、編寫測試頁面
為了區別2台tomcat的訪問,分別編寫頁面並打包部署:
1、為tomcat_1編寫測試頁面,顯示 “ response from tomcat_1 ”,同時頁面提供按鈕顯示當前session值,打包並發布到 tomcat_1 服務器;
2、為tomcat_2編寫測試頁面,顯示 “ response from tomcat_2 ”,同時頁面提供按鈕顯示當前session值,打包並發布到 tomcat_2 服務器;
此時分別訪問 http://10.129.221.70:8080 和 http://10.129.221.70:9090 地址,因為訪問的是不同web服務器,所以各自顯示不同的頁面內容及session值肯定不同。
六、tomcat session manager 配置
修改配置使用 tomcat-redis-session-manager-master 作為 tomcat session 管理器
1、分別將第三步生成的 tomcat-redis-session-manager-master 及依賴jar包覆蓋到 tomcat 安裝目錄的 lib 文件夾
2、分別修改2台 tomcat 的 context.xml 文件,使 tomcat-redis-session-manager-master 作為session管理器,同時指定redis地址和端口。
context.xml 增加以下配置:
<Context> <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="localhost" port="6379" database="0" maxInactiveInterval="60" /> </Context>
3、分別重啟2台 tomcat 服務器。
七、nginx 配置
1、修改 default.conf 配置文件,啟用 upstream 負載均衡 tomcat Cluster,默認使用輪詢方式。
upstream site { server localhost:8080; server localhost:9090; } server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { #root /usr/share/nginx/html; #index index.html index.htm; index index_tel.jsp index.jsp index.html index.htm ; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_buffers 32 4k; proxy_connect_timeout 3; proxy_send_timeout 30; proxy_read_timeout 30; proxy_pass http://site; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
2、nginx 重新加載配置
nginx -s reload
八、測試結果
1、訪問 http://10.129.221.70:8080 直接請求到tomcat_1服務器,
顯示 “ response from tomcat_1 ”, session 值為 ‘56E2FAE376A47F1C0961D722326B8423’;
2、訪問 http://10.129.221.70:9090 直接請求到tomcat_2服務器,
顯示 “ response from tomcat_2 ”, session 值為 ‘56E2FAE376A47F1C0961D722326B8423’;
3、訪問 http://10.129.221.70 (默認80端口)請求到 nginx 反向代理到指定Web服務器,由於默認使用輪詢負載方式,
反復刷新頁面顯示的內容在“ response from tomcat_1 ” 和 “ response from tomcat_2 ”之間切換,但 session 值保持為 ‘56E2FAE376A47F1C0961D722326B8423’;
4、使用 redis-cli 連接 redis 服務器,查看會顯示有 “56E2FAE376A47F1C0961D722326B8423” key的 session 數據,value為序列化數據。
九、至此實現了基於nginx負載均衡下 tomcat 集群的 session 一致性。