基于Redis实现Apache和Tomcat集群的Session共享配置
之前博主文章有介绍过apache和tomcat主备的配置步骤,在那个架构中,为了解决负载均衡时的session问题,apache配置里强制把session sticky在某个tomcat,或者直接使用主备。这样虽然可以解决不同的需求,但是如果项目更新或者重启tomcat,session肯定丢失,而且也不能实现项目的平滑更新。所以为了让tomcat可以承受更多并发,或者让tomcat更加高可用,或者对用户更加友好,维持session的方式有同步和共享两种。博主这里介绍session共享的配置,搭建统一session服务器。
一、安装Redis
安装参考上一篇文章:https://zhangnq.com/2478.html
二、安装apache,两台tomcat
参考文章:
https://zhangnq.com/1743.html
https://zhangnq.com/1702.html
三、获取tomcat redis session manager
从网上下载下来的还是觉得不怎么靠谱,所以决定自己从源码打包。
1、获取第三方插件源码
git clone https://github.com/jcoleman/tomcat-redis-session-manager.git
2、源码构建基于gradle,所以需要先配置gradle环境,不要使用ubuntu下的apt方式安装。
root@ubuntu:~/softbak/# wget https://services.gradle.org/distributions/gradle-2.14.1-all.zip root@ubuntu:~/softbak/# unzip gradle-2.14.1-all.zip root@ubuntu:~/softbak/# mv gradle-2.14.1 /usr/local/ root@ubuntu:~/softbak/# cd /usr/local/ root@ubuntu:/usr/local# ln -s gradle-2.14.1 gradle root@ubuntu:/usr/local# vi /etc/profile root@ubuntu:/usr/local# #添加gradle的环境变量 root@ubuntu:/usr/local# gradle -v ------------------------------------------------------------ Gradle 2.14.1 ------------------------------------------------------------ Build time: 2016-07-18 06:38:37 UTC Revision: d9e2113d9fb05a5caabba61798bdb8dfdca83719 Groovy: 2.4.4 Ant: Apache Ant(TM) version 1.9.6 compiled on June 29 2015 JVM: 1.7.0_45 (Oracle Corporation 24.45-b08) OS: Linux 3.5.0-23-generic amd64
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.52' 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.52' } 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
1、分别将第三步生成的 tomcat-redis-session-manager-master 及依赖jar包覆盖到 tomcat 安装目录的 lib 文件夹。
2、分别修改2台 tomcat 的 context.xml 文件,使 tomcat-redis-session-manager-master 作为session管理器,同时指定redis地址和端口。
context.xml 增加类似以下配置:
<!-- redis session manager --> <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="192.168.188.203" port="6379" database="0" maxInactiveInterval="60" />
有关配置的其他参数可以参考:https://github.com/izerui/tomcat-redis-session-manager
配置修改完成后分别重启tomcat 。
五、配置apache
apache的主要配置类似如下:
#tomcat主备、负载均衡配置例子 开始 ProxyRequests off ProxyPreserveHost On #Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED <Proxy balancer://blog.nbhao.org> BalancerMember ajp://192.168.188.112:8009 route=node1 loadfactor=1 BalancerMember ajp://192.168.188.112:9009 route=node2 loadfactor=1 #ProxySet stickysession=JSESSIONID #ProxySet stickysession=ROUTEID #byrequests(default),bytraffic,bybusyness ProxySet lbmethod=byrequests ProxySet nofailover=On </Proxy> #不代理balancer目录 ProxyPass /balancer ! ProxyPass / balancer://blog.nbhao.org/ ProxyPassReverse / balancer://blog.nbhao.org/ #balancer目录,查看节点状态 <Location /balancer> SetHandler balancer-manager Proxypass ! Order allow,deny Allow from all </Location> <Proxy *> Order allow,deny Allow from all </Proxy> #tomcat主备、负载均衡配置例子 结束
重启apache服务。
六、添加测试页面
测试页面代码index.jsp:
<%@ page language="java" %> <html> <head><title>Tomcat 1</title></head> <body> <h1><font color="green">Tomcat 1</font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <% session.setAttribute("blog.nbhao.org","blog.nbhao.org"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html>
七、测试
创建好后就可以通过浏览器打开,可以看到tomcat集群生效,而且session保持不变。
redis中可以看到保存的session 。
参考连接:http://www.cnblogs.com/lengfo/p/4260363.html