CentOS7 Nginx+Redis+Tomcat集羣實現session保持和共享

1、環境相關

    操做系統:CentOS7html

    軟件版本:nginx/1.9.15  Apache Tomcat/7.0.69  redis 3.20
java

    網絡環境:Nginx 192.168.5.250  Tomat1:192.168.5.251 Tomcat2:192.168.5.252  Redis:192.168.5.253nginx

    相關軟件網盤地址:http://pan.baidu.com/s/1skBIlXVgit

2、相關軟件配置

   一、Nignx編譯與添加自啓腳本:http://my.oschina.net/huangweibin/blog/665239github

Nginx 192.168.5.250 
 
[root@nginx ~]# vim /etc/nginx/nginx.conf
 
http上下文 
upstream tomcat {
   server 192.168.5.251:8080;
   server 192.168.5.252:8080;
}
server上下文
location / {
 proxy_pass tomcat;
}

  二、Reids編譯與添加自啓腳本:http://my.oschina.net/huangweibin/blog/671408web

  三、Tomcat編譯與添加自啓腳本:CentOS7 編譯Tomcat並添加自啓動腳本(注意:本文Tomcat要求版本爲tomcat-7.x)redis

       http://my.oschina.net/huangweibin/blog/668412shell

4、編譯tomcat-redis-session-manager

     1.官方下載地址  https://github.com/jcoleman/tomcat-redis-session-managervim

     2.配置編譯環境Gradle:http://my.oschina.net/huangweibin/blog/671419api

[root@localhost ~]# unzip tomcat-redis-session-manager-master.zip 
[root@localhost ~]# cd tomcat-redis-session-manager-master/ 
[root@localhost tomcat-redis-session-manager-master]# vim build.gradle 

註釋如下內容:  
//signing {
//  sign configurations.archives
//} 
      //repository(url: "
https://oss.sonatype.org/service/local/staging/deploy/maven2/
") {
       // authentication(userName: sonatypeUsername, password: sonatypePassword)
      //}
 
增長依賴包的輸出腳本
task copyJars(type: Copy) {
  from configurations.runtime
  into 'dist'  
}

  3.執行gradle命令構建源碼,編譯輸出tomcat-redis-session-manager-master 及依賴jar包(其餘Tomcat只須要複製jar文件到tomcat目錄)

[root@localhost tomcat-redis-session-manager-master]# gradle build -x test  copyJars
[root@localhost tomcat-redis-session-manager-master]# mkdir /tmp/jar
[root@localhost tomcat-redis-session-manager-master]# cp -a build/libs/*.jar /tmp/jar/ && cp -a dist/*.jar /tmp/jar/
 
[root@localhost jar]# ls /tmp/jar
commons-pool2-2.2.jar
jedis-2.5.2.jar
tomcat-annotations-api-7.0.27.jar
tomcat-api-7.0.27.jar
tomcat-catalina-7.0.27.jar
tomcat-juli-7.0.27.jar
tomcat-redis-session-manager-master-2.0.0.jar
tomcat-redis-session-manager-master-2.0.0-javadoc.jar
tomcat-redis-session-manager-master-2.0.0-sources.jar
tomcat-servlet-api-7.0.27.jar
tomcat-util-7.0.27.jar

[root@localhost jar]# cp -a /tmp/jar/* /usr/local/tomcat/lib/

5、配置Tomcat參數並建立測試頁面(其餘Tomcat配置相似)

[root@localhost jar]# vim /usr/local/tomcat/conf/context.xml 

<Context>
      <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
      <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
           host="192.168.5.253"
           port="6379"
           database="0"
           maxInactiveInterval="60" />
</Context> 

[root@localhost jar]# mkdir -pv /usr/local/tomcat/webapps/session/WEB-INF/{classes,lib}  
 
[root@localhost jar]# vim /usr/local/tomcat/webapps/session/index.jsp
<%@ page language="java" %>
<html>
  <head><title>Tomcat1</title></head>
  <body>
    <h1><font color="red">Tomcat1.example.com</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("Tomcat.example.com","Tomcat.example.com"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>

6、測試

相關文章
相關標籤/搜索