Tomcat集群session復制,httpd/nginx反代Tomcat集群
一個大型站點都會涉及到動態應用,動態應用都需要做會話保持,常見的會話保持方式就三種,一是session stick,二是session replication,三是session share,對於小型規模的tomcat集群,大多者會采用session replication方式,但閱讀官方文檔也好,查詢大牛博客也罷,發現均有不準確之處,所以親測成功實現之後得出如下文檔,還望高人指點。
實驗環境:
操作系統:CentOS 7.2
tomcat版本:tomcat-7.0.54(yum安裝方式)
httpd版本:httpd-2.4.6(yum安裝方式)
nginx版本:nginx-1.10.2(yum安裝方式)
iptables狀態:disable
SELinux狀態:disable
TomcatA配置過程
[[email protected] ~]# ifconfig
eno16777736:flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.20 netmask255.255.255.0 broadcast 192.168.0.255
inet6 fe80::20c:29ff:fefe:64f3 prefixlen 64 scopeid0x20<link>
ether 00:0c:29:fe:64:f3 txqueuelen 1000 (Ethernet)
RX packets 1147 bytes 141324(138.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 659 bytes 102242 (99.8KiB)
TX errors 0 dropped 0 overruns0 carrier 0 collisions 0
lo:flags=73<UP,LOOPBACK,RUNNING> mtu65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns0 carrier 0 collisions 0
[[email protected] ~]# cat/etc/yum.repos.d/hehe.repo
[cdrom-repo]
name=cdrom-repo
baseurl=file:///cdrom
gpgcheck=0
[aliyun-epel]
name=aliyun-epel
baseurl=http://mirrors.aliyun.com/epel/7/x86_64/
gpgcheck=0
[[email protected]A ~]#
[[email protected] ~]# yum install -y tomcat
[[email protected] ~]# mkdir -pv/usr/share/tomcat/webapps/ROOT/WEB-INF
mkdir: created directory‘/usr/share/tomcat/webapps/ROOT’
mkdir: created directory‘/usr/share/tomcat/webapps/ROOT/WEB-INF’
[[email protected] ~]# vim/usr/share/tomcat/webapps/ROOT/index.jsp
[[email protected] ~]# cat/usr/share/tomcat/webapps/ROOT/index.jsp
<%@ page language="java" %> <html> <head><title>TomcatA</title></head> <body> <h1><fontcolor="red">TomcatA</font></h1> <table border="1"> <tr> <td>SessionID</td> <%session.setAttribute("test.com","test.com"); %> <td><%=session.getId() %></td> </tr> <tr> <td>Createdon</td> <td><%=session.getCreationTime() %></td> </tr> </table> </body> </html>
[[email protected] ~]# scp/usr/share/tomcat/webapps/ROOT/index.jsp 192.168.0.21:/usr/share/tomcat/webapps/ROOT/
The authenticity of host ‘192.168.0.21(192.168.0.21)‘ can‘t be established.
ECDSA key fingerprint isfc:7f:15:2b:2d:69:c9:c1:b0:79:7c:ce:ab:c0:0c:36.
Are you sure you want to continueconnecting (yes/no)? yes
Warning: Permanently added ‘192.168.0.21‘(ECDSA) to the list of known hosts.
[email protected]‘s password:
index.jsp 100% 410 0.4KB/s 00:00
[[email protected] ~]# ss -tnl
State Recv-Q Send-Q LocalAddress:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 128 :::22 :::*
[[email protected] ~]# systemctl start tomcat
[[email protected] ~]# ss -tnl
State Recv-Q Send-Q LocalAddress:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 :::8009 :::*
LISTEN 0 100 :::8080 :::*
LISTEN 0 128 :::22 :::*
[[email protected] ~]#
TomcatB環境配置
[[email protected] ~]# ifconfig
eno16777736:flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.21 netmask255.255.255.0 broadcast 192.168.0.255
inet6 fe80::20c:29ff:fed8:be3b prefixlen 64 scopeid0x20<link>
ether 00:0c:29:d8:be:3b txqueuelen 1000 (Ethernet)
RX packets 839 bytes 107014(104.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 430 bytes 68356 (66.7KiB)
TX errors 0 dropped 0 overruns0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns0 carrier 0 collisions 0
[[email protected] ~]# cat/etc/yum.repos.d/hehe.repo
[cdrom-repo]
name=cdrom-repo
baseurl=file:///cdrom
gpgcheck=0
[aliyun-epel]
name=aliyun-epel
baseurl=http://mirrors.aliyun.com/epel/7/x86_64/
gpgcheck=0
[[email protected] ~]#
[[email protected] ~]# yum install -y tomcat
[[email protected] ~]# mkdir -pv/usr/share/tomcat/webapps/ROOT/WEB-INF
mkdir: created directory‘/usr/share/tomcat/webapps/ROOT’
mkdir: created directory‘/usr/share/tomcat/webapps/ROOT/WEB-INF’
[[email protected] ~]# vim/usr/share/tomcat/webapps/ROOT/index.jsp
[[email protected] ~]# cat/usr/share/tomcat/webapps/ROOT/index.jsp
<%@ page language="java" %> <html> <head><title>TomcatB</title></head> <body> <h1><fontcolor="blue">TomcatB</font></h1> <table border="1"> <tr> <td>SessionID</td> <%session.setAttribute("test.com","test.com"); %> <td><%=session.getId() %></td> </tr> <tr> <td>Createdon</td> <td><%=session.getCreationTime() %></td> </tr> </table> </body> </html>
[[email protected] ~]# ss -tnl
State Recv-Q Send-Q LocalAddress:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 128 :::22 :::*
[[email protected] ~]# systemctl start tomcat
[[email protected] ~]# ss -tnl
State Recv-Q Send-Q LocalAddress:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 :::8009 :::*
LISTEN 0 100 :::8080 :::*
LISTEN 0 128 :::22 :::*
[[email protected] ~]#
測試兩臺tomcat服務器工作是否正常
使用瀏覽器訪問結果如圖所示
接下來就需要配置前端的反代服務器,以及tomcat集群session復制了
httpd反代tomcat集群配置
在這裏我們為了節約資源就把httpd反代服務器配置到TomcatA服務器上
[[email protected] ~]# yum install -y httpd
[[email protected] ~]# vim/etc/httpd/conf.d/httpd_tomcat.conf
[[email protected] ~]# cat/etc/httpd/conf.d/httpd_tomcat.conf
#LoadModule proxy_modulemodules/mod_proxy.so #LoadModule proxy_ajp_modulemodules/mod_proxy_ajp.so #LoadModule proxy_balancer_modulemodules/mod_proxy_balancer.so <proxy balancer://tomcatcluster> BalancerMemberajp://192.168.0.20:8009 route=TomcatA loadfactor=1 BalancerMemberajp://192.168.0.21:8009 route=TomcatB loadfactor=1 ProxySetlbmethod=byrequests #ProxySetstickysession=JSESSIONID </Proxy> <VirtualHost *:80> ServerNameproxy.test.com ProxyViaOn ProxyRequestsOff ProxyPreserveHostOn <Proxy*> Requireall granted </Proxy> ProxyPass/ balancer://tomcatcluster/ ProxyPassReverse/ balancer://tomcatcluster/ <Location/> Requireall granted </Location> <Location/balancer-manager> SetHandlerbalancer-manager ProxyPass! Requireall granted </Location> </VirtualHost>
[[email protected] ~]# vim/etc/httpd/conf/httpd.conf
修改如下內容
ServerName www.example.com:80 <IfModule dir_module> DirectoryIndex index.jsp </IfModule>
[[email protected] ~]# httpd -t
Syntax OK
[[email protected] ~]# cp/etc/tomcat/server.xml{,.bak}
[[email protected] ~]# vim/etc/tomcat/server.xml
默認配置文件需要修改以下幾個地方
<Engine name="Catalina"defaultHost="localhost" jvmRoute="TomcatA">
Engine標簽下添加集群配置內容
<ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8"> <ManagerclassName="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/> <ChannelclassName="org.apache.catalina.tribes.group.GroupChannel"> <MembershipclassName="org.apache.catalina.tribes.membership.McastService" address="228.0.0.4" port="45564" frequency="500" dropTime="3000"/> <ReceiverclassName="org.apache.catalina.tribes.transport.nio.NioReceiver" address="192.168.0.20" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/> <SenderclassName="org.apache.catalina.tribes.transport.ReplicationTransmitter"> <TransportclassName="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/> </Sender> <InterceptorclassName="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/> <InterceptorclassName="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/> </Channel> <ValveclassName="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/> <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/> <DeployerclassName="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/> <ClusterListenerclassName="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/> <ClusterListenerclassName="org.apache.catalina.ha.session.ClusterSessionListener"/> </Cluster>
修改日誌格式為httpd combined格式
<Valve className="org.apache.catalina.valves.AccessLogValve"directory="logs" prefix="localhost_access_log."suffix=".txt" pattern="%h %l %u %t"%r" %s %b "%{Referer}i" "%{User-Agent}i"" />
[[email protected] ~]# mv/etc/tomcat/server.xml{,.bak}
[[email protected] ~]# scp/etc/tomcat/server.xml 192.168.0.21:/etc/tomcat/
[email protected]‘s password:
server.xml 100% 8226 8.0KB/s 00:00
[[email protected] ~]#
對比TomcatA需要修改的地方
<Engine name="Catalina"defaultHost="localhost" jvmRoute="TomcatB"> <ReceiverclassName="org.apache.catalina.tribes.transport.nio.NioReceiver" address="192.168.0.21"
添加應用程序自己的web.xml配置文件,需要有<distributable/>這個標簽
[[email protected] ~]# vim/usr/share/tomcat/webapps/ROOT/WEB-INF/web.xml
[[email protected] ~]# cat/usr/share/tomcat/webapps/ROOT/WEB-INF/web.xml
<?xml version="1.0"encoding="ISO-8859-1"?> <web-appxmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"> <distributable/> </web-app>
[[email protected] ~]# scp/usr/share/tomcat/webapps/ROOT/WEB-INF/web.xml192.168.0.21:/usr/share/tomcat/webapps/ROOT/WEB-INF
[email protected]‘s password:
web.xml 100% 356 0.4KB/s 00:00
[[email protected] ~]# systemctl restart tomcat
[[email protected] ~]# systemctl start httpd
[[email protected] ~]# ss -tnl
State Recv-Q Send-Q LocalAddress:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 :::8009 :::*
LISTEN 0 128 :::80 :::*
LISTEN 0 100 :::8080 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 50 ::ffff:192.168.0.20:4000 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8005 :::*
[[email protected] ~]#
TomcatA重啟成功日誌如下
[[email protected] ~]# tail -f/var/log/tomcat/catalina.2017-09-26.log
Sep 26, 2017 2:57:34 AMorg.apache.catalina.core.StandardServer await
INFO: A valid shutdown command was receivedvia the shutdown port. Stopping the Server instance.
Sep 26, 2017 2:57:34 AMorg.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler["http-bio-8080"]
Sep 26, 2017 2:57:35 AMorg.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Nativelibrary which allows optimal performance in production environments was notfound on the java.library.path:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Sep 26, 2017 2:57:35 AMorg.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler["http-bio-8080"]
Sep 26, 2017 2:57:35 AMorg.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler["ajp-bio-8009"]
Sep 26, 2017 2:57:35 AMorg.apache.catalina.startup.Catalina load
INFO: Initialization processed in 379 ms
Sep 26, 2017 2:57:35 AMorg.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Sep 26, 2017 2:57:35 AMorg.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: ApacheTomcat/7.0.54
Sep 26, 2017 2:57:35 AM org.apache.catalina.ha.tcp.SimpleTcpClusterstartInternal
INFO: Cluster is about to start
Sep 26, 2017 2:57:35 AMorg.apache.catalina.tribes.transport.ReceiverBase bind
INFO: Receiver Server Socket boundto:/192.168.0.20:4000
Sep 26, 2017 2:57:35 AM org.apache.catalina.tribes.membership.McastServiceImplsetupSocket
INFO: Setting cluster mcast soTimeout to500
Sep 26, 2017 2:57:35 AMorg.apache.catalina.tribes.membership.McastServiceImpl waitForMembers
INFO: Sleeping for 1000 milliseconds toestablish cluster membership, start level:4
Sep 26, 2017 2:57:36 AMorg.apache.catalina.tribes.membership.McastServiceImpl waitForMembers
INFO: Done sleeping, membershipestablished, start level:4
Sep 26, 2017 2:57:36 AMorg.apache.catalina.tribes.membership.McastServiceImpl waitForMembers
INFO: Sleeping for 1000 milliseconds toestablish cluster membership, start level:8
Sep 26, 2017 2:57:37 AMorg.apache.catalina.tribes.membership.McastServiceImpl waitForMembers
INFO: Done sleeping, membershipestablished, start level:8
Sep 26, 2017 2:57:37 AMorg.apache.catalina.ha.deploy.FarmWarDeployer start
SEVERE: FarmWarDeployer can only work ashost cluster subelement!
Sep 26, 2017 2:57:37 AMorg.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory/var/lib/tomcat/webapps/ROOT
Sep 26, 2017 2:58:13 AMorg.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance forsession ID generation using [SHA1PRNG] took [35,019] milliseconds.
Sep 26, 2017 2:58:13 AMorg.apache.catalina.ha.session.DeltaManager startInternal
INFO: Register manager localhost# tocluster element Engine with name Catalina
Sep 26, 2017 2:58:13 AMorg.apache.catalina.ha.session.DeltaManager startInternal
INFO: Starting clustering manager atlocalhost#
Sep 26, 2017 2:58:13 AMorg.apache.catalina.ha.session.DeltaManager getAllClusterSessions
INFO: Manager [localhost#]: skipping statetransfer. No members active in cluster group.
Sep 26, 2017 2:58:13 AMorg.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web applicationdirectory /var/lib/tomcat/webapps/ROOT has finished in 35,563 ms
Sep 26, 2017 2:58:13 AMorg.apache.catalina.ha.session.JvmRouteBinderValve startInternal
INFO: JvmRouteBinderValve started
Sep 26, 2017 2:58:13 AMorg.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler["http-bio-8080"]
Sep 26, 2017 2:58:13 AMorg.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler["ajp-bio-8009"]
Sep 26, 2017 2:58:13 AM org.apache.catalina.startup.Catalinastart
INFO: Server startup in 37684 ms
TomcatB重啟成功後TomcatA會產生如下日誌
Sep 26, 2017 3:01:05 AMorg.apache.catalina.tribes.io.BufferPool getBufferPool
INFO: Created a buffer pool with maxsize:104857600 bytes of type:org.apache.catalina.tribes.io.BufferPool15Impl
Sep 26, 2017 3:01:06 AMorg.apache.catalina.ha.tcp.SimpleTcpCluster memberAdded
INFO: Replication memberadded:org.apache.catalina.tribes.membership.MemberImpl[tcp://{192, 168, 0,21}:4000,{192, 168, 0, 21},4000, alive=1014, securePort=-1, UDP Port=-1, id={5025 -81 7 -94 116 76 51 -100 -57 116 -83 10 93 -110 -108 }, payload={},command={}, domain={}, ]
TomcatB需做如下修改
[[email protected] ~]# cat/usr/share/tomcat/webapps/ROOT/WEB-INF/web.xml
<?xml version="1.0"encoding="ISO-8859-1"?> <web-appxmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"> <distributable/> </web-app>
[[email protected] ~]# systemctl restart tomcat
TomcatB成功重啟過程日誌如下
[[email protected] ~]# tail -f/var/log/tomcat/catalina.2017-09-26.log
Sep 26, 2017 3:01:03 AMorg.apache.catalina.core.StandardServer await
INFO: A valid shutdown command was receivedvia the shutdown port. Stopping the Server instance.
Sep 26, 2017 3:01:03 AMorg.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler["http-bio-8080"]
Sep 26, 2017 3:01:04 AMorg.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Nativelibrary which allows optimal performance in production environments was notfound on the java.library.path:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Sep 26, 2017 3:01:04 AMorg.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler["http-bio-8080"]
Sep 26, 2017 3:01:04 AMorg.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler["ajp-bio-8009"]
Sep 26, 2017 3:01:04 AM org.apache.catalina.startup.Catalinaload
INFO: Initialization processed in 413 ms
Sep 26, 2017 3:01:04 AMorg.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Sep 26, 2017 3:01:04 AMorg.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: ApacheTomcat/7.0.54
Sep 26, 2017 3:01:04 AMorg.apache.catalina.ha.tcp.SimpleTcpCluster startInternal
INFO: Cluster is about to start
Sep 26, 2017 3:01:04 AMorg.apache.catalina.tribes.transport.ReceiverBase bind
INFO: Receiver Server Socket boundto:/192.168.0.21:4000
Sep 26, 2017 3:01:04 AMorg.apache.catalina.tribes.membership.McastServiceImpl setupSocket
INFO: Setting cluster mcast soTimeout to500
Sep 26, 2017 3:01:04 AMorg.apache.catalina.tribes.membership.McastServiceImpl waitForMembers
INFO: Sleeping for 1000 milliseconds toestablish cluster membership, start level:4
Sep 26, 2017 3:01:05 AMorg.apache.catalina.ha.tcp.SimpleTcpCluster memberAdded
INFO: Replication memberadded:org.apache.catalina.tribes.membership.MemberImpl[tcp://{192, 168, 0,20}:4000,{192, 168, 0, 20},4000, alive=209565, securePort=-1, UDP Port=-1,id={6 -86 -55 -67 102 127 77 -57 -67 -67 -105 -20 -37 -55 76 -22 }, payload={},command={}, domain={}, ]
Sep 26, 2017 3:01:05 AMorg.apache.catalina.tribes.membership.McastServiceImpl waitForMembers
INFO: Done sleeping, membershipestablished, start level:4
Sep 26, 2017 3:01:05 AMorg.apache.catalina.tribes.membership.McastServiceImpl waitForMembers
INFO: Sleeping for 1000 milliseconds toestablish cluster membership, start level:8
Sep 26, 2017 3:01:05 AMorg.apache.catalina.tribes.io.BufferPool getBufferPool
INFO: Created a buffer pool with maxsize:104857600 bytes of type:org.apache.catalina.tribes.io.BufferPool15Impl
Sep 26, 2017 3:01:06 AM org.apache.catalina.tribes.membership.McastServiceImplwaitForMembers
INFO: Done sleeping, membershipestablished, start level:8
Sep 26, 2017 3:01:06 AMorg.apache.catalina.ha.deploy.FarmWarDeployer start
SEVERE: FarmWarDeployer can only work ashost cluster subelement!
Sep 26, 2017 3:01:06 AMorg.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory/var/lib/tomcat/webapps/ROOT
Sep 26, 2017 3:01:48 AMorg.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance forsession ID generation using [SHA1PRNG] took [41,566] milliseconds.
Sep 26, 2017 3:01:48 AMorg.apache.catalina.ha.session.DeltaManager startInternal
INFO: Register manager localhost# tocluster element Engine with name Catalina
Sep 26, 2017 3:01:48 AMorg.apache.catalina.ha.session.DeltaManager startInternal
INFO: Starting clustering manager atlocalhost#
Sep 26, 2017 3:01:48 AMorg.apache.catalina.ha.session.DeltaManager getAllClusterSessions
INFO: Manager [localhost#], requestingsession state from org.apache.catalina.tribes.membership.MemberImpl[tcp://{192,168, 0, 20}:4000,{192, 168, 0, 20},4000, alive=253179, securePort=-1, UDPPort=-1, id={6 -86 -55 -67 102 127 77 -57 -67 -67 -105 -20 -37 -55 76 -22 },payload={}, command={}, domain={}, ]. This operation will timeout if no sessionstate has been received within 60 seconds.
Sep 26, 2017 3:01:48 AMorg.apache.catalina.ha.session.DeltaManager waitForSendAllSessions
INFO: Manager [localhost#]; session statesend at 9/26/17 3:01 AM received in 114 ms.
Sep 26, 2017 3:01:48 AMorg.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web applicationdirectory /var/lib/tomcat/webapps/ROOT has finished in 42,286 ms
Sep 26, 2017 3:01:48 AMorg.apache.catalina.ha.session.JvmRouteBinderValve startInternal
INFO: JvmRouteBinderValve started
Sep 26, 2017 3:01:48 AMorg.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler["http-bio-8080"]
Sep 26, 2017 3:01:48 AMorg.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler["ajp-bio-8009"]
Sep 26, 2017 3:01:48 AMorg.apache.catalina.startup.Catalina start
INFO: Server startup in 44401 ms
集群session配置成功,使用瀏覽器訪問反代服務器的80端口檢驗效果
至此,httpd反代tomcat集群,並且tomcat集群session復制都成功實現了
Nginx反代tomat集群配置
在這裏我們為了節約資源就把nginx反代服務器配置到TomcatB服務器上
[[email protected] ~]# yum install -y nginx
[[email protected] ~]# cp/etc/nginx/nginx.conf{,.bak}
[[email protected] ~]# vim /etc/nginx/nginx.conf
Nginx默認配置文件需要修改以下幾處
定義後端tomcat集群
upstreamtomcatservers { server 192.168.0.20:8080; server 192.168.0.21:8080; }
註釋IPV6監聽
server { listen 80 default_server; #listen [::]:80default_server;
定義完全反代至後端tomcat集群
location / { root /usr/share/tomcat/wepapps/ROOT; index index.jsp; proxy_pass http://tomcatservers; }
定義讓後端tomcat集群獲取客戶端真實IP
proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
[[email protected] ~]# nginx -t
nginx: the configuration file/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conftest is successful
[[email protected] ~]#
繼httpd反代tomcat集群配置之後需要修改的地方
[[email protected] ~]# vim/etc/tomcat/server.xml
<ValveclassName="org.apache.catalina.valves.AccessLogValve"directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%{X-Forwarded-For}i %l %u %t "%r" %s %b"%{Referer}i" "%{User-Agent}i""/> <!--pattern="%h %l %u %t"%r" %s %b "%{Referer}i" "%{User-Agent}i""/> -->
[[email protected] ~]# systemctl restart tomcat
[[email protected] ~]# vim/etc/tomcat/server.xml
<ValveclassName="org.apache.catalina.valves.AccessLogValve"directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%{X-Forwarded-For}i %l %u %t "%r" %s %b"%{Referer}i" "%{User-Agent}i""/> <!--pattern="%h %l %u %t"%r" %s %b "%{Referer}i" "%{User-Agent}i""/> -->
[[email protected] ~]# systemctl restart tomcat
Httpd 反代tomcat集群無需在httpd添加額外傳遞客戶端ip的參數以及修改tomcat日誌記錄格式就可以讓後端tomcat記錄客戶端的真實IP,但nginx反代tomcat集群時必須首先在nginx上添加向後端tomat服務器傳遞客戶端真實IP的參數,其次還需要修改tomcat服務器的默認日誌記錄格式,才能成功記錄客戶端的真實IP
[[email protected] ~]# ss -tnl
State Recv-Q Send-Q LocalAddress:Port PeerAddress:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 :::8009 :::*
LISTEN 0 100 :::8080 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 50 ::ffff:192.168.0.21:4000 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8005 :::*
[[email protected] ~]# systemctl start nginx
[[email protected] ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 :::8009 :::*
LISTEN 0 100 :::8080 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 50 ::ffff:192.168.0.21:4000 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8005 :::*
[[email protected] ~]#
Nginx反代tomcat集群session復制效果檢驗
這裏主要是為了給大家展示tomcat集群session復制的效果,所以在httpd或者nginx反代tomat集群這方面並沒有給大家展示更多情況,httpd反代tomcat集群可以使用http協議、ajp協議、mod_jk協議三種方式反代,httpd反代tomcat集群時可以在實現session復制的同時,實現session粘性,nginx反代tomcat集群時還可以配置權重、健康狀態檢查、sorry server,在這裏不做全面展示,有需求者可查閱本博客相應專業板塊。
本文出自 “愛情防火墻” 博客,請務必保留此出處http://183530300.blog.51cto.com/894387/1969003
Tomcat集群session復制,httpd/nginx反代Tomcat集群