负载均衡器
session除了可以存储在redis/memcached还可存以数据库,cookies等
LVS
Nginx
是一个高性能的HTTP(OSI第7层)和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。在1.9.13版本后,Nginx已经支持端口转发(OSI第4层),注意版本一定要大于1.9.1,编译的时候需要–with-stream这个模块支持
安装
xiaoz写好的一键脚本安装Nginx,省时、省力,直接执行下面的命令即可
1 | 执行下面的命令,根据提示完成安装 |
配置
端口转发
在nginx.conf添加如下配置,并使用nginx -s reload重载nginx使其生效,同时注意防火墙/安全组放行对应的端口。
1 | stream { |
Tomcat集群方案
Tomcat内部实现集群(不好对session进行管理,小集群适用)
- 通过组播消息实现,修改tomcat的server.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34<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.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="192.168.1.245"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
Tomcat+Redis集群(稳定)
- 下载tomcat-redis-session
- 安装redis
- 对应的jar包放入
tomcat\lib
tomcat\conf\content.xml
增加1
2
3
4
5
6
7<!-- tomcat-redis-session共享 -->
<ValveclassName="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/>
<ManagerclassName="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="localhost"
port="6379"
database="0"
maxInactiveInterval="60" />- 部署两个tomcat建立页面测试