tomcat lb cluster
session会话保持
source ip cookienginx proxy>>tomcat1>>tomcat2
nginx
vim /etc/hostsweb1.test.com 192.168.100.101 web2.test.com 192.168.100.102vim /etc/nginx/nginx.confupstream webs { ip_hash; server web1.test.com:8080; server web2.test.com:8080;}server {... location ~* \.(jsp|do)$ { proxy_pass http://webs; }}nginx -t nginx -s reload
tomcat1
catalina.sh configtestcatalina.sh stopcatalina.sh start
tomcat2
catalina.sh configtestcatalina.sh stopcatalina.sh start
客户端测试
firefox http://192.168.200.102/index.jsp
apache proxy >>tomcat1>>tomcat2
(1)apache:
mod_proxy mod_proxy_http mod_proxy_balancer tomcat: http connector (2)apache: mod_proxy mod_proxy_ajp mod_proxy_balancer tomcat: ajp connector (3)apache: mod_jk tomcat: ajp connectortomcat session绑定+负载均衡
httpd
balancerMember http://192.168.100.101:8080 loadfactor=10 route=TomcatA balancerMember http://192.168.100.102:8080 loadfactor=10 route=TomcatB ServerName www.test.com ProxyVia On ProxyRequests Off ProxyPreserveHost On Require all granted ProxyPass /status ! ProxyPass / balancer://lbcluster1/ ProxyPassReverse / balancer://lbcluster1/Require all granted
tomcata
vim index.jsp<%@ page language="java" %><%@ page import="java.util.*" %> JSP Test Page TomcatA.test.com
Session ID <% session.setAttribute("test.com","test.com"); %><%=session.getId() %> Created on <%=session.getCreationTime() %>
tomcatb
vim index.jsp<%@ page language="java" %><%@ page import="java.util.*" %> JSP Test Page TomcatB.test.com
Session ID <% session.setAttribute("test.com","test.com"); %><%=session.getId() %> Created on <%=session.getCreationTime() %>
client
firefox http://192.168.100.101
启用balancer模块内置的manager
vim vhosts.confHeader add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGEDbalancerMember http://192.168.100.101:8080 loadfactor=10 route=TomcatA balancerMember http://192.168.100.102:8080 loadfactor=10 route=TomcatB ProxySet stickysession=ROUTEID ServerName web1.test.com ProxyVia On ProxyRequests Off ProxyPreserveHost On firefox http://192.168.100.101/balancer-managerRequire all granted ProxyPass /status ! ProxyPass / balancer://lbcluster1/ ProxyPassReverse / balancer://lbcluster1/Require all granted SetHandler balancer-managerProxypass !Require all granted#Ordey Deny,Allow#Allow from all
http装载模块 tomcat-connectors
mod_jk 额外编译安装
yum install httpd-devel gcc glibc-devel -yyum groupinstall "Development Tools"which apxstar xf tomcat-connectors-1.2.46-src.tar.gzcd tomcat-connectors-1.2.46-src/native./configure --with-apxs=/usr/bin/apxsmake -j4 && make installvim /etc/httpd/conf.d/mod_jk.conf LoadModule jk_module modules/mod_jk.soJkWorkersFile /etc/httpd/conf.d/workers.propertiesJkLogFile logs/mod_jk.log JkLogLevel debug JkMount /* TomcatA JkMount /status/ stat1vim /etc/httpd/conf.d/workers.properties worker.list=TomcatA,stat1worker.TomcatA.port=8009worker.TomcatA.host=192.168.100.101worker.TomcatA.type=ajp13worker.TomcatA.lbfactor=1worker.stat1.type=statussystemctl restart httpdfirefox http://192.168.100.101/index.jspfirefox http://192.168.100.101/status/
负载均衡+session绑定
http
[root@localhost conf.d]# cat mod_jk.conf LoadModule jk_module modules/mod_jk.soJkWorkersFile /etc/httpd/conf.d/workers.propertiesJkLogFile logs/mod_jk.log JkLogLevel debug JkMount /* lbcluster1 JkMount /status/ stat1[root@localhost conf.d]# cat workers.properties worker.list=lbcluster1,stat1worker.TomcatA.port=8009worker.TomcatA.host=192.168.100.101worker.TomcatA.type=ajp13worker.TomcatA.lbfactor=1worker.TomcatB.port=8009worker.TomcatB.host=192.168.100.102worker.TomcatB.type=ajp13worker.TomcatB.lbfactor=1worker.lbcluster1.type=lbworker.lbcluster1.sticky_session=1#worker.lbcluster1.sticky_session=0 #0表示不绑定session,1表示开启worker.lbcluster1.balance_workers=TomcatA,TomcatBworker.stat1.type=status
session cluster
示例一
1、各节点配置使用deltamaanager
2、为需要使用的session cluster的webapps开启session disribution的功能 WEB-INF/web.xmltomcata
vim server.xmlcp /usr/local/tomcat/conf/web.xml /data/webapps/ROOT/WEB-INF/vim web.xml catalina.sh configtestcatalina.sh stopcatalina.sh starttail -100 /usr/local/tomcat/logs/catalina.out
tomcatb
vim server.xmlcp /usr/local/tomcat/conf/web.xml /data/webapps/ROOT/WEB-INF/vim web.xml catalina.sh configtestcatalina.sh stopcatalina.sh starttail -100 /usr/local/tomcat/logs/catalina.out
http
vim vhosts.conf#Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGEDbalancerMember http://192.168.100.101:8080 loadfactor=10 route=TomcatA balancerMember http://192.168.100.102:8080 loadfactor=10 route=TomcatB# ProxySet stickysession=ROUTEID ServerName www.test.com ProxyVia On ProxyRequests Off ProxyPreserveHost On Require all granted ProxyPass /status ! ProxyPass / balancer://lbcluster1/ ProxyPassReverse / balancer://lbcluster1/Require all granted SetHandler balancer-managerProxypass !Require all granted#Ordey Deny,Allow#Allow from all
client
firefox http://192.168.100.101
示例2
装载模块
httpcat mod_jk.conf LoadModule jk_module modules/mod_jk.soJkWorkersFile /etc/httpd/conf.d/workers.propertiesJkLogFile logs/mod_jk.log JkLogLevel debug JkMount /* lbcluster1 JkMount /status/ stat1cat workers.properties worker.list=lbcluster1,stat1worker.TomcatA.port=8009worker.TomcatA.host=192.168.100.101worker.TomcatA.type=ajp13worker.TomcatA.lbfactor=1worker.TomcatB.port=8009worker.TomcatB.host=192.168.100.102worker.TomcatB.type=ajp13worker.TomcatB.lbfactor=1worker.lbcluster1.type=lb#worker.lbcluster1.sticky_session=1worker.lbcluster1.sticky_session=0worker.lbcluster1.balance_workers=TomcatA,TomcatBworker.stat1.type=status
示例3
nginx
upstream webs { #ip_hash; server web1.test.com:8080; server web2.test.com:8080;} location / { } location ~* \.(jsp|do)$ { proxy_pass http://webs; }