tomcat redis session共享

 

  1. 編譯redis所須要的序列化包

安裝 gradlecss

Linux & MacOS users html

Configure your PATH environment variable to include the bin directory of the unzipped distribution, e.g.: java

$ export PATH=$PATH:/opt/gradle/gradle-5.3/binlinux

 

$ mkdir /opt/gradle git

$ unzip -d /opt/gradle gradle-5.3-bin.zip github

$ ls /opt/gradle/gradle-5.3 web

 

 

  1. 拉源碼包

git clone https://github.com/jcoleman/tomcat-redis-session-manager.gitredis

 

  1. 編譯

[root@oldboy tomcat-redis-session-manager]# cat build.gradleexpress

 

tomcat 版本 7.0.78apache

 

jdk版本 1.8

[root@oldboy ~]# java -version

java version "1.8.0_151"

Java(TM) SE Runtime Environment (build 1.8.0_151-b12)

 

 

  1 [root@oldboy tomcat-redis-session-manager]# cat build.gradle
  2 apply plugin: 'java'
  3 apply plugin: 'maven'
  4 apply plugin: 'signing'
  5 
  6 group = 'com.orangefunction'
  7 version = '2.0.0'
  8 
  9 repositories {
 10   mavenCentral()
 11 }
 12 
 13 compileJava {
 14   sourceCompatibility = 1.8
 15   targetCompatibility = 1.8
 16 }
 17 
 18 dependencies {
 19   compile group: 'org.apache.tomcat', name: 'tomcat-catalina', version: '7.0.78'
 20   compile group: 'redis.clients', name: 'jedis', version: '2.5.2'
 21   compile group: 'org.apache.commons', name: 'commons-pool2', version: '2.6.0'
 22   //compile group: 'commons-codec', name: 'commons-codec', version: '1.9'
 23 
 24   testCompile group: 'junit', name: 'junit', version: '4.+'
 25   testCompile 'org.hamcrest:hamcrest-core:1.3'
 26   testCompile 'org.hamcrest:hamcrest-library:1.3'
 27   testCompile 'org.mockito:mockito-all:1.9.5'
 28   testCompile group: 'org.apache.tomcat', name: 'tomcat-coyote', version: '7.0.78'
 29 }
 30 
 31 task javadocJar(type: Jar, dependsOn: javadoc) {
 32   classifier = 'javadoc'
 33   from 'build/docs/javadoc'
 34 }
 35 
 36 task sourcesJar(type: Jar) {
 37   from sourceSets.main.allSource
 38   classifier = 'sources'
 39 }
 40 
 41 artifacts {
 42   archives jar
 43 
 44   archives javadocJar
 45   archives sourcesJar
 46 }
 47 
 48 //signing {
 49  // sign configurations.archives
 50 //}
 51 task copyJars(type: Copy) {
 52   from configurations.runtime
 53   into 'dist'  
 54 }
 55 
 56 uploadArchives {
 57   repositories {
 58     mavenDeployer {
 59       beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
 60 
 61      //repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
 62      //authentication(userName: sonatypeUsername, password: sonatypePassword)
 63      //}
 64      //repository(url: "https://oss.sonatype.org/content/repositories/snapshots") {
 65      //authentication(userName: sonatypeUsername, password: sonatypePassword)
 66      //}
 67 
 68       pom.project {
 69         name 'tomcat-redis-session-manager'
 70         packaging 'jar'
 71         description 'Tomcat Redis Session Manager is a Tomcat extension to store sessions in Redis'
 72         url 'https://github.com/jcoleman/tomcat-redis-session-manager'
 73 
 74         issueManagement {
 75           url 'https://github.com:jcoleman/tomcat-redis-session-manager/issues'
 76           system 'GitHub Issues'
 77         }
 78 
 79         scm {
 80           url 'https://github.com:jcoleman/tomcat-redis-session-manager'
 81           connection 'scm:git:git://github.com/jcoleman/tomcat-redis-session-manager.git'
 82           developerConnection 'scm:git:git@github.com:jcoleman/tomcat-redis-session-manager.git'
 83         }
 84 
 85         licenses {
 86           license {
 87             name 'MIT'
 88             url 'http://opensource.org/licenses/MIT'
 89             distribution 'repo'
 90           }
 91         }
 92 
 93         developers {
 94           developer {
 95             id 'jcoleman'
 96             name 'James Coleman'
 97             email 'jtc331@gmail.com'
 98             url 'https://github.com/jcoleman'
 99           }
100         }
101       }
102     }
103   }
104 }

 

 

 

[root@oldboy tomcat-redis-session-manager]# gradle build -x test copyJars

 

在tomcat-redis-session-manager/build/libs/目錄下生成如下幾個包

 

在tomcat-redis-session-manager/dist/下生成如下幾個包

 

複製3個文件到tomcat/lib目錄下

[root@oldboy dist]# cp jedis-2.5.2.jar tomcat-redis-session-manager-2.0.0.jar commons-pool2-2.6.0.jar /usr/local/tomcat/lib/

 

編寫tomcat context.xml文件添加redis信息

[root@oldboy ~]# vim /usr/local/tomcat/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="10.0.0.51"

port="6379"

password='123'

database="0"

maxInactiveInterval="60" />

</Context>

 

 

  1. 測試結果.隨便刷新 seisson不變

 

  1. 存到redis的session數據

 

 

  1. jdk安裝
    rpm -ivh jdk-8u151-linux-x64.rpm 
    如圖:

    2
    tomcat下載、解壓
    tomcat
    下載地址:http://tomcat.apache.org/
    tar xf apache-tomcat-7.0.78.tar.gz -C /usr/local/
    ln -s apache-tomcat-7.0.78 tomcat
    3
    、配置path環境變量
    vi /etc/profile.d/tomcat.sh
    export CATALINA_BASE=/usr/local/tomcat
    export PATH=$CATALINA_BASE/bin:$PATH 
    chmod +x /etc/profile.d/tomcat.sh #
    賦予可執行權限
    source /etc/profile.d/tomcat.sh
    4
    、建立測試網頁的發佈目錄
    mkdir /usr/local/tomcat/webapps/test/

[root@oldboy dist]# cat /usr/local/tomcat/webapps/test/index.jsp

<%@ page language="java" %>

<html>

<head><title>TomcatA</title></head>

<body>

<h1><font color="red">TomcatA.linuxinfo.top</font></h1>

<tablealign="centre" border="1">

<tr>

<td>SessionID</td>

<% session.setAttribute("linuxinfo.top","linuxinfo.top");%>

<td><%=session.getId() %></td>

</tr>

<tr>

<td>Createdon</td>

<td><%=session.getCreationTime() %></td>

</tr>

</table>

</body>

</html>

 

6、分別啓動tomcat
catalina.sh start #
可也用全路徑/usr/local/tomcat/bin/startup.sh
7
、訪問192.168.2.197,測試以下,查看session ID

 

 

 

1下載五個jar包,將jar包放在/usr/local/tomcat/lib:

mgetjavolution-5.4.3.1.jar

memcached-session-manager-1.8.0jar

memcached-session-manager-tc7-1.8.0.jar

msm-javolution-serializer-1.8.0.jar

spymemcached-1.8.0.jar

 

 

  1. contest.xml段定義一個用於測試的context容器,並在其中建立一個會話管理器。
    添加以下信息
    vim /usr/local/tomcat/conf/context.xml"
  2. <?xml version='1.0' encoding='utf-8'?>
  3. <!--
  4. Licensed to the Apache Software Foundation (ASF) under one or more
  5. contributor license agreements. See the NOTICE file distributed with
  6. this work for additional information regarding copyright ownership.
  7. The ASF licenses this file to You under the Apache License, Version 2.0
  8. (the "License"); you may not use this file except in compliance with
  9. the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. -->
  17. <!-- The contents of this file will be loaded for each web application -->
  18. <Context>
  19. <!-- Default set of monitored resources -->
  20. <WatchedResource>WEB-INF/web.xml</WatchedResource>
  21. <!-- Uncomment this to disable session persistence across Tomcat restarts -->
  22. <!--
  23. <Manager pathname="" />
  24. -->
  25. <!-- Uncomment this to enable Comet connection tacking (provides events
  26. on session expiration as well as webapp lifecycle) -->
  27. <!--
  28. <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
  29. -->
  30. <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
  31. memcachedNodes="m1:192.168.2.37:11211,m2:192.168.2.27:11211"
  32. failoverNodes="m2"
  33. requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
  34. transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"/>
  35. </Context>

     

     


  36. 重啓服務:
    catalina.sh stop
    catalina.sh start
    3
    、下載memcached,啓動服務yum install memcached -ysystemctl start memcached
相關文章
相關標籤/搜索