Tomcat集群配方方法:
服务器参数版本:
0 1 2 3 4 5 6 7 8 9 |
$ cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) $ uname -a Linux nginx_realserver_02 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux 主机IP: Tomcat1:1.1.1.251(nginx_realserver_01) Tomcat2:1.1.1.252(nginx_realserver_02) Nginx:1.1.1.250(nginx_lb) Redis:1.1.1.150 LB:1.1.1.200、192.168.2.253(lvs_nat_01) |
架构图:
一、官方集群方案
①:配置文件
0 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
<?xml version="1.0" encoding="UTF-8"?> <Server port="8005" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <Service name="Catalina"> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1"> <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8"> <!-- <Manager className="org.apache.catalina.ha.session.BackupManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true" mapSendOptions="6"/> --> <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="1.1.1.252" port="4000" selectorTimeout="100" 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.MessageDispatchInterceptor"/> <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/> </Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt"/> <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.ClusterSessionListener"/> </Cluster> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host appBase="/usr/local/tomcat/webapps/ROOT" name="localhost" unpackWARs="true"> <Context debug="0" docBase="/usr/local/tomcat/webapps/ROOT" path="" privileged="true" reloadable="false"/> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> </Service> </Server> |
②:参数说明SourceByrd's Weblog-https://note.t4x.org/service/tomcat-clustering-session-replication/
③:注意事项:SourceByrd's Weblog-https://note.t4x.org/service/tomcat-clustering-session-replication/
1:集群复制必须
012 # tail -2 /usr/local/tomcat/webapps/ROOT/WEB-INF/web.xml<distributable/></web-app>SourceByrd's Weblog-https://note.t4x.org/service/tomcat-clustering-session-replication/
二、Redis集群方案
①:步骤SourceByrd's Weblog-https://note.t4x.org/service/tomcat-clustering-session-replication/
1:下载软件:tomcat-redis-session-manager-master以及需要的web测试包(非必须);
2:将下载软件lib目录下的jar放到tomcat的lib目录下;
3:修改tomcat配置目录下的content.xml SourceByrd's Weblog-https://note.t4x.org/service/tomcat-clustering-session-replication/
②:配置文件
0 1 2 3 4 5 6 7 8 9 10 |
$ tail /usr/local/tomcat/conf/context.xml --> <Valve className="com.naritech.nicole.gump.RedisSessionHandlerValve" /> <Manager className="com.naritech.nicole.gump.RedisSessionManager" host="1.1.1.150" password="123456" port="6379" database="4" maxInactiveInterval="6" /> </Context> |
③:验证
0 1 2 3 4 |
1.1.1.150:6379> select 4 1.1.1.150:6379[4]> keys * 1) "99C06C02C16692F2888FF44FB551B447.tomcat1" 1.1.1.150:6379[4]> get 99C06C02C16692F2888FF44FB551B447.tomcat1 "\xac\xed\x00\x05sr\x005com.naritech.nicole.gump.SessionSerializationMetadataei\xe7\xc7\xc6\xcd7\xea\x03\x00\x01[\x00\x15sessionAttributesHasht\x00\x02[Bxpw\x14\x00\x00\x00\x10{\x1f\xa1\x86a\xa5\xa7\x94\xc0%\xb9a\xe3\x18\xb4\xf8xsr\x00\x0ejava.lang.Long;\x8b\xe4\x90\xcc\x8f#\xdf\x02\x00\x01J\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x01b\x88\x18\xc6\xb6sq\x00~\x00\x03\x00\x00\x01b\x88\x18\xc6\xb6sr\x00\x11java.lang.Integer\x12\xe2\xa0\xa4\xf7\x81\x878\x02\x00\x01I\x00\x05valuexq\x00~\x00\x04\x00\x00\a\bsr\x00\x11java.lang.Boolean\xcd r\x80\xd5\x9c\xfa\xee\x02\x00\x01Z\x00\x05valuexp\x00sq\x00~\x00\t\x01sq\x00~\x00\x03\x00\x00\x01_\x9b~\xc9tt\x00(99C06C02C16692F2888FF44FB551B447.tomcat1sq\x00~\x00\a\x00\x00\x00\x01t\x00\at4x.orgt\x00\at4x.orgw\b\x00\x00\x01b\x88\x18\xc6\xb6" |
4:软件SourceByrd's Weblog-https://note.t4x.org/service/tomcat-clustering-session-replication/
jar包:http://pan.baidu.com/s/1o8RX9gA 密码:xbg5 SourceByrd's Weblog-https://note.t4x.org/service/tomcat-clustering-session-replication/
5:页面代码
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$ cat /usr/local/tomcat/webapps/ROOT/index.jsp <%@ page language="java" %> <html> <head><title>Tomcat A</title></head> <body> <h1><font color="red">Tomcat--1.t4x.org</font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <% session.setAttribute("t4x.org","t4x.org"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html> |
参考文档:
1:https://tomcat.apache.org/tomcat-9.0-doc/cluster-howto.html
2:https://note.t4x.org/environment/nginx-tomcat-redis-session/SourceByrd's Weblog-https://note.t4x.org/service/tomcat-clustering-session-replication/
SourceByrd's Weblog-https://note.t4x.org/service/tomcat-clustering-session-replication/