首页 > 代码库 > Tomcat 负责均衡及cluster session
Tomcat 负责均衡及cluster session
一、nginx负载均衡tomcat
二、apache基于http负载均衡tomcat
三、apache基于ajp负载均衡tomcat
四、haproxy负载均衡tomcat
五、tomcat cluster session
一、nginx负载均衡tomcat
环境:
IP:172.16.110.47 OS:CentOS7.3 Nginx:1.10.2
IP:172.16.110.25 OS:CentOS7.3 Tomcat:8.0.39 jdk:java-1.8.0-openjdk
IP:172.16.110.49 OS:CentOS7.3 Tomcat:8.0.39 jdk:java-1.8.0-openjdk
架构图:
1.安装tomcat
在172.16.110.49上部署
1.1.安装部署tomcat
1.1.1.部署jdk
yum install java
1.1.2.查看jdk及java版本
# rpm -qa | grep jdk java-1.8.0-openjdk-1.8.0.121-0.b13.el7_3.x86_64 java-1.8.0-openjdk-headless-1.8.0.121-0.b13.el7_3.x86_64 # java -version openjdk version "1.8.0_121" OpenJDK Runtime Environment (build 1.8.0_121-b13) OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)
1.2.安装tomcat
tar -xf apache-tomcat-8.0.39.tar.gz -C /usr/local/ cd /usr/local/ ln -s apache-tomcat-8.0.39/ tomcat
1.3.配置环境变量
vim /etc/profile export PATH=$PATH:/usr/local/tomcat/bin source /etc/profile
1.4.配置虚拟主机
1.4.1.部署目录
# mkdir -pv /data/kaka/{lib,classes,WEB-INF,META-INF,servlets}
1.4.2.配置访问页面
# vim index.jsp <html> <head> <title>ckl JSP~</title> </head> <body> <% out.println("this page on 49 ! "); %> </body> </html>
1.4.3.配置tomcat虚拟主机为默认页面
# cd /usr/local/tomcat/conf/ vim server.xml ... <Engine name="Catalina" defaultHost="www1.ckl.com"> ... <Host name="www1.ckl.com" appBase="/data/kaka" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="/data/kaka"/> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="ckl_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> ...
1.4.4.启动进程
# catalina.sh start Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started.
1.5.在172.16.110.47上测试访问
# curl http://172.16.110.49:8080 <html> <head> <title>ckl JSP~</title> </head> <body> this page on 49 ! </body> </html>
2.在172.16.110.25上部署tomcat
2.1.1.部署jdk
yum install java
2.1.2.查看jdk及java版本
# rpm -qa | grep jdk java-1.8.0-openjdk-1.8.0.121-0.b13.el7_3.x86_64 java-1.8.0-openjdk-headless-1.8.0.121-0.b13.el7_3.x86_64 # java -version openjdk version "1.8.0_121" OpenJDK Runtime Environment (build 1.8.0_121-b13) OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)
2.2.安装tomcat
tar -xf apache-tomcat-8.0.39.tar.gz -C /usr/local/ cd /usr/local/ ln -s apache-tomcat-8.0.39/ tomcat
2.3.配置环境变量
vim /etc/profile export PATH=$PATH:/usr/local/tomcat/bin source /etc/profile
2.4.配置虚拟主机
2.4.1.部署目录
# mkdir -pv /data/kaka/{lib,classes,WEB-INF,META-INF,servlets}
2.4.2.配置访问页面
# vim index.jsp
<html>
<head>
<title>ckl JSP~</title>
</head>
<body>
<%
out.println("this page on 25 ! ");
%>
</body>
</html>
2.4.3.配置tomcat虚拟主机为默认页面
# cd /usr/local/tomcat/conf/ vim server.xml ... <Engine name="Catalina" defaultHost="www1.ckl.com"> ... <Host name="www1.ckl.com" appBase="/data/kaka" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="/data/kaka"/> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="ckl_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> ...
2.5.在172.16.110.47上测试访问
# curl http://172.16.110.25:8080 <html> <head> <title>ckl JSP~</title> </head> <body> this page on 25 ! </body> </html>
3.安装配置nginx(172.16.110.47)
3.1.安装nginx
编译安装参考其他文档,这里使用yum安装
yum install nginx
3.2.配置nginx做负载均衡
3.2.1.配置主文件
# vim nginx.conf user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘ ‘$status $body_bytes_sent "$http_referer" ‘ ‘"$http_user_agent" "$http_x_forwarded_for"‘; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; upstream tomcat_serv { server 172.16.110.25:8080; server 172.16.110.49:8080; } include /etc/nginx/conf.d/*.conf; }
3.2.2.配置子文件
vim conf.d/default.conf server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm index.jsp; } location ~* \.(jpg|png|bmp|gif)$ { root /usr/share/nginx/html; expires 30d; } location ~* \.(jsp|do)$ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_buffering off; proxy_pass http://tomcat_serv; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
3.3.重启服务
# systemctl restart nginx.service
3.4.测试访问
http://172.16.110.47/index.jsp
4.关于nginx session绑定:
4.1.修改配置文件
vim nginx.conf ... upstream tomcat_serv { ip_hash; server 172.16.110.25:8080; server 172.16.110.49:8080; } ...
4.2.测试访问
http://172.16.110.47/index.jsp
二、apache基于http负载均衡tomcat
环境:
IP:172.16.110.48 OS:CentOS7.3 apache:2.4.6
IP:172.16.110.25 OS:CentOS7.3 Tomcat:8.0.39 jdk:java-1.8.0-openjdk
IP:172.16.110.49 OS:CentOS7.3 Tomcat:8.0.39 jdk:java-1.8.0-openjdk
架构图:
1.tomcat在25和49配置
配置不变,使用原有的配置
2.配置apache
2.1.安装apache
yum install httpd
查看版本:
# rpm -qa | grep httpd httpd-tools-2.4.6-45.el7.centos.x86_64 httpd-2.4.6-45.el7.centos.x86_64
查看apache支持的模块:
# httpd -M | grep proxy AH00558: httpd: Could not reliably determine the server‘s fully qualified domain name, using localhost.localdomain. Set the ‘ServerName‘ directive globally to suppress this message proxy_module (shared) proxy_ajp_module (shared) proxy_balancer_module (shared) proxy_connect_module (shared) proxy_express_module (shared) proxy_fcgi_module (shared) proxy_fdpass_module (shared) proxy_ftp_module (shared) proxy_http_module (shared) proxy_scgi_module (shared) proxy_wstunnel_module (shared)
2.2.配置文件修改
2.2.1.官网文档地址http://apache.chinahtml.com/mod/mod_proxy.html
官网示例:
2.2.增加配置
cd /etc/httpd/conf.d/ vim http_tomcat.conf ProxyRequests Off <proxy balancer://tom_cluster1> BalancerMember http://172.16.110.25:8080 loadfactor=10 BalancerMember http://172.16.110.49:8080 loadfactor=10 route=ckltom2 </proxy> <VirtualHost *:80> ServerAdmin admin@ckl.com ServerName www.ckl.com ProxyPass / balancer://tom_cluster1 ProxyPassReverse / balancer://tom_cluster1 </VirtualHost>
2.3.启动进程
systemctl start httpd.service
2.4.测试访问
http://172.16.110.48/
3.apache基于http代理的session绑定
参考:http://serverfault.com/questions/303675/apache-load-balancing-doesnt-set-cookie-with-route-information
3.1.修改apache配置文件
cd /etc/httpd/conf.d/ vim http_tomcat.conf ProxyRequests Off Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED <proxy balancer://tom_cluster1> BalancerMember http://172.16.110.25:8080 loadfactor=10 route=ckltom1 BalancerMember http://172.16.110.49:8080 loadfactor=10 route=ckltom2 ProxySet stickysession=ROUTEID </proxy> <VirtualHost *:80> ServerAdmin admin@ckl.com ServerName www.ckl.com ProxyPass / balancer://tom_cluster1 ProxyPassReverse / balancer://tom_cluster1 </VirtualHost>
3.2.重启进程
systemctl restart httpd.service
3.3.测试访问
http://172.16.110.48/
三、apache基于ajp负载均衡tomcat
IP:172.16.110.48 OS:CentOS7.3 apache:2.4.6
IP:172.16.110.25 OS:CentOS7.3 Tomcat:8.0.39 jdk:java-1.8.0-openjdk
IP:172.16.110.49 OS:CentOS7.3 Tomcat:8.0.39 jdk:java-1.8.0-openjdk
架构图:
1.tomcat在25和49配置
配置不变,使用原有的配置
2.配置apache基于ajp的方式
2.1.配置文件修改
2.2.1.官网文档地址http://apache.chinahtml.com/mod/mod_proxy.html
cd /etc/httpd/conf.d/ vim ajp_tomcat.conf ProxyRequests Off <proxy balancer://tom_cluster1> BalancerMember ajp://172.16.110.25:8009 loadfactor=10 BalancerMember ajp://172.16.110.49:8009 loadfactor=10 </proxy> <VirtualHost *:80> ServerAdmin admin@ckl.com ServerName www.ckl.com ProxyPass / balancer://tom_cluster1 ProxyPassReverse / balancer://tom_cluster1 </VirtualHost>
2.3.启动进程
systemctl start httpd.service
2.4.测试访问
http://172.16.110.48/
3.apache基于ajp代理的session绑定
参考:http://serverfault.com/questions/303675/apache-load-balancing-doesnt-set-cookie-with-route-information
3.1.修改apache配置文件
cd /etc/httpd/conf.d/ vim http_tomcat.conf ProxyRequests Off Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED <proxy balancer://tom_cluster1> BalancerMember http://172.16.110.25:8080 loadfactor=10 route=kaka1 BalancerMember http://172.16.110.49:8080 loadfactor=10 route=kaka2 ProxySet stickysession=ROUTEID </proxy> <VirtualHost *:80> ServerAdmin admin@ckl.com ServerName www.ckl.com ProxyPass / balancer://tom_cluster1 ProxyPassReverse / balancer://tom_cluster1 </VirtualHost>
3.2.重启进程
systemctl restart httpd.service
3.3.测试访问
http://172.16.110.48/
暂时未成功
四、haproxy负载均衡tomcat
环境:
IP:172.16.110.50 OS:CentOS7.3 haproxy:1.5.18
IP:172.16.110.25 OS:CentOS7.3 Tomcat:8.0.39 jdk:java-1.8.0-openjdk
IP:172.16.110.49 OS:CentOS7.3 Tomcat:8.0.39 jdk:java-1.8.0-openjdk
架构图:
1.添加用户
# groupadd -g 200 haproxy # adduser -u 200 -g 200 -s /sbin/nologin haproxy
2.安装haproxy
yum install haproxy
3.配置haproxy负责均衡
cd /etc/haproxy vim haproxy.cfg global log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon stats socket /var/lib/haproxy/stats defaults mode http log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 frontend main *:80 use_backend webserver frontend stats_ser bind *:8800 use_backend web_stats backend webserver balance roundrobin server web1 172.16.110.25:8080 weight 1 maxconn 500 maxqueue 300 cookie cklser1 check server web2 172.16.110.49:8080 weight 1 maxconn 500 maxqueue 300 cookie cklser2 check backend web_stats server web1 172.16.110.25:8080 check server web2 172.16.110.49:8080 check stats enable stats hide-version stats uri /ckl?stats stats realm Haproxy\ Statistics stats auth ckl:1QAZnji9 stats auth zld:4rfvBHU8 stats auth admin:7uj4rf99 stats admin if TRUE stats refresh 5s
4.启动进程
systemctl start haproxy.service
5.测试访问
http://172.16.110.50/
session绑定简单略过
五、tomcat cluster session
文档参考地址:http://tomcat.apache.org/tomcat-8.0-doc/cluster-howto.html
1.nginx去掉session绑定
vim nginx.conf .... upstream tomcat_serv { #ip_hash; server 172.16.110.25:8080; server 172.16.110.49:8080; } ....
2.重启nginx
systemctl restart nginx.service
3.配置cluster session
172.16.110.25上配置
cd /usr/local/tomcat/conf
在虚拟机配置中增加如下:
本文出自 “深呼吸再出击” 博客,请务必保留此出处http://ckl893.blog.51cto.com/8827818/1893848
Tomcat 负责均衡及cluster session