集群Clustering与负载均衡Load Balancing的区别,可以参考这篇文章: Clustering vs. Load Balancing – What is the difference?
使用集群可以分发请求提高吞吐量,应对单机故障、高可用性HA,提供不间断服务。而负载均衡只是集群的第一步,当然也是最重要的,集群还包含很多内容,比如:
- 会话管理Session Management
- 上传文件同步(可以通过共享文件NFS mount、实时同步rsync、独立文件服务、使用类似于Amazon S3的存储服务等实现)
- 计划任务Job(可以使用基于数据库的Quartz实现)
- War文件自动部署(可以通过Tomat的FarmWarDeployer组件实现,但目前该组件还不稳定)
最简单的负载均衡是DNS Round Robin,通过DNS域解析提供不同的IP,多于用全球化的系统,依据地理位置不同提供最近的服务器。基于硬件可以实现负载均衡,比如F5;基于软件也有很多可以实现负载均衡,比如:Apache、Nginx、LVS、HAProxy、Varnish等。这里只讨论用目前市场占有率比较高的Apache和Nginx来实现负载均衡以及Tomcat的会话管理。
(1)基于Apache的负载均衡
Apache和Tomcat的连接:
- mod_jk(mod_jk2已停止更新)
- mod_proxy_http
- mod_proxy_ajp
mod_proxy_http是Apache的标准模块,可加密httpd和tomcat之间的通讯;mod_jk是非标准模块,支持SSL,可实现负载均衡;mod_proxy_ajp是Apache2的标准模块,mod_jk的标准实现。
AJP(Apache JServ Protocol),目前最新版本1.3,所以一般都说AJP13。它是一个二进制协议,性能比http11快。mod_jk会把接收到的HTTP请求转换为AJP后转发给Tomcat,相应也会把Tomcat的AJP应答转换HTTP应答返回给用户的Web浏览器。而mod_proxy_http并没有什么协议的转换是直接把HTTP请求和Tomcat之间进行转发。虽然mod_proxy_http支持加密,但一般Web服务器都部署在子域下,加密通讯没多大必要。所以目前大多用的是mod_jk或mod_proxy_ajp。性能优先用mod_jk、安装配置简单用mod_proxy_ajp。
Apache HTTP Server与Apache Tomcat的区别
引用
・别名:Apache HTTP Server、Apache Web Server、Apache、httpd
・官网:http://httpd.apache.org
・HTTP Web Server,用来HTTP协议的文件服务
・主要处理静态文件,通过扩展支持CGI、PHP、Perl等脚本语言
・除了Java基本上能通过扩展支持任意语言
・处理Request提供Response相应,实现Load Balancing
・C语言实现
・其他类似开源实现Nginx: http://nginx.org/
引用
・别名:Jakarta-Tomcat、Apache Tomcat、Tomcat
・官网:http://tomcat.apache.org
・Servlet Container(叫Catalina),JSP Engine(Jasper)是Java Servlets和JSP的官方实现
・可以处理静态文件,本身也实现了Web server(叫Coyote、不需要Apache),主要处理Servlet/JSP
・可以执行Perl等脚本语言,但是很少用于Java以外的语言
・不是处理Request提供Response相应,提供Servlets和JSP的所有功能
・纯Java语言实现(为了提升性能,TC-Native用C实现了一部分核心功能)
・其他类似服务器: http://rensanning.iteye.com/blog/1695196
APR和AJP的区别
- Apache Portable Runtime (APR) 连接器实现 比如:BIO/NIO/NIO.2/APR
- Apache JServ Protocol (AJP) 协议 比如:HTTP, AJP, Websocket, FastCGI
conf/server.xml
引用
<Connector port="8080" protocol="HTTP/1.1" .../>
protocol默认是HTTP/1.1,安装了tomcat-native的话使用Http11AprProtocol,没安装的话使用Http11Protocol。也可以自己直接指定protocol:
- org.apache.coyote.http11.Http11Protocol Traditional Connector
- org.apache.coyote.http11.Http11AprProtocol Native Connector
- org.apache.coyote.http11.Http11NioProtocol NIO Connector
- org.apache.coyote.http11.Http11Nio2Protocol NIO2 Connector
Tomcat默认是开启APR的:
conf/server.xml
引用
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
不安装Tomcat APR(Tomcat Native Library)会看到以下的提示:
引用
The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path:........
正确安装后的提示:
引用
Loaded APR based Apache Tomcat Native library 1.1.32 using APR version 1.5.1.
①配置mod_jk
conf/httpd.conf
# Load module
LoadModule jk_module path/to/apache2/mod_jk.so
# Specify path to worker configuration file
JkWorkersFile /path/to/apache2/conf/workers.properties
# Configure logging and memory
JkShmFile /path/to/desired/log/location/mod_jk.shm
JkLogFile /path/to/desired/log/location/mod_jk.log
JkLogLevel info
# Configure monitoring
JkMount /jkmanager/* jkstatus
<Location /jkmanager>
Order deny, allow
Deny from all
Allow from localhost
</Location>
# Configure applications
JkMount /webapp-directory/* LoadBalancer
conf/workers.properties
# Define worker names
worker.list=jkstatus, loadbalancer, stat
# Create virtual workers
# The status worker allows us to get statistical data
worker.jkstatus.type=status
worker.loadbalancer.type=lb
# Declare Tomcat server workers 1 through n
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009
# ...
worker.worker[n].type=ajp13
worker.worker[n].port=8010
worker.worker[n].host=localhost
# Associate real workers with virtual LoadBalancer worker
worker.loadbalancer.balance_workers=worker1,worker2,…worker[n]
workers.properties的具体参数设置,参考 官方文档
可以通过 http://localhost/jkmanager查看负载均衡的状态。
②配置mod_proxy_ajp
conf/httpd.conf
# Required Modules
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule status_module modules/mod_status.so
# Reverse Proxy
<Proxy balancer://mybalancer>
BalancerMember ajp://localhost:8301 retry=30 loadfactor=1 route=jvm1
BalancerMember ajp://localhost:8302 retry=30 loadfactor=1 route=jvm2
BalancerMember ajp://localhost:8303 retry=30 loadfactor=5 route=jvm3
ProxySet lbmethod=bytraffic
ProxySet nofailover=Off
ProxySet stickysession=JSESSIONID
</Proxy>
# Pass All Request
ProxyPass / balancer://mybalancer/
ProxyPassReverse / balancer://mybalancer/
# Forward Proxy
ProxyRequests Off
<Proxy *>
Order deny,allow
Deny from none
Allow from localhost
</Proxy>
# Balancer-manager, for monitoring
<Location /balancer-manager>
SetHandler balancer-manager
Order deny,allow
Deny from all
Allow from 192.168.21.0/24
</Location>
按项目转发:
<Proxy balancer://webapp>
BalancerMember ajp://localhost:8001/webapp route=jvm1 loadfactor=10
BalancerMember ajp://localhost:8002/webapp route=jvm2 loadfactor=10
ProxySet lbmethod=bytraffic
ProxySet nofailover=Off
ProxySet stickysession=JSESSIONID
</Proxy>
<Location /webapp/>
ProxyPass balancer://webapp/ stickysession=JSESSIONID
ProxyPassReverse balancer://webapp/ stickysession=JSESSIONID
</Location>
Tomcat默认使用JSESSIONID,也可以自己定义session的key值。
$CATALINA_HOME/conf/context.xml
引用
<Context sessionCookieName="yourCookieName">
route里设置的jvm1、jvm1、jvm3就是为了实现粘性Session,和Tomat的server.xml里配置一致。
引用
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
可以通过 http://localhost/balancer-manager查看负载均衡的状态。
(2)基于Nginx的负载均衡
Nginx本身并不支持AJP连接,只有HTTP连接。由于Nginx可以keep-alive连接,所以性能不比AJP差。当然也可以使用第三方模块 nginx_ajp_module来支持AJP。
conf/nginx.conf
# Defines a group of servers
upstream tomcatcluster {
server 127.0.0.1:8181 weight=2;
server 127.0.0.1:8282 max_fails=3 fail_timeout=30s;
server 127.0.0.1:8383;
keepalive 10;
}
# Pass to the backend servers
server {
location / {
proxy_pass http://tomcatcluster;
}
}
如上定义即可实现负载均衡,但是non-sticky session。在定义Upstream时使用ip_hash可以将相同IP的请求分发到同一Tomcat实现sticky session.
upstream tomcatcluster {
ip_hash;
server 127.0.0.1:8181;
server 127.0.0.1:8282;
server 127.0.0.1:8383;
}
nginx本身也支持sticky指令,但是用于商业版本。
所以需要用到 nginx-sticky-module、节点的健康检查 nginx_upstream_check_module
(3)Tomcat的会话管理
Tomcat的会话共享:粘性会话Sticky Session和会话复制Session Replication。
①粘性会话Session affinity/Sticky Session
将同一用户的请求转发到同一Tomcat服务器上,如果Tomcat服务器down了,会话信息就丢了。
conf/server.xml
引用
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
session-id的值:<随机数>.<jvmRoute的值>
引用
Cookie:JSESSIONID=40025608F7B50E42DFA2785329079227.jvm1
②会话复制Session Replication
基于会话复制可以自动实现故障转移Session Failover。
IP组播(In Memory Session Broadcasting/IP Multicasting):利用Tomcat的一个模块Tribes支持服务器集群中的组通信SimpleTcpCluster,不是所有操作系统支持IP组播。
引用
[root@localhost ~]# ifconfig -a
eth0 Link encap:Ethernet HWaddr 00:1D:09:31:69:C6
inet addr:192.168.21.140 Bcast:192.168.21.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
[root@localhost ~]# cat /proc/net/dev_mcast
2 eth0 1 0 01005e000001
通过tcpdump可以查看multicast的通讯情况(默认500毫秒通讯一次):
引用
# /usr/sbin/tcpdump -i eth1 -n ip multicast
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
01:36:00.054028 IP 192.168.21.3.50001 > 228.0.0.104.50001: UDP, length 77
01:36:00.554770 IP 192.168.21.3.50001 > 228.0.0.104.50001: UDP, length 77
01:36:01.055657 IP 192.168.21.3.50001 > 228.0.0.104.50001: UDP, length 77
01:36:01.556332 IP 192.168.21.3.50001 > 228.0.0.104.50001: UDP, length 77
4种类型Session Manager:
- Standard Manager 默认值,单实例。Tomcat替我们管理会话,Tomcat重启graceful或应用重加载时将HttpSession保存到SESSIONS.ser中复原。
- Persistent Manager 一定期间(不是实时)增量备份会话信息到文件、或经JDBC到DB
- Delta Manager 用于小的集群,依靠multicast复制会话信息到其他实例,(全节点会话同步、All-to-All复制)
- Backup Manager 用于大的集群,和Delta Manager功能相同,但只是获取一个实例作为backup来存贮会话信息(单节点会话备份 Primary-Backup复制)
・单实例存储到文件(pathname设置为空时就不保存)
<Manager className="org.apache.catalina.session.StandardManager"
pathname="/var/sessiondata/mysessions.ser"
sessionIdLength="32">
</Manager>
・存储到文件(PersistenceManager + FileStore):
<Manager className="org.apache.catalina.session.PersistentManager">
<Store className="org.apache.catalina.session.FileStore" directory="c:\\cluster\shareddir"/>
</Manager>
・存储到数据库(PersistenceManager + JDBCStore):
需要把postgresql-9.3-1102.jdbc4.jar拷贝到$CATALINA_HOME/lib里。
<Manager className="org.apache.catalina.session.PersistentManager">
<Store className="org.apache.catalina.session.JDBCStore"
connectionURL="jdbc:postgresql://localhost:5432/tomcat?user=tomcat&password=tomcat"
driverName="org.postgresql.Driver"
sessionIdCol="session_id"
sessionValidCol="valid_session"
sessionMaxInactiveCol="max_inactive"
sessionLastAccessCol="last_access"
sessionTable="tomcat_sessions"
sessionAppCol="app_context"
sessionDataCol="session_data"
/>
</Manager>
CREATE USER tomcat WITH PASSWORD 'tomcat';
CREATE DATABASE tomcat WITH OWNER = tomcat ENCODING = 'UTF8';
CREATE TABLE tomcat_sessions
(
session_id character varying(100) NOT NULL,
valid_session character(1) NOT NULL,
max_inactive integer NOT NULL,
last_access bigint NOT NULL,
app_context character varying(255),
session_data bytea,
CONSTRAINT tomcat_sessions_pkey PRIMARY KEY (session_id)
);
ALTER TABLE tomcat_sessions OWNER TO tomcat;
CREATE INDEX idx_tomcat_sessions_app_context ON tomcat_sessions(app_context);
也可以使用datasource:
<Resource name="jdbc/sessions" auth="Container" type="javax.sql.DataSource"
username="tomcat"
password="tomcat"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/tomcat"
maxActive="20"
maxIdle="10"
validationQuery="select 1" />
<Manager className="org.apache.catalina.session.PersistentManager"
distributable="true" processExpiresFrequency="3" maxIdleBackup="1" >
<Store className="org.apache.catalina.session.JDBCStore"
dataSourceName="jdbc/sessions"
sessionIdCol="session_id"
sessionValidCol="valid_session"
sessionMaxInactiveCol="max_inactive"
sessionLastAccessCol="last_access"
sessionTable="tomcat_sessions"
sessionAppCol="app_context"
sessionDataCol="session_data"
/>
</Manager>
2种Tomcat集群:
- Static Tomcat Cluster:不需要Multicast,各自定义配置
- Dynamic Tomcat Cluster:无需定义配置,通过heartbeat信号
静态集群的例子:
<Cluster channelSendOptions="8" channelStartOptions="3" className="org.apache.catalina.ha.tcp.SimpleTcpCluster">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender" />
</Sender>
<Receiver address="192.168.21.88" autoBind="0"
className="org.apache.catalina.tribes.transport.nio.NioReceiver"
maxThreads="6" port="4100" selectorTimeout="5000"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpPingInterceptor" staticOnly="true"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector" />
<!-- 静态Member设置 -->
<Interceptor className="org.apache.catalina.tribes.group.interceptors.StaticMembershipInterceptor">
<Member
className="org.apache.catalina.tribes.membership.StaticMember"
port="4100"
host="192.168.21.89"
uniqueId="{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2}" />
</Interceptor>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor" />
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*
\.css;.*\.txt;" />
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener" />
</Cluster>
动态集群:
<Engine name="Catalina" defaultHost="www.mysite.com" jvmRoute="[worker name]">
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">
<Manager
className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"
/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership
className="org.apache.catalina.tribes.membershipt.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"
/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Receiver
className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"
/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<!-- 不进行会话共享的List Pattern -->
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*\.gif;.*\.js;.*\.jpg;.*\.htm;.*\.html;.*\.txt;" />
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
</Engine>
会话复制的设置:
・Enable Multicast routing
sudo route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
・conf/server.xml <Cluster>
conf/server.xml
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
・web.xml <distributable /> 让Tomcat知道你的Web应用可以发布到集群里,需要注意的是放入Session里的东西必须实现serializable。
WEB-INF/web.xml
<web-app
.....
<distributable />
</web-app>
或者META-INF/context.xml
引用
<Context distributable="true"></Context>
如果不想修改自己的web.xml,可以修改CATALINA_BASE/conf/context.xml来
引用
<Context distributable="true" className="org.apache.catalina.ha.context.ReplicatedContext" >
・如果使用硬件做负载均衡的话,也可以配置Tomcat实现会话复制!
・Tomcat集群使用JULI(java.util.logging)日志框架,配置logging.properties里的org.apache.catalina.tribes.MESSAGES。
・Tomcat动态集群可以使用mod_cluster!
・如果Tomcat采用AJP连接建议不要开启8080端口启用AJP的8009端口。
・防止会话劫持Session Hijacking:开启HTTPS使用TLS、用户成功登陆后更换SessiongID。
③同一台机器安装多个Tomcat
只要合理规划Tomcat配置conf/server.xml中的各个端口,就能在一台服务器上起多个Tomcat实例,一般由于开发测试。
- 80xx -> Tomcat Server Shutdown Port
- 81xx -> Tomcat Connector Port (HTTP)
- 82xx -> Tomcat SSL Redirect Port
- 83xx -> Tomcat AJP Port
- 40xx -> Tomcat tcp receive port for NioReceiver(4000-4100)
例:
<Server port="8001" shutdown="SHUTDOWN">
<Service name="Catalina">
<Connector port="8101" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8201" />
<Connector port="8201" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
<Connector port="8301" protocol="AJP/1.3" redirectPort="8201" />
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto" port="4001" autoBind="100" selectorTimeout="5000" maxThreads="6" />
</Channel>
</Cluster>
</Engine>
</Service>
</Server>
(4)基于Cache服务器的会话管理
- Sticky模式:每次请求都会被映射到同一台Web Server,直到该Web Server宕机,session会被存放在服务器本地,等到请求处理完成再同步到memcached服务器;再次请求时被映射到其他Web Server可以从后端memcache中恢复session。
- Non-Sticky模式:请求映射到哪儿不一定。当请求到来时,从memcache中加载session;当请求处理完成时,将session再写回到memcache。
①Memcached Session Manager
https://github.com/magro/memcached-session-manager
②Redis Session Manager
https://github.com/jcoleman/tomcat-redis-session-manager
③Hazelcast
https://github.com/hazelcast/hazelcast
(5)其他
①跨平台的会话管理:Java、PHP、.NET等系统平台之间的会话管理
需要合理安排会话的存储、数据结构、序列化/反序列化等。
②自动部署
http://serverfault.com/questions/298886/farm-deployer-not-working-in-tomcat-cluster
③JMX监视
https://tomcat.apache.org/tomcat-8.0-doc/monitoring.html
④开源框架
- Apache Shiro http://shiro.apache.org/
- Spring Security http://projects.spring.io/spring-security/
- Spring Session http://projects.spring.io/spring-session/
参考:
https://tomcat.apache.org/tomcat-8.0-doc/cluster-howto.html
http://www.datadisk.co.uk/html_docs/java_app/tomcat6/tomcat6_clustering.htm
http://examples.javacodegeeks.com/enterprise-java/tomcat/tomcat-clustering-session-replication-tutorial/