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,请先安装 tomcatredisnginx 相关环境,不作详细描述,本文测试配置如下:

 

 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中央仓库,同时注释签名相关脚本并增加依赖包的输出脚本 copyJarsdist目录),修改后的 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 端口号为80809090,并确保都能正常工作,当然如果分布在不同的主机则可以使用相同端口号。

五、编写测试页面

  为了区别2tomcat的访问,分别编写页面并打包部署:

  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.<Context>

2.<Valve className='com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve'/>

3.<Manager className='com.orangefunction.tomcat.redissessions.RedisSessionManager'

4.host='localhost'

5.port='6379'

6.database='0'

7.maxInactiveInterval='60' />

8.</Context>

  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.<a href="http://www.it165.net/pro/webjsp/" target="_blank"class="keylink">jsp</a> index.<a href="http://www.it165.net/pro/webjsp/"target="_blank" class="keylink">jsp</a> 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.}

  2nginx 重新加载配置

view sourceprint?

1.nginx -s reload

八、配置Tomcat

保存的session实体是放到redis中,tomcat可以记录session-id值与redis进行对比,session-id是从cookie中取得的,不同的tomcat存储sessioncookie的位置是不同的,所以必须修改所有tomcatconf/context.xml,修改内容如下:

<Context 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共享
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值