本篇內(nèi)容主要講解“如何使用redis3.0實(shí)現(xiàn)Session共享”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“如何使用Redis3.0實(shí)現(xiàn)Session共享”吧!
撫松網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)2013年至今到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專(zhuān)注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
Tomcat版本:Tomca8
如果對(duì)此章節(jié)不了解,可以查閱https://my.oschina.net/u/3155476/blog/3070575(Keepalived + Nginx實(shí)現(xiàn)高可用Web負(fù)載均衡)。
1、單節(jié)點(diǎn)訪問(wèn) http://192.168.186.129:8080/dubbo-web-boss/login.do :
2、增加多一個(gè)消費(fèi)者節(jié)點(diǎn):192.168.186.129,以同樣的方式部署dubbo-web-boss工程。(另外一個(gè)消費(fèi)節(jié)點(diǎn)同樣的原理,在此不再多次描述)
先驗(yàn)證新增節(jié)點(diǎn)也可正常訪問(wèn) http://192.168.186.129:8080/dubbo-web-boss/login.do
3、在Keepalived+Nginx組成的反向代理集群中的三個(gè)節(jié)點(diǎn)同步增加如下兩處配置:
user root;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
## web-boss
upstream web_boss {
server 192.168.186.129:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.186.132:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.186.133:8080 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 88;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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 html;
}
## web-boss Cluster
location /dubbo-web-boss{
root html;
index index.html index.htm;
proxy_pass http://web_boss/dubbo-web-boss;
proxy_set_header Host $http_host;
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
client_max_body_size 100m;
}
}
}
4、重啟Nginx
# /usr/local/nginx/sbin/nginx -s reload
5、通過(guò)反向代理集群的VIP訪問(wèn)dubbo-web-boss時(shí),有時(shí)可以登錄成功,但有時(shí)又會(huì)提示驗(yàn)證碼錯(cuò)誤,原因就是Session沒(méi)有同步。
http://192.168.186.50:88/dubbo-web-boss/login.do
接下來(lái)就是要解決Tomcat的Session共享問(wèn)題,使用開(kāi)源項(xiàng)目:
https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/tag/3.0.2
注意,因?yàn)槲覀兪褂玫氖荝edis3.0集群,相應(yīng)的插件一定要支持Redis3.0集群。
192.168.186.129:7111
192.168.186.129:7112
192.168.186.132:7113
192.168.186.132:7114
192.168.186.133:7115
192.168.186.133:7116
6、點(diǎn)擊紅色框下載最新版本tomcat-cluster-redis-session-manager
解壓,找到lib目錄中的依賴(lài)的jar包
并將這些jar包上傳到Tomcat8中的 lib 目錄
找到conf目錄下的properties
編輯文件如下,按需調(diào)整
8、添加Tomcat的環(huán)境變量 (可選)
catalina.base="/home/huangkejie/dubbo/web/tomcat8.5.40/"
9、在Tomcat8中的 conf/context.xml 中增加如下兩行配置:
11、在Tomcat的conf/web.xml中核對(duì)確認(rèn)Tomcat的Session超時(shí)時(shí)間,默認(rèn)為30分鐘。
可按需修改。
最后附上作者的readme說(shuō)明內(nèi)容
/** * Tomcat clustering with Redis data-cache implementation. * * author: Ranjith Manickam */ Supports: - Apache Tomcat 7 - Apache Tomcat 8 - Apache Tomcat 9 Pre-requisite: 1. jedis.jar 2. commons-pool2.jar 3. slf4j-api.jar more details.. https://github.com/ran-jit/tomcat-cluster-redis-session-manager/wiki Steps to be done, 1. Move the downloaded jars to tomcat/lib directory - tomcat/lib/ 2. Add tomcat system property "catalina.base" - catalina.base="/home/huangkejie/dubbo/web/tomcat8.5.40/" 3. Extract downloaded package (tomcat-cluster-redis-session-manager.zip) to configure Redis credentials in redis-data-cache.properties file and move the file to tomcat/conf directory - tomcat/conf/redis-data-cache.properties 4. Add the below two lines in tomcat/conf/context.xml5. Verify the session expiration time (minutes) in tomcat/conf/web.xml 60 Note: - All your session attribute values must implement java.io.Serializable. - Supports redis default, sentinel and cluster based on the redis-data-cache.properties configuration.
到此,相信大家對(duì)“如何使用Redis3.0實(shí)現(xiàn)Session共享”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!