1. 準(zhǔn)備web應(yīng)用
-
index.jsp頁面直接轉(zhuǎn)發(fā)到HelloServlet
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" %> <jsp:forward page="HelloServlet"></jsp:forward>
-
HelloServlet向頁面打印服務(wù)端口號(hào)
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(); // 用戶第一次訪問的時(shí)候,生成隨機(jī)的userId,并設(shè)置到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{ // 用戶刷新頁面的時(shí)候地来,直接獲取其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); } }
- 配置兩個(gè)Tomcat服務(wù)器,分別使用8080端口和8081端口啟動(dòng)此web項(xiàng)目
2. Nginx配置
-
查看windows的IP地址
我這里是在虛擬中啟動(dòng)的Nginx服務(wù)琳要,然后在Windows本地啟動(dòng)的兩個(gè)Tomcat服務(wù),虛擬機(jī)的通信方式為NAT,所以應(yīng)該查看VMnet8網(wǎng)卡的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. 反向代理測(cè)試
- 訪問
10.0.0.101/nginx_demo
,其中IP是nginx所在服務(wù)器的IP天揖,刷新頁面可以看到輪詢的將請(qǐng)求轉(zhuǎn)發(fā)到10.0.0.1:8080和10.0.0.1:8081
4. 使用redis解決兩個(gè)Tomcat的session中的變量無法共享的問題
4.1 使用gradle編譯tomcat-redis-session-manager源碼
下載gradle,下載地址:https://gradle.org/releases/跪帝,本文使用:gradle-6.3-all.zip,解壓后配置環(huán)境變量
下載tomcat-redis-session-manager源碼些阅,下載地址:https://github.com/jcoleman/tomcat-redis-session-manager/releases
-
將源碼包解壓伞剑,使用IDEA導(dǎo)入項(xiàng)目
-
修改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.+' }
-
等待源碼編譯
確保依賴的版已經(jīng)修改為我們改過之后的版本
-
打包
4.2 Tomcat配置
將
jedis-2.10.2.jar
、commons-pool2-2.4.3.jar
缤谎、slf4j-api-1.7.22.jar
和tomcat-redis-session-manager-1.2-tomcat-7-1.2.jar
放到tomcat的lib目錄下-
修改tomcat的
context.xml
配置文件抒倚,在Context
標(biāo)簽中添加以下內(nèi)容:<!-- 注意,標(biāo)簽是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 測(cè)試
- 啟動(dòng)redis服務(wù)
- 啟動(dòng)兩個(gè)web項(xiàng)目
測(cè)試成功!