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

  • 2020 年 4 月 10 日
  • 筆記

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

測試成功!