使用Nginx作为Tomcat的负载平衡器,Tomcat的会话Session数据存储在Redis,能够实现0当机的7x24运营效果。因为将会话存储在Redis中,因此Nginx就不必配置成stick粘粘某个Tomcat方式,这样才能真正实现后台多个Tomcat负载平衡,用户请求能够发往任何一个tomcat主机,当我们需要部署新应用代码时,只要停止任何一台tomcat,所有当前在线用户都会导向到运行中的tomcat实例,因为会话数据被序列化到Redis,在线用户不会受到影响,一旦停掉的tomcat实例上线,另外其他重复部署过程。
工具 | 版本 | 端口 |
nginx | 1.9.4 | 127.0.0.1:80(默认) |
tomcat7_1 | 7.0.61 | 127.0.0.1:8091 |
tomcat7_2 | 7.0.61 | 127.0.0.1:8092 |
tomcat7_3 | 7.0.61 | 127.0.0.1:8093 |
redis |
2.7.3+ |
127.0.0.1:6379(默认) |
介绍:tomcat-redis-session-manager 是一个用来将 Tomcat 的 Session 数据存储在 Redis 库中的项目。
官网:https://github.com/jcoleman/tomcat-redis-session-manager
从官网下载相关源代码,找到build.gradle文件,由于作者使用了第三方仓库(sonatype),需要注册帐号,太麻烦,注释后直接使用maven中央仓库,同时注释签名相关脚本并增加依赖包的输出脚本 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.7 targetCompatibility = 1.7 } dependencies { compile group: 'org.apache.tomcat', name: 'tomcat-catalina', version: '7.0.61' compile group: 'redis.clients', name: 'jedis', version: '2.7.3' 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.61' } 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:[email protected]: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 '[email protected]' url 'https://github.com/jcoleman' } } } } } }
我对版本进行了修改,tomcat使用7.0.61版本,redis使用2.7.3版本。
执行gradle命令构建源码,编译输出tomcat-redis-session-manager-master 及依赖jar包
gradle build -x test copyJars
生成的jar如下:
安装配置两台 tomcat web服务器,分别修改 Connector 端口号为8091和8092,并确保都能正常工作,当然如果分布在不同的主机则可以使用相同端口号。
tomcat1:Server port="8005",Connector port="8091",ajp Connector port="8009"
tomcat2:Server port="8006",Connector port="8092",ajp Connector port="8010"
tomcat3:Server port="8007",Connector port="8093",ajp Connector port="8011"
新建web项目test,新建index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Nginx+tomcat负载均衡+redis会话管理</title> </head> <body> <h1><font color="blue">集群节点1</font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> <p> </body> </html>
分别部署运行:
看到实验结果是,两个web显示的是不同的内容,不同的session。
为了实现session共享,我们要进行下一步操作,就是使用上面编译的tomcat-redis-session-manager,作为tomcat session得管理器。
1、分别将第二步生成的 tomcat-redis-session-manager-master 及依赖jar包覆盖到 tomcat 安装目录的 lib 文件夹
2、分别修改3台 tomcat 的 context.xml 文件,使 tomcat-redis-session-manager-master 作为session管理器,同时指定redis地址和端口。
context.xml 增加以下配置:
<Context> <!-- Default set of monitored resources --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="localhost" port="6379" database="0" maxInactiveInterval="60" /> </Context>
3、重启tomcat
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream site { server localhost:8091; server localhost:8092; server localhost:8093; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { # root 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 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; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
重启nginx
nginx -s reload
访问web项目,出现下面错误:
这是因为redis服务器还没有启动导致。我是在windows10 64位下测试,所以我们需要下载相关版本,
官方的下载地址是: http://redis.io/download
在win64一栏中可以看到redis原本是没有windows版本的,windows版本是 Microsoft Open Tech团队开的
https://github.com/MSOpenTech/redis
解压即可开箱使用
使用命令【redis-server.exe redis.windows.conf】,启动redis 服务【如果您没出现如下的错误,直接跳过】。如果您也像我一样出现如下的错误,不用急,总有解决办法滴!
解决办法:
根据提示,是 maxheap 标识有问题,打开配置文件 redis.windows.conf ,搜索 maxheap , 然后直接指定好内容即可.
......
#
# maxheap <bytes>
maxheap 1024000000
.......
然后再次启动,OK,成功.
接着刷新tomcat服务器页面
你会发现成功了,三个服务器的session值完全一样,无论如何刷新都不会发生改变了
接着我们使用nginx代理服务器进行访问web,你会发现惊喜
我们反复刷新页面,页面会在不同的页面进行显示,但是session保持了一致性。这是因为我们使用了nginx反向代理,当我们的请求到达nginx得时候反向代理倒指定服务器,nginx配置了默认轮询负载方式,所以就完成上述的现状。
现在我们使用 redis-cli 连接 redis 服务器去查看下如何。
至此实现了基于nginx负载均衡下 tomcat 集群的 session 一致性。
CSDN下载地址:http://download.csdn.net/detail/zhdevelop/9236497