基于nginx tomcat-redis-session-manager实现session共享
来源:互联网 发布:淘宝网羽绒服女款套装 编辑:程序博客网 时间:2024/04/28 16:18
一、前言
nginx 作为目前最流行的开源反向代理HTTP Server,用于实现资源缓存、web
server负载均衡等功能,由于其轻量级、高性能、高可靠等特点在互联网项目中有着非常普遍的应用,相关概念网上有丰富的介绍。分布式web
server集群部署后需要实现session共享,针对 tomcat 服务器的实现方案多种多样,比如 tomcat cluster
session 广播、nginx IP hash策略、nginx sticky module等方案,本文主要介绍了使用 redis
服务器进行 session 统一存储管理的共享方案。
相关应用结构参照下图:
二、环境配置
系统:Centos7
tomcat:apache-tomcat-7.0.78
redis:redis-2.8.9
三、构建 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 文件如下:
apply plugin: 'java'apply plugin: 'maven'apply plugin: 'signing'group = 'com.orangefunction'version = '2.0.0'repositories { mavenCentral()}compileJava { sourceCompatibility = 1.7 targetCompatibility = 1.7}dependencies { compile group: 'org.apache.tomcat', name: 'tomcat-catalina', version: '7.0.27' compile group: 'redis.clients', name: 'jedis', version: '2.5.2' compile group: 'org.apache.commons', name: 'commons-pool2', version: '2.2' //compile group: 'commons-codec', name: 'commons-codec', version: '1.9' testCompile group: 'junit', name: 'junit', version: '4.+' testCompile 'org.hamcrest:hamcrest-core:1.3' testCompile 'org.hamcrest:hamcrest-library:1.3' testCompile 'org.mockito:mockito-all:1.9.5' testCompile group: 'org.apache.tomcat', name: 'tomcat-coyote', version: '7.0.27'}task javadocJar(type: Jar, dependsOn: javadoc) { classifier = 'javadoc' from 'build/docs/javadoc'}task sourcesJar(type: Jar) { from sourceSets.main.allSource classifier = 'sources'}artifacts { archives jar archives javadocJar archives sourcesJar}//signing {// sign configurations.archives//}task copyJars(type: Copy) { from configurations.runtime into 'dist' }uploadArchives { repositories { mavenDeployer { beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } //repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") { // authentication(userName: sonatypeUsername, password: sonatypePassword) //} //repository(url: "https://oss.sonatype.org/content/repositories/snapshots") { // authentication(userName: sonatypeUsername, password: sonatypePassword) //} pom.project { name 'tomcat-redis-session-manager' packaging 'jar' description 'Tomcat Redis Session Manager is a Tomcat extension to store sessions in Redis' url 'https://github.com/jcoleman/tomcat-redis-session-manager' issueManagement { url 'https://github.com:jcoleman/tomcat-redis-session-manager/issues' system 'GitHub Issues' } scm { url 'https://github.com:jcoleman/tomcat-redis-session-manager' connection 'scm:git:git://github.com/jcoleman/tomcat-redis-session-manager.git' developerConnection 'scm:git:git@github.com:jcoleman/tomcat-redis-session-manager.git' } licenses { license { name 'MIT' url 'http://opensource.org/licenses/MIT' distribution 'repo' } } developers { developer { id 'jcoleman' name 'James Coleman' email 'jtc331@gmail.com' url 'https://github.com/jcoleman' } } } } }}
4、执行gradle命令构建源码,编译输出tomcat-redis-session-manager-master 及依赖jar包
gradle build -x test copyJars
文件列表如下:
四、tomcat 配置
两台tomcat服务器分别在不同的虚拟机上,地址:
http://192.168.31.84:8080http://192.168.31.82:8080
五、编写测试页面
在tomcat安装目录下webapps/ROOT/添加test.jsp文件,内容为:
<%@ 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 82(根据情况修改,以便区别) !"); %>
六、tomcat session manager 配置
1.添加commons-pool2-2.2、jedis-2.5.2、tomcat-redis-session-manager-master-2.0.0jar包到tomcat 安装目录的 lib 文件夹;
2.修改两台tomcat的context.xml文件,使 tomcat-redis-session-manager-master 作为session管理器,同时指定redis地址和端口。
context.xml配置:
<Context> <WatchedResource>WEB-INF/web.xml</WatchedResource> <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="192.168.31.84" port="6379" database="0" maxInactiveInterval="60" /></Context>
3.重启两台tomcat
七、nginx 配置
1.修改nginx.conf文件,内容如下:
#user nobody;worker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream tomcat_nginx { server localhost:8080; server 192.168.31.82:8080; } server { listen 80; server_name localhost; location / { proxy_pass http://tomcat_nginx; root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}
2.重启nginx:
nginx -s reload
八、测试
访问http://192.168.31.84/test.jsp,如下:
nginx默认采用轮询的方式进行负载均衡,刷新页面:
参考 http://www.cnblogs.com/lengfo/p/4260363.html
- 基于nginx tomcat-redis-session-manager实现session共享
- tomcat-redis-session-manager实现tomcat session共享
- 使用tomcat-redis-session-manager实现session共享
- nginx+tomcat+redis实现session共享
- Redis+Nginx+Tomcat实现Session共享
- tomcat redis nginx 实现session共享
- Nginx+Tomcat+Redis实现session共享
- tomcat、redis、nginx实现session共享
- 三、Nginx+Tomcat+Redis实现Session共享
- Tomcat+Nginx+Redis实现分布式Session共享
- nginx+redis实现tomcat session共享
- 基于Nginx+Tomcat集群实现Session共享
- Nginx+Tomcat搭建集群,Spring Session+Redis实现Session共享
- Nginx+Tomcat搭建集群,Spring Session+Redis实现Session共享
- Nginx+Tomcat搭建集群,Spring Session+Redis实现Session共享
- Nginx+Tomcat搭建集群,Spring Session+Redis实现Session共享
- tomcat+redis+nginx session共享
- Nginx Redis tomcat session共享
- 书目
- MYSQL分库分表总结
- Ubuntu下配置Caffe
- Maven项目开发____Maven多模块开发亲测
- java实现冒泡排序法
- 基于nginx tomcat-redis-session-manager实现session共享
- 多数据对比,,上下左右滑动对比数据
- 利用Python、openCV打造自己的人脸识别AI系统
- jsoup加载CSDN分页后文章目录
- 什么是面向对象编程
- vue.js权威指南-第一章 遇见 vue.js
- 使用rxjava2实现界面跳转
- JAVA源码解析(6)-java.beans.PropertyDescriptor
- 中断服务子程序 ISR