Tomcat Cluster负载均衡

author:JevonWei

版权声明:原创作品


Tomcat Cluster负载均衡

环境

tomcatA    172.16.253.108
tomcatB 172.16.253.105
代理服务器 172.16.253.191

Tomcat集群

Tomcat A

[root@tomcatA ~]#  yum -y install java-1.8.0-openjdk*
[root@tomcatA ~]# yum -y install tomcat-lib tomcat tomcat-webapps tomcat-docs-webapp tomcat-admin-webapps
[root@tomcatA ~]# iptables -F
[root@tomcatA ~]# setenforce 0
[root@tomcatA ~]# mkdir -pv /data/webapps/ROOT/{classes,lib,WEB-INF}
[root@tomcatA ~]# vim /etc/tomcat/server.xml
<Engine name="Catalina" defaultHost="www.jevon1.com"> \\修改默认主机为www.jevon1.com
<Host name="www.jevon1.com" appBase="/data/webapps" unpackWARs="true" autoDeploy="true" >
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="jevon_access_log" suffix=".log"
pattern="%h %l %u %t &quot;%r&quot; %s %b" />
</Host>
[root@tomcatA ~]# vim /data/webapps/ROOT/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA.magedu.com</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("magedu.com","magedu.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
[root@tomcatA ~]# systemctl start tomcat
[root@tomcatA ~]# ss -ntl

Tomcat B

[root@tomcatB ~]#  yum -y install java-1.8.0-openjdk*
[root@tomcatB ~]# yum -y install tomcat-lib tomcat tomcat-webapps tomcat-docs-webapp tomcat-admin-webapps
[root@TomcatB ~]# iptables -F
[root@TomcatB ~]# setenforce 0
[root@tomcatB ~]# mkdir -pv /data/webapps/ROOT/{classes,lib,WEB-INF}
[root@tomcatB ~]# vim /etc/tomcat/server.xml
<Engine name="Catalina" defaultHost="www.jevon2.com"> \\修改默认主机为www.jevon21.com
<Host name="www.jevon2.com" appBase="/data/webapps" unpackWARs="true" autoDeploy="true" >
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="jevon_access_log" suffix=".log"
pattern="%h %l %u %t &quot;%r&quot; %s %b" />
</Host> [root@TomcatB ~]# vim /data/webapps/ROOT/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="blue">TomcatB.magedu.com</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("magedu.com","magedu.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
[root@tomcatB ~]# systemctl start tomcat
[root@tomcatB ~]# ss -ntl

Nginx代理

代理服务器

[root@danran ~]# vim /etc/hosts
172.16.253.108 www.jevon1.com
172.16.253.105 www.jevon2.com
[root@danran ~]# yum -y install nginx
[root@danran ~]# vim /etc/nginx/nginx.conf
http {
upstream appsrvs {
server www.jevon1.com:8080;
server www.jevon2.com:8080;
}
}
server {
listen 80;
server_name www.danran.com;
index index.jsp index.html;
root /usr/share/nginx/html;
location / {
proxy_pass http://appsrvs/;
}
}
[root@danran ~]# nginx -t
[root@danran ~]# nginx start nginx

client

elinks www.danran.com

Tomcat Cluster负载均衡

elinks www.danran.com

Tomcat Cluster负载均衡

Haproxy代理

代理服务器

[root@danran ~]# vim /etc/hosts
172.16.253.108 www.jevon1.com
172.16.253.105 www.jevon2.com
[root@httpd ~]# yum -y install haproxy
[root@httpd ~]# vim /etc/haproxy/haproxy.cfg
frontend tomcat
bind *:80
default_backend appsrvs
backend appsrvs
balance roundrobin
server app1 www.jevon1.com:8080 check
server app2 www.jevon2.com:8080 check
listen stats
bind *:9009
stats enable
[root@httpd ~]# systemctl start haproxy
[root@httpd ~]# ss -ntl

client

elinks www.danran.com

Tomcat Cluster负载均衡

elinks www.danran.com

Tomcat Cluster负载均衡

http://www.danran.com:9009/haproxy?stats

Tomcat Cluster负载均衡

Htppd代理

代理服务器

[root@httpd ~]# yum -y install httpd
[root@httpd ~]# httpd -M \\确保以下三个模块
proxy_balancer_module (shared)
proxy_module (shared)
proxy_http_module (shared)

proxy_ajp_module (shared) [root@httpd ~]# vim /etc/httpd/conf.d/tomcat-http.conf
<proxy balancer://appsrvs> \\定义balancer集群组
BalancerMember http://www.jevon1.com:8080
BalancerMember http://www.jevon2.com:8080
ProxySet lbmethod=byrequests \\调度算法byrequests为轮询;bybusyness根据繁忙程度调度,相当于least-connest;bytraffic根据流量调度
</Proxy>
<VirtualHost *:80>
ServerName www.danran.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://appsrvs/
ProxyPassReverse / balancer://appsrvs/
<Location />
Require all granted
</Location>
</VirtualHost>
[root@httpd ~]# httpd -t
[root@httpd ~]# systemctl start httpd

client

elinks www.danran.com

Tomcat Cluster负载均衡

elinks www.danran.com

Tomcat Cluster负载均衡

httpd的AJP协议代理

代理服务器

[root@httpd ~]# vim /etc/httpd/conf.d/tomcat-ajp.conf
<proxy balancer://appsrvs> \\定义balancer集群组
BalancerMember ajp://www.jevon1.com:8009 \\tomcat服务的8080端口不支持ajp协议,8009端口支持aip协议
BalancerMember ajp://www.jevon2.com:8009
ProxySet lbmethod=byrequests \\调度算法byrequests为轮询;bybusyness根据繁忙程度调度,相当于least-connest;bytraffic根据流量调度
</Proxy>
<VirtualHost *:80>
ServerName www.danran.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://appsrvs/
ProxyPassReverse / balancer://appsrvs/
<Location />
Require all granted
</Location>
</VirtualHost>
[root@httpd ~]# httpd -t
[root@httpd ~]# systemctl start httpd

client

elinks www.danran.com

Tomcat Cluster负载均衡

elinks www.danran.com

Tomcat Cluster负载均衡

实现Tomcat集群的session粘性

Tomcat A

[root@danran ~]# vim /etc/httpd/conf.d/tomcat-ajp.conf
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<proxy balancer://appsrvs>
BalancerMember ajp://www.jevon1.com:8009 route=tcA \\route=tcA为cookie标识,tcA为自定义信息
BalancerMember ajp://www.jevon2.com:8009 route=tcB
ProxySet lbmethod=byrequests
ProxySet stickysession=ROUTEID \\ROUTEID同header上设置的Sookie信息一致;
</Proxy>
<VirtualHost *:80>
ServerName www.danran.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://appsrvs/
ProxyPassReverse / balancer://appsrvs/
<Location />
Require all granted
</Location>
</VirtualHost> balancer的状态界面
<Location /balancer-manager> 管理接口的状态界面的自定义URL
SetHandler balancer-manager 启用的内部处理器为balancer-manager,固定的
ProxyPass ! \\不反代
Require all granted \\运行某
</Location>
[root@danran ~]# httpd -t
[root@danran ~]# systemctl restart httpd

client访问www.danran.com实现会话粘性

[root@danran ~]# curl -I www.danran.com
HTTP/1.1 200 OK
Date: Sun, 03 Sep 2017 00:47:42 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux) PHP/5.4.16
Set-Cookie: JSESSIONID=F36265B9FF2CC34C7203E41372FE4A0A; Path=/; HttpOnly
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 416
Set-Cookie: ROUTEID=.tcA; path=/

client显示balancer状态界面

http://www.danran.com/balancer-manager

Tomcat Cluster负载均衡

上一篇:【原】webapp开发中兼容Android4.0以下版本的css hack


下一篇:CSS iconfont阿里巴巴矢量图库在开发中实战使用