系列文章
Spring-Session基於Redis管理Session
前言
Nginx+Tomcat對Session的管理一直有了解,但是一直沒有實際操作一遍,本文從最簡單的安裝啓動開始,通過實例的方式循序漸進的介紹了幾種管理session的方式。
nginx安裝配置
1.安裝nginx
[root@localhost ~]# yum install nginx
提示報如下錯誤:
No package nginx available.
解決辦法安裝epel:EPEL是企業版 Linux 附加軟件包的簡稱,EPEL是一個由Fedora特別興趣小組創建、維護並管理的,針對 紅帽企業版 Linux(RHEL)及其衍生髮行版(比如 CentOS、Scientific Linux、Oracle Enterprise Linux)的一個高質量附加軟件包項目;
[root@localhost ~]# yum install epel-release
安裝完之後,即可成功安裝nginx;
2.啓動、停止nginx
先進入nginx的目錄
[root@localhost nginx]# cd /usr/sbin/
執行命令
./nginx 開啓
./nginx -s stop 使用kill命令強制殺掉進程
./nginx -s quit 待nginx進程處理任務完畢進行停止
./nginx -s reload
nginx+tomcat負載均衡
1.準備2個tomcat,分別指定端口爲8081,8082
drwxr-xr-x. 9 root root 4096 May 7 14:16 apache-tomcat-7.0.88_8081
drwxr-xr-x. 9 root root 4096 May 7 14:16 apache-tomcat-7.0.88_8082
修改webapps/ROOT的index.jsp,方便測試
<%
if(request.getSession().getAttribute("key")==null){
out.println("key is null,ready init.....");
request.getSession().setAttribute("key","value");
}else{
out.println("key is not null,key="+request.getSession().getAttribute("key"));
}
%>
<br>
sessionID:<%=session.getId()%>
<br>
sessionCreateTime:<%= session.getCreationTime() %>
<br>
<%
out.println("tomcat port 8081");
%>
最後的輸出在兩個tomcat下面指定各自的端口號8081和8082
2.nginx配置負載均衡(默認策略)
修改/etc/nginx/下面的nginx.conf
upstream tomcatTest {
server 127.0.0.1:8081; #tomcat-8081
server 127.0.0.1:8082; #tomcat-8082
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://tomcatTest;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
此處配置的負載均衡策略是默認的輪詢策略,nginx還支持其他策略包括:ip_hash、weight、fair(第三方)、url_hash(第三方);
默認策略每個web請求按時間順序逐一分配到不同的後端服務器,這種情況下每次請求都會創建一個新的session,下面做簡單測試:
第一次請求http://ip/
key is null,ready init.....
sessionID:E7A9782DED29FF04E21DF94078CB4F62
sessionCreateTime:1527732911441
tomcat port 8082
第二次刷新http://ip/
key is null,ready init.....
sessionID:7812E8E21DBB74CC7FBB75A0DFF2E9CB
sessionCreateTime:1527732979810
tomcat port 8081
第三次刷新http://ip/
key is null,ready init.....
sessionID:8895F41E299785A21995D5F8BB734B86
sessionCreateTime:1527733011878
tomcat port 8082
可以發現每次都產生一個新的session,而且消息按時間順序逐一分配到不同的後端服務器,一般需要保持session會話的網站都不允許出現每次請求都產生一個session;
3.nginx配置負載均衡(黏性Session)
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端服務器,可以解決session的問題;nginx可以通過在upstream模塊配置ip_hash來實現黏性Session;
upstream tomcatTest {
ip_hash;
server 127.0.0.1:8081; #tomcat-8081
server 127.0.0.1:8082; #tomcat-8082
}
下面做簡單測試:
第一次請求http://ip/
key is null,ready init.....
sessionID:859BADFB09A4ECEAEC5257F518C228A0
sessionCreateTime:1527734181450
tomcat port 8081
第二次刷新http://ip/
key is not null,key=value
sessionID:859BADFB09A4ECEAEC5257F518C228A0
sessionCreateTime:1527734181450
tomcat port 8081
第三次刷新http://ip/
key is not null,key=value
sessionID:859BADFB09A4ECEAEC5257F518C228A0
sessionCreateTime:1527734181450
tomcat port 8081
可以發現第一次請求設置了key=value,後面每次都能獲取到key值,sessionId沒有改變,tomcat也沒有改變,實現了黏性Session;
此時可以把port=8081的tomcat停掉,然後再觀察
第四次刷新http://ip/
key is null,ready init.....
sessionID:3C15FE2C8E8A9DCDC6EAD48180B78B80
sessionCreateTime:1527735994476
tomcat port 8082
第五次刷新http://ip/
key is not null,key=value
sessionID:3C15FE2C8E8A9DCDC6EAD48180B78B80
sessionCreateTime:1527735994476
tomcat port 8082
可以發現消息轉發到了tomcat-8082,並且session丟失,重新創建了新的session;
如何讓這種情況session不丟失,也有兩種方案:Session複製和Session共享;Session共享從擴展性,性能方面都更加好,下面重點介紹一下Session共享如何實現;
nginx+tomcat實現Session共享
Session共享思想就是把session保存到一個公共的地方,用的時候再從裏面取出來,具體這個公共的地方可以是:redis,db,memcached等,下面已redis爲實例
1.redis安裝配置
yum install redis
安裝完成以後配置文件/etc/redis.conf
啓動redis服務端
redis-server /etc/redis.conf
啓動客戶端
redis-cli
2.Tomcat引入依賴的jar
$TOMCAT_HOME/lib添加如下jar包
<dependency>
<groupId>com.bluejeans</groupId>
<artifactId>tomcat-redis-session-manager</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.2</version>
</dependency>
3.Tomcat修改配置
修改$TOMCAT_HOME/conf目錄下的context.xml文件
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="localhost"
port="6379"
database="0"
maxInactiveInterval="60"/>
Tomcat提供了一個開放的session管理和持久化的org.apache.catalina.session.ManagerBase,繼承這個抽象類並做一些簡單的配置,即可讓你的session管理類接管Tomcat的session讀取和持久化,這裏使用的是tomcat-redis-session-manager來管理session;
RedisSessionManager繼承於org.apache.catalina.session.ManagerBase類,對session的相關操作都在此類中;
4.測試
第一次請求http://ip/
key is null,ready init.....
sessionID:1131499E5A65DE1591152465E7B24B1F
sessionCreateTime:1527740273682
tomcat port 8081
第二次刷新http://ip/
key is not null,key=value
sessionID:1131499E5A65DE1591152465E7B24B1F
sessionCreateTime:1527740273682
tomcat port 8081
將tomcat-8081停掉, 第三次刷新http://ip/
key is not null,key=value
sessionID:1131499E5A65DE1591152465E7B24B1F
sessionCreateTime:1527740273682
tomcat port 8082
可以發現此時消息已經轉發到tomcat-8082節點了,但是session沒有改變,同時key也可以獲取到值;
5.查看redis
[root@localhost ~]# redis-cli
127.0.0.1:6379> keys *
1) "1131499E5A65DE1591152465E7B24B1F"
127.0.0.1:6379> get 1131499E5A65DE1591152465E7B24B1F
"\xac\xed\x00\x05sr\x00Dcom.orangefunction.tomcat.redissessions.SessionSerializationMetadataB\xd9\xd9\xf7v\xa2\xdbL\x03\x00\x01[\x00\x15sessionAttributesHasht\x00\x02[Bxpw\x14\x00\x00\x00\x10}\xc8\xc9\xcf\xf6\xc3\xb5Y\xc7\x0c\x8eF\xa5\xfaQ\xe8xsr\x00\x0ejava.lang.Long;\x8b\xe4\x90\xcc\x8f#\xdf\x02\x00\x01J\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x01c\xb4j\x94\x12sq\x00~\x00\x03\x00\x00\x01c\xb4j\x94\x12sr\x00\x11java.lang.Integer\x12\xe2\xa0\xa4\xf7\x81\x878\x02\x00\x01I\x00\x05valuexq\x00~\x00\x04\x00\x00\a\bsr\x00\x11java.lang.Boolean\xcd r\x80\xd5\x9c\xfa\xee\x02\x00\x01Z\x00\x05valuexp\x01q\x00~\x00\nsq\x00~\x00\x03\x00\x00\x01c\xb4j\x94*t\x00 1131499E5A65DE1591152465E7B24B1Fsq\x00~\x00\a\x00\x00\x00\x01t\x00\x03keyt\x00\x05valuew\b\x00\x00\x01c\xb4j\x94\x12"
可以發現redis裏面已經存放了session對象,並且使用sessionId作爲key值,存放了session的二進制數據;
總結
本文簡單介紹了Nginx整合Tomcat,以及Nginx的負載均衡策略,用實例的方式展示了默認策略和ip_hash策略對session的管理;最後介紹了使用session共享的方式來解決前兩種方式對session管理的弊端;後續繼續瞭解Tomcat是如何將session讀取和持久化交給其他系統管理的,session更新是否實時,序列化方案,有效期等問題。