Load Balance Tomcat with Nginx and Store Sessions in Redis--reference

Source: Internet
Author: User
Tags install redis nginx load balancing

An awkward title, but that's exactly what we ' re going to do. For some time, I is looking for a-a-type to push code to production systems with zero downtime and zero impact to any active Users. Surprisingly, the solution took very little time to implement. At a high level, we have Nginx load balancing, instances of Tomcat. Tomcat stores it ' s sessions in Redis. Nginx is configured as Non-sticky, since a request can go to any node in the cluster. When we need to push the new code, simply take the down any Tomcat instance. All current users would now get routed to the active instance. Since session data is externalized in Redis, active users would not be impacted. Once the inactive instance have been updated, bring it up and repeat for the other node.

We ' ll start with Nginx:
[email protected] ~]$ sudo rpm-ivh nginx-1.4.2-1.el6.ngx.x86_64.rpm

Edit/etc/nginx/nginx.conf and add the bolded text below

View Source print? 1. http { 2. upstream tomcat  { 3. server localhost:8080; 4. server localhost:8081; 5. } 6. include       /etc/nginx/mime.types; 7. default_type  application/octet-stream;

Update/etc/nginx/conf.d/default.conf and replace the location sections with this:

View Source print? 1. location / { 2. proxy_pass  http://tomcat; 3. }

Restart Nginx:
[[email protected] nginx]$ sudo service nginx restart

Next, install both instances of Tomcat. Change the server ports of the second instance, so the they does not conflict. At this point if you enter Https://localhost in your browser, you'll be taken to the default Tomcat page. However, since we have not setup sticky sessions, every request would get load balanced in round robin, which effectively m EANs It'll be creating a new session per request. You can easily see this behavior using the built in Tomcat examples. Navigate tohttp://localhost/examples/servlets/servlet/sessionexample and Refresh this page a few times and notice the Sess Ion ID changing each time. Let us fix this.

Download and install Redis. There is good documentation at Http://redis.io/download so I ' m not going into the details. Start the server and use the client program to check the IT ' s working.

Finally, we need to configure TOMCAT to store it's sessions in Redis. For this we'll be using Tomcat-redis-session-manager (Https://github.com/jcoleman/tomcat-redis-session-manager). This does not work out-of-the-box and required some tweaking. You'll need to download the source code of this project and re-build it after updating the dependent library versions. The versions I used is Commons-pool2-2.2.jar and Jedis-2.6.1.jar. Copy these jars to the Lib directory of both the Tomcat instances.

Update The versions of Commons-pool, Jedis and the Tomcat version that is using in Build.gradle of Tomcat-redis-sessi On-manager and build the project. Then copy the built Tomcat-redis-session-manager-1.2.jar to Tomcat Lib directory of each instance. Add the following to both the Tomcat ' s context.xml:

View Source print? 1. < Valve  className = "com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"  /> 2. < Manager  className = "com.orangefunction.tomcat.redissessions.RedisSessionManager" 3. host = "localhost" 4. port = "6379" 5. database = "0" 6. maxInactiveInterval = "60"  />

Restart The Tomcat instances and we ' re done. You can now see Tomcat's session in Redis. Use the previous example and try various combinations by taking the Tomcat instances up and down. The session data would remain unaffected. I even noticed so if you take down both the instances and then bring them back up, the user's existing session would be r Estored.

Thank for your time.

Http://java.dzone.com/articles/load-balance-tomcat-nginx-and

Load Balance Tomcat with Nginx and Store Sessions in Redis--reference

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.