0x0 前言
近日,為解決生產(chǎn)環(huán)境熱部署問(wèn)題,決定在服務(wù)器中增加一個(gè)tomcat組成集群,利用集群解決熱部署問(wèn)題。這樣既能解決高并發(fā)瓶頸問(wèn)題,又能解決熱部署(不影響用戶(hù)使用的情況下平滑更新生產(chǎn)服務(wù)器)問(wèn)題。因為項目是前后端分離的,所以本以為成本很低,沒(méi)想到遇到了一系列的坑,解決了2天才搞定,發(fā)現了很多不是集群而是項目本身的問(wèn)題。我是同一個(gè)服務(wù)器下配置tomcat和nginx等,本文主要面向有一定基礎的讀者,基本配置就不在本文累述了(基礎問(wèn)題可以留言或者發(fā)郵件)。1
2
3
4
5
0x0_1 服務(wù)器環(huán)境
服務(wù)器: CentOS 6.5web容器:Tomcat 7.0.25反向代理:Nginx 1.8java: jdk 1.71
2
3
4
5
0x0_2 集群
我是同一個(gè)服務(wù)器部署兩個(gè)tomcat,所以地址是127.0.0.1 也可以換成其他服務(wù)器的ip
tomcat文件名ip地址端口項目名
tomcat7-1127.0.0.18080borrow
tomcat7-2127.0.0.18090borrow
0x1 tomcat集群配置
修改tomcat的conf/server.xml 如下
tomcat7-1:
<Server port="8005" shutdown="SHUTDOWN"><Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" compression="on" compressionMinSize="2048" noCompressionUserAgents="gozilla,traviata" compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain" /><Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />1
2
3
4
5
6
7
8
9
tomcat7-2:
<Server port="8015" shutdown="SHUTDOWN"><Connector port="8090" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" compression="on" compressionMinSize="2048" noCompressionUserAgents="gozilla,traviata" compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain" /><Connector port="8019" protocol="AJP/1.3" redirectPort="8443" />1
2
3
4
5
6
7
8
9
以上配置,只有端口號(port)是必要配置,其他按平常配置即可,多個(gè)tomcat以此類(lèi)推。
創(chuàng )建測試頁(yè)面
自己分別在tomcat中創(chuàng )建測試頁(yè)面,可以區分不同的tomcat服務(wù)器即可
<table align="centre" border="1"> <tr> <td>Tomcat7_1 Session ID</td> <% session.setAttribute("www.zemo.com","www.zemo.com"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr></table>1
2
3
4
5
6
7
8
9
10
11
啟動(dòng)2個(gè)tomcat,確認都啟動(dòng)了即可。
0x2 nginx均衡負載配置
這里使用ip_hash 集群方式,盡量分流。但是如果是同一個(gè)局域網(wǎng)下用戶(hù)大量訪(fǎng)問(wèn)就沒(méi)有用了,不過(guò)可以再增加其他的措施,比如緩存session,因為這里主要是為了解決熱部署問(wèn)題, 所以都不是問(wèn)題。
upstream borrow { ip_hash; server 127.0.0.1:8080 weight=1 max_fails=3 fail_timeout=20s; server 127.0.0.1:8090 weight=1 max_fails=3 fail_timeout=20s;}server { listen 80; server_name www.zemo.com;#這個(gè)是隨便寫(xiě)的...不用訪(fǎng)問(wèn)了 access_log /usr/local/var/log/nginx/borrow_access.log; location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; client_max_body_size 20m; client_body_buffer_size 128k; proxy_connect_timeout 600; proxy_send_timeout 600; proxy_read_timeout 900; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; proxy_pass http://borrow; #這里borrow 指向上面名為borrow的upstream }}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
weight 訪(fǎng)問(wèn)權重
max_fails 最大失敗次數
fail_timeout 最大失敗等待時(shí)間
測試配置是否正確
zemochen:Program SuperZemo$ sudo nginx -tnginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is oknginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful1
2
3
重啟nginx
zemochen:Program SuperZemo$ sudo nginx -s reload1
觀(guān)察tomcat的session
先訪(fǎng)問(wèn)以下www.zemo.com
關(guān)閉tomcat7_1,只留tomcat7_2觀(guān)察session id
開(kāi)啟tomcat7_1,關(guān)閉tomcat7_2觀(guān)察session id
你會(huì )發(fā)現兩個(gè)session id,下面講解tomcat 的session共享
0x3 tomcat session共享
0x3_1 server.xml配置
tomcat 有自帶復制session功能,在server.xml中增加<Cluster> 配置即可,不知道寫(xiě)在哪里的,找到server.xml中的<Cluster>注釋樣例,寫(xiě)在下面即可?;旧暇褪菍?xiě)在<Host></Host>里面最后就行了。配置如下:
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6"> <Manager className="org.apache.catalina.ha.session.BackupManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true" mapSendOptions="6"/> <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="127.0.0.1" <!--這里寫(xiě)本tomcat的IP地址 --> port="5000" selectorTimeout="100" /> <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"/> <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/> </Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/> <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>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
修改server.xml 中的<Engine> 增加jvmRoute="tomcat7-1",配置如下:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat7-1">1
tomcat7-2同理,配置這兩個(gè)。
0x3_2 web.xml配置
在conf/web.xml中的<webapp></webapp> 中增加<distributable /> 我的項目是j2ee項目,所以寫(xiě)在了項目的web.xml 里,項目中的web.xml優(yōu)先級高于tomcat本身的。
配置代碼塊如下:
<web-app> <!--其他配置省略,下面mime-mapping 也是與本問(wèn)無(wú)關(guān),為了體現最下面所以帶了一部分--> <mime-mapping> <extension>apk</extension> <mime-type>application/vnd.android.package-archive</mime-type> </mime-mapping> <distributable /></web-app>1
2
3
4
5
6
7
8
0x4 注意:
這里要注意,Nginx只需要配置一份,而tomcat有幾個(gè)節點(diǎn),server.xml 、web.xml就配置幾個(gè):
shutdown端口都不同
Connect 的http端口都不同
Ajp 端口不同
<Cluster> 中的<Receiver> 屬性 address 指向改tomcat的IP地址
<Engine> 中的jvmRoute 名要與tomcat文件名相同
0x5 結果截圖
整個(gè)環(huán)境全都啟動(dòng)后, 訪(fǎng)問(wèn)域名, 可以看到如下截圖,我的圖可以看出,訪(fǎng)問(wèn)到的是tomcat7-1,注意,我的測試網(wǎng)頁(yè)并沒(méi)有寫(xiě)tomcat7-1,而是自己加到session id里的,這里是自動(dòng)增加jvmRoute的值,說(shuō)明session復制成功…
因為剛才確定被代理到了tomcat7-1,我們用的是ip_hash的分配方式,所以我們直接關(guān)閉tomcat7-1,再刷新頁(yè)面,確保代理到另一個(gè)tomcat,效果圖如下,可以看到只有后綴.tomcat7-2 不同,至此一個(gè)均衡負載集群就可以了,既解決了熱部署問(wèn)題,又解決了分擔服務(wù)器壓力,負載均衡問(wèn)題。
0x6關(guān)于我
@Author:Zemo
@Email:zemochen#126.com
J2EE 兩年多研發(fā)經(jīng)驗,日常公司打雜,處于迷茫期
歡迎轉載,讓更多的人學(xué)到東西