CentOS7 Nginx+Redis+Tomcat集群实现session保持和共享

一、环境相关

    操作系统:CentOS7

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

    网络环境:Nginx 192.168.5.250  Tomat1:192.168.5.251 Tomcat2:192.168.5.252  Redis:192.168.5.253

    相关软件网盘地址:http://pan.baidu.com/s/1skBIlXV

二、相关软件配置

   1、Nignx编译与添加自启脚本:http://my.oschina.net/huangweibin/blog/665239

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;
}

  2、Reids编译与添加自启脚本:http://my.oschina.net/huangweibin/blog/671408

  3、Tomcat编译与添加自启脚本:CentOS7 编译Tomcat并添加自启动脚本(注意:本文Tomcat要求版本为tomcat-7.x)

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

四、编译tomcat-redis-session-manager

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

     2.配置编译环境Gradle:http://my.oschina.net/huangweibin/blog/671419

[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/

五、配置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>

六、测试

CentOS7 Nginx+Redis+Tomcat集群实现session保持和共享_第1张图片

CentOS7 Nginx+Redis+Tomcat集群实现session保持和共享_第2张图片

你可能感兴趣的:(CentOS7)