千家信息网

如何使用Redis3.0实现Session共享

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,本篇内容主要讲解"如何使用Redis3.0实现Session共享",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何使用Redis3.0实现Session共
千家信息网最后更新 2026年11月03日如何使用Redis3.0实现Session共享

本篇内容主要讲解"如何使用Redis3.0实现Session共享",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何使用Redis3.0实现Session共享"吧!

Tomcat版本:Tomca8

如果对此章节不了解,可以查阅https://my.oschina.net/u/3155476/blog/3070575(Keepalived + Nginx实现高可用Web负载均衡)。

1、单节点访问 http://192.168.186.129:8080/dubbo-web-boss/login.do :

2、增加多一个消费者节点:192.168.186.129,以同样的方式部署dubbo-web-boss工程。(另外一个消费节点同样的原理,在此不再多次描述)

先验证新增节点也可正常访问 http://192.168.186.129:8080/dubbo-web-boss/login.do

3、在Keepalived+Nginx组成的反向代理集群中的三个节点同步增加如下两处配置:

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、通过反向代理集群的VIP访问dubbo-web-boss时,有时可以登录成功,但有时又会提示验证码错误,原因就是Session没有同步。

http://192.168.186.50:88/dubbo-web-boss/login.do

接下来就是要解决Tomcat的Session共享问题,使用开源项目:

https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/tag/3.0.2

注意,因为我们使用的是Redis3.0集群,相应的插件一定要支持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、点击红色框下载最新版本tomcat-cluster-redis-session-manager

解压,找到lib目录中的依赖的jar包

并将这些jar包上传到Tomcat8中的 lib 目录

找到conf目录下的properties

编辑文件如下,按需调整

8、添加Tomcat的环境变量 (可选)

catalina.base="/home/huangkejie/dubbo/web/tomcat8.5.40/"

9、在Tomcat8中的 conf/context.xml 中增加如下两行配置:


11、在Tomcat的conf/web.xml中核对确认Tomcat的Session超时时间,默认为30分钟。

30

可按需修改。

最后附上作者的readme说明内容

/** * Tomcat clustering with Redis data-cache implementation. * * author: Ranjith Manickam */Supports:   - Apache Tomcat 7   - Apache Tomcat 8   - Apache Tomcat 9Pre-requisite:        1. jedis.jar        2. commons-pool2.jar        3. slf4j-api.jarmore details.. https://github.com/ran-jit/tomcat-cluster-redis-session-manager/wikiSteps 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.xml                                        5. 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.

到此,相信大家对"如何使用Redis3.0实现Session共享"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

节点 集群 内容 目录 就是 版本 代理 同步 学习 消费 配置 验证 实用 更深 均衡 成功 接下来 三个 作者 兴趣 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 广东万易互联网科技有限公司惠州分公司 互联网金融 科技金融 致骋互联网科技 互联网 视角下的科技创新 大白互联网科技 阿里巴巴算科技企业还是互联网 华服互联网科技官网 昭通互联网科技选哪家 知域互联网科技有限公司大骗子 互联网科技公司有风险吗 互联网科技产业特征 每天都有哪些好的互联网科技 广州富喜互联网络科技 半导体是互联网高科技吗 米泉互联网科技专业怎么样 三亚科技创新互联网基地 盐城市攀科互联网科技有限公司 华人运通互联网科技公司 河南商务圈互联网科技有限公司 黑龙江互联网科技公司 科技互联网企业 互联网巨头反垄断金融科技 金诺互联网科技 广州互联网科技公司 奇酷互联网科技 奇酷互联网络科技公司 团飞科技与伯堂互联网服务平台 互联网和黑科技的看法 米聚互联网科技南京有限公司排面 上海互联网科技公司ppt
0