1 起因
最近對新開發的web系統進行了壓力測試,發現tomcat預設配置下壓到600人的並發登入首頁響應速度就有比較嚴重的影響,一輪出現2000多個的500和502錯誤。我把登入的時間統計做了一下,把伺服器處理總時間列印出來,看了一下發現有個別響應確實在20秒,但平均時間和lr測試出來的還是相差很遠。所以可以斷定不是程式處理處理花費了這麼多時間,由於在區域網路測試,所以也可以排除網路問題。這就把問題圈定在tomcat的請求響應能力上了。先把tomcat線程數提升到1000,發現500和502的報錯降到幾十個,但是回應時間上還沒什麼提高。後來啟動了2個tomcat,用nginx做負載平衡,回應時間下降了40%,兩個tomcat的處理時間長度都保持在1秒左右。
看來tomcat效能確實是系統的一個瓶頸,很有必要假設多個伺服器來加強響應能力。之前由於只是測試登入,多個tomcat還不用共用session,但真正使用時是必須要能一起工作的。現記錄一下負載平衡的安裝配置過程。
2 解決方案的選擇
多個tomcat要一起協同工作有幾種辦法,可以考慮的方案有以下幾個:
1. 使用tomcat內建的cluster方式,多個tomcat見自動即時複製session資訊,配置起來很簡單。但這個方案的效率比較低,在大並發下表現並不好。
2. 利用nginx的基於訪問ip的hash路由策略,保證訪問的ip始終被路由到同一個tomcat上,這個配置更簡單。但是我們的應用很可能是某一個區域網路大量使用者同時登入,這樣負載平衡就沒什麼作用了。
3. 利用memcached把多個tomcat的session集中管理,這是最直接的解決方案,但是操作起來也最為複雜。
我們的系統既要求效能,又要比較好的利用上負載平衡,所以第3個方案是首選。接下來就是安裝搭建之路了。
3 安裝配置
3.1 memcached的安裝
1)先下載libevent-1.4.14b-stable.tar.gz和memcached-1.4.7.tar.gz的源碼包,前者是後者的依賴包,就是一個事件驅動的包。
2)安裝非常順利,還是經典的那幾個編譯安裝命令:
tar zxvf libevent-1.4.14b-stable.tar.gz
cd libevent-1.4.14b-stable
./configure --prefix=/usr/local/libevent-1.4.14b
make
make install
tar zxvf memcached-1.4.7.tar.gz
cd memcached-1.4.7
./configure --prefix=/usr/local/memcached-1.4.7 --with-libevent=/usr/local/libevent-1.4.14b/
make
make install
3)啟動memcached:
./bin/memcached -d -m 256 -u root -p 11211 -c 1024 -P /tmp/memcached.pid
3.2 memcached-session-manager配置
讓tomcat調用memcached來儲存session早就是一個很成熟的解決方案了,開源的msm就可以解決這個問題。比較折騰的就是要用到的jar包,官方文檔說的也比較含糊,我這裡用的是kryo的序列化方案,所以用到的包多一些,分別是:
kryo-1.03.jar
kryo-serializers-0.8.jar
memcached-2.5.jar(我在官方看最新已經到2.7了,但是msm官方說用2.5,可能新包沒測試過,特別是2.6版本changelog裡面提到api有調整,還是不要亂升的好)
memcached-session-manager-1.5.1.jar
memcached-session-manager-tc7-1.5.1.jar
minlog-1.2.jar
msm-kryo-serializer-1.5.1.jar
reflectasm-0.9.jar
以上這些包都放在$CATALINA_HOME/lib目錄下。
另外提一下,官方給出的4種序列化方案,其中kryo是效率最高的,具體比較看http://code.google.com/p/memcached-session-manager/wiki/SerializationStrategies。
接下來是修改tomcat的設定檔$CATALINA_HOME/conf/context.xml,調整成新的session儲存方式。設定檔中加入以下內容:
- <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
- memcachedNodes="n1:127.0.0.1:11211"
- sticky="false"
- lockingMode="auto"
- sessionBackupAsync="false"
- sessionBackupTimeout="1000"
- transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
- />
在$CATALINA_HOME/conf/logging.properties檔案中添加de.javakaffee.web.msm.level=FINE,就可以在catalina.out的日誌中看到詳細的session存取情況。
另外在Manager配置中加上requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$",用chrome瀏覽器測試發現居然sessionID會突然變掉,然後就被攔截器給跳回首頁了。去掉就一切正常,但攔截器只會去檢測action的,按理說應該完全沒關係,望高人指點!
3.3 nginx配置
nginx非常簡單,只要在upstream裡面多配置幾個server就可以了,這裡把我的配置貼出來:
- #user nobody;
- worker_processes 16;
-
-
- events {
- use epoll;
- worker_connections 65535;
- }
-
-
- http {
- include mime.types;
- default_type application/octet-stream;
-
- #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- # '$status $body_bytes_sent "$http_referer" '
- # '"$http_user_agent" "$http_x_forwarded_for"';
-
- #access_log logs/access.log main;
-
- client_header_buffer_size 32k;
- large_client_header_buffers 4 32k;
- client_max_body_size 8m;
- client_body_buffer_size 128k;
-
- sendfile on;
- tcp_nopush on;
-
- #keepalive_timeout 0;
- keepalive_timeout 65;
-
- gzip on;
- gzip_types text/javascript text/plain text/css application/xml application/x-javascript;
- gzip_disable "MSIE [1-6]\.(?!.*SV1)";
-
- proxy_connect_timeout 300;
- proxy_send_timeout 300;
- proxy_read_timeout 300;
- proxy_buffer_size 16k;
- proxy_buffers 4 32k;
-
- proxy_set_header X-Forwarded-For $remote_addr;
- proxy_set_header Connection Close;
- server_names_hash_max_size 1024;
- server_names_hash_bucket_size 1024;
-
- # Default cache parameters for use by virtual hosts
- # Set the cache path to tmpfs mounted disk, and the zone name
- # Set the maximum size of the on disk cache to less than the tmpfs file system size
- proxy_cache_path ./cache levels=1:2 keys_zone=pscms:100m max_size=800m;
- proxy_temp_path ./proxy;
-
- #配置後端伺服器資訊
- upstream web_server {
- #ip_hash;
- server localhost:8080 max_fails=3 fail_timeout=30s;
- server localhost:8180 max_fails=3 fail_timeout=30s;
- }
-
- server {
- listen 8888; ## listen for ipv4
- #listen [::]:80 default ipv6only=on; ## listen for ipv6
- server_name localhost;
-
- charset utf-8;
- log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"';
- access_log logs/host.access.log main;
- #access_log off;
-
- location ~ .*\.(jsp|action)?$ {
- proxy_set_header Host $http_host;
- proxy_redirect off;
- proxy_pass http://web_server;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- }
-
- location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {
- #如果後端的伺服器返回502、504、執行逾時等錯誤,自動將請求轉寄到upstream負載平衡池中的另一台伺服器,實現容錯移轉。
- proxy_next_upstream http_502 http_504 error timeout invalid_header;
-
- proxy_cache pscms; #進行緩衝,使用Web緩衝區cache_one
- proxy_cache_valid 200 304 1h; #對不同的HTTP狀態代碼設定不同的緩衝時間
- proxy_cache_valid 301 302 5m;
- proxy_cache_valid any 1m;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $remote_addr;
- proxy_set_header Accept-Encoding ""; #(或是後台伺服器關閉gzip),這樣這台機器才不會緩衝被壓縮的檔案,造成亂碼
- proxy_ignore_headers "Cache-Control" "Expires"; #這段配置加上後,proxy_cache就能支援後台設定的expires。
- proxy_pass http://web_server;
- expires 15m;
- }
-
- location / {
- proxy_set_header Host $http_host;
- proxy_redirect off;
- proxy_pass http://web_server;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- }
-
- }
-
- }
參考文檔:
1. http://code.google.com/p/memcached-session-manager/wiki/SetupAndConfiguration
2. http://wangrui.iteye.com/blog/500921
3 http://passover.blog.51cto.com/2431658/648182