nginx 做爲目前最流行的開源反向代理HTTP Server,用於實現資源緩存、web server負載均衡等功能,因爲其輕量級、高性能、高可靠等特色在互聯網項目中有着很是廣泛的應用,相關概念網上有豐富的介紹。分佈式web server集羣部署後須要實現session共享,針對 tomcat 服務器的實現方案多種多樣,好比 tomcat cluster session 廣播、nginx IP hash策略、nginx sticky module等方案,本文主要介紹了使用 redis 服務器進行 session 統一存儲管理的共享方案。php
相關應用結構參照下圖:css
測試環境基於 Linux CentOS 6.5,請先安裝 tomcat、redis、nginx 相關環境,不做詳細描述,本文測試配置以下:html
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 |
一、因爲源碼構建基於 gradle,請先配置 gradle 環境。java
二、從 github 獲取 tomcat-redis-session-manager-master 源碼,地址以下:nginx
https://github.com/jcoleman/tomcat-redis-session-manager
三、找到源碼中的 build.gradle 文件,因爲做者使用了第三方倉庫(sonatype),須要註冊賬號,太麻煩,註釋後直接使用maven中央倉庫,同時註釋簽名相關腳本並增長依賴包的輸出腳本 copyJars(dist目錄),修改後的 build.gradle 文件以下:git
apply plugin: 'java' apply plugin: 'maven' apply plugin: 'signing' group = 'com.orangefunction' version = '2.0.0' repositories { mavenCentral() } compileJava { sourceCompatibility = 1.7 targetCompatibility = 1.7 } dependencies { compile group: 'org.apache.tomcat', name: 'tomcat-catalina', version: '7.0.27' compile group: 'redis.clients', name: 'jedis', version: '2.5.2' compile group: 'org.apache.commons', name: 'commons-pool2', version: '2.2' //compile group: 'commons-codec', name: 'commons-codec', version: '1.9' testCompile group: 'junit', name: 'junit', version: '4.+' testCompile 'org.hamcrest:hamcrest-core:1.3' testCompile 'org.hamcrest:hamcrest-library:1.3' testCompile 'org.mockito:mockito-all:1.9.5' testCompile group: 'org.apache.tomcat', name: 'tomcat-coyote', version: '7.0.27' } task javadocJar(type: Jar, dependsOn: javadoc) { classifier = 'javadoc' from 'build/docs/javadoc' } task sourcesJar(type: Jar) { from sourceSets.main.allSource classifier = 'sources' } artifacts { archives jar archives javadocJar archives sourcesJar } //signing { // sign configurations.archives //} task copyJars(type: Copy) { from configurations.runtime into 'dist' } uploadArchives { repositories { mavenDeployer { beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } //repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") { // authentication(userName: sonatypeUsername, password: sonatypePassword) //} //repository(url: "https://oss.sonatype.org/content/repositories/snapshots") { // authentication(userName: sonatypeUsername, password: sonatypePassword) //} pom.project { name 'tomcat-redis-session-manager' packaging 'jar' description 'Tomcat Redis Session Manager is a Tomcat extension to store sessions in Redis' url 'https://github.com/jcoleman/tomcat-redis-session-manager' issueManagement { url 'https://github.com:jcoleman/tomcat-redis-session-manager/issues' system 'GitHub Issues' } scm { url 'https://github.com:jcoleman/tomcat-redis-session-manager' connection 'scm:git:git://github.com/jcoleman/tomcat-redis-session-manager.git' developerConnection 'scm:git:git@github.com:jcoleman/tomcat-redis-session-manager.git' } licenses { license { name 'MIT' url 'http://opensource.org/licenses/MIT' distribution 'repo' } } developers { developer { id 'jcoleman' name 'James Coleman' email 'jtc331@gmail.com' url 'https://github.com/jcoleman' } } } } } }
四、執行gradle命令構建源碼,編譯輸出tomcat-redis-session-manager-master 及依賴jar包github
gradle build -x test copyJars
全部輸出列表文件以下:web
安裝配置兩臺 tomcat web服務器,分別修改 Connector 端口號爲8080和9090,並確保都能正常工做,固然若是分佈在不一樣的主機則可使用相同端口號。redis
爲了區別2臺tomcat的訪問,分別編寫頁面並打包部署:apache
一、爲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 地址,由於訪問的是不一樣web服務器,因此各自顯示不一樣的頁面內容及session值確定不一樣。
修改配置使用 tomcat-redis-session-manager-master 做爲 tomcat session 管理器
一、分別將第三步生成的 tomcat-redis-session-manager-master 及依賴jar包覆蓋到 tomcat 安裝目錄的 lib 文件夾
二、分別修改2臺 tomcat 的 context.xml 文件,使 tomcat-redis-session-manager-master 做爲session管理器,同時指定redis地址和端口。
context.xml 增長如下配置:
<Context> <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="localhost" port="6379" database="0" maxInactiveInterval="60" /> </Context>
三、分別重啓2臺 tomcat 服務器。
一、修改 default.conf 配置文件,啓用 upstream 負載均衡 tomcat Cluster,默認使用輪詢方式。
upstream site { server localhost:8080; server localhost:9090; } server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { #root /usr/share/nginx/html; #index index.html index.htm; index index_tel.jsp index.jsp index.html index.htm ; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_buffers 32 4k; proxy_connect_timeout 3; proxy_send_timeout 30; proxy_read_timeout 30; proxy_pass http://site; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
二、nginx 從新加載配置
nginx -s reload
一、訪問 http://10.129.221.70:8080 直接請求到tomcat_1服務器,
顯示 「 response from tomcat_1 」, session 值爲 ‘56E2FAE376A47F1C0961D722326B8423’;
二、訪問 http://10.129.221.70:9090 直接請求到tomcat_2服務器,
顯示 「 response from tomcat_2 」, session 值爲 ‘56E2FAE376A47F1C0961D722326B8423’;
三、訪問 http://10.129.221.70 (默認80端口)請求到 nginx 反向代理到指定Web服務器,因爲默認使用輪詢負載方式,
反覆刷新頁面顯示的內容在「 response from tomcat_1 」 和 「 response from tomcat_2 」之間切換,但 session 值保持爲 ‘56E2FAE376A47F1C0961D722326B8423’;
四、使用 redis-cli 鏈接 redis 服務器,查看會顯示有 「56E2FAE376A47F1C0961D722326B8423」 key的 session 數據,value爲序列化數據。