An awkward title, but that’s exactly what we’re going to do. For some time, I was looking for a way 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 two 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 new code, simply take down any Tomcat instance. All current users will now get routed to the active instance. Since session data is externalized in Redis, active users will not be impacted. Once the inactive instance has been updated, bring it up and repeat for the other node.

We’ll start with Nginx:
[raoul@raoul-wp ~]$ sudo rpm -ivh nginx-1.4.2-1.el6.ngx.x86_64.rpm

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

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 section with this:

1.location / {
2.proxy_pass  http://tomcat;
3.}

Restart nginx:
[raoul@raoul-wp nginx]$ sudo service nginx restart

Next, install two instances of Tomcat. Change the server ports of the second instance, so that they do not conflict. At this point if you enter https://localhost in your browser, you will be taken to the default tomcat page. However, since we have not setup sticky sessions, every request will get load balanced in round robin, which effectively means it will 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 Session 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 that 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 did not work out-of-the-box and required some tweaking. You will need to download the source code of this project and re-build it after updating the dependent library versions. The versions I used are 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 you are using in build.gradle of tomcat-redis-session-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:

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 will remain unaffected. I even noticed that if you take down both the instances and then bring them back up, the user’s existing session will be restored.

Thank you for your time.

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

Load Balance Tomcat with Nginx and Store Sessions in Redis--reference的更多相关文章

  1. Docker : Tomcat Clustering with Load Balancer (Tomcat and Nginx)

    Tomcat Clustering Series Part 5 : NginX as Load Balancer - Ramki Technical Bloghttps://www.ramkitech ...

  2. LB(Load balance)负载均衡集群--{LVS-[NAT+DR]单实例实验+LVS+keeplived实验} 菜鸟入门级

    LB(Load balance)负载均衡集群 LVS-[NAT+DR]单实例实验 LVS+keeplived实验 LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一 ...

  3. Using load balance for thrift servers

    Software load balance .Nginx(http://nginx.org) 1.Install nginx download source code from http://ngin ...

  4. Flume 高可用配置案例+load balance负载均衡+ 案例:日志的采集及汇总

    高可用配置案例 (一).failover故障转移 在完成单点的Flume NG搭建后,下面我们搭建一个高可用的Flume NG集群,架构图如下所示: (1)节点分配 Flume的Agent和Colle ...

  5. Vertica7 Native Connection Load Balance

    原文链接:Vertica7 Native Connection Load Balance 在Vertica7曾经的版本号中,Vertica是通过Linux的Virtual IP来实现连接的负载均衡的, ...

  6. eclipse Run On Server 异常:could not load the Tomcat Server configuration at Servers\tomcat V5.0 Sertomcat

    eclipse Run On Server 异常:could not load the Tomcat Server configuration at Servers\tomcat V5.0 Serto ...

  7. Server Data Synchronization Via Linux rsync、rsync+inotify Between Load Balance Server

    目录 . 远程文件同步的应用场景 . rsync+crontab . rsync+inotify 1. 远程文件同步的应用场景 在负载均衡集群的应用场景中,往往在多台web server的前端有一个提 ...

  8. Oracle RAC 服务器端连接负载均衡(Load Balance)

    Oracle RAC服务器端的负载均衡是根据RAC中各节点的连接负荷数情况,将新的连接请求分配到负荷最小的节点上去.当数据库处于运行时,RAC中各节点的PMON进程每3秒会将各自节点的连接负荷数更新到 ...

  9. Oracle RAC 客户端连接负载均衡(Load Balance)

    实现负载均衡(Load Balance)是Oracle RAC最重要的特性之一,主要是把负载平均分配到集群中的各个节点,以提高系统的整体吞吐能力.通常情况下有两种方式来实现负载均衡,一个是基于客户端连 ...

随机推荐

  1. 推荐一款不错的GIF录制软件附带.NET源码

    源码下载地址:http://www.51aspx.com/Code/ScreenToGif10

  2. 解决CAS单点登录出现PKIX path building failed的问题

    在一次调试中,出现了这个错误: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderExceptio ...

  3. Android 获取SDCard中某个目录下图片

    本文介绍Android开发中如何获取SDCard中某目录下的所有图片并显示出来,下面的我们提供的这个函数是通用的,只要提供路径就可以查询出该目录下所有图片的路径信息,并保存到一个List<Str ...

  4. zabbix 通过key 获取

    zabbix:/root# zabbix_get -s 192.168.2.224 -k "perf_counter[\Processor(_Total)\% Processor Time] ...

  5. 产品设计中先熟练使用铅笔 不要依赖Axure

    在互联网产品领域,Axure已成为产品经理.产品设计师以及交互设计师的必备工具,从某种程度讲,Axure帮助我们建立低保真模型,便于与用户的需求验证,也帮助我们构思交互细节,使前端和开发人员更容易理解 ...

  6. hadoop2.2编程: 重写comparactor

    要点: 类型比较在hadoop的mapreduce中非常重要,主要用来比较keys; hadoop中的RawComparator<T>接口继承自java的comparator, 主要用来比 ...

  7. c#生成注册码的两种方法(mac地址与IP地址)

    using System; using System.Management; using System.Security.Cryptography; using System.IO; using Sy ...

  8. BZOJ_1015_星球大战_[JSOI2008]_(并查集)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1015 n 个点,被 m 条边相连.进行k次删点操作,问第一次操作前和每次操作后的集合数(直接或 ...

  9. 【Android 复习】:Android五种布局的使用方法

    ---恢复内容开始--- 在Android布局中,有五种常用的布局,下面我们就来学习一下这几种布局的使用方式 1) 线性布局:LinearLayout 2) 帧布局:  FrameLayout 3)  ...

  10. HDU-2500 做一个正气的杭电人

    http://acm.hdu.edu.cn/showproblem.php?pid=2500 做一个正气的杭电人 Time Limit: 1000/1000 MS (Java/Others)    M ...