首頁  >  文章  >  後端開發  >  session共享

session共享

WBOY
WBOY原創
2016-07-30 13:30:091208瀏覽
由於tomcat的同時數瓶頸問題,可以說使用tomcat的web應用,幾乎都存在session不同步問題。 借鏡網路上的資料,我也找時間實驗一把。 文中涉及的軟體下載和安裝,一一略過,想必大家也沒必要看。 註:本文不對memcached和redis做任何口水討論,望各個網友自行問谷歌和度娘。 (個人愚見,它們作為一個軟體,能獲得各自眾多支持者,想必它們自然有各自的優點,重點還是從實際需要出發,選擇合適自己的東東。)一一。 、nginx+tomcat+memcached 
2.tomcat配置

tomcat1(192.168.159.128:8081)

tomcat2(192.168.159.128:8082)

       首先,是設定tomcat,使其將session儲存到memcached上。有兩種方法:

方法一:在server.xml中設定。

找到host節點,加入
 

方法二:在context.xml中設定。

找到Context節點,加入

       其次,配置nginx,用於測試session保持共享。
upstream  xxy.com  {       server   192.168.159.128:8081 ;       server   192.168.159.128:8082 ; } log_format  www_xy_com  '$remote_addr - $remote_user [$time_local] $request '                '"$status" $body_bytes_sent "$http_referer"'                 '"$http_user_agent" "$http_x_forwarded_for"'; server {       listen  80;       server_name  xxy.com;       location / {                proxy_pass        http://xxy.com;                proxy_set_header   Host             $host;                proxy_set_header   X-Real-IP        $remote_addr;                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;       }       access_log  /data/base_files/logs/www.xy.log  www_xy_com; }最後,將你的應用程式放到兩個tomcat中,並依序啟動memcached、tomcat、nginx。存取你的nginx,可以發現兩個tomcat中的session可以保持共享了。

二、nginx+tomcat+redis   (依賴套件下載)

1.redis設定(192.168.159.131:163083530.

tomcat1( 192.168.159.130:8081)tomcat2(192.168.159.130:8082)3.nginx安裝在192.168.159.131。        首先,是設定tomcat,使其將session儲存到redis上。有兩種方法,也是在server.xml或context.xml中配置,不同的是memcached只需要加入一個manager標籤,而redis需要增加的內容如下:(
注意:valve標籤一定要在manager前面。

其次,配置nginx,用於測試session保持共享。

upstream  redis.xxy.com  {       server   192.168.159.130:8081;       server   192.168.159.130:8082; } log_format  www_xy_com  '$remote_addr - $remote_user [$time_local] $request '                '"$status" $body_bytes_sent "$http_referer"'                 '"$http_user_agent" "$http_x_forwarded_for"'; server {       listen  80;       server_name redis.xxy.com;        location / {                proxy_pass        http://redis.xxy.com;                proxy_set_header   Host             $host;                proxy_set_header   X-Real-IP        $remote_addr;                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;       }       access_log  /data/base_files/logs/redis.xxy.log  www_xy_com; }
最後,將你的應用程式放到兩個tomcat中,並依序啟動redis、tomcat、nginx。存取你的nginx,可以發現兩個tomcat中的session可以保持共享了。

上面
文章

中,有一點需要說明的是:

如果tomcat配置中,將manager放在server.xml中,那麼使用maven做熱部署時,會發生失敗。所以,本人推薦放在context.xml中。

以上就介紹了session分享,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn