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. jquery 判断是否 ie6 ie7 ie8

      var isIE = !!window.ActiveXObject; var isIE6 = isIE && !window.XMLHttpRequest; var isIE8 = ...

  2. BZOJ 1711: [Usaco2007 Open]Dingin吃饭

    Description 农夫JOHN为牛们做了很好的食品,但是牛吃饭很挑食. 每一头牛只喜欢吃一些食品和饮料而别的一概不吃.虽然他不一定能把所有牛喂饱,他还是想让尽可能多的牛吃到他们喜欢的食品和饮料. ...

  3. Wordpress 网站搭建及性能监控方法详解!

    前言 说到 Wordpress,大家往往想到的是博客,其实,如今的 WordPress 已经成为全球使用量最多的开源 CMS 系统.并且,如果你有一定的技术基础稍加改动,就可以搭建出新闻网站.企业网站 ...

  4. OneAPM Cloud Test——系统性能监控神器

    2015 年 8 月,OneAPM 推出了一款系统性能监控产品--Cloud Test,产品上线以来以「两低一高」的特点迅速成为市场增长率最快的一匹黑马.「两低一高」,即低使用成本.低学习成本以及高服 ...

  5. AD10 gerber生成,及导入cam350 多图详细步骤

    Protel99转Gerber文件导入到CAM350中看为什么钻孔层偏位 这是因为你导入CAM350 时的格式没有设置正确.你用PROTEL 导出钻孔 TXT 时记住是什么格式,例如: 2:3,2:4 ...

  6. 使用 Puppet 在 Windows Azure 中配备 Linux 和 Windows 环境

     发布于 2013-12-11 作者 Ross Gardler 微软开放技术有限公司 (MS Open Tech) 很高兴地宣布发行新的 Windows Azure Puppet 模块.通过这个模 ...

  7. 请求转发:MVC设计模式、细节、请求域属性的编程实例、请求重定向和请求转发的区别

      请求转发:MVC设计模式.细节.请求域属性的编程实例.请求重定向和请求转发的区别 MVC设计模式将一次请求的响应过程分成三个功能模块(一般称之为层)来协同完成,这三个模块分别是Model(模型层) ...

  8. Linux下安装搜狗输入法

    目前的搜狗输入法 for Linux 是Linux Deepin 社区版的测试版,基于Fcitx 框架. 话不多说,直接上. 准备工作:卸载Ubuntu默认的ibus输入法: sudo apt-get ...

  9. Oracle由ID生成父ID的函数

    /*表结构*/ CREATE TABLE ly_md ( bh BYTE), mc BYTE), pym BYTE), f_bh BYTE), ch NUMBER, ID NUMBER ); INSE ...

  10. HTML页面规范

    <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title> ...