您好,欢迎访问一九零五行业门户网

nginx+tomcat+redis实现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 源码,地址如下:
view sourceprint?
1.https://github.com/jcoleman/tomcat-redis-session-manager
3、找到源码中的 build.gradle 文件,由于作者使用了第三方仓库(sonatype),需要注册帐号,太麻烦,注释后直接使用maven中央仓库,同时注释签名相关脚本并增加依赖包的输出脚本 copyjars(dist目录),修改后的 build.gradle 文件如下:
view sourceprint?
001.apply plugin: 'java'
002.apply plugin: 'maven'
003.apply plugin: 'signing'
004. 
005.group = 'com.orangefunction'
006.version = '2.0.0'
007. 
008.repositories {
009.mavencentral()
010.}
011. 
012.compilejava {
013.sourcecompatibility = 1.7
014.targetcompatibility = 1.7
015.}
016. 
017.dependencies {
018.compile group: 'org.apache.tomcat', name: 'tomcat-catalina', version: '7.0.27'
019.compile group: 'redis.clients', name: 'jedis', version: '2.5.2'
020.compile group: 'org.apache.commons', name: 'commons-pool2', version: '2.2'
021.//compile group: 'commons-codec', name: 'commons-codec', version: '1.9'
022. 
023.testcompile group: 'junit', name: 'junit', version: '4.+'
024.testcompile 'org.hamcrest:hamcrest-core:1.3'
025.testcompile 'org.hamcrest:hamcrest-library:1.3'
026.testcompile 'org.mockito:mockito-all:1.9.5'
027.testcompile group: 'org.apache.tomcat', name: 'tomcat-coyote', version: '7.0.27'
028.}
029. 
030.task javadocjar(type: jar, dependson: javadoc) {
031.classifier = 'javadoc'
032.from 'build/docs/javadoc'
033.}
034. 
035.task sourcesjar(type: jar) {
036.from sourcesets.main.allsource
037.classifier = 'sources'
038.}
039. 
040.artifacts {
041.archives jar
042. 
043.archives javadocjar
044.archives sourcesjar
045.}
046. 
047.//signing {
048.//  sign configurations.archives
049.//}
050. 
051.task copyjars(type: copy) {
052.from configurations.runtime
053.into 'dist' 
054.}
055. 
056.uploadarchives {
057.repositories {
058.mavendeployer {
059.beforedeployment { mavendeployment deployment -> signing.signpom(deployment) }
060. 
061.//repository(url: 'https://oss.sonatype.org/service/local/staging/deploy/maven2/') {
062.//  authentication(username: sonatypeusername, password: sonatypepassword)
063.//}
064.//repository(url: 'https://oss.sonatype.org/content/repositories/snapshots') {
065.//  authentication(username: sonatypeusername, password: sonatypepassword)
066.//}
067. 
068.pom.project {
069.name 'tomcat-redis-session-manager'
070.packaging 'jar'
071.description 'tomcat redis session manager is a tomcat extension to store sessions in redis'
072.url 'https://github.com/jcoleman/tomcat-redis-session-manager'
073. 
074.issuemanagement {
075.url 'https://github.com:jcoleman/tomcat-redis-session-manager/issues'
076.system 'github issues'
077.}
078. 
079.scm {
080.url 'https://github.com:jcoleman/tomcat-redis-session-manager'
081.connection 'scm:git:git://github.com/jcoleman/tomcat-redis-session-manager.git'
082.developerconnection 'scm:git:git@github.com:jcoleman/tomcat-redis-session-manager.git'
083.}
084. 
085.licenses {
086.license {
087.name 'mit'
088.url 'http://opensource.org/licenses/mit'
089.distribution 'repo'
090.}
091.}
092. 
093.developers {
094.developer {
095.id 'jcoleman'
096.name 'james coleman'
097.email 'jtc331@gmail.com'
098.url 'https://github.com/jcoleman'
099.}
100.}
101.}
102.}
103.}
104.}
4、执行gradle命令构建源码,编译输出tomcat-redis-session-manager-master 及依赖jar包
view sourceprint?
1.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 增加以下配置:
view sourceprint?
1.
2.
3.4.host='localhost'
5.port='6379'
6.database='0'
7.maxinactiveinterval='60' />
8.
3、分别重启2台 tomcat 服务器。
七、nginx 配置
1、修改 default.conf 配置文件,启用 upstream 负载均衡 tomcat cluster,默认使用轮询方式。
view sourceprint?
01.upstream site { 
  ip_hash;               //基于ip_hash分发
02.server localhost:8080;
03.server localhost:9090;
04.} 
05. 
06.server {
07.listen       80;
08.server_name  localhost;
09. 
10.#charset koi8-r;
11.#access_log  /var/log/nginx/log/host.access.log  main;
12. 
13.location / {
14.#root   /usr/share/nginx/html;
15.#index  index.html index.htm;
16.index  index_tel.http://www.it165.net/pro/webjsp/ target=_blankclass=keylink>jsp index.http://www.it165.net/pro/webjsp/target=_blank class=keylink>jsp index.html index.htm ; 
17.proxy_redirect          off;   
18.proxy_set_header        host            $host;   
19.proxy_set_header        x-real-ip       $remote_addr;   
20.proxy_set_header        x-forwarded-for $proxy_add_x_forwarded_for;   
21.client_max_body_size    10m;   
22.client_body_buffer_size 128k;   
23.proxy_buffers           32 4k; 
24.proxy_connect_timeout   3;   
25.proxy_send_timeout      30;   
26.proxy_read_timeout      30;  
27.proxy_pass http://site;
28. 
29.}
30. 
31.#error_page  404              /404.html;
32. 
33.# redirect server error pages to the static page /50x.html
34.#
35.error_page   500 502 503 504  /50x.html;
36.location = /50x.html {
37.root   /usr/share/nginx/html;
38.}
39. 
40.# proxy the php scripts to apache listening on 127.0.0.1:80
41.#
42.#location ~ .php$ {
43.#    proxy_pass   http://127.0.0.1;
44.#}
45. 
46.# pass the php scripts to fastcgi server listening on 127.0.0.1:9000
47.#
48.#location ~ .php$ {
49.#    root           html;
50.#    fastcgi_pass   127.0.0.1:9000;
51.#    fastcgi_index  index.php;
52.#    fastcgi_param  script_filename  /scripts$fastcgi_script_name;
53.#    include        fastcgi_params;
54.#}
55. 
56.# deny access to .htaccess files, if apache's document root
57.# concurs with nginx's one
58.#
59.#location ~ /.ht {
60.#    deny  all;
61.#}
62.}
2、nginx 重新加载配置
view sourceprint?
1.nginx -s reload
八、配置tomcat
保存的session实体是放到redis中,tomcat可以记录session-id值与redis进行对比,session-id是从cookie中取得的,不同的tomcat存储sessioncookie的位置是不同的,所以必须修改所有tomcat中conf/context.xml,修改内容如下:
 sessioncookiepath=/>
九、测试结果
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 一致性。
启动顺序:redis——nginx——tomcat
redis启动脚本=/usr/locat/redis.2.0.1/src/redis-server
nginx+tomcat+redis实现session共享 以上就介绍了nginx+tomcat+redis实现session共享,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。
其它类似信息

推荐信息