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

相关文章
相关标签/搜索