Tomcat經過Redis實現session共享的完整部署記錄 Tomcat經過Redis實現session共享的完整部署記錄

Tomcat經過Redis實現session共享的完整部署記錄javascript

 

 

對於生產環境有了必定規模的tomcat集羣業務來講,要實現session會話共享,比較穩妥的方式就是使用數據庫持久化session.爲何要持久化session(共享session)呢?由於在客戶端每一個用戶的Session對象存在Servlet容器中,若是Tomcat服務器重啓或者宕機的話,那麼該session就會丟失,而客戶端的操做會因爲session丟失而形成數據丟失;若是當前用戶訪問量巨大,每一個用戶的Session裏存放大量數據的話,那麼就很佔用服務器大量的內存,進而導致服務器性能受到影響。數據庫持久化session,分爲物理數據庫和內存數據庫。物理數據庫備份session,因爲其性能緣由,不推薦;內存數據庫能夠使用redis和memcached,以前已經介紹了memcached方式實現session共享了,下面說下redis方式:php

可是有一點須要清楚:就是Redis這種方式目前還暫不支持Tomcat8環境(由於如今網上插件不支持tomcat8,非要支持tomcat8,則需修改插件jar包的源代碼)!下面就先說下Tomcat7+Redis+Nginx實現session會話共享的操做記錄:css

實現達到的效果
當客戶端訪問Nginx服務器時,Nginx負載均衡會自動將請求轉發到Tomcat1節點或Tomcat2節點服務器,以減輕Tomcat壓力,從而達到Tomcat集羣化部署,爲了使各Tomcat之間共享同一個Session,將採用Redis緩存服務來集中管理Session存儲。Nginx實現負載均衡,並使用Redis實現session共享。本案例採用的是redis單點方式(能夠給這個redis節點配置一個從節點,採用redis主從模式,鏈接redis的master節點.redis默認不支持主主模式),若是使用redis集羣方式,則採用Sentinels鏈接。html

1) 基礎信息java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ip                 主機名           應用                       端口
192.168.10.200     Nginx-node       nginx1.12.2                80
192.168.10.201     Tomcat-node1     java8.13一、tomcat7.0.90    8080
192.168.10.202     Tomcat-node2     java8.13一、tomcat7.0.90    8080
192.168.10.203     redis-node       redis4.0.1                6379
 
下面操做在三臺機器上一樣執行:
[root@Nginx-node ~] # cat /etc/redhat-release
CentOS release 6.9 (Final)
     
爲了方便測試,關閉iptables防火牆和selinux。若是是生產環境,開啓iptables後,須要開放對應的應用端口。
[root@Nginx-node ~] # setenforce 0
[root@Nginx-node ~] # getenforce
disabled
[root@Nginx-node ~] # cat /etc/sysconfig/selinux |grep "SELINUX=disabled"
SELINUX=disabled
[root@Nginx-node ~] # /etc/init.d/iptables stop
 
本案例環境部署中所需的軟件下載地址:https: //pan .baidu.com /s/1f9GM4iVAzfJsjskpAwv8Og
提取密碼:dsfs
下載到服務器上的 /usr/local/src 目錄下.另外:節點服務器的系統時間必定要保持一致!!

2) 安裝Nginx(在192.168.10.200機器上操做)node

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
[root@Nginx-node ~] # cd /usr/local/src/
[root@Nginx-node src] # ll
total 8920
-rw-rw-r--. 1 root root  981687 Oct 27  2017 nginx-1.12.2. tar .gz
-rw-rw-r--. 1 root root 5453234 Aug 23  2018 openssl-1.1.0i. tar .gz
-rw-rw-r--. 1 root root 2081413 Aug 23  2018 pcre-8.42. tar .gz
-rw-rw-r--. 1 root root  607698 Jan 16  2017 zlib-1.2.11. tar .gz
  
安裝依賴包
[root@Nginx-node src] # yum -y install gcc gcc-c++
  
安裝pcre庫
[root@Nginx-node src] # tar -zvxf pcre-8.42.tar.gz
[root@Nginx-node src] # cd pcre-8.42
[root@Nginx-node pcre-8.42] # ./configure && make && make install
  
安裝zlib庫
[root@Nginx-node pcre-8.42] # cd /usr/local/src/
[root@Nginx-node src] # tar -zvxf zlib-1.2.11.tar.gz
[root@Nginx-node src] # cd zlib-1.2.11
[root@Nginx-node zlib-1.2.11] # ./configure && make && make install
  
安裝openssl
[root@Nginx-node zlib-1.2.11] # cd /usr/local/src/
[root@Nginx-node src] # tar -zvxf openssl-1.1.0i.tar.gz
[root@Nginx-node src] # cd openssl-1.1.0i
[root@Nginx-node openssl-1.1.0i] # ./config && make && make install
  
安裝nginx,特別注意要指定prce zlib openssl原碼包位置
[root@Nginx-node openssl-1.1.0i] # cd /usr/local/src/
[root@Nginx-node src] # tar -zvxf nginx-1.12.2.tar.gz
[root@Nginx-node src] # cd nginx-1.12.2               
[root@Nginx-node nginx-1.12.2] # ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.42 --with-zlib=/usr/local/src/zlib-1.2.11  --with-openssl=/usr/local/src/openssl-1.1.0i
[root@Nginx-node nginx-1.12.2] # make && make install
  
安裝成功後配置nginx
[root@Nginx-node nginx-1.12.2] # cd /usr/local/nginx/conf/
[root@Nginx-node conf] # cp nginx.conf nginx.conf.bak
[root@Nginx-node conf] # cat nginx.conf
#user  nobody;
worker_processes  8;
   
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
   
#pid        logs/nginx.pid;
   
worker_rlimit_nofile 65535;
events {
    use epoll;
     worker_connections  65535;
}
   
http {
     include       mime.types;
     default_type  application /octet-stream ;
     charset utf-8;
           
     ######
     ## set access log format
     ######
     log_format  main  '$http_x_forwarded_for $remote_addr $remote_user [$time_local] "$request" '
                       '$status $body_bytes_sent "$http_referer" '
                       '"$http_user_agent" "$http_cookie" $host $request_time' ;
   
     #######
     ## http setting
     #######
     sendfile       on;
     tcp_nopush     on;
     tcp_nodelay    on;
     keepalive_timeout  65;
     fastcgi_connect_timeout 30000;
     fastcgi_send_timeout 30000;
     fastcgi_read_timeout 30000;
     fastcgi_buffer_size 256k;
     fastcgi_buffers 8 256k;
     fastcgi_busy_buffers_size 256k;
     fastcgi_temp_file_write_size 256k;
     fastcgi_intercept_errors on;
   
     ##cache##
     client_header_timeout 60s;
     client_body_timeout 60s;
     client_max_body_size 10m;
     client_body_buffer_size 1m;
     proxy_connect_timeout 5;
     proxy_read_timeout 60;
     proxy_send_timeout 5;            
     proxy_buffer_size 64k;
     proxy_buffers 4 128k;
     proxy_busy_buffers_size 128k;
     proxy_temp_file_write_size 1m;
     proxy_temp_path /home/temp_dir ;
     proxy_cache_path /home/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
     ##end##
   
     gzip  on;
     gzip_min_length  1k;
     gzip_buffers     4 16k;
     gzip_http_version 1.1;
     gzip_comp_level 9;
     gzip_types       text /plain application /x-javascript text /css application /xml text /javascript application /x-httpd-php ;
     gzip_vary on;
   
     ## includes vhosts
     include vhosts/*.conf;
}
  
[root@Nginx-node conf] # mkdir vhosts
[root@Nginx-node conf] # cd vhosts/
[root@Nginx-node vhosts] # vim lb_tomcat.conf
   upstream tomcat-lb {
       server 192.168.10.201:8080;
       server 192.168.10.202:8080;
       }
                     
       server {
       listen  80;
       server_name www.kevin.com;
       location / {
           proxy_pass http: //tomcat-lb ;
           proxy_set_header  X-Real-IP $remote_addr;
           proxy_set_header  REMOTE-HOST $remote_addr;
           proxy_set_header  Host $host;
           proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
         }
         location ~ .*\.(gif|jpg|png|htm|html|css|ico|flv|swf)(.*) {
               proxy_pass http: //tomcat-lb ;
               proxy_redirect off;
               proxy_set_header Host $host;
               proxy_cache cache_one;
               proxy_cache_valid 200 302 1h;
               proxy_cache_valid 301 1d;
               proxy_cache_valid any 10m;
               expires 30d;
               proxy_cache_key $host$uri$is_args$args;
         }
}
  
[root@Nginx-node vhosts] # /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx .conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx .conf test is successful
[root@Nginx-node conf] # /usr/local/nginx/sbin/nginx
[root@Nginx-node conf] # lsof -i:80
COMMAND   PID   USER   FD   TYPE   DEVICE SIZE /OFF NODE NAME
nginx   25292   root    6u  IPv4 19679665      0t0  TCP *:http (LISTEN)
nginx   25293 nobody    6u  IPv4 19679665      0t0  TCP *:http (LISTEN)
nginx   25294 nobody    6u  IPv4 19679665      0t0  TCP *:http (LISTEN)
nginx   25295 nobody    6u  IPv4 19679665      0t0  TCP *:http (LISTEN)
nginx   25296 nobody    6u  IPv4 19679665      0t0  TCP *:http (LISTEN)
nginx   25297 nobody    6u  IPv4 19679665      0t0  TCP *:http (LISTEN)
nginx   25298 nobody    6u  IPv4 19679665      0t0  TCP *:http (LISTEN)
nginx   25299 nobody    6u  IPv4 19679665      0t0  TCP *:http (LISTEN)
nginx   25300 nobody    6u  IPv4 19679665      0t0  TCP *:http (LISTEN)
  
將域名www.kevin.com解析到192.168.10.200上,訪問http: //www .kevin.com,發現訪問請求結果會負載到192.168.10.201和192.168.10.202的tomcat上了。

3)安裝tomcat(在192.168.10.201和192.168.10.202兩臺機器上操做)linux

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
安裝java8環境。先卸載掉系統自帶的java7,而後安裝java8
[root@Tomcat-node1 ~] # java -version
java version "1.7.0_131"
OpenJDK Runtime Environment (rhel-2.6.9.0.el6_8-x86_64 u131-b00)
OpenJDK 64-Bit Server VM (build 24.131-b00, mixed mode)
[root@Tomcat-node1 ~] # yum -y remove java-1.7.0-openjdk*
[root@Tomcat-node1 ~] # yum -y remove tzdata-java.noarch
[root@Tomcat-node1 ~] # java -version                
- bash : /usr/bin/java : No such file or directory
    
[root@Tomcat-node1 ~] # ll /usr/local/src/jdk-8u131-linux-x64_.rpm
-rw-rw-r--. 1 root root 169983496 Nov 19  2017 /usr/local/src/jdk-8u131-linux-x64_ .rpm
[root@Tomcat-node1 ~] # rpm -ivh /usr/local/src/jdk-8u131-linux-x64_.rpm --force
[root@Tomcat-node1 ~] # vim /etc/profile
......
JAVA_HOME= /usr/java/jdk1 .8.0_131
JAVA_BIN= /usr/java/jdk1 .8.0_131 /bin
PATH= /usr/local/sbin : /usr/local/bin : /usr/sbin : /usr/bin : /root/bin : /bin : /sbin/
CLASSPATH=.: /lib/dt .jar: /lib/tools .jar
export JAVA_HOME JAVA_BIN PATH CLASSPATH
    
[root@Tomcat-node1 ~] # source /etc/profile
[root@Tomcat-node1 ~] # java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
You have new mail in /var/spool/mail/root
    
安裝配置tomcat7
[root@Tomcat-node1 ~] # cd /usr/local/src/
[root@Tomcat-node1 src] # ll apache-tomcat-7.0.90.tar.gz
-rw-rw-r--. 1 root root 9472492 Nov  9  2017 apache-tomcat-7.0.90. tar .gz
[root@Tomcat-node1 src] # tar -zvxf apache-tomcat-7.0.90.tar.gz
[root@Tomcat-node1 src] # mv apache-tomcat-7.0.90 /usr/local/tomcat7
    
配置tomcat7
[root@Tomcat-node1 ~] # cd /usr/local/tomcat7/conf/
[root@Tomcat-node1 conf] # cp server.xml server.xml.bak
[root@Tomcat-node1 conf] # vim server.xml
.......
  <Engine name= "Catalina" defaultHost= "localhost" >                #這一行不須要修改,不必動
.......
  <Host name= "localhost" appBase= "/data/webapps/" unpackWARS= "true" autoDeploy= "true" >
         <Context path= "" docBase= "/data/webapps" reloadable= "true" />
......
         <Valve className= "org.apache.catalina.valves.AccessLogValve" directory= "/data/logs"
                prefix= "web1_access_log." suffix= ".txt"
                pattern= "%h %l %u %t " %r " %s %b" />
   < /Host >
 
 
建立項目目錄
[root@Tomcat-node1 conf] # mkdir -p /data/webapps/{WEB-INF,META-INF,classes,lib}
[root@Tomcat-node1 conf] # mkdir /data/logs
 
建立測試文件
[root@Tomcat-node1 ~] # touch /data/webapps/index.jsp
[root@Tomcat-node1 ~] # vim /data/webapps/index.jsp        
<html>
     <body bgcolor= "green" >    
     <center>   
     <%=  request.getSession().getId()  %>   
     <h1>192.168.10.201< /h1 >
     <h1>port:8080< /h1 >
     <h1>this is Tomcat-node1! < /h1 >
     < /center >
     < /body >
< /html >
  
<%@ page contentType= "text/html;charset=UTF-8" isELIgnored= "false" %>
SessionID:<%=session.getId()%><BR>
SessionIP:<%=request.getServerName()%> <BR>
SessionPort:<%=request.getServerPort()%>
<%     out.println( "This is Tomcat server 201 !" );     %>
You have new mail in /var/spool/mail/root
 
 
另外一個節點的測試文件爲:
[root@Tomcat-node2 ~] # vim /data/webapps/index.jsp
<html>
     <body bgcolor= "yellow" >    
     <center>   
     <%=  request.getSession().getId()  %>   
     <h1>192.168.10.202< /h1 >
     <h1>port:8080< /h1 >
     <h1>this is Tomcat-node2! < /h1 >
     < /center >
     < /body >
< /html >
  
<%@ page contentType= "text/html;charset=UTF-8" isELIgnored= "false" %>
SessionID:<%=session.getId()%><BR>
SessionIP:<%=request.getServerName()%> <BR>
SessionPort:<%=request.getServerPort()%>
<%     out.println( "This is Tomcat server 202 !" );     %>
 
 
接着配置Redis+Session共享
[root@Tomcat-node1 ~] # cd /usr/local/tomcat7/conf/
[root@Tomcat-node1 conf] # cp context.xml context.xml.bak
[root@Tomcat-node1 conf] # cat context.xml
......
     <Valve className= "com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> 
     <Manager className= "com.orangefunction.tomcat.redissessions.RedisSessionManager" 
              host= "192.168.10.203" 
              port= "6379" 
              database= "0"
              maxInactiveInterval= "60" />
 
 
===============================================================
舒適提示:
若是遠程redis設置了密碼,則就多加一行配置:password= "123456"
===============================================================
 
 
最後將依賴的三個jar包拷貝到tomcat7的lib目錄下
[root@Tomcat-node1 jar] # pwd
/usr/local/src/tomcat_redis_session/jar
[root@Tomcat-node1 jar] # ll
total 676
-rw-rw-r--. 1 root root 111969 Jul 29  2015 commons-pool2-2.4.2.jar
-rw-rw-r--. 1 root root 553762 Oct 16  2017 jedis-2.9.0.jar
-rw-rw-r--. 1 root root  20241 Aug 27 16:45 tomcat-redis-session-manager1.2.jar
[root@Tomcat-node1 jar] # \cp -rf ./* /usr/local/tomcat7/lib/
 
 
啓動tomcat
[root@Tomcat-node1 src] # /usr/local/tomcat8/bin/startup.sh
Using CATALINA_BASE:   /usr/local/tomcat8
Using CATALINA_HOME:   /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME:        /usr/java/jdk1 .8.0_131
Using CLASSPATH:       /usr/local/tomcat8/bin/bootstrap .jar: /usr/local/tomcat8/bin/tomcat-juli .jar
Tomcat started.
You have new mail in /var/spool/mail/root
[root@Tomcat-node1 src] # ps -ef|grep tomcat
root      8477     1 87 03:11 pts /0    00:00:03 /usr/java/jdk1 .8.0_131 /bin/java -Djava.util.logging.config. file = /usr/local/tomcat8/conf/logging .properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -classpath /usr/local/tomcat8/bin/bootstrap .jar: /usr/local/tomcat8/bin/tomcat-juli .jar -Dcatalina.base= /usr/local/tomcat8 -Dcatalina.home= /usr/local/tomcat8 -Djava.io.tmpdir= /usr/local/tomcat8/temp org.apache.catalina.startup.Bootstrap start
root      8528  6829  0 03:11 pts /0    00:00:00 grep tomcat
[root@Tomcat-node1 src] # lsof -i:8080
COMMAND  PID USER   FD   TYPE   DEVICE SIZE /OFF NODE NAME
java    8477 root   49u  IPv6 12974768      0t0  TCP *:webcache (LISTEN)

4) redis安裝nginx

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
[root@redis-node ~] # cd /usr/local/src/
[root@redis-node src] # ll redis-4.0.1.tar.gz
-rw-rw-r-- 1 root root 1711660 Aug 27 16:19 redis-4.0.1. tar .gz
 
編寫redis一鍵安裝腳本
[root@redis-node src] # cat redis_install.sh
#!/usr/bin/env bash
# It's Used to be install redis.
# Created on 2018/08/27 11:18.
# @author: wangshibo.
# Version: 1.0
    
function install_redis () {
#################################################################################################
         cd /usr/local/src
         tar -zxvf /usr/local/src/redis-4 .0.1. tar .gz
         cd redis-4.0.1
         make PREFIX= /usr/local/redis install
         mkdir -p /usr/local/redis/ {etc,var}
         rsync -avz redis.conf  /usr/local/redis/etc/
         sed -i 's@pidfile.*@pidfile /var/run/redis-server.pid@' /usr/local/redis/etc/redis .conf
         sed -i "s@logfile.*@logfile /usr/local/redis/var/redis.log@" /usr/local/redis/etc/redis .conf
         sed -i "s@^dir.*@dir /usr/local/redis/var@" /usr/local/redis/etc/redis .conf
         sed -i 's/daemonize no/daemonize yes/g' /usr/local/redis/etc/redis .conf
         sed -i 's/^# bind 127.0.0.1/bind 0.0.0.0/g' /usr/local/redis/etc/redis .conf
  #################################################################################################
}
    
install_redis
 
 
賦予腳本執行權限,並進行安裝
[root@redis-node src] # chmod 755 /usr/local/src/redis_install.sh
[root@redis-node src] # /bin/bash -x /usr/local/src/redis_install.sh
 
 
編寫redis-server啓動腳本
[root@redis-node src] # cat /etc/init.d/redis-server
#!/bin/bash
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig:   - 85 15
# description:  Redis is a persistent key-value database
# processname: redis-server
# config:      /usr/local/redis/etc/redis.conf
# config:      /etc/sysconfig/redis
# pidfile:     /usr/local/redis/var/redis-server.pid
   
# Source function library.
. /etc/rc .d /init .d /functions
   
# Source networking configuration.
. /etc/sysconfig/network
   
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
   
redis= "/usr/local/redis/bin/redis-server"
prog=$( basename $redis)
   
REDIS_CONF_FILE= "/usr/local/redis/etc/redis.conf"
   
[ -f /etc/sysconfig/redis ] && . /etc/sysconfig/redis
   
lockfile= /var/lock/subsys/redis-server
   
start() {
     [ -x $redis ] || exit 5
     [ -f $REDIS_CONF_FILE ] || exit 6
     echo -n $ "Starting $prog: "
     daemon $redis $REDIS_CONF_FILE
     retval=$?
     echo
     [ $retval - eq 0 ] && touch $lockfile
     return $retval
}
   
stop() {
     echo -n $ "Stopping $prog: "
     killproc $prog
     retval=$?
     echo
     [ $retval - eq 0 ] && rm -f $lockfile
     return $retval
}
   
restart() {
     stop
     start
}
   
reload() {
     echo -n $ "Reloading $prog: "
     killproc $redis -HUP
     RETVAL=$?
     echo
}
   
force_reload() {
     restart
}
   
rh_status() {
     status $prog
}
   
rh_status_q() {
     rh_status > /dev/null 2>&1
}
   
case "$1" in
     start)
         rh_status_q && exit 0
         $1
         ;;
     stop)
         rh_status_q || exit 0
         $1
         ;;
     restart)
         $1
         ;;
     reload)
         rh_status_q || exit 7
         $1
         ;;
     force-reload)
         force_reload
         ;;
     status)
         rh_status
         ;;
     condrestart|try-restart)
         rh_status_q || exit 0
             ;;
     *)
         echo $ "Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
         exit 2
esac
 
 
賦予腳本執行權限,並啓動redis-server
[root@redis-node src] # /etc/init.d/redis-server start
[root@redis-node src] # lsof -i:6379
COMMAND     PID USER   FD   TYPE   DEVICE SIZE /OFF NODE NAME
redis-ser 24439 root    6u  IPv4 24642923      0t0  TCP dns02.kevin.cn:6379 (LISTEN)
 
舒適提示:
須要將redis.conf文件中的bind改成本機ip.不能使用默認的127.0.0.1,不然遠程鏈接該redis就會失敗
[root@redis-node src] # vim /usr/local/redis/etc/redis.conf
.....
bind 192.168.10.203
 
重啓redis-server服務
[root@redis-node src] # /etc/init.d/redis-server restart
Stopping redis-server:                                     [  OK  ]
Starting redis-server:                                     [  OK  ]
[root@redis-node src] # lsof -i:6379
COMMAND    PID USER   FD   TYPE   DEVICE SIZE /OFF NODE NAME
redis-ser 8184 root    6u  IPv4 24688720      0t0  TCP dns02.kevin.cn:6379 (LISTEN)
 
最好在tomcat兩個節點上使用 "telnet 192.168.10.203 6379" 驗證下redis是否能成功鏈接

5) tomcat經過redis共享session測試
訪問http://www.kevin.com,不斷刷新頁面,發現頁面的其餘信息改變,可是sessionid保持不變,即說明實現了session共享!c++

關閉兩個節點中的任意一個tomcat服務,繼續訪問頁面,發現sessionid任然保持不變!web

而後在redis機器上查看有沒有上面sessionid的key值存在

?
1
2
3
4
[root@redis-node ~] # /usr/local/redis/bin/redis-cli -h 192.168.10.203 -p 6379
192.168.10.203:6379> keys *
1) "F7FE0AE8BD1654584A330A804A7BE5FE"
192.168.10.203:6379>

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />

     <Manager className= "com.orangefunction.tomcat.redissessions.RedisSessionManager"
              sentinels= "192.168.9.223:30001,192.168.9.223:30002" />          #這裏是redis哨兵的地址,多個sentinels集羣就寫多個ip地址
              maxInactiveInterval= "60"
              sentinelMaster= "mymaster"                                       #這裏是sentinel的master名稱,必定不要寫錯
              password= "123456"                                               #還有認證密碼

Tomcat+Redis Sentinel工做原理:
原此時咱們鏈接的不在是redis主從,而是直接鏈接哨兵,哨兵會監測後端的redis主從,當主掛掉以後,會挑選一臺從當主機,而後在自身的狀態中更新,此時,就不用擔憂主掛掉以後,IP飄移的問題了,哨兵能夠多個,當某個哨兵掛掉也不會影響。

網絡拓撲圖以下:

"com.naritech.nicole.gump.RedisSessionHandlerValve" />

<Manager className= "com.naritech.nicole.gump.RedisSessionManager"
     host= "192.168.10.203"
     port= "6379"
     database= "0"
     maxInactiveInterval= "60"
  />

案例中配置以下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[root@Tomcat-node1 ~] # cat /usr/local/tomcat8/conf/context.xml
<?xml version= "1.0" encoding= "UTF-8" ?>
<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License" ); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
 
       http: //www .apache.org /licenses/LICENSE-2 .0
 
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>
 
     <!-- Default set of monitored resources. If one of these changes, the    -->
     <!-- web application will be reloaded.                                   -->
     <WatchedResource>WEB-INF /web .xml< /WatchedResource >
     <WatchedResource>${catalina.base} /conf/web .xml< /WatchedResource >
     <Valve className= "com.naritech.nicole.gump.RedisSessionHandlerValve" />
     <Manager className= "com.naritech.nicole.gump.RedisSessionManager"
         host= "192.168.10.203"
         port= "6379"
         database= "0"
         maxInactiveInterval= "60"
      />
     <!-- Uncomment this to disable session persistence across Tomcat restarts -->
     <!--
     <Manager pathname= "" />
     -->
< /Context >

重啓tomcat服務

?
1
2
3
4
5
6
[root@Tomcat-node1 ~] # chmod 755 /usr/local/tomcat8/bin/*
[root@Tomcat-node1 ~] # ps -ef|grep tomcat|awk '{print $2}'|xargs kill -9
[root@Tomcat-node1 ~] # /usr/local/tomcat8/bin/startup.sh               
[root@Tomcat-node1 ~] # lsof -i:8080
COMMAND   PID USER   FD   TYPE DEVICE SIZE /OFF NODE NAME
java    13439 root   50u  IPv6 685905      0t0  TCP *:webcache (LISTEN)

最後訪問http://www.kevin.com頁面,不斷刷新,發現頁面內容發生改變了,可是sessionid一直不變!說明實現了session共享!!!

相關文章
相關標籤/搜索