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

 

已經有很多文章介紹使用tomcat-redis-session-manager來實現Redis存儲Tomcat的Session,實現分佈式Session管理。可是如今官方編譯的tomcat-redis-session-manager的jar包已經很舊了,基於的Redis版本也很低。這裏我把我本身從新編譯並部署它的步驟介紹一下。html

 

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

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

確保你使用的是master分支。git

 

2,下載的工程是沒辦法直接導入到eclipse中編譯的(雖然不是必須的,可是我傾向於爲它建立一個eclipse工程。這個下步再說),而是經過gradle來編譯,所以咱們須要下載gradle,地址是:github

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

下載「Binary only distribution」就能夠了。我解壓放在C:\gradle-2.13目錄下,gradle直接就能夠用了。你也能夠把路徑C:\gradle-2.13\bin放在PATH環境變量裏,會方便一些。但我沒這麼作。apache

 

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

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的片斷,如高亮的地方所示,tomcat

1)把java版本改成你對應的java版本session

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

相關文章
相關標籤/搜索