基于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文件如下:
View Code
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_namelocalhost;
#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.jspindex.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_indexindex.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 一致性。
分享到:
相关推荐
linux nginx+redis+tomcat分布式负载均衡生产环境部署完成 技术:reids集群 nginx部署 tomcat负载均衡 说明包含: nginx+redis+tomcat分布式负载均衡文档 session共享资料 安装程序 nginx+redis+tomcat分布式...
nginx+redis+tomcat实现分布式session共享,资源包含配置文档和相关工具
分布式web server集群部署后需要实现session共享,针对 tomcat 服务器的实现方案多种多样,比如 tomcat cluster session 广播、nginx IP hash策略、nginx sticky module等方案,本文主要介绍了使用 redis 服务器进行...
一个分布式Java Web Session。基于filter机制。 优点 支持多种序列化方式,默认使用Java自带的Serializable方式(兼容性最好); 支持多种Session存储方案,默认是redis; 合并session的写操作,一个request里的多次...
使用nginx搭建集群tomcat8,redis实现session共享 这里是tomcat 集成工具包
* 将用户登录信息不直接存入session,而是存入Redis缓存,以实现分布式session共享 * 3.提交Data数据的异步任务支持。通过线程池实现异步地将Redis中缓存队列添加到数据库,减少数据库的写入压力。 * 4.nginx与...
使用SSM搭建的仿天猫电商网站,数据库采用MySql。包含用户管理,订单,品类,产品,购物车,地址,...项目的演进会逐步融合tomcat集群,nginx负载均衡,redis缓存分布式,redis分布式锁,单点登录,SpringSession,S…
采用SSM框架的电商网站,数据库采用的是MySql。...项目的演进会逐步融合tomcat集群,nginx负载均衡,redis缓存分布式,redis分布式锁,单点登录,Spring Session,Spring Schedule定时关单,Redisson等
* 将用户登录信息不直接存入session,而是存入Redis缓存,以实现分布式session共享 * 3.提交Data数据的异步任务支持。通过线程池实现异步地将Redis中缓存队列添加到数据库,减少数据库的写入压力。 * 4.nginx与...
使用Redis进行资源缓存以及实现分布式Session;Druid连接池。 使用关系型数据库MySQL。 使用Tomcat服务器集群以及AWS提供的Application layer Load Balancer。 此外还使用了处于网络层第四层的Load Balancer,实现了...
Spring session | 分布式Session管理 | [http://projects.spring.io/spring-session/](http://projects.spring.io/spring-session/) MyBatis | ORM框架 | [http://www.mybatis.org/mybatis-3/zh/index.html]...
1、tomcat做集群配置session复制。如果集群中节点很多,会形成网络风暴。推荐节点数量不要超过5个。 2、分布式架构。拆分成多个子系统。(本项目使用) ### shop-order(订单系统) * 当用户提交订单时此时必须...
SVN/IDEA开发⼯具 分布式版本管理⼯具Git与Github Mysql⾼级 Mycat实现MySQL的分布式、分库分表、读写分离 Nginx反向代理、负载均衡、动静分离 JVM内存模型、参数调优 JUC线程⾼级 分布式架构注册中⼼Zookeeper 虚拟...
【Nginx】Nginx如何配置防止DDOS攻击? 139 限制每秒请求数 139 限制IP连接数 139 白名单设置 139 【数据结构】数组与链表的优缺点 139 【算法】什么是hash? 140 【算法】排序 141 【算法】冒泡排序 141 【算法】...