基于nginx+tomcat+redis 分布式web应用的session共享 的配置过程

一:结构图

21122119_9lTf.png

二:环境

测试环境基于 Linux CentOS 7.X,请先安装 tomcat、redis、nginx 相关环境,不作详细描述,本文测试配置如下:

   Version  IP_Port
 nginx  1.8.0  192.168.1.200:80
 tomcat_1  7.0.67  192.168.1.200:8080
 tomcat_2  7.0.67  192.168.1.200:9090
 redis  3.0.6  192.168.1.200:6379



三:依赖环境

Jdk:最好7以上;

gradle:简介与说明

git:简介与说明

redis:安装与说明

Nginx:参考安装地址(yum安装)

四:构建 tomcat-redis-session-manager-master

首先从git上clone下来到本地

git clone git@github.com:jcoleman/tomcat-redis-session-manager.git
cd tomcat-redis-session-manager.git

先不执行build,先修改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.67'
  compile group: 'redis.clients', name: 'jedis', version: '2.8.0'
  compile group: 'org.apache.commons', name: 'commons-pool2', version: '2.4.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.67'
}

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
}
task copyJars(type: Copy) {
  from configurations.runtime
  into 'dist'  
}

//signing {
//  sign configurations.archives
//}

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'
          }
        }
      }
    }
  }
}

PS:注意上面打注释和task copyJars 的地方  去除第三方仓库(sonatype),用了中央仓库,然后后面的copyJars是拷贝jar包到dist目录中

然后(window下cmd进入clone的目录下)进行编译 


gradle build -x test  copyJars



然后生成的jar如下(session-manager的那个在build\libs下)


五:两台tomcat 调整

1.上述jar包如果tomcat\lib下没有,则放入lib下

2.修改tomcat conf下的context.xml,在Context间加入

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
           host="127.0.0.1"
           port="6379"
           database="0"
           maxInactiveInterval="60" />



3.修改server.xml中的相关端口,调整http端口分别为8080与9090

4.放如sessionId JSP测试页面,输出SESSIONID

5.启动tomcat

六:Nginx 配置

修改默认的default.conf-->

upstream site {  
    server localhost:8080; 
    server localhost:9090; 
}  

server {
    listen       80;
    server_name  localhost;
    location / {
        index  index_tel.jsp index.jsp index.html index.htm ;  
        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;
        client_max_body_size    10m;    
        client_body_buffer_size 128k;    
        proxy_buffers           32 4k;  
        proxy_connect_timeout   3;    
        proxy_send_timeout      30;    
        proxy_read_timeout      30;   
        proxy_pass http://site; 
         
    }
}



重启Nginx服务

七:测试效果



八:大功告成(强行8)



转载于:https://my.oschina.net/undefine/blog/607310

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值