Redis3.2+Tomcat實現集群的Session管理 -- tomcat-redis-session-manager的編譯和開發部署環境搭建


 

已經有不少文章介紹使用tomcat-redis-session-manager來實現Redis存儲Tomcat的Session,實現分布式Session管理。但是現在官方編譯的tomcat-redis-session-manager的jar包已經很舊了,基於的Redis版本也很低。這里我把我自己重新編譯並部署它的步驟介紹一下。

 

1,首先,從Github上clone下載tomcat-redis-session-manager工程的源代碼,地址是:

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

確保你使用的是master分支。

 

2,下載的工程是沒辦法直接導入到eclipse中編譯的(雖然不是必須的,但是我傾向於為它創建一個eclipse工程。這個下步再說),而是通過gradle來編譯,因此我們需要下載gradle,地址是:

https://gradle.org/gradle-download/

下載“Binary only distribution”就可以了。我解壓放在C:\gradle-2.13目錄下,gradle直接就可以用了。你也可以把路徑C:\gradle-2.13\bin放在PATH環境變量里,會方便一些。但我沒這么做。

 

3,打開/tomcat-redis-session-manager/build.gradle。修改依賴包的版本,盡量使用最新的版本。

compileJava {
  sourceCompatibility = 1.7
  targetCompatibility = 1.7
}

dependencies {
  compile group: 'org.apache.tomcat', name: 'tomcat-catalina', version: '7.0.69'
  compile group: 'redis.clients', name: 'jedis', version: '2.8.1'
  compile group: 'org.apache.commons', name: 'commons-pool2', version: '2.4.2'
  //compile group: 'commons-codec', name: 'commons-codec', version: '1.10'

  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.69'
}

上面是build.gradle的片段,如高亮的地方所示,

1)把java版本改為你對應的java版本

2)把tomcat-catalina依賴的版本改為你安裝的tomcat版本,我是7.0.69

3)把jedis版本改為最新的,當前是2.8.1

4)把commons-pool2版本改為最新的2.4.2

5)也可以把testCompile下的引用也修改為最新的,但這應該不要緊了。

因為gradle也是從maven倉庫中下載jar包的,所以最好到http://mvnrepository.com/上檢查一下,以上引用的group,name,version能在倉庫找到。否則gradle還是下載不到jar包會報錯。

 

4,(可選)導入為eclipse工程。還是要打開build.gradle文件。在第一行加上apply plugin:‘eclipse’

apply plugin: 'eclipse
'
apply plugin: 'java'
apply plugin: 'maven'

 

然后執行命令

cd [root]/tomcat-redis-session-manager
c:\gradle-2.13\bin\gradle eclipse

這樣gradle會創建.project, .classpath文件,你就可以把項目導入到eclipse中了。

 

5, 你可以用eclipse進行編譯。或者用gradle編譯。

c:\gradle-2.13\bin\gradle build

編譯成功:

image

但是我編譯的時候其實是遇到一些問題的,如果你也有同樣的問題,看我下面的trouble shooting。

 

6, TroubleShooting

在以上第4步,第5步中,如果使用gradle遇到以下錯誤:

No such property: sonatypeUsername for class: org.gradle.api.publication.maven.internal.deployer.DefaultGroovyMavenDeployer

 

在build.gradle中,注釋掉下面高亮的一行。

repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
        //authentication(userName: sonatypeUsername
,
 password: sonatypePassword)
      }

參考:https://github.com/wealdtech/hawk/issues/16

 

如果遇到以下錯誤:

* What went wrong:
Execution failed for task ':signArchives'.
> Cannot perform signing task ':signArchives' because it has no configured signatory

 

在build.gradle中,注釋掉以下3行:

// signing {
//  sign configurations.archives
//}

參考:https://groups.google.com/forum/#!topic/gaelyk/WfdEDBOzIOM

 

7,在tomcat-redis-session-manager\build\libs目錄下,可以找到編譯成功的jar文件:

tomcat-redis-session-manager-2.0.0.jar

 

在C:\Users\[user]\.gradle目錄下,可以找到另外2個依賴的jar文件(或者從maven里下載)

commons-pool2-2.4.2.jar

jedis-2.8.1.jar

 

把這3個文件拷貝到\apache-tomcat-7.0.69\lib目錄

 

8,打開\apache-tomcat-7.0.69\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" />

其中的host和port是Redis的地址。(至於Redis集群的配置方式,以后再更新)

如果你是用eclipse中用tomcat進行調試,那么在你的動態網站項目下,在Project Explorer中的Servers里,找到context.xml文件,來添加以上的配置。

 

 

以上已經完成了配置,現在簡單的測試一下。

1, 在項目中添加一個簡單的jsp頁。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
SessionID:<%=session.getId()%>
</body>
</html>

 

2,啟動后,看到

image

 

3,用redis的python客戶端redis-py查看對應的key是不是創建

>>> import redis
>>> r=redis.StrictRedis(host='127.0.0.1',port=6379)
>>> r.get('289B7B17C0609FE930617ED659272C60')

 

image

 

參考

http://blog.csdn.net/chszs/article/details/42610365

http://www.cnblogs.com/weixiaole/p/4431679.html

 

Binhua Liu原創文章,轉載請注明原地址http://www.cnblogs.com/Binhua-Liu/p/5561008.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM