nginx+redis+4个tomcat 负载均衡
1,先配置nginx ,如果80接口被占用,且80 的端口又惹不起,参考:https://www.cnblogs.com/xiaohu1218/p/10267602.html
2,下载redis,并配置。、
3,下载tomcat ,两个tomcat8,两个tomcat9。并修改端口号(如不在同一个电脑,可以不用需改),我是同一个电脑,为了方便,标记如下:
<h3>tomcatX:</h3> // X 1,2,3,4 <% HttpSession session = request.getSession(); %> <%= session.getId() %>
4,修改tomcat中%Home%\conf 中context.xml ,加入
<Manager className="tomcat.request.session.redis.SessionManager" host="localhost" port="6379" database="0" maxInactiveInterval="60" />
5,将 这四个jar包,加入到%home%lib中,
1. jedis.jar 2. commons-pool2.jar 3. commons-logging.jar 4.tomcat-cluster-redis-session-manager.jar
或者:
1. Move the downloaded jars to tomcat/lib directory * tomcat/lib/ 2. Add tomcat system property "catalina.base" * catalina.base="TOMCAT_LOCATION" 3. Extract downloaded package (tomcat-cluster-redis-session-manager.zip) to configure Redis credentials in redis-data-cache.properties file and move the file to tomcat/conf directory * tomcat/conf/redis-data-cache.properties 4. Add the below two lines in tomcat/conf/context.xml <Valve className="tomcat.request.session.redis.SessionHandlerValve" /> <Manager className="tomcat.request.session.redis.SessionManager" /> 5. Verify the session expiration time (minutes) in tomcat/conf/web.xml <session-config> <session-timeout>60<session-timeout> <session-config>
配置redis-data-cache.properties
#-- Redis data-cache configuration
#- redis hosts ex:
redis.hosts=127.0.0.1:6379
#- redis password (for stand-alone mode)
#redis.password=
#- set true to enable redis cluster mode
redis.cluster.enabled=false
#- redis database (default 0)
redis.database=0
#- redis connection timeout (default 2000)
redis.timeout=2000
6,启动四个tomcat
7,修改niginx中的config/nginx.conf,
加入
#服务器集群 upstream localhost { server localhost:8080 weight=1; server localhost:8081 weight=2; server localhost:8082 weight=3; server localhost:8180 weight=4; } server { listen 88; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://localhost; root html; index index.html index.htm; }
8,启动nginx,即可。