Redis+Tomcat实现集群的Session管理

Redis+Tomcat实现集群的Session管理

本地环境
JDK java version “1.8.0_102”
Tomcat apache-tomcat-7.0.90
Redis Redis-x64-3.0.503
Nginx nginx-1.4.7

大概环境如上所述,多的不说了,下面只说下主要的实现步骤。

  1. Tomcat

Tomcat安装 如下图所示,测试时使用了3个Tomat
这里写图片描述

测试项目,测试的项目是使用的普通web项目
这里写图片描述
web预测3个站点的效果
这里写图片描述

B与C的session内容是不一致的,我们的目的是让A、B、C的session内容相同。

  1. Nginx
    下载nginx-1.4.7解压到本地磁盘,如 D:\Program Files\nginx-1.4.7
    修改nginx配置conf/nginx.conf
    以下是我本地测试电脑配置文件的内容。
#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 server.domain {
    server 10.1.65.178:8001;
    server 10.1.65.178:8002;
    server 10.1.65.178:8003;
    }
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            #index  index.html index.htm;
        proxy_pass http://server.domain;
        #以下配置解决nginx反应慢问题(其中一台tomcat异常)
        proxy_ignore_client_abort on; 
        proxy_connect_timeout 1s;
            proxy_read_timeout 1s;
            client_max_body_size 100m;
        }

        #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;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

配置完成后启动
将redis加入到windows的服务中(service和loglevel前都是两个-)
redis-server.exe –service-install redis.windows.conf –loglevel verbose

nginx安装ok后的效果。
这里写图片描述

3.Tomcat+redis的配置
①分别修改3个Tomcat目录下/conf/context.xml为以下内容:

<?xml version='1.0' encoding='utf-8'?>
<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <!-- database session -->
    <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
    <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
         host="localhost"
         port="6379"
         password="123456"
         database="0"
         maxInactiveInterval="60"/>
</Context>

②将所需要的jar文件拷贝到tomcat安装目录的/lib目录下。
这里写图片描述

做完以上工作后重新启动Tomcat。浏览nginx监听的端口,如下图所示:
这里写图片描述

以下内容涉及到的资源下载连接
集群jar文件
https://download.csdn.net/download/xiejia2lsm/10646783
集群所用到的工具
https://download.csdn.net/download/xiejia2lsm/10646731

第一天写博客,哎,感觉不怎么样,太花时间了。这个编辑工具也不太熟悉,中间写着的时候还没保存,重来了一次。希望能坚持写下去…

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值