前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >002.Nginx反向代理案例以及tomcat-redis-session-manager的使用

002.Nginx反向代理案例以及tomcat-redis-session-manager的使用

作者头像
CoderJed
发布2020-04-09 22:43:20
6060
发布2020-04-09 22:43:20
举报

1. 准备web应用

  • index.jsp页面直接转发到HelloServlet
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
<jsp:forward page="HelloServlet"></jsp:forward>
 
  • HelloServlet向页面打印服务端口号
public class HelloServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session= request.getSession();
        int port = request.getLocalPort();

        // 用户第一次访问的时候,生成随机的userId,并设置到session域中
        if(session.getAttribute("userId") == null){
            String userId = String.valueOf(new Random().nextInt(100)) ;
            session.setAttribute("userId", userId);
            response.getWriter().append("<h1>Hello, " + userId + ", this is " + port + " port</h1>");
        }else{
            // 用户刷新页面的时候,直接获取其userId
            String userId = (String) session.getAttribute("userId");
            response.getWriter().append("Welcome back, " + userId +", this is " + port +" port") ;
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
  • 配置两个Tomcat服务器,分别使用8080端口和8081端口启动此web项目

2. Nginx配置

  • 查看windows的IP地址 我这里是在虚拟中启动的Nginx服务,然后在Windows本地启动的两个Tomcat服务,虚拟机的通信方式为NAT,所以应该查看VMnet8网卡的IP地址:
  • nginx配置
http {
    
    upstream myserver {
        # 改为windows的IP
        server 10.0.0.1:8080 weight=1;
        server 10.0.0.1:8081 weight=1;
    }
    
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://myserver;
            proxy_connect_timeout 10;
        }
        ......
    }
    
}
  • 重新加载配置
/usr/local/nginx/sbin/nginx -s reload

3. 反向代理测试

  • 访问10.0.0.101/nginx_demo,其中IP是nginx所在服务器的IP,刷新页面可以看到轮询的将请求转发到10.0.0.1:8080和10.0.0.1:8081

4. 使用redis解决两个Tomcat的session中的变量无法共享的问题

4.1 使用gradle编译tomcat-redis-session-manager源码

  • 修改build.gradle文件,将tomcat版本修改为你自己的版本,但要<8.5,将jedis的版本修改为<3.0的最新版本
apply plugin: 'java'
version = '1.2'

repositories {
  mavenCentral()
}

dependencies {
  compile group: 'org.apache.tomcat', name: 'tomcat-catalina', version: '7.0.99'
  compile group: 'redis.clients', name: 'jedis', version: '2.10.2'
  // compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
  // testCompile group: 'junit', name: 'junit', version: '4.+'
}
 
  • 等待源码编译

确保依赖的版已经修改为我们改过之后的版本

  • 打包

4.2 Tomcat配置

  • jedis-2.10.2.jarcommons-pool2-2.4.3.jarslf4j-api-1.7.22.jartomcat-redis-session-manager-1.2-tomcat-7-1.2.jar放到tomcat的lib目录下
  • 修改tomcat的context.xml配置文件,在Context标签中添加以下内容:
<!-- 注意,标签是Valve,而不是Value,类名也是RedisSessionHandlerValve而不是RedisSessionHandlerValue -->  
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<!-- redis的IP和端口修改为你自己的 -->
<Manager className="com.radiadesign.catalina.session.RedisSessionManager" host="10.0.0.101" port="6379" database="0" maxInactiveInterval="60" />
 

4.3 测试

  • 启动redis服务
  • 启动两个web项目

测试成功!

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 准备web应用
  • 2. Nginx配置
  • 3. 反向代理测试
  • 4. 使用redis解决两个Tomcat的session中的变量无法共享的问题
    • 4.1 使用gradle编译tomcat-redis-session-manager源码
      • 4.2 Tomcat配置
        • 4.3 测试
        相关产品与服务
        云数据库 Redis
        腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档