memcached演練(4) 使用apache+memcached-session-manager+tomcat集群,存在一些問(wèn)題。問(wèn)題列表
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名注冊(cè)、網(wǎng)站空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、婺城網(wǎng)站維護(hù)、網(wǎng)站推廣。
問(wèn)題1:2個(gè)tomcat節(jié)點(diǎn),apache,memcached服務(wù)器均放在一個(gè)虛擬機(jī),不能真正模擬真實(shí)環(huán)境。
問(wèn)題2:不能整整模擬分布式環(huán)境。
本文,重新設(shè)計(jì)下物理結(jié)構(gòu)。沿用《memcached演練(4) 使用apache+memcached-session-manager+tomcat集群》配置。僅僅將主機(jī)進(jìn)行了調(diào)整。
操作步驟。
1.將配置好的tomcat節(jié)點(diǎn)分別復(fù)制到 2個(gè)主機(jī)
scp -r apache-tomcat-node1 root@hadoop2:/u01 scp -r apache-tomcat-node2 root@hadoop3:/u01
2.修改apache的worker.properties
[root@hadoop1 /]# vi /etc/httpd/conf/workers.properties worker.list=loadbalancer worker.tomcat1.port=8009 worker.tomcat1.host=192.168.163.156 worker.tomcat1.type=ajp13 worker.tomcat1.lbfactor=1 worker.tomcat2.port=8019 worker.tomcat2.host=192.168.163.166 worker.tomcat2.type=ajp13 worker.tomcat2.lbfactor=1 worker.loadbalancer.type=lb worker.loadbalancer.balance_workers=tomcat1,tomcat2 worker.loadbalancer.sticky_session=true
3.修改每個(gè)節(jié)點(diǎn)的tomcat的context相關(guān)MSM配置
指向192.168.163.146:11211
4.測(cè)試下吞吐量
c:\Apache24\bin>ab -n 10000 -c 4 http://192.168.163.146/examples/testSession.jsp This is ApacheBench, Version 2.3 <$Revision: 1604373 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 192.168.163.146 (be patient) Completed 1000 requests ... Completed 10000 requests Finished 10000 requests Server Software: Apache/2.2.15 Server Hostname: 192.168.163.146 Server Port: 80 Document Path: /examples/testSession.jsp Document Length: 331 bytes Concurrency Level: 4 Time taken for tests: 10.322 seconds Complete requests: 10000 Failed requests: 0 Total transferred: 5930000 bytes HTML transferred: 3310000 bytes Requests per second: 968.84 [#/sec] (mean) Time per request: 4.129 [ms] (mean) Time per request: 1.032 [ms] (mean, across all concurrent requests) Transfer rate: 561.06 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.4 1 5 Processing: 0 4 11.7 3 585 Waiting: 0 4 11.7 3 585 Total: 0 4 11.7 3 585 ERROR: The median and mean for the initial connection time are more than twice the standard deviation apart. These results are NOT reliable.
吞吐量:968.84。比上節(jié)中的測(cè)試結(jié)果“727.55”超出不少。
經(jīng)過(guò)多次測(cè)試,差別有點(diǎn)大。
Requests per second: 1281.74 [#/sec] (mean)
Requests per second: 1451.22 [#/sec] (mean)
模擬一臺(tái)節(jié)點(diǎn)宕機(jī)情況,測(cè)試吞吐量
c:\Apache24\bin>ab -n 10000 -c 4 http://192.168.163.146/examples/testSession.jsp This is ApacheBench, Version 2.3 <$Revision: 1604373 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 192.168.163.146 (be patient) Completed 1000 requests ... Finished 10000 requests Server Software: Apache/2.2.15 Server Hostname: 192.168.163.146 Server Port: 80 Document Path: /examples/testSession.jsp Document Length: 331 bytes Concurrency Level: 4 Time taken for tests: 7.994 seconds Complete requests: 10000 Failed requests: 0 Total transferred: 5930000 bytes HTML transferred: 3310000 bytes Requests per second: 1250.92 [#/sec] (mean) Time per request: 3.198 [ms] (mean) Time per request: 0.799 [ms] (mean, across all concurrent requests) Transfer rate: 724.41 [Kbytes/sec] received
吞吐量:1250.92。FUCK出乎意料啊。竟然比2個(gè)節(jié)點(diǎn)的情況下還高。
多次測(cè)試,發(fā)現(xiàn)均在1700左右。
Requests per second: 1729.64 [#/sec] (mean)
Requests per second: 1779.67 [#/sec] (mean)。
結(jié)果出乎我的意料。 記住這個(gè)結(jié)果,以后做個(gè)監(jiān)控分析下。
最后討論
關(guān)于session應(yīng)該不應(yīng)該使用memcached存儲(chǔ),可詳見(jiàn)《為什么不能用memcached存儲(chǔ)Session》
簡(jiǎn)單整理下表格
usual session pattern | usual memcached session pattern |
* Blobs (250 bytes to 5k+) * Read from datastore on every single page load * Usually written to the datastore on every page load * Reaped from the DB after inactivity | * Read from memcached on page load * 'set' over the existing session with a new expiration time * Cache misses mean user is logged out |
1.Run your instances out of memory and people get logged out early, or can't log in at all 2.Upgrading memcached, the OS, hardware, etc, now kicks people off 3.Adding or removing servers to the cluster, now kicks people off | |
如果非常希望借助memcached提高Session讀取速度,那么可以借鑒Norkūnas提出的memcached+RDBMS(在有些情況下,NOSQL也可以)的模式:
當(dāng)用戶登錄時(shí),將Session “set”到memcached,并寫(xiě)入數(shù)據(jù)庫(kù);
在Session中增加一個(gè)字段,標(biāo)識(shí)Session最后寫(xiě)入數(shù)據(jù)庫(kù)的時(shí)間;
每個(gè)頁(yè)面加載的時(shí)候,優(yōu)先從memcached讀取Session,其次從數(shù)據(jù)庫(kù)讀?。?/p>
每加載N頁(yè)或者Y分鐘后,再次將Session寫(xiě)入數(shù)據(jù)庫(kù);
從數(shù)據(jù)庫(kù)中獲取過(guò)期Session,優(yōu)先從memcached中獲取最新數(shù)據(jù)。
內(nèi)容來(lái)自:http://www.infoq.com/cn/news/2015/01/memcached-store-session