1. 程式人生 > >tomcat+redis接上篇

tomcat+redis接上篇

資料庫允許遠端連線 把資料庫中的mysql資料庫中的user表裡的host項為localhost的改為 % update user set host = '%' where user = 'root' and host = "localhost";   會話是什麼 1. 由於HTTP協議是無狀態的協議,所以服務端需要記錄使用者的狀態時,就需要用某種機制來識具體的使用者,這個機制就是Session.典型的場景比如購物車,當你點選下單按鈕時,由於HTTP協議無狀態,所以並不知道是哪個使用者操作的,所以服務端要為特定的使用者建立了特定的Session,用用於標識這個使用者,並且跟蹤使用者,這樣才知道購物車裡面有幾本書。這個Session是儲存在服務端的,有一個唯一標識。在服務端儲存Session的方法很多,記憶體、資料庫、檔案都有。叢集的時候也要考慮Session的轉移,在大型的網站,一般會有專門的Session伺服器叢集,用來儲存使用者會話,這個時候 Session 資訊都是放在記憶體的,使用一些快取服務比如Memcached之類的來放 Session。 2. 思考一下服務端如何識別特定的客戶?這個時候Cookie就登場了。每次HTTP請求的時候,客戶端都會發送相應的Cookie資訊到服務端。實際上大多數的應用都是用 Cookie 來實現Session跟蹤的,第一次建立Session的時候,服務端會在HTTP協議中告訴客戶端,需要在 Cookie 裡面記錄一個Session ID,以後每次請求把這個會話ID傳送到伺服器,我就知道你是誰了。有人問,如果客戶端的瀏覽器禁用了 Cookie 怎麼辦?一般這種情況下,會使用一種叫做URL重寫的技術來進行會話跟蹤,即每次HTTP互動,URL後面都會被附加上一個諸如 sid=xxxxx 這樣的引數,服務端據此來識別使用者。 3. Cookie其實還可以用在一些方便使用者的場景下,設想你某次登陸過一個網站,下次登入的時候不想再次輸入賬號了,怎麼辦?這個資訊可以寫到Cookie裡面,訪問網站的時候,網站頁面的指令碼可以讀取這個資訊,就自動幫你把使用者名稱給填了,能夠方便一下使用者。這也是Cookie名稱的由來,給使用者的一點甜頭。 所以,總結一下: Session是在服務端儲存的一個數據結構,用來跟蹤使用者的狀態,這個資料可以儲存在叢集、資料庫、檔案中; Cookie是客戶端儲存使用者資訊的一種機制,用來記錄使用者的一些資訊,也是實現Session的一種方式。   會話保持的三種方法 (1) session sticky(貼) 基於hash 和cookie 來實現會話保持,簡單的負載均衡演算法     基於source_ip(源地址hash)   nginx: ip_hash 、 haproxy: source 、 lvs: sh (2) session cluster:delta session manager 基於tomcat叢集會話保持     分析:tomcat自身帶的機制 session cluster,基於組播的方式,一個tomcat 被使用者登入訪問,記錄session;通過組播給叢集中的其他機器複製一份;那麼當用戶再次訪問時,每個機器都有session 會話記錄;從而實現了會話保持 (3) session server:redis(store), memcached(cache) 共享儲存     分析:新建立一個存放各個tomcat session記錄的server,每臺tomcat伺服器都將自己的session記錄在這個伺服器中,使用者再次訪問,每臺tomcat 都從這個server中獲取;實現會話保持   第一種方法可以基於基於ip hash   http {     .........       upstream tomcat {             ip_hash;             server 10.0.0.111:8080 weight=1 max_fails=2 fail_timeout=2;
            server 10.0.0.112:8080 weight=1 max_fails=2 fail_timeout=2;      }      server {          listen       80;
         server_name  localhost;              location / {              proxy_pass http://tomcat
;
             proxy_set_header X-Real-IP $remote_addr;              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;              }        }      }   第二種基於tomcat會話叢集實現LNMT的會話保持  to  sesion 原理:Tomcat叢集的會話管理器,它通過將改變了會話資料同步給叢集中的其它節點實現會話複製。這種實現會將所有會話的改變同步給叢集中的每一個節點   nginx配置 upstream tomcat_srv {    #在http 段配置 server 192.168.30.106:8080 weight=1; server 192.168.30.7:8080 weight=2; }   location / {    #在server 段配置         proxy_pass http://tomcat;} location ~* \.(jsp|do)$ {         proxy_pass http://tomcat; }             tomcat叢集         第三種基於會話共享儲存 redis下載地址:http://download.redis.io/releases/  redis-4.0.6.tar.gz redis-4.0.6.tar.gz   redis安裝 cd /usr/local tar -zxvf redis-4.0.6.tar.gz yum install gcc cd redis-4.0.6 make MALLOC=libc cd src && make install   測試是否安裝完成 [[email protected] src]# ./redis-server 3110:C 23 May 03:50:41.620 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 3110:C 23 May 03:50:41.620 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=3110, just started 3110:C 23 May 03:50:41.620 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf 3110:M 23 May 03:50:41.621 * Increased maximum number of open files to 10032 (it was originally set to 1024).                 _._                                                              _.-``__ ''-._                                                    _.-``    `.  `_.  ''-._           Redis 4.0.6 (00000000/0) 64 bit   .-`` .-```.  ```\/    _.,_ ''-._                                    (    '      ,       .-`  | `,    )     Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379 |    `-._   `._    /     _.-'    |     PID: 3110   `-._    `-._  `-./  _.-'    _.-'                                    |`-._`-._    `-.__.-'    _.-'_.-'|                                   |    `-._`-._        _.-'_.-'    |           http://redis.io   `-._    `-._`-.__.-'_.-'    _.-'                                    |`-._`-._    `-.__.-'    _.-'_.-'|                                   |    `-._`-._        _.-'_.-'    |                                     `-._    `-._`-.__.-'_.-'    _.-'                                          `-._    `-.__.-'    _.-'                                                  `-._        _.-'                                                          `-.__.-'                                      #按 ctrl + c可以關閉視窗   以後臺程序方式啟動redis,修改一下引數 vi /usr/local/redis-4.0.6/redis.conf ..... bind 10.0.0.139 port 6379 daemonize yes ......   指定redis.conf檔案啟動 [[email protected]]# /usr/local/redis-4.0.6/src/redis-server /usr/local/redis-4.0.6/redis.conf   tomcat配置 jar包下載地址:https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/  tomcat-cluster-redis-session-manager.zip tomcat-cluster-redis-session-manager.zip   將資料包解壓後的lib目錄下的檔案放在tomcat伺服器的/usr/local/tomcat/lib/ 將資料包解壓後的conf目錄下的檔案放在tomcat伺服器的/usr/local/tomcat/conf/   修改tomcat配置檔案,將redis與tomcat相結合 [[email protected] ~]# vim /usr/local/tomcat/conf/context.xml <Context>       <Valve className="tomcat.request.session.redis.SessionHandlerValve" />     <Manager className="tomcat.request.session.redis.SessionManager" />   </Context>   [[email protected] ~]# vim /usr/local/tomcat/conf/redis-data-cache.properties redis.hosts=10.0.0.139:6379 redis.password=   重啟服務測試 在tomcat的webapps/ROOT目錄下,建立session.jsp檔案,新增以下內容 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>shared session</title> </head> <body>   session id=<%=session.getId()%>   tomcat 1 </body> </html>   兩臺tomcat的配置一樣   訪問http://10.0.0.101/session.jsp