Use nginx server for load balancing

The prerequisite of using Nginx is to implement tomcat cluster session sharing

The first step is to configure the nginx.conf load balancing module to select a host from the list of back-end hosts defined by the “upstream” directive. Nginx uses the load balancing module to find a host and then uses the upstream module to interact with that host.

user root; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; Upstream {server 192.168.25.100:8080 weight=1 max_fails=1 fail_timeout=20; Server 192.168.25.101:8080 weight = 1; keepalive 16; Server {listen 8888; server_name localhost; location / { root html; index index.html; proxy_pass http://tomcatproxy; }}}Copy the code

Step 2 Copy the jar package required by Tomcat to ${TOMCAT_HOME}/lib

  • tomcat-redis-session-manager.jar
  • Jedis – 2.5.2. Jar
  • Commons – pool2-2.2. The jar

Modify the RedissessionManager.java initializeSerializer method (to support Tomcat8)

Context context=this.getContext(); if(context! =null) { loader =context.getLoader(); }Copy the code

Step 3: Modify the tomcat/conf/context. XML configuration file to add Valve and Manager configurations. Add the following configuration to the context node of context. XML:

<Context> <WatchedResource>WEB-INF/web.xml</WatchedResource> <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource> <Valve className="com.s.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager The className = "com. S.t omcat. Redissessions. RedisSessionManager" host = "127.0.0.1 port =" 6379 "database =" 0 "password =" 123456" maxInactiveInterval="60" /> </Context>Copy the code

Step 4 Verify that the webapps\ROOT\index.jsp change is added:

<%=request.getSession().getid () %>Copy the code

Through the nginx forwarding the same request to realize the session is the same (refresh) http://192.168.25.100:8888/