环境:
192.168.0.18 nginx redis
192.168.0.13 tomcat1 192.168.0.14 tomcat2
所需文件下载:
http://central.maven.org/maven2/redis/clients/jedis/2.5.2/jedis-2.5.2.jar
http://central.maven.org/maven2/org/apache/commons/commons-pool2/2.2/commons-pool2-2.2.jar
我们需要依赖一个由美国的James Coleman同学编写的tomcat-redis-session-manager项目:https://github.com/jcoleman/tomcat-redis-session-manager,该项目是用gradle构建的。所以你需要用到gradle。
安装gradlegradle官网地址:http://gradle.org/gradle下载页面:http://gradle.org/gradle-download/gradle2.9完整包下载地址:https://services.gradle.org/distributions/gradle-2.9-all.zip下载完成后,解压并配置环境变量:GRADLE_HOME=/xxx/xxx/xxxx/gradle-2.9Path=原Path地址;%GRADLE_HOME%\bin输入命令测试gradle:gradle -v 显示版本即可
用gradle编译tomcat-session-manager
首先你需要在github上将该项目clone下来:git clone https://github.com/jcoleman/tomcat-redis-session-manager.git
然后在该目录输入命令: gradle build 如果你想查看gradle构建中遇到的详细错误信息栈则添加参数--stacktrace:gradle build --stacktrace
结果报出错误: No such property: sonatypeUsername for class: org.gradle.api.publication.maven.internal.deployer.DefaultGroovyMavenDeployer
那么请在signing节点中添加required = false以及删掉或注释掉build.gradle文件中的sonatype的repository代码即可
再次运行gradle build --stacktrace 构建成功,然后打包的jar会放到build/libs目录下
此时只需要将tomcat-redis-session-manager-2.0.0.jar以及另外两个依赖包:jedis- 2.5.2.jar,commons-pool2-2.2.jar一并拷入tomcat的lib目录下即可(依赖包可以在gradle的用户目录下找到)
配置tomcat对redis的依赖:
Add the following into your Tomcat context.xml (or the context block of the server.xml if applicable.)
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="localhost" <!-- optional: defaults to "localhost" --> port="6379" <!-- optional: defaults to "6379" --> database="0" <!-- optional: defaults to "0" --> maxInactiveInterval="60" <!-- optional: defaults to "60" (in seconds) --> sessionPersistPolicies="PERSIST_POLICY_1,PERSIST_POLICY_2,.." <!-- optional --> sentinelMaster="SentinelMasterName" <!-- optional --> sentinels="sentinel-host-1:port,sentinel-host-2:port,.." <!-- optional --> />
nginx分发
http { upstream myapp1 { server srv1.example.com; server srv2.example.com; server srv3.example.com; } log_format www_abc_com '$remote_addr - $remote_user [$time_local] $request ' '"$status"$body_bytes_sent "$http_referer"' '"$http_user_agent" "$http_x_forwarded_for"'; server { listen 80; location / { proxy_pass http://myapp1; 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; } access_log /data/base_files/logs/redis.xxy.log www_abc_com; } }
创建session测试程序
分别放入两个tomcat中,192.168.0.13 标记为tomcat1 192.168.0.14 标记为tomcat2
<%@page language="java"%>
<html>
<body>
<h1><font color="red">Session serviced by tomcat1</font></h1>
<table aligh="center" border="1">
<tr>
<td>Session ID</td>
<td><%=session.getId() %></td>
<% session.setAttribute("abc","abc");%>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
<html>
访问http://192.168.0.18 刷新几次可以显示访问不同的服务器