Redis保存Java Session
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
http://www.cnblogs.com/lengfo/p/4260363.html
一、前言
nginx 作為目前最流行的開(kāi)源反向代理HTTP Server,用于實(shí)現(xiàn)資源緩存、web server負(fù)載均衡等功能,由于其輕量級(jí)、高性能、高可靠等特點(diǎn)在互聯(lián)網(wǎng)項(xiàng)目中有著非常普遍的應(yīng)用,相關(guān)概念網(wǎng)上有豐富的介紹。分布式web server集群部署后需要實(shí)現(xiàn)session共享,針對(duì) tomcat 服務(wù)器的實(shí)現(xiàn)方案多種多樣,比如 tomcat cluster session 廣播、nginx IP hash策略、nginx sticky module等方案,本文主要介紹了使用 redis 服務(wù)器進(jìn)行 session 統(tǒng)一存儲(chǔ)管理的共享方案。
相關(guān)應(yīng)用結(jié)構(gòu)參照下圖:
?
二、環(huán)境配置
測(cè)試環(huán)境基于 Linux CentOS 6.5,請(qǐng)先安裝 tomcat、redis、nginx 相關(guān)環(huán)境,不作詳細(xì)描述,本文測(cè)試配置如下:
| ? | ?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 |
?
三、構(gòu)建 tomcat-redis-session-manager-master
1、由于源碼構(gòu)建基于 gradle,請(qǐng)先配置 gradle 環(huán)境。
2、從 github 獲取 tomcat-redis-session-manager-master 源碼,地址如下:
https://github.com/jcoleman/tomcat-redis-session-manager3、找到源碼中的?build.gradle?文件,由于作者使用了第三方倉(cāng)庫(kù)(sonatype),需要注冊(cè)帳號(hào),太麻煩,注釋后直接使用maven中央倉(cāng)庫(kù),同時(shí)注釋簽名相關(guān)腳本并增加依賴(lài)包的輸出腳本 copyJars(dist目錄),修改后的?build.gradle 文件如下:
apply plugin: 'java' apply plugin: 'maven' apply plugin: 'signing'group = 'com.orangefunction' version = '2.0.0'repositories {mavenCentral() }compileJava {sourceCompatibility = 1.7targetCompatibility = 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.allSourceclassifier = 'sources' }artifacts {archives jararchives javadocJararchives sourcesJar }//signing { // sign configurations.archives //}task copyJars(type: Copy) {from configurations.runtimeinto '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'}}}}} }4、執(zhí)行g(shù)radle命令構(gòu)建源碼,編譯輸出tomcat-redis-session-manager-master 及依賴(lài)jar包
gradle build -x test copyJars
所有輸出列表文件如下:
?
四、tomcat 配置
安裝配置兩臺(tái) tomcat web服務(wù)器,分別修改 Connector 端口號(hào)為8080和9090,并確保都能正常工作,當(dāng)然如果分布在不同的主機(jī)則可以使用相同端口號(hào)。
?
五、編寫(xiě)測(cè)試頁(yè)面
為了區(qū)別2臺(tái)tomcat的訪問(wèn),分別編寫(xiě)頁(yè)面并打包部署:
1、為tomcat_1編寫(xiě)測(cè)試頁(yè)面,顯示 “ response from tomcat_1 ”,同時(shí)頁(yè)面提供按鈕顯示當(dāng)前session值,打包并發(fā)布到 tomcat_1 服務(wù)器;
2、為tomcat_2編寫(xiě)測(cè)試頁(yè)面,顯示 “ response from tomcat_2 ”,同時(shí)頁(yè)面提供按鈕顯示當(dāng)前session值,打包并發(fā)布到 tomcat_2 服務(wù)器;
此時(shí)分別訪問(wèn) http://10.129.221.70:8080?和 http://10.129.221.70:9090?地址,因?yàn)樵L問(wèn)的是不同web服務(wù)器,所以各自顯示不同的頁(yè)面內(nèi)容及session值肯定不同。
?
六、tomcat session manager 配置
修改配置使用 tomcat-redis-session-manager-master 作為 tomcat?session 管理器
1、分別將第三步生成的 tomcat-redis-session-manager-master 及依賴(lài)jar包覆蓋到 tomcat 安裝目錄的 lib 文件夾
2、分別修改2臺(tái) tomcat 的 context.xml 文件,使 tomcat-redis-session-manager-master 作為session管理器,同時(shí)指定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>3、分別重啟2臺(tái) tomcat 服務(wù)器。
?
七、nginx 配置
1、修改 default.conf 配置文件,啟用?upstream 負(fù)載均衡?tomcat Cluster,默認(rèn)使用輪詢(xún)方式。
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;#} }2、nginx 重新加載配置
nginx -s reload?
八、測(cè)試結(jié)果
1、訪問(wèn)?http://10.129.221.70:8080?直接請(qǐng)求到tomcat_1服務(wù)器,
顯示?“ response from tomcat_1 ”, session 值為 ‘56E2FAE376A47F1C0961D722326B8423’;
2、訪問(wèn)?http://10.129.221.70:9090?直接請(qǐng)求到tomcat_2服務(wù)器,
顯示?“ response from tomcat_2 ”,?session 值為 ‘56E2FAE376A47F1C0961D722326B8423’;
3、訪問(wèn)?http://10.129.221.70?(默認(rèn)80端口)請(qǐng)求到 nginx 反向代理到指定Web服務(wù)器,由于默認(rèn)使用輪詢(xún)負(fù)載方式,
反復(fù)刷新頁(yè)面顯示的內(nèi)容在“ response from tomcat_1 ” 和?“ response from tomcat_2 ”之間切換,但 session 值保持為 ‘56E2FAE376A47F1C0961D722326B8423’;
4、使用 redis-cli 連接 redis 服務(wù)器,查看會(huì)顯示有 “56E2FAE376A47F1C0961D722326B8423” key的 session 數(shù)據(jù),value為序列化數(shù)據(jù)。
?
九、至此實(shí)現(xiàn)了基于nginx負(fù)載均衡下 tomcat 集群的 session 一致性。
轉(zhuǎn)載于:https://my.oschina.net/wuyizhong/blog/1603762
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Redis保存Java Session的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 烟雨江湖药王谷在哪
- 下一篇: Java多线程的几种写法