美文网首页
002.Nginx反向代理案例以及tomcat-redis-se

002.Nginx反向代理案例以及tomcat-redis-se

作者: CoderJed | 来源:发表于2020-03-30 12:30 被阅读0次

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项目

测试成功!

相关文章

网友评论

      本文标题:002.Nginx反向代理案例以及tomcat-redis-se

      本文链接:https://www.haomeiwen.com/subject/wlivyhtx.html