Linux系统Nginx+Tomcat+Redis实现Session共享

用户:root

Nginx版本:nginx-1.10.0

Tomcat版本:apache-tomcat-7.0.52

Redis版本:redis-3.0.7

下载安装

  • 下载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安装配置
  • 174918_hjcB_39617.png
  • 174956_zqid_39617.png
  • 生成的jar包

  • 175016_O30D_39617.png
  • 拷贝替换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:6379

  • 测试使用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的内容修改为:

  • <%@ 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/地址 
  • 180449_ZLbQ_39617.png180458_A9E3_39617.png
  • 多刷新几次,即可看到两台tomcat的test工程在不同的切换,但是session id不变,即实现多机Tomcat使用Redis实现session共享。 

over! 

转载于:https://my.oschina.net/Listening/blog/674759

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值