首頁  >  文章  >  後端開發  >  nginx+tomcat+redis實作session共享

nginx+tomcat+redis實作session共享

WBOY
WBOY原創
2016-08-08 09:24:041736瀏覽

nginx 作為目前最受歡迎的開源反向代理HTTP Server,用於實現資源快取、web server負載等功能,由於其輕量級、高效能、高可靠度特徵在高績效、高可靠性等功能互聯網專案中有著非常普遍的應用,相關概念網上有豐富的介紹。分散式web server叢集部署後需要實作session共享,針對 tomcat 伺服器的實作方案 hash策略、nginx sticky module等方案,本文主要介紹了使用 redis 伺服器進行 session    相關應用結構參考下圖:二、環境配置  測試環境基於 

Linux CentOS 6.5

nginx 

相關環境,不作詳細描述,本文檢驗配置如下: Version IP_Port

 IP_Port1 7.0.54 2.8.19

三、建置 tomcat-redis-session-manager-master

1、由於原始碼建置基於 gradle,請先設定版本。 2

、從 

github 取得 tomcat-redis-session-manager-master ?1. https://github.com/jcoleman/tomcat-redis-session-manager

3

、找到源碼中的 build.gradle 文件,由於作者使用了第三方倉庫( ),需要註冊帳號,太麻煩,註釋後直接使用

maven

中央倉庫,同時註釋簽名相關腳本並增加依賴包的輸出腳本 

copyJarsdist目錄),修改後的build.gradle 文件如下:view sourceprint?. 003.apply plugin: 'signing'004. 005.group = 'com.orangefunction'006.version = '2.0.0'com )

010. }011. 012.compileJava {013.sourceCompatibility = 1.7

014.targetCompatibility = 1.7

014.targetCompatibility dependencies {

018.compile group: 'org. apache。 compile group: 'org.apache.commons', name: 'commons-pool2', version: '2.2'

021.//compile group: 'commons-codec', name: 'commons- 1.9'

022. 

023.testCompile group: 'junit', name: 'junit', version: '4.+'

024.testile version: '4.+'

core',0.com testCompile 'org.hamcrest:hamcrest-library:1.3'

026.testCompile 'org.mockito:mockito-all:1.9.5'

027.testCompile group: 'org.apachename.cat', coyote', version: '7.0.27'

028.}

029. 

030.task com

(type: Jar, dependsOn:030.task 🠎)🠟S.atoOn:

032.from ' build/docs/javadoc'

033.}

034. 

035.task sourcesJar(type: Jar) {

036.from .

038. }

039. 

040.artifacts {

041.archives jar

042. 

043.archives 

042. 

043.archives 

042. 

043.archives 

042。

046. 

047.//signing {

048.//  sign configurations.archives

into 'dist' 

054.}

055. 

056.​​uploadArchives {

057.repositories {

058.mavenDeployer {p. > signing.signPom(deployment) }

060. 

061 .//repository(url: '

https://oss.sonatype.org/service/local/staging/deploy/maven2/

') {

062.//  authentication(userName: sonatype1

063.//}

064.//repository(url: '

https://oss.sonatype.org/content/repositories/snapshots

') {

,065.////> : sonatypePassword)

066.//}

067. 

068.pom.project {

069.name 'tomcat-redis-session-manager'

069.name 'tomcat-redis-session-manager'

.07. scription ' Tomcat Redis Session Manager is a Tomcat extension to store sessions in Redis'

072.url '

https://github.com/jcoleman/tomcat-redis-session-man

https://github.com/jcoleman/tomcat-redis-034man issueManagement {

075.url '

https://github.com

:jcoleman/tomcat-redis-session-manager/issues'

076.system 'GitHub Issues'

076.system 'GitHub Issues'

077..7. scm {

080.url '

https://github.com

:jcoleman/tomcat-redis-session-manager'

081.connection 'scm:tomgit:

git://github.com/ -redis-session-manager.git

'

082.developerConnection 'scm:git:git@github.com:jcoleman/tomcat-redis-session-manager.git'

083.}

084. 

🜎085.licenses 087.name 'MIT'

088.url '

http://opensource.org/licenses/MIT

'

089.distribution 'repo'

090.}091. 093.developers {

094.developer {

095.id 'jcoleman'

096.name 'James Coleman'

097.e2P.com github.com/jcoleman

'

099.}

100.}

101.}

102.}

103.}

102.}103.}

104. gradle

指令建構原始碼,編譯輸出

tomcat-redis-session-manager-master 

及依賴

jar

🎠 test  copyJars   所有輸出清單檔案如下:四、tomcat 設定  安裝配置兩台伺服器80809090

,並確保都能正常運作,當然如果分佈在不同的主機則可以使用相同連接埠號碼。 五、編寫測試頁  為了區別2

tomcat

的訪問,分別編寫頁面並打包部署:tomcat的訪問,分別編寫頁面並打包部署:

1 編寫測驗頁面,顯示 「 response from tomcat_1 」,而頁面提供按鈕顯示目前session值,包裝並發佈到 tomcat_1 、為tomcat_2

編寫測試頁面,顯示 

「 response from tomcat_2 」

,同時頁面提供按鈕顯示目前session值,包裝並發佈到 tomcat_2; http:// 10.129.221.70:8080 和 

http://10.129.221.70:9090 地址,因為訪問的是不同地址,因為訪問的是不同值肯定不同。 六、tomcat session manager 配置  修改配置使用 tomcat-redis-session-manager-master sion作為管理器作為管理器

1、分別將第三步驟產生的 tomcat-redis-session-manager-master 及依賴jar包覆蓋到 tomcat 安裝目錄的檔案夾雜2、分別修改2

台 tomcat 的 context.xml 管理器,同時指定 redis位址和連接埠。 context.xml 

增加以下設定:view source

print?.Confopmm. essions. RedisSessionHandlerValve'/>3.4.host='localhost'

5.port='6379' 7.maxInactiveInterval='60' />8.3、分別重啟2、分別重啟2七、

nginx 

設定1、修改 default.conf up1、修改 default.conf tomcat Cluster,預設使用輪詢方式。 view sourceprint?01.upstream site { 

ip_hash分發

02.server localhost:8080;

03.server localhost :9090;04.} 05. 

06.server {

07.listen       80;

07.listen       

08.server_name  localhost;

09. 

10.#charset koi8-r;

11.#access_log  /var/log/nginx/log/host.access.log  main;

12. 

13.location / {

14.#root   /usr/share/nginx/html;

15.#index  index.html index.htm;

15.#index  index.html index.htm;15.#index  index.html index.htm;16.index  index.html index.htm; //www.it165.net/pro/webjsp/

" target="_blank"class="keylink">jsp; 索引。 

17.proxy_redirect          關閉;   

18.proxy_set_header         

19.proxy_set_header        X-真實IP       $remote_addr;   

20.proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;   

21.client_max_body_size   

21.client_max_body_size   

21.client_max_body_size   11 28k;   

23.proxy_buffers           32 4k; 

   30;   

26.proxy_read_timeout      30;  

27.proxy_pass http://site;

28. 

29.}

30. 

31.#error_page  404              /404.html;

32。 page   500 502 503 50 4  / 50x .html;

36.location = /50x.html {

37 .root   /usr/share/nginx/html;

38.}

39. 

40.#將 PHP 腳本代理到 Apache 監聽 127.0.0.1:80

41.#

42.#location ~.php$ {

43. 44 .#}

45。 

46.# 將PHP 腳本載入到FastCGI 伺服器,監聽127.0.0.1:9000

47.#

48.#location  ;

50.#    fastcgi _pass 127.0 .0.1:9000;

51.#    fastcgi_index  index.php;

52.#    fastcgi_param           fastcgi_params;包括

5 4.#}

55。 

56.# 如果 Apache 的文檔根

57.# 與 nginx 的文檔根一致

58.#

59.#location ~ /.ht {

59.#location ~ /.ht {

.htaccess .nginx -s re負載

八、配置Tomcat

保存的

session

實體被放置

redis

中,

tomcat

進行對比,

修改session-id是從cookie是從

cookie

中取得的,不同的tomcat儲存

sessioncookie

的位置

儲存

sessioncookie的位置.xml,如下: session修改CookiePath="/">九、測試結果120、訪問直接要求到tomcat_1伺服器,不顯示 「來自tomcat_1的回應」會話等於工作 3'

2、訪問 http://10.129.221.70:9090 直接要求到tomcat_2㟎直接要求到

tomcat_2㟎

直接要求到 ponse from tomcat_2 ” session 值為'56E2FAE376A47F1C0961D722326B8423'

3、訪問80埠)請求到 nginx 反向代理到指定 Web伺服器,由於預設使用輪詢負載方式,    反覆刷新頁面顯示的內容在“ response from tomcat_1 ” 

“ response from tomcat_1 ” 

之間切換,但 session 值維持為 '56E2FAE376A47F1C0961D722326B8423'4cli redis 

伺服器,查看會顯示有 「56E2FAE376A47F1C0961D722326B8423」 keysession 數據,value為序列化數據。 十、至此實現了基於nginx負載平衡下 tomcat 叢集的 session 

一致。 啟動順序:redis——nginx——tomcat

redis/c nginx+tomcat+redis實作session共享 以上就介紹了nginx+tomcat+redis實現session共享,包含了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

 IP_Port

.6.2

 10.129.221.70:80

 tomcat_1

 7.0.54

 10.129.221.70:80800

 10.129.221.70:9090

redis

 10.129.221.70:6379

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