基於nginx+tomcat+redis 分佈式web應用的session共享 的配置過程

一:結構圖

二:環境

測試環境基於 Linux CentOS 7.X,請先安裝 tomcat、redis、nginx 相關環境,不做詳細描述,本文測試配置以下: html

   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以上; java

gradle:簡介與說明 nginx

git:簡介與說明git

redis:安裝與說明 github

Nginx:參考安裝地址(yum安裝) redis

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

首先從git上clone下來到本地 shell

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

先不執行build,先修改build.gradle,否則會報錯 apache

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目錄中 tomcat

而後(window下cmd進入clone的目錄下)進行編譯  session


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)

相關文章
相關標籤/搜索