前言:相比用memcached作session共享,redis其實方便不少,也不用考慮jar包版本的問題,memcached作共享的jar包實在太多了,版本不對還會出各類錯誤。git
準備工做:github
1,安裝好redisredis
可參考:Redis之安裝篇(windows 7 64位)apache
2,準備好兩個tomcat用來測試windows
3,安裝好Nginx,並按照上一篇進行相關配置,這裏再也不作配置說明了tomcat
下面進入正題,詳細講解配置步驟:session
1,打開 https://github.com/jcoleman/tomcat-redis-session-manager ,下載源碼app
2,新建maven工程,將剛下載的源碼拷貝進maven工程中,須要注意jdk的編譯版本,我這裏使用的是1.7的版本maven
3,修改pom.xml文件memcached
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.viking</groupId> <artifactId>redis-session</artifactId> <version>0.0.1-SNAPSHOT</version> <name>redis-session Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>jdk.tools</groupId> <artifactId>jdk.tools</artifactId> <version>1.7</version> <scope>system</scope> <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-catalina</artifactId> <version>7.0.26</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.0</version> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>utf8</encoding> </configuration> </plugin> </plugins> </build> </project>
4,用maven命令打包,生成對應的jar包 redis-session.jar
5,將 redis-session.jar以及依賴包commons-pool2-2.3.jar、jedis-2.8.0.jar(選擇你用的jar包版本)拷貝到tomcat的lib目錄裏,一共須要拷貝這三個jar包,相比memcached的共享jar包,實在太精簡了。
6,配置tomcat的context.xml(和memcached的共享配置很相似)
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="localhost" port="6379" database="0" maxInactiveInterval="60" />
7,在tomcat底下建立測試頁面(可參考上篇)
8,啓動tomcat,訪問http://localhost 查看效果