nginx 作為目前最流行的開源反向 ProxyHTTP Server,用於實現資源緩衝、web server負載平衡等功能,由於其輕量級、高效能、高可靠等特點在互連網項目中有著非常普遍的應用,相關概念網上有豐富的介紹。分布式web server叢集部署後需要實現session共用,針對 tomcat 伺服器的實現方案多種多樣,比如 tomcat cluster session 廣播、nginx IP hash策略、nginx sticky module等方案,本文主要介紹了使用 redis 伺服器進行 session 統一儲存管理的共用方案。
相關應用結構參照:
二、環境配置
測試環境基於 Linux CentOS 6.5,請先安裝 tomcat、redis、nginx 相關環境,不作詳細描述,本文測試組態如下:
|
Version |
IP_Port |
nginx |
1.6.2 |
10.129.221.70:80 |
tomcat_1 |
7.0.54 |
10.129.221.70:8080 |
tomcat_2 |
7.0.54 |
10.129.221.70:9090 |
redis |
2.8.19 |
10.129.221.70:6379 |
三、構建 tomcat-redis-session-manager-master
1、由於源碼構建基於 gradle,請先配置 gradle 環境。
2、從 github 擷取 tomcat-redis-session-manager-master 源碼,地址如下:
view sourceprint?
1.https://github.com/jcoleman/tomcat-redis-session-manager
3、找到源碼中的 build.gradle 檔案,由於作者使用了第三方倉庫(sonatype),需要註冊帳號,太麻煩,注釋後直接使用maven中央倉庫,同時注釋簽名相關指令碼並增加依賴包的輸出指令碼 copyJars(dist目錄),修改後的 build.gradle 檔案如下:
view sourceprint?
001.apply plugin: 'java'
002.apply plugin: 'maven'
003.apply plugin: 'signing'
004.
005.group = 'com.orangefunction'
006.version = '2.0.0'
007.
008.repositories {
009.mavenCentral()
010.}
011.
012.compileJava {
013.sourceCompatibility = 1.7
014.targetCompatibility = 1.7
015.}
016.
017.dependencies {
018.compile group: 'org.apache.tomcat', name: 'tomcat-catalina', version: '7.0.27'
019.compile group: 'redis.clients', name: 'jedis', version: '2.5.2'
020.compile group: 'org.apache.commons', name: 'commons-pool2', version: '2.2'
021.//compile group: 'commons-codec', name: 'commons-codec', version: '1.9'
022.
023.testCompile group: 'junit', name: 'junit', version: '4.+'
024.testCompile 'org.hamcrest:hamcrest-core:1.3'
025.testCompile 'org.hamcrest:hamcrest-library:1.3'
026.testCompile 'org.mockito:mockito-all:1.9.5'
027.testCompile group: 'org.apache.tomcat', name: 'tomcat-coyote', version: '7.0.27'
028.}
029.
030.task javadocJar(type: Jar, dependsOn: javadoc) {
031.classifier = 'javadoc'
032.from 'build/docs/javadoc'
033.}
034.
035.task sourcesJar(type: Jar) {
036.from sourceSets.main.allSource
037.classifier = 'sources'
038.}
039.
040.artifacts {
041.archives jar
042.
043.archives javadocJar
044.archives sourcesJar
045.}
046.
047.//signing {
048.// sign configurations.archives
049.//}
050.
051.task copyJars(type: Copy) {
052.from configurations.runtime
053.into 'dist'
054.}
055.
056.uploadArchives {
057.repositories {
058.mavenDeployer {
059.beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
060.
061.//repository(url: 'https://oss.sonatype.org/service/local/staging/deploy/maven2/') {
062.// authentication(userName: sonatypeUsername, password: sonatypePassword)
063.//}
064.//repository(url: 'https://oss.sonatype.org/content/repositories/snapshots') {
065.// authentication(userName: sonatypeUsername, password: sonatypePassword)
066.//}
067.
068.pom.project {
069.name 'tomcat-redis-session-manager'
070.packaging 'jar'
071.description 'Tomcat Redis Session Manager is a Tomcat extension to store sessions in Redis'
072.url 'https://github.com/jcoleman/tomcat-redis-session-manager'
073.
074.issueManagement {
075.url 'https://github.com:jcoleman/tomcat-redis-session-manager/issues'
076.system 'GitHub Issues'
077.}
078.
079.scm {
080.url 'https://github.com:jcoleman/tomcat-redis-session-manager'
081.connection 'scm:git:git://github.com/jcoleman/tomcat-redis-session-manager.git'
082.developerConnection 'scm:git:git@github.com:jcoleman/tomcat-redis-session-manager.git'
083.}
084.
085.licenses {
086.license {
087.name 'MIT'
088.url 'http://opensource.org/licenses/MIT'
089.distribution 'repo'
090.}
091.}
092.
093.developers {
094.developer {
095.id 'jcoleman'
096.name 'James Coleman'
097.email 'jtc331@gmail.com'
098.url 'https://github.com/jcoleman'
099.}
100.}
101.}
102.}
103.}
104.}
4、執行gradle命令構建源碼,編譯輸出tomcat-redis-session-manager-master 及依賴jar包
view sourceprint?
1.gradle build -x test copyJars
所有輸出資料行表檔案如下:
四、tomcat 配置
安裝配置兩台 tomcat web伺服器,分別修改 Connector 連接埠號碼為8080和9090,並確保都能正常工作,當然如果分布在不同的主機則可以使用相同連接埠號碼。
五、編寫測試頁面
為了區別2台tomcat的訪問,分別編寫頁面並打包部署:
1、為tomcat_1編寫測試頁面,顯示 “ response from tomcat_1 ”,同時頁面提供按鈕顯示當前session值,打包並發布到 tomcat_1 伺服器;
2、為tomcat_2編寫測試頁面,顯示 “ response from tomcat_2 ”,同時頁面提供按鈕顯示當前session值,打包並發布到 tomcat_2 伺服器;
此時分別訪問 http://10.129.221.70:8080 和 http://10.129.221.70:9090 地址,因為訪問的是不同web伺服器,所以各自顯示不同的頁面內容及session值肯定不同。
六、tomcat session manager 配置
修改配置使用 tomcat-redis-session-manager-master 作為 tomcat session 管理器
1、分別將第三步產生的 tomcat-redis-session-manager-master 及依賴jar包覆蓋到 tomcat 安裝目錄的 lib 檔案夾
2、分別修改2台 tomcat 的 context.xml 檔案,使 tomcat-redis-session-manager-master 作為session管理器,同時指定redis地址和連接埠。
context.xml 增加以下配置:
view sourceprint?
1.
2.
3.
4.host='localhost'
5.port='6379'
6.database='0'
7.maxInactiveInterval='60' />
8.
3、分別重啟2台 tomcat 伺服器。
七、nginx 配置
1、修改 default.conf 設定檔,啟用 upstream 負載平衡 tomcat Cluster,預設使用輪詢方式。
view sourceprint?
01.upstream site {
ip_hash; //基於ip_hash分發
02.server localhost:8080;
03.server localhost:9090;
04.}
05.
06.server {
07.listen 80;
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;
16.index index_tel.http://www.it165.net/pro/webjsp/" target="_blank"class="keylink">jsp index.http://www.it165.net/pro/webjsp/"target="_blank" class="keylink">jsp index.html index.htm ;
17.proxy_redirect off;
18.proxy_set_header Host $host;
19.proxy_set_header X-Real-IP $remote_addr;
20.proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
21.client_max_body_size 10m;
22.client_body_buffer_size 128k;
23.proxy_buffers 32 4k;
24.proxy_connect_timeout 3;
25.proxy_send_timeout 30;
26.proxy_read_timeout 30;
27.proxy_pass http://site;
28.
29.}
30.
31.#error_page 404 /404.html;
32.
33.# redirect server error pages to the static page /50x.html
34.#
35.error_page 500 502 503 504 /50x.html;
36.location = /50x.html {
37.root /usr/share/nginx/html;
38.}
39.
40.# proxy the PHP scripts to Apache listening on 127.0.0.1:80
41.#
42.#location ~ .php$ {
43.# proxy_pass http://127.0.0.1;
44.#}
45.
46.# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
47.#
48.#location ~ .php$ {
49.# root html;
50.# fastcgi_pass 127.0.0.1:9000;
51.# fastcgi_index index.php;
52.# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
53.# include fastcgi_params;
54.#}
55.
56.# deny access to .htaccess files, if Apache's document root
57.# concurs with nginx's one
58.#
59.#location ~ /.ht {
60.# deny all;
61.#}
62.}
2、nginx 重新載入配置
view sourceprint?
1.nginx -s reload
八、配置Tomcat
儲存的session實體是放到redis中,tomcat可以記錄session-id值與redis進行對比,session-id是從cookie中取得的,不同的tomcat儲存sessioncookie的位置是不同的,所以必須修改所有tomcat中conf/context.xml,修改內容如下:
sessionCookiePath="/">
九、測試結果
1、訪問 http://10.129.221.70:8080 直接請求到tomcat_1伺服器,
顯示 “ response from tomcat_1 ”, session 值為 ‘56E2FAE376A47F1C0961D722326B8423’;
2、訪問 http://10.129.221.70:9090 直接請求到tomcat_2伺服器,
顯示 “ response from tomcat_2 ”, session 值為 ‘56E2FAE376A47F1C0961D722326B8423’;
3、訪問 http://10.129.221.70 (預設80連接埠)請求到 nginx 反向 Proxy到指定Web伺服器,由於預設使用輪詢負載方式,
反覆重新整理頁面顯示的內容在“ response from tomcat_1 ” 和 “ response from tomcat_2 ”之間切換,但 session 值保持為 ‘56E2FAE376A47F1C0961D722326B8423’;
4、使用 redis-cli 串連 redis 伺服器,查看會顯示有 “56E2FAE376A47F1C0961D722326B8423” key的 session 資料,value為序列化資料。
十、至此實現了基於nginx負載平衡下 tomcat 叢集的 session 一致性。
啟動順序:redis——nginx——tomcat
redis啟動指令碼=/usr/locat/redis.2.0.1/src/redis-server
nginx+tomcat+redis實現session共用
以上就介紹了nginx+tomcat+redis實現session共用,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。