• Nginx-tomcat-redis------负载均衡以及session共享


    测试环境

      Nginx 1.10.1

      tomcat 7.0.70

      Redis-x64-3.2.100

    说明 tomcat 8 和 redis 实现session共享 有问题。

      寻找源码 发现tomcat8 中的 catalina.jar 已经没有了 org.apache.catalina.util.LifecycleSupport 这个类。

         然而这个类在redis的启动中需要加载,所以就一直报错 java.lang.NoClassDefFoundError: org/apache/catalina/util/LifecycleSupport

      lz 很无奈,只好只用了tomcat7 版本,

      这是redis 的官方说明 https://github.com/jcoleman/tomcat-redis-session-manager

      好了 进入正题

      第一步 安装 在windows  下安装 redis

      参考 : http://blog.csdn.net/renfufei/article/details/38474435

      下载地址 :https://github.com/MSOpenTech/redis

      下载或解压即可 启动命令: redis-server  redis.windows.conf  

      

      第二步,配置nginx 负载均衡 配置文件位置(D: ginx-1.10.1 ginx-1.10.1conf)

    http {
        ...
        #tomcat负载均衡
        upstream mytomcats{
        server 127.0.0.1:8080;
        server 127.0.0.1:9090;
        }
        ...
        server {
            ...
            listen       80;
            server_name  localhost;
            location / {
            proxy_pass http://mytomcats;
            }
            ...
    }

    之后启动

      第三步 配置 集群tomcat

    准备两份tomcat

    分别修改tomcat的端口号 一个为默认的8080  另外一个修改为9090

    (D:apache-tomcat-7.0.70-windows-x64apache-tomcat-7.0.70-8080confserver.xml)

    <Server port="9005" shutdown="SHUTDOWN">
    <Connector port="9090" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443" />
    <Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />
    

      

    分别在两个tomcat中的context.xml 中的 context段中添加一下内容

    (D:apache-tomcat-7.0.70-windows-x64apache-tomcat-7.0.70-8080confcontext.xml)

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

    最后分别在tomcat的lib文件下添加 redis 的相关依赖jar  commons-pool-1.5.4.jar   jedis-2.6.2.jar  tomcat-redis-session-manager1.2.jar

    tomcat 中项目zzxt  中的首页index.jsp  内容如下

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
    <!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=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	<%=session.getId()%><br>
    	<%
    		String msg = (String) session.getAttribute("msg");
    		if (null == msg) {
    			session.setAttribute("msg", "Hello!");
    		} else {
    			session.setAttribute("msg", msg + 0);
    		}
    	%>
    	<%=session.getAttribute("msg")%>
    </body>
    </html>
    

      分别启动tomcat 成功

    测试结果:

    成功,请求分别进入到两个tomcat中 进行轮训处理

      

  • 相关阅读:
    将文献的bibtex引用格式批量转换为bibitem格式参考文献
    ubuntu下webbench作网站压力测试教程【webbench安装】
    Windows10安装虚拟机VMware并且安装ubuntu16系统
    ubuntu 16.04系统下解决MySQL 的root用户重置密码问题
    elementui 中 日期时间插件 结束时间大于开始时间
    SqlDbType 与 .Net 数据类型对照表
    可用的datatable转换成List<T>
    【beyond compare4 秘钥】亲测4.1.6可用
    winform 自定义控件圆按钮插件
    net framework 4.0 wcf发布到IIS
  • 原文地址:https://www.cnblogs.com/chihirotan/p/5791401.html
Copyright © 2020-2023  润新知