Load Balance Tomcat with Nginx and Store Sessions in Redis--reference
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:
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的更多相关文章
- Docker : Tomcat Clustering with Load Balancer (Tomcat and Nginx)
Tomcat Clustering Series Part 5 : NginX as Load Balancer - Ramki Technical Bloghttps://www.ramkitech ...
- LB(Load balance)负载均衡集群--{LVS-[NAT+DR]单实例实验+LVS+keeplived实验} 菜鸟入门级
LB(Load balance)负载均衡集群 LVS-[NAT+DR]单实例实验 LVS+keeplived实验 LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一 ...
- Using load balance for thrift servers
Software load balance .Nginx(http://nginx.org) 1.Install nginx download source code from http://ngin ...
- Flume 高可用配置案例+load balance负载均衡+ 案例:日志的采集及汇总
高可用配置案例 (一).failover故障转移 在完成单点的Flume NG搭建后,下面我们搭建一个高可用的Flume NG集群,架构图如下所示: (1)节点分配 Flume的Agent和Colle ...
- Vertica7 Native Connection Load Balance
原文链接:Vertica7 Native Connection Load Balance 在Vertica7曾经的版本号中,Vertica是通过Linux的Virtual IP来实现连接的负载均衡的, ...
- 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 ...
- Server Data Synchronization Via Linux rsync、rsync+inotify Between Load Balance Server
目录 . 远程文件同步的应用场景 . rsync+crontab . rsync+inotify 1. 远程文件同步的应用场景 在负载均衡集群的应用场景中,往往在多台web server的前端有一个提 ...
- Oracle RAC 服务器端连接负载均衡(Load Balance)
Oracle RAC服务器端的负载均衡是根据RAC中各节点的连接负荷数情况,将新的连接请求分配到负荷最小的节点上去.当数据库处于运行时,RAC中各节点的PMON进程每3秒会将各自节点的连接负荷数更新到 ...
- Oracle RAC 客户端连接负载均衡(Load Balance)
实现负载均衡(Load Balance)是Oracle RAC最重要的特性之一,主要是把负载平均分配到集群中的各个节点,以提高系统的整体吞吐能力.通常情况下有两种方式来实现负载均衡,一个是基于客户端连 ...
随机推荐
- 关于ADMM的研究(一)
关于ADMM的研究(一) 最近在研究正则化框架如何应用在大数据平台上.找到了<Distributed Optimization and Statistical Learning via the ...
- ubuntu系统下创建软件桌面快捷方式
转自ubuntu系统下创建软件桌面快捷方式 默认情况下,ubuntu会将自动安装的软件快捷方式保存在/usr/share/applications目录下,如果我们要创建桌面快捷方式,只需要右键-复制- ...
- 【UVA 10369】 Arctic Network (最小生成树)
[题意] 南极有n个科研站, 要把这些站用卫星或者无线电连接起来,使得任意两个都能直接或者间接相连.任意两个都有安装卫星设备的,都可以直接通过卫星通信,不管它们距离有多远. 而安装有无线电设备的两个站 ...
- 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- DHTMLX 前端框架 建立你的一个应用程序教程(四)--添加一个工具条toolbar
工具条例子 样本如下: 这里我们使用的是dhtmlxToolbar 组件. 添加工具栏到布局中: 1.使用attachToolbar() 方法初始化页面 添加代码到index.html中 dhtmlx ...
- [topcoder]NinePuzzle
http://community.topcoder.com/stat?c=problem_statement&pm=11225&rd=14427 http://apps.topcode ...
- 转载:java保留2位小数
转载:http://blog.csdn.net/wj_j2ee/article/details/8560132 java保留两位小数问题: 方式一: 四舍五入 double f = 11 ...
- java学习多线程之创建多线程一
现在我们有这么一个需求,就是在主线程在运行的同时,我们想做其他的任务,这个时候我们就用到了多线程.那么如何创建多线程,我们知道在系统当中qq的多线程创建是由操作系统来完成的,那么如果我们想在java当 ...
- 使用表达式避免拼接SQL语句
在SQL语句编写过程中,无论在存储过程中还是在程序中,有时为了使语句兼容全部情况与某字段的特殊情况,不得不拼接SQL字串 如下表 商品ID 类别ID 1 1 2 1 3 2 4 3 5 3 如果我们要 ...
- 实验记录贴 —— 账号同步实验 RTX 和 LDAP(AD域)
目前,公司有多个系统,RTX,邮箱(MD),OA,NC. 这些系统之间,如果要实现单点登录的话,账户肯定需要同步,或者某一种映射机制. 如果所有数据都和中央账号数据库(LDAP,这里是AD域)看齐,那 ...