Tomcat集羣—使用Redis3.0實現Session共享

Tomcat版本:Tomca8html

 

若是對此章節不瞭解,能夠查閱http://www.javashuo.com/article/p-fraugeep-cz.html(Keepalived + Nginx實現高可用Web負載均衡)。java

一、單節點訪問 http://192.168.186.129:8080/dubbo-web-boss/login.donginx

 

二、增長多一個消費者節點:192.168.186.129,以一樣的方式部署dubbo-web-boss工程。(另一個消費節點一樣的原理,在此再也不屢次描述)git

先驗證新增節點也可正常訪問 http://192.168.186.129:8080/dubbo-web-boss/login.dogithub

 

三、在Keepalived+Nginx組成的反向代理集羣中的三個節點同步增長以下兩處配置:web

user  root;redis

worker_processes  1;api

 

#error_log  logs/error.log;tomcat

#error_log  logs/error.log  notice;cookie

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

 

events {

    worker_connections  1024;

}

 

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;

 

    sendfile        on;

    #tcp_nopush     on;

 

    #keepalive_timeout  0;

    keepalive_timeout  65;

 

    #gzip  on;

      ## web-boss
      upstream web_boss {
         server 192.168.186.129:8080 weight=1 max_fails=2 fail_timeout=30s;
         server 192.168.186.132:8080 weight=1 max_fails=2 fail_timeout=30s;
         server 192.168.186.133:8080 weight=1 max_fails=2 fail_timeout=30s;
      }

 

    server {

        listen       88;

        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root   html;

            index  index.html index.htm;

        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

        ## web-boss Cluster

        location /dubbo-web-boss{

            root   html;

            index  index.html index.htm;

            proxy_pass  http://web_boss/dubbo-web-boss;

            proxy_set_header Host  $http_host;

            proxy_set_header Cookie $http_cookie;

            proxy_set_header X-Real-IP $remote_addr;

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_set_header X-Forwarded-Proto $scheme;

            client_max_body_size  100m;

        }

    }

}

 

四、重啓Nginx

# /usr/local/nginx/sbin/nginx -s reload

 

五、經過反向代理集羣的VIP訪問dubbo-web-boss時,有時能夠登陸成功,但有時又會提示驗證碼錯誤,緣由就是Session沒有同步。

http://192.168.186.50:88/dubbo-web-boss/login.do

 

接下來就是要解決Tomcat的Session共享問題,使用開源項目:

https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/tag/3.0.2

注意,由於咱們使用的是Redis3.0集羣,相應的插件必定要支持Redis3.0集羣。

192.168.186.129:7111

192.168.186.129:7112

192.168.186.132:7113

192.168.186.132:7114

192.168.186.133:7115

192.168.186.133:7116

 

六、點擊紅色框下載最新版本tomcat-cluster-redis-session-manager

解壓,找到lib目錄中的依賴的jar包

並將這些jar包上傳到Tomcat8中的 lib 目錄

 

找到conf目錄下的properties

編輯文件以下,按需調整

 

八、添加Tomcat的環境變量 (可選)

catalina.base="/home/huangkejie/dubbo/web/tomcat8.5.40/"

九、在Tomcat8中的 conf/context.xml 中增長以下兩行配置:

      <Valve className="tomcat.request.session.redis.SessionHandlerValve" />
       <Manager className="tomcat.request.session.redis.SessionManager" />

十一、在Tomcat的conf/web.xml中核對確認Tomcat的Session超時時間,默認爲30分鐘

  <!-- ==================== Default Session Configuration ================= -->

  <!-- You can set the default session timeout (in minutes) for all newly   -->

  <!-- created sessions by modifying the value below.                       -->

    <session-config>

        <session-timeout>30</session-timeout>

    </session-config>

可按需修改。

最後附上做者的readme說明內容

/**
 * Tomcat clustering with Redis data-cache implementation.
 *
 * author: Ranjith Manickam
 */

Supports:
   - Apache Tomcat 7
   - Apache Tomcat 8
   - Apache Tomcat 9

Pre-requisite:
	1. jedis.jar
	2. commons-pool2.jar
	3. slf4j-api.jar

more details.. https://github.com/ran-jit/tomcat-cluster-redis-session-manager/wiki

Steps to be done,
	1. Move the downloaded jars to tomcat/lib directory
		- tomcat/lib/

	2. Add tomcat system property "catalina.base"
		- catalina.base="/home/huangkejie/dubbo/web/tomcat8.5.40/"

	3. Extract downloaded package (tomcat-cluster-redis-session-manager.zip) to configure Redis credentials in redis-data-cache.properties file and move the file to tomcat/conf directory
		- tomcat/conf/redis-data-cache.properties

	4. Add the below two lines in tomcat/conf/context.xml
		<Valve className="tomcat.request.session.redis.SessionHandlerValve" />
		<Manager className="tomcat.request.session.redis.SessionManager" />

	5. Verify the session expiration time (minutes) in tomcat/conf/web.xml
		<session-config>
			<session-timeout>60<session-timeout>
		<session-config>

Note:
  - All your session attribute values must implement java.io.Serializable.
  - Supports redis default, sentinel and cluster based on the redis-data-cache.properties configuration.
相關文章
相關標籤/搜索