Nginx+Tomcat+Redis负载均衡集群服务搭建

来源:互联网 发布:csol由于网络设定问题 编辑:程序博客网 时间:2024/05/22 05:36

转载请注明出处:http://blog.csdn.net/l1028386804/article/details/52216000

        在之前的一篇《Nginx+Tomcat+Memcached负载均衡集群服务搭建》中,向大家详细介绍了,基于Nginx+Tomcat+Memcached实现负载均衡集群服务搭建,今天向大家介绍如何基于Nginx+Tomcat+Redis实现负载均衡集群服务的搭建。我们直接进入正题:

插曲:

官方截止到2015-10-12前是不支持Tomcat8的,详情见官网:https://github.com/jcoleman/tomcat-redis-session-manager 

下面我们将它修改为支持Tomcat8,

修改的源代码:RedisSessionManager.java

@SuppressWarnings("deprecation")private void initializeSerializer() throws ClassNotFoundException, IllegalAccessException, InstantiationException {    log.info("Attempting to use serializer :" + serializationStrategyClass);    serializer = (Serializer) Class.forName(serializationStrategyClass).newInstance();    Loader loader = null;    if (getContainer() != null) {        loader = getContainer().getLoader();    }    ClassLoader classLoader = null;    if (loader != null) {        classLoader = loader.getClassLoader();    }    serializer.setClassLoader(classLoader);}
修改后的内容
private void initializeSerializer() throws ClassNotFoundException, IllegalAccessException, InstantiationException {    log.info("Attempting to use serializer :" + serializationStrategyClass);    serializer = (Serializer) Class.forName(serializationStrategyClass).newInstance();    Loader loader = null;    Context context = this.getContext();    if (context != null) {        loader = context.getLoader();    }    ClassLoader classLoader = null;    if (loader != null) {        classLoader = loader.getClassLoader();    }    serializer.setClassLoader(classLoader);}

一、前言

         nginx 作为目前最流行的开源反向代理HTTP 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 相关环境,不作详细描述,本文测试配置如下:

版本名称IP与端口nginx1.9.13192.168.106.130:80Tomcat1 7.0.54192.168.106.130:8081Tomcat2 7.0.54192.168.106.130:8082Tomcat3 7.0.54192.168.106.130:8083 redis2.8.19192.168.106.130:6379注:安装nginx请参见《Nginx+Tomcat+Memcached负载均衡集群服务搭建》,安装redis请参见《Redis之——CentOS 6.5安装配置redis》

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

1、由于源码构建基于 gradle,请先配置 gradle 环境。
2、从 github 获取 tomcat-redis-session-manager-master 源码,地址如下:

https://github.com/jcoleman/tomcat-redis-session-manager
3、找到源码中的 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.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'          }        }      }    }  }}
4、执行gradle命令构建源码,编译输出tomcat-redis-session-manager-master 及依赖jar包
gradle build -x test  copyJars
所有输出列表文件如下:

四、tomcat 配置

安装配置三台 tomcat web服务器,分别修改 Connector 端口号为8081、8082、8083,并确保都能正常工作,当然如果分布在不同的主机则可以使用相同端口号。

五、编写测试页面

  为了区别3台tomcat的访问,分别编写页面并打包部署:
  1、为tomcat_1编写测试页面,显示 “ response from tomcat_1 ”,同时页面提供按钮显示当前session值,打包并发布到 tomcat_1 服务器;
  2、为tomcat_2编写测试页面,显示 “ response from tomcat_2 ”,同时页面提供按钮显示当前session值,打包并发布到 tomcat_2 服务器;
   3、为tomcat_3编写测试页面,显示 “ response from tomcat_3 ”,同时页面提供按钮显示当前session值,打包并发布到 tomcat_3 服务器;
  此时分别访问 http://192.168.106.130:8081 、http://192.168.106.130:8082、http://192.168.106.130:8083 地址,因为访问的是不同web服务器,所以各自显示不同的页面内容及session值肯定不同。

六、tomcat session manager 配置

  修改配置使用 tomcat-redis-session-manager-master 作为 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>      <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />      <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"           host="localhost"           port="6379"           database="0"           maxInactiveInterval="60" /></Context>
3、分别重启3台 tomcat 服务器

七、nginx 配置

1、配置nginx.conf

#创建进程的用户和用户组user      php php;#服务进程数量,一般等于CPU数量worker_processes 8;#全局错误日志定义,建议开启error级别日志.[ debug | info | notice | warn | error | crit ]worker_rlimit_nofile 655350;error_log logs/error.log error;#error_log logs/error.log  notice;#error_log logs/error.log  info;#记录进程ID的文件#pid       logs/nginx.pid;events {    #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能.Linux建议使用epoll,FreeBSD建议使用kqueue.    use epoll;    #一个worker_processe允许的最近并发连接数量   worker_connections  1024;}http {   include       mime.types;   default_type application/octet-stream;client_max_body_size     15m;       client_body_buffer_size  128k;       proxy_connect_timeout    600;       proxy_read_timeout       600;       proxy_send_timeout       6000;       proxy_buffer_size        16k;       proxy_buffers            4 64k;       proxy_busy_buffers_size 64k;       proxy_temp_file_write_size 64k;   #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;     #http连接的持续时间   keepalive_timeout  65;     #gzip压缩设置    gzip  on;           #开启gzip   gzip_min_length 1k;  #最小压缩文件大小   gzip_buffers 4 16k;  #压缩缓冲区    #http的协议版本(1.0/1.1),默认1.1,前端如果是squid2.5请使用1.0   gzip_http_version 1.1;    #gzip压缩比,1压缩比最小处理速度最快,9压缩比最大但处理速度最慢(传输快但比较消耗cpu)   gzip_comp_level 2;       #和http头有关系,加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩    gzip_vary on;    #gzip压缩类型,不用添加text/html,否则会有警告信息   gzip_types text/plain text/javascript text/css application/xmlapplication/x-javascript application/json;     #设定负载均衡的服务器列表,可以设置多个upstream,但mysvr名字要区分    upstream myClusterServer1 {       #weigth参数表示权值,权值越高被分配到的几率越大        #本机上的Squid开启3128端口       server 127.0.0.1:8081  weight=5;       server 127.0.0.1:8082  weight=5;       server 127.0.0.1:8083  weight=5;    }    server {       #nginx监听的端口号       listen       80;        #域名可以有多个,用空格隔开       server_name  127.0.0.1;               #字符编码方式       charset utf-8;         #设定本虚拟主机的访问日志。关闭日志可以减少IO,提高性能。       #access_log logs/host.access.log  main;         #默认请求       location / {            #定义服务器的默认网站根目录位置           root   html;            #定义首页索引文件的名称           index  index.html index.htmindex.jsp;            #请求转向mysvr 定义的服务器列表           proxy_pass    http://myClusterServer1;           proxy_redirect default;            #跟代理服务器连接的超时时间,必须留意这个time out时间不能超过75秒,当一台服务器当掉时,过10秒转发到另外一台服务器。           proxy_connect_timeout 10;        }        #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;        #}    }}
2、nginx 重新加载配置
nginx -s reload

八、测试结果

1、访问 http://192.168.106.130:8081 直接请求到tomcat_1服务器,
    显示 “ response from tomcat_1 ”, session 值为 ‘56E2FAE376A47F1C0961D722326B8423’;
2、访问 http://192.168.106.130:8082 直接请求到tomcat_2服务器,
    显示 “ response from tomcat_2 ”, session 值为 ‘56E2FAE376A47F1C0961D722326B8423’;
3、访问 http://192.168.106.130:8083 直接请求到tomcat_3服务器,
    显示 “ response from tomcat_3 ”, session 值为 ‘56E2FAE376A47F1C0961D722326B8423’;
4、访问 http://192.168.106.130 (默认80端口)请求到 nginx 反向代理到指定Web服务器,由于默认使用轮询负载方式,
    反复刷新页面显示的内容在“ response from tomcat_1 ” 、 “ response from tomcat_2 ”和“ response from tomcat_3 ”之间切换,但 session 值保持为 ‘56E2FAE376A47F1C0961D722326B8423’;
5、使用 redis-cli 连接 redis 服务器,查看会显示有 “56E2FAE376A47F1C0961D722326B8423” key的 session 数据,value为序列化数据。

至此实现了基于nginx负载均衡下 tomcat 集群的 session 一致性


1 0