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
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"
- />
<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;
- }
- }
- }
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; } } }
以上轉載 http://passover.blog.51cto.com/2431658/648182
關於多tomcat之間的session共享問題,我們的實現是放棄了使用session,通過寫cookie的方式,在過濾器中檢測是否有相應的cookie,然後放入到request裏面。後來發現再多個環境切換的時候,會出現混亂。進而使用過了ThreadLocal,如果有相應的cookie,則將數據放入ThreadLocal中,而ThreadLocal爲每一個線程都提供了變量的副本,使得每個線程在某一時間訪問到的並不是同一個對象,這樣就隔離了多個線程對數據的數據共享。