Load-balancing Tomcat with Apache
作者:anotherbug 日期:2008-02-27 11:10:39
| web-application.zip(19.2 K) | |
| apache-load-balance-load-test-script.jmx(13.5 K) | |
| LoadBalancingTomcatApache.gif(9.5 K) | |
| clip_image002.gif(6.6 K) | |
| clip_image012.gif(8.1 K) | |
| clip_image004.jpg(21.9 K) | |
| clip_image006.jpg(20.0 K) | |
| clip_image006_0000.jpg(20.0 K) | |
| clip_image008.jpg(38.5 K) | |
| clip_image010.jpg(32.8 K) | |
Introduction
Tomcat is a popular application server used to host web applications. Apache is a popular web server which provides services like https encryption and decryption, URL rewriting etc. Apache can also be used a load balancer to balance load between several Tomcat application servers.
This article briefly discusses some alternatives for load balancing an application server. It discusses implementation details for setting up load balancing with Apache using ‘mod_proxy’ module. It also looks at some of the features provided by apache such as ‘server affinity’ and safe removal of node.
Downloadable web application is provided which can be used to test load balancing with Apache. Jmeter script is also provided for load testing Apache.
Setting up Apache and load balancing is the role of the System Administrator. Unless a java developer works in a small team or is setting up a test environment, he wont get involved in setting up load balancing. However, it is good to understand the principles behind load balancing. The knowledge might help a developer to fix issues in live environment.
This article discusses load balancing in the context of a web application. The application is accessed using https, requires a user to login and some user specific information is stored in session.
Background knowledge
It is assumed that the reader is familiar with the following concepts and technologies:
- Apache (Version 2.2)
- Tomcat (version 5.5.23)
- Jmeter for load testing web applications
Some important terms
Load balancing – user requests are processed by more than one server with all servers sharing the load ‘equally’
Server affinity (sticky session) – With server affinity, multiple requests from a user are processed by the same server. This is required for non clustered servers as the user session data is held on one server only and all requests from that user have to go to the server which has the session data for the user.
Transparent failover – User is not aware of a server crash. Transparent failover can be request level or session level. Transparent failover can be achieved by clustering application servers. With clustering, all the servers are the same and so the loss of a server does not interrupt the service. With load balancing alone, the user has to login again when server crashes.
Server Cluster – A group of servers which appear to be a single server to the user. Loss of a server is transparent to the user. User data is held on all servers in the cluster group. Any server can process any user request and loss of any server does not lead to interruption of service. The user does not have to login after a server failure. Since the user session data is replicated over the network to more than one server, there is a performance overhead. So clustering should be avoided unless transparent failover is required.
Scalability – measure of the ability of a system to handle increasing load without reducing response time
Response time – time taken to process a user request
Real workers – Term used by apache to refer to servers which are components of a load balanced system. The real workers do the actual work and the real worker is usually a remote host.
Virtual worker – In Apache, the load balancer is referred to as the virtual worker which delegates processing to real workers.
Load balancing algorithms
- Round robin – requests are Reduced likelihood of version conflicts
- Weighted round robin – servers of different capacity are assigned requests in proportion to their capacity (as defined by a load factor). Apache as 2 versions of this:
- Request counting algorithm – requests are delegated in round robin manner irrespective of the nature of the request
- Weighted traffic counting algorithm – Apache delegates traffic to real worker based on the number of bytes in the request
Compare Load balancing with Clustering
Both Load balancing and clustering aim to improve scalability by spreading load over more than one server. They both aim to provide horizontal scalability.
Load balancing | Clustering |
User has to login after server crash | User does not have to login after server crash. So failover is transparent to the user |
Load balancing is done by the web server or using DNS or using hardware load balancer or using Tomcat balancer web application | Clustering capability is provided by the application server. Clustering also requires load balancing |
The application servers (e.g. Tomcat) do not communicate with each other | The application servers (e.g. Tomcat) communicate with each other. |
There is minimal effect on response time when moving up from a single server to load balanced servers under the same load | Response times could deteriorate when moving to a clustered system from a single server as the session data is now replicated over the network to other servers. ? More the session data, more is the deterioration in performance compared to a single server. ? Response time also depends on number of nodes in the cluster. More the number of nodes, more is the deterioration in performance as data is replicated using TCP to every single node in cluster. This can be reduced by using UDP to replicate session data. With UDP, session data could be lost during session replication. So a user might have to login again if a server crashes |
Load balancing can be used independently of clustering | Clustering also requires load balancing but makes ‘server affinity’ redundant. It provides ‘transparent failover’ capability over load balancing at cost of decreased response time and more complex configuration |
Usually no changes are required to move an application from a single server to a load balanced set of servers | Application must meet certain criteria for it to work in a clustered environment. The user variables stored in the session must be ‘serializable’. To get good response time, only small objects must be stored in the session. |
Choices for implementing Load balancing
Hardware based load balancing
- Pros
- Fast
- Cons
- Expensive
- Proprietary
- Less flexible
Software based load balancing (e.g. Apache or Tomcat balancer)
- Pros
- Open source and free to implement with Apache and Tomcat balancer application
- Easy to configure
- More flexib
- Cons
- Lower performance compared to hardware based solution
Alternatives for software based load balancing
- Apache ‘mod_proxy’ module or ‘mod_jk’ module. However ‘mod_proxy’ is easier to configure and newer than ‘mod-jk’ module.
- Using Tomcat balancer application
- Linux virtual server
- Using DNS for load balancing
The rest of this article discusses load balancing using Apache ‘mod_proxy’ module.
Load balancing with server affinity
A simple load balanced setup which does not provide ‘server affinity’ is not suitable for stateful web applications. In stateful web applications, user state (session data) is held on one server. All further requests from that user must be processed by the same server. Hence server affinity (sticky sessions) is necessary for stateful web applications which don’t use clustering. The minimum load balanced setup is with 2 application servers and one web server (load balancer). If https decryption is required, then the same Apache server can also be used for https decryption.In a load balanced system with server affinity, all requests from user 1 go to Tomcat instance 1. This is shown below:

Apache implements server affinity by rewriting the ‘jsessionid’ sent by Tomcat to the browser. The Tomcat worker name is added to the end of ‘jsessionid’ before the ‘jsessionid’ is sent to the browser. In the next request from the same user, the Tomcat worker name is read from the ‘jsessionid’ and the request delegated to this Tomcat real worker.The ‘jsessionid’ stored as a cookie in the browser has the tomcat worker name as shown in screenshot below:

The Apache configuration (in httpd.conf) to setup Apache as a load balancer for 2 application servers is shown below:
ProxyPass /apache-load-balancing-1.0 balancer://mycluster stickysession=JSESSIONID
<Proxy balancer://mycluster>
BalancerMember ajp://tomcat1:8009/apache-load-balancing-1.0 route=tomcat1 loadfactor=50
BalancerMember ajp://tomcat2:8009/apache-load-balancing-1.0 route=tomcat2 loadfactor=50
</Proxy>
The above setup requires ‘mod_proxy’ module to be loaded.The first line sets up a reverse proxy for request ‘/apache-load-balancing-1.0’ and delegates all requests to load balancer (virtual worker) with name ‘mycluster’. Sticky sessions are enabled and implemented using cookie ‘JSESSIONID’.The ‘Proxy’ section lists all the servers (real workers) which the load balancer can use. For each participant in load balancing, we define the url (using http, ftp or ajp protocol) and give it a name which matches the name of the Tomcat engine defined in the Tomcat ‘server.xml’. The ‘loadfactor’ can be a number between 1 and 100. Set it to 50 so it can be increased or decreased dynamically later on.The tomcat ‘server.xml’ in ‘TOMCAT_FOLDER/conf’ folder should have this configuration:
....
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
...
With 2 browser sessions, the Tomcat server console and the server response show that all requests from the same user are processed by the same server.
Load balancing manager and safe removal of server node
Apache load balancing manager Apache includes a ‘balancer manager’ which can be used to check the status of the servers used for load balancing and to prepare for safe removal of a node. To enable balancer manager, add this section to Apache ‘httpd.conf’ file:
<Location /balancer-manager>
SetHandler balancer-manager
</Location>
Balancer manager requires ‘mod_proxy’ and ‘mod_proxy_balancer’ modules to be loaded.
Balancer manager will now be accessible at the url ‘/balancer-manager’.
Screenshot of this is shown below:

Safe removal of a server node When an application server (real worker) has to be taken offline for maintenance, set a very low load factor for the server due to be taken offline. Few new users will be served by that server. All existing users ‘sticking’ to this real worker will continue to be processed by this real worker. This will help reduce the number of users who will have to login again when the real worker is taken offline.After some time, the real worker is disabled and taken offline. Once it is ready to come online, the worker is enabled again in balancer manager.
Tests
About the attached web application and JMeter scriptThe attached web application contains a single servlet and 3 JSP pages. The servlet has hard coded usernames and passwords for 10 users and is used to authenticate the user and store the username in the session. The servlet also prints the username and server name of server. The login JSP (index.jsp) is used to login and the second JSP (greeting.jsp) is used to print a greeting after the user logs in. This JSP also prints the username and server name in the response. The third JSP (user_details.jsp) is used to print user details and the server name and IP address of the server name used to process the request. This JSP also prints the username and server name of the server to the console. The name of the server is setup as a context parameter in the web.xml. Change it before deploying it so that the 2 web applications have different names to make it easier to identify them
<context-param>
<param-name>serverName</param-name>
<param-value>Tomcat instance 1</param-value>
</context-param>
The attached JMeter script sets up 10 threads with each thread being used to login 1 user and request user details. Login is done once per thread/user and all subsequent requests from the user are requests to get user details.Manual test with 2 browser windows to show server affinityOpen 2 browser windows and login using different usernames. The server consoles in Tomcat will show that requests from one user will always go to a particular Tomcat instance demonstrating server affinity. See screenshot below:

Load testing with JMeterThe attached JMeter script is used to simulate a load of 10 users. The attached JMeter script has been setup to record the response. Comparing the response from the server and the server console, we can see that the load of 10 users is shared equally between 2 servers and all requests from one user are processed by the same server.

Sudden loss of one server and then restoring the serverThis can be simulated with either shutting down tomcat to simulate a server crash. This is best simulated using JMeter as a client to simulate a load of 10 users continuously requesting pages from the servers.Run the attached JMeter script and once the load test is running, take one server down. Any subsequent requests to the offline server will be redirected to the second server. When one server goes down, the user session data is lost and so all the users who have ‘affinity’ to that server will have to login again.
Reducing single point of failure
Load balancer can become the single point of failure. This can be reduced by using round robin DNS to delegate user requests to more than one load balancer. The load balancer delegates requests to more than one application server. In this scenario, if the load balancer and/or the application server goes down, the other load balancer and application servers can still provide some level of service. This is illustrated in the diagram below
:
Conclusion
Apache can be used to load balance Tomcat servers. This setup provides other useful features such as ‘Server affinity’ and safe removal of nodes for scheduled maintenance. Load balancing is recommended if transparent failover is not required. It is easy to setup load balancing and ‘server affinity’ with Apache.JMeter can be used to load test the configuration and to test the behaviour in case of a server crash.The load balancer can become the single point of failure. This can be reduced by using 2 load balancer and using round robin DNS to delegate request to more than one server.
Source Files
web application.zip
apache load balance load test script.jmx
Biography
Avneet Mangat 6 years experience in Java/J2EE. Currently working as Lead developer at Active Health Partners ( www.ahp.co.uk ). Bachelors degree in Software Engineering, Sun Certified Web developer and Java programmer, Adobe certified Flash Designer and Prince2 certified (foundation). Lead developer of open source tool DBBrowser, please see http://databasebrowser.sourceforge.net/ Outside interests include photography and travelling. Please contact me at avneet.mangat@ahp.co.uk or avneet.mangat@gmail.com for more information.
订阅
上一篇
|

文章来自:
标签: 
成人用品
iuewuqsa69
自吸泵
自吸泵
自吸泵
自吸泵
液下泵
液下泵
液下泵
油泵
油泵
油泵
管道泵
管道泵
Lotro Accounts
| Buy Lotro Accounts
Lord Of The Rings Online Power Leveling | Lord Of The Rings Online PowerLeveling
Lotro Cd Key | Lord Time Card
Lotro Gold | Lotro Gold Instant Delivery
lord of the rings online accounts | lord of the rings online accounts for sale
Lotro Power Leveling | Lotro Powerleveling
Lord Of The Rings Online Cd Key | Lord Of The Rings Online Time Card
LoTRO Gold
Lord of the Rings Online Gold
Buy Lotro Gold
Sell LoTRO Gold
LoTRO CD Key
LoTRO Europe Gold
Cheap LoTRO Accounts
Lord of the Rings Online Power Leveling
Lord of the Rings online CD Key
Cheap Lotro Gold
性保健品 性用品
性器具
北京成人用品
性用品
圣荷丰胸
丰胸
泰国圣荷
干洗机
干洗设备
干洗加盟
干洗加盟
干洗店加盟
干洗店
干洗连锁
干洗机价格
水洗机
上海保洁公司
上海保洁
上海清洗公司
上海清洗
上海地毯清洗
干洗设备
干洗加盟
干洗机
干洗店
干洗
干洗连锁
干洗店加盟
干洗机价格
网站优化
搜索引擎优化
SEO
网站优化
服装搭配
服饰搭配
美容护肤
时尚美容
服饰美容
干洗机
干洗店
干洗
干洗店加盟
干洗设备
干洗加盟
干洗连锁
干洗机价格
加盟干洗店
上海干洗机
水洗机
水洗机价格
水洗房设备
石油干洗机
上海保洁公司
上海清洗公司
上海保洁
上海清洗
上海地毯清洗
沙发清洗
干洗
干洗机
干洗设备
上海空调维修
上海管道疏通
上海空调移机
上海空调加液
上海空调回收
干洗加盟
干洗设备
干洗机
干洗
服饰
上海空调维修
上海中央空调维修
上海空调加液
上海管道疏通
进口管道疏通机
疏通机
上海空调移机
上海空调回收
上海回收中央空调
上海空调保养
上海空调清洗
上海空调安装
上海疏通阴沟
上海管道清洗
上海下水道疏通
上海保洁公司
石材翻新
石材养护
gtg08y
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
泵
泵
泵
泵
水泵
水泵
水泵
水泵
水泵
齿轮输油泵
齿轮输油泵
环保设备
环保设备
China Travel
China Tours
China Tours
beijing Tours
beijing Tours
beijing Travel
beijing Travel
shanghai Tours
shanghai Tours
shanghai Travel
shanghai Travel
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
碳雕
炭雕
活性炭
活性炭雕
空气净化产品
罗茨鼓风机
三叶罗茨鼓风机
罗茨风机
章丘罗茨鼓风机
鼓风机
三叶罗茨风机
章丘鼓风机
章丘三叶罗茨风机
铣刀
好想你枣
北京好想你枣
轴承
进口轴承
FAG轴承
NTN轴承
NSK轴承
SKF轴承
网站建设
网站推广
googel左侧优化
googel左侧推广
搜索引擎优化
仓壁振动器
给料机
分子蒸馏
短程蒸馏
薄膜蒸发器
导热油
真空泵油
飘人|飘人2008|云淡风清
园林机械
胎毛笔
手足印
婴儿纪念品
婴幼儿纪念品
草坪机
油锯
小型收割机
收割机
割灌机
割草机
电动喷雾器
地钻
采茶机
婚纱
北京婚纱
婚纱礼服
北京婚纱店
个性婚纱
礼服
北京礼服
礼服定制
礼服出租
意大利留学
留学意大利
WoW Gold
Buy WoW Accounts
Buy WoW Gold
WoW Power Leveling
WoW Honor Points Leveling
WoW Skill Leveling
WoW Reputation Leveling
WoW Leveling Packages
WoW CD Key
WoW Game Time Card
WOW Gold Europe
Buy WoW EU Gold
WoW Accounts For Sale
Cheap WoW Power Leveling
Lineage 2 Adena
Lineage2 Adena
Lineage 2 Accounts
L2 Account
Lineage 2 Power Leveling
Lineage 2 CD Key
Lineage 2 Time Card
Lotro Gold
Lotro Gold
LoTRO Accounts
Lotro Power Leveling
Lotro Cd Key
Lotro Time Card
LoTRO EU Gold
Cheap LoTRO Accounts
Lord of the Rings Online Power Leveling
Cheap WoW Gold
Wow gold
Wow gold
Wow powerleveling
Wow honor leveling
Wow cd key
Wow accounts
World of warcraft gold
Wow PTR Patch 2.3.2
Cheapest wow gold
Buy wow gold
Lineage 2 adena
Lineage 2 accounts
Lineage 2 powerleveling
Lotro gold
Lotro powerleveling
WoW Gold Reviews
WoW Burning Crusade
World of warcraft gold
Buy wow gold
Sell world of warcraft gold
Cheapest wow gold
World of warcraft gold
Buy wow account
Buy world of warcraft account
Wow power leveling
World of warcraft power leveling
Wow Honor Points Leveling
Wow Pvp Honor Leveling
Wow skill leveling
Wow Honor Leveling
Wow Cd Key
Wow Game Time Card
Wow gold for sale
Buy wow gold
World of warcraft gold buying
Wow accounts
Cheap wow power leveling
World Of Warcraft Cd Key
World Of Warcraft Time Card
Lineage 2 adena
Cheap lineage2 adena
Buy l2 adena
L2 adena
Lineage2 adena
Lineage 2 accounts
L2 accounts
Lineage 2 power leveling
Cheap Lotro Gold
Lord Of The Rings Online Gold
Buy Lotro Accounts
Lord Of The Rings Online Power Leveling
Lotro Cd Key
Lord Time Card
Cheapest Lotro Gold
Lord Of The Ring Online Gold
Lotro Gold
Lord of the rings online accounts
Lotro Power Leveling
Lord Of The Rings Online Cd Key
Lord Of The Rings Online Time Card
World of Warcraft Gold
WoW Gold
Lotro Gold
Lineage 2 Adena
Lineage2 Adena
Lineage 2 Accounts
Lineage 2 Power Leveling
Lineage 2 CD Key
Buy WoW Gold
WoW Accounts
WOW Power
LevelingWoW CD Key
WOW Gold Europe
Buy WoW EU Gold
WoW EU Accounts
World of Warcraft Powerleveling
World of Warcraft CD Key
Buy LoTRO US Gold
LoTRO Accounts
Lotro Power Leveling
LoTRO US CD Key
LoTRO Europe Gold
World of Warcraft Gold
Buy LoTRO EU Gold
Cheap LoTRO Accounts
WoW Gold
Age of Conan time card
Age of Conan cd key
Age of Conan Gold
Buy Age of Conan Gold
Age of Conan Accounts
Age of Conan Power Leveling
Cheap Age of Conan Gold
AoC Gold
AoC Accounts
Cheap Age of Conan Power Leveling
Age of Conan Gold
Cheap Age of Conan Gold
Age of Conan Accounts
Aoc Power Leveling
Age of Conan CD Key
Aoc Gold
Age of Conan Gold Europe
Aoc Accounts
Age of Conan Power Leveling
Age of Conan Time Card
高低温试验箱
振动试验台
恒温恒湿试验箱
恒温恒湿箱
恒温箱
振动台
盐雾箱
老化台
盐雾试验箱
高低温箱
低温试验箱
振动试验机
合同纠纷
房产纠纷
劳动纠纷
房地产律师
制氮机
在职研究生
液体壁纸
清水模板
冷弯型钢
牛仔服
牛仔服装厂
牛仔休闲
牛仔裤
牛仔品牌
牛仔专卖店
节能胶带机
胶带机价格
虹吸
虹吸雨水
虹吸排水
有压流
同层排水
恒温器
马达保护器
热保护器
温度开关
温控器
过流保护器
真空泵
藏獒
全自动表面张力仪/界面张力仪
舞台设计
烧烤网
振动筛网
拖链
光纤熔接机
光缆监测系统
时光域反射仪
不锈钢反应釜
风机
捏合机
制氮机
代孕
代孕妈妈
货架
货架
搬家公司
管道疏通
肺癌
肝癌
婚庆
写字楼出租
写字楼
货架
Cheap Warhammer gold
Warhammer Online gold
WAR Gold
Buy Warhammer Gold
Warhammer Online Gold
Warhammer Gold for sale
Warhammer Accounts
Buy Warhammer Accounts
Cheap Warhammer Accounts
Warhammer Power leveling
WAR Power leveling
Warhammer Online Power Leveling
Warhammer CD Key
WAR CD Key
Warhammer Online CD Key
Warhammer Game Time Card
xll19880204xll
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
留学荷兰
新西兰留学
韩国留学
留学韩国
澳洲留学
留学澳洲
意大利留学
buy wow account
wow account
wow accounts
buy wow accounts
buy wow account
wow account
wow accounts
buy wow accounts
wow account
wow account
wow account
wow account
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
wow gold
产后恢复
产后恢复身材
产后修复
产后修复身材
针灸减肥的原理
针灸减肥
祛痘祛印
祛痘祛斑
针灸丰胸
呼死你
呼死你软件
短信群发软件
优惠的打折机票|国际机票|上海机票|飞机票|特价机票为您提供优质服务。详情请进http://www.01fly.com.cn。
专业的搬家公司|上海搬家公司|上海搬家|搬场公司|上海搬场公司|上海搬场公司为您提供优质的搬家,搬场服务。详情请进http://www.shdz-bc.cn。
专业的大众搬场公司|上海大众搬场|大众搬场公司为您提供优质的搬家,搬场服务。详情请进http://www.dz-bc.com.cn。
上海中铁公司为您提供上海货运公司|上海货运|物流公司|上海物流公司|上海物流竭诚欢迎各企业及新老客户前来洽谈合作事宜。详情请进http://www.didicars.cn,本公司全天候24小时为您服务。