Linux系統Nginx+Tomcat+Redis實現Session共享

用戶:roothtml

Nginx版本:nginx-1.10.0java

Tomcat版本:apache-tomcat-7.0.52nginx

Redis版本:redis-3.0.7git

下載安裝

  • 下載tomcat使用redis做爲session存儲的插件
  • 官網地址:https://github.com/jcoleman/tomcat-redis-session-manager,下載tomcat-redis-session-manager-master.zip,上傳至Linux系統,並解壓
  • [root@localhost session]# pwd
    /home/listen/session
    [root@localhost session]# ll
    total 44
    drwxrwxr-x. 9 listen listen  4096 May 10 01:23 tomcat-redis-session-manager-master
    -rw-rw-r--. 1 listen listen 40891 May 10 00:42 tomcat-redis-session-manager-master.zip
    [root@localhost session]#

     

  • 編輯編譯的構建文件build.gradle

  • [root@localhost tomcat-redis-session-manager-master]# pwd
    /home/listen/session/tomcat-redis-session-manager-master
    [root@localhost tomcat-redis-session-manager-master]# ll
    total 36
    drwxr-xr-x. 7 listen listen    75 May 10 01:23 build
    -rw-rw-r--. 1 listen listen  2852 May 10 00:44 build.gradle
    -rw-rw-r--. 1 listen listen  2726 May 10 00:43 build.gradle.bak
    drwxr-xr-x. 2 listen listen  4096 May 10 03:26 dist
    drwxrwxr-x. 3 listen listen    57 Apr  8  2015 example-app
    -rw-rw-r--. 1 listen listen    70 Apr  8  2015 Gemfile
    -rw-rw-r--. 1 listen listen   592 Apr  8  2015 Gemfile.lock
    -rw-rw-r--. 1 listen listen  1061 Apr  8  2015 license.txt
    -rw-rw-r--. 1 listen listen 11057 Apr  8  2015 README.markdown
    drwxrwxr-x. 4 listen listen    56 Apr  8  2015 spec
    drwxrwxr-x. 3 listen listen    17 Apr  8  2015 src
    drwxrwxr-x. 3 listen listen    33 Apr  8  2015 vagrant
    [root@localhost tomcat-redis-session-manager-master]# vi 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.61'
      compile group: 'redis.clients', name: 'jedis', version: '2.7.3'
      compile group: 'org.apache.commons', name: 'commons-pool2', version: '2.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.61'
    }
      
    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
    }
      
    //signing {
    //  sign configurations.archives
    //}
      
    task copyJars(type: Copy) {
      from configurations.runtime
      into 'dist'  
    }
      
    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'
              }
            }
          }
        }
      }
    }

     

  • 編譯構建項目

  • [root@localhost tomcat-redis-session-manager-master]# gradle build -x test copyJars
     若是發現gradle命令不能識別,請先安裝gradle工具,請參考Linux系統Nginx安裝配置
  • 生成的jar包

  • 拷貝替換jar包

  • 拷貝dist目錄下面全部的jar包及build/libs目錄下的tomcat-redis-session-manager-master-2.0.0.jar包至{tomcat_home} /lib目錄下(兩臺tomcat)
  • [root@localhost libs]# cp /home/listen/session/tomcat-redis-session-manager-master/dist/*.jar /home/listen/session/tomcat-redis-session-manager-master/build/libs/tomcat-redis-session-manager-master-2.0.0.jar /home/listen/tomcat/apache-tomcat-7.0.52/lib
    [root@localhost libs]# cp /home/listen/session/tomcat-redis-session-manager-master/dist/*.jar /home/listen/session/tomcat-redis-session-manager-master/build/libs/tomcat-redis-session-manager-master-2.0.0.jar /home/listen/tomcat/apache-tomcat-7.0.52-02/lib

     

  • 修改2個tomcat的context.xml配置文件

  • [root@localhost tomcat]# pwd 
    /home/listen/tomcat
    [root@localhost tomcat]# vi apache-tomcat-7.0.52/conf/context.xml
     內容爲:
  • <?xml version='1.0' encoding='utf-8'?>
    <!--
      Licensed to the Apache Software Foundation (ASF) under one or more
      contributor license agreements.  See the NOTICE file distributed with
      this work for additional information regarding copyright ownership.
      The ASF licenses this file to You under the Apache License, Version 2.0
      (the "License"); you may not use this file except in compliance with
      the License.  You may obtain a copy of the License at
    
          http://www.apache.org/licenses/LICENSE-2.0
    
      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.
    -->
    <!-- The contents of this file will be loaded for each web application -->
    <Context>
    
        <!-- Default set of monitored resources -->
        <WatchedResource>WEB-INF/web.xml</WatchedResource>
    
        <!-- Uncomment this to disable session persistence across Tomcat restarts -->
        <!--
        <Manager pathname="" />
        -->
    
        <!-- Uncomment this to enable Comet connection tacking (provides events
             on session expiration as well as webapp lifecycle) -->
        <!--
        <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
        -->
    
    <!--如下爲增長的內容-->
    <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />  
    <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"  
             host="192.168.75.141"  
             port="6379"  
             database="0"  
             maxInactiveInterval="60" />  
    
    </Context>

    能夠看出鏈接的redis爲192.168.75.141:6379github

  • 測試使用Redis實現Tomcat多機的session共享

  • 新建test工程,爲web模式,修改index.jsp的內容爲:
  • <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Nginx+tomcat負載均衡+redis會話管理</title>
    </head>
    <body>
    <h1><font color="blue">集羣節點1</font></h1>
        <table align="centre" border="1">
          <tr>
            <td>Session ID</td>
            <td><%= session.getId() %></td>
          </tr>
          <tr>
            <td>Created on</td>
            <td><%= session.getCreationTime() %></td>
         </tr>
        </table>
     <p>
    </body>
    </html>

    第二臺tomcat部署的test工程的index.jsp的內容修改成:web

  • <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Nginx+tomcat負載均衡+redis會話管理</title>
    </head>
    <body>
    <h1><font color="blue">集羣節點2</font></h1>
        <table align="centre" border="1">
          <tr>
            <td>Session ID</td>
            <td><%= session.getId() %></td>
          </tr>
          <tr>
            <td>Created on</td>
            <td><%= session.getCreationTime() %></td>
         </tr>
        </table>
     <p>
    </body>
    </html>

     

  • 部署測試session
  • 將兩個test工程分別部署至兩個tomcat實例,而後重啓2個tomcat,啓動Nginx,(Nginx配置可參考Linux系統Nginx安裝配置)而後刷新而後刷新http://192.168.75.141/test/地址 
  • 多刷新幾回,便可看到兩臺tomcat的test工程在不一樣的切換,可是session id不變,即實現多機Tomcat使用Redis實現session共享。 

over! redis

相關文章
相關標籤/搜索