nginx+tomcat+redis实现会话共享
1.環境準備(安裝redis報錯)
nginx,兩個tomcat,redis (因為測試全部安裝在同一臺)
安裝redis:
cd?redis-4.0.9.tar.gz && cd?redis-4.0.9 && make && cd src && make test
#make test的是報錯1如下:
You need tcl 8.5 or newer in order to run the Redis test
make: *** [test] Error 1
解決:yum -y install tcl
#報錯2如下:
[exception]: Executing test client: NOREPLICAS Not enough good slaves to write..
NOREPLICAS Not enough good slaves to write.
解決:
?
vim tests/integration/replication-2.tcl
?
- after 1000
?
+ after 10000
?
繼續:make test && make install
啟動redis :./redis-server
2.測試文件(在tomcat的webapps下面新建一個項目,兩個tomcat項目名字一樣,文件也一樣,因為測試,訪問的文件內容(最好加上session id)設置不一樣看效果
??
3.tomcat配置
添加支持redis會話共享的插件(3個jar包)到tomcat/lib目錄下
然后更改tomcat/conf下context.xml文件【context】節點下加如下代碼【兩個tomcat配置文件都要改下】:?
注:這是配置redis的鏈接信息,如果沒有密碼可以把passowrd項去掉 ,tomcat7 ,和tomcat8 對應支持resissession的jar也不一樣(有三個),tomcat7 的插件在網上容易找到,現在網上插件(jar包)不支持tomcat8,能用的都是改過源碼的,比較難找(本人暫時沒有找到可以用的)
----------------------針對tomcat7的配置----------------------------
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
? ? ? host="127.0.0.1"??
? ? ? port="6379"
? ? ? database="0"
? ? ? password="666"
? ? ? maxInactiveInterval="60" />
-------------------------針對tomcat8的配置-----------------------
<Valve className="com.demo.redis_session.RedisSessionHandlerValve" />
<Manager className="com.demo.redis_session.RedisSessionManager"
host="127.0.0.1" port="6379" database="0" password="666" maxInactiveInterval="60" />
?4.nginx配置? (附上主要代碼)
cat /etc/nginx/conf.d/default.conf upstream images_server { #ip_hash; server 192.168.110.28:8080; server 192.168.110.28:8081; }server { listen 80; server_name vm1.ql.com;location / { root /usr/share/nginx/html; index index.html index.htm; } location ~*/NginxTest/ { proxy_pass http://images_server; }error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }?
5.結果驗證
瀏覽器本地訪問:(本地hosts加上域名解析)
http://vm1.ql.com/NginxTest/index.jsp
不斷刷新瀏覽器,顯示頁面內容不一樣,但session-id一直,則可說明已經實現了會話共享。
轉載于:https://www.cnblogs.com/junzhu2/p/9055681.html
總結
以上是生活随笔為你收集整理的nginx+tomcat+redis实现会话共享的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Wix 安装部署教程(十四) -- 多语
- 下一篇: ACM_无聊者序列(斐波那契数列大数取余