`
maosheng
  • 浏览: 551009 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

tomcat+redis+nginx实现session共享缓存

阅读更多
1.准备

apache tomcat 7.0.55

nginx 1.7.2

redis 2.8.9

配置环境使用三个tomcat, 三台tomcat、redis和nginx都在一台机器上,为了方便测试和部署。

大致的整个配置的架构:




在这个图中,nginx做为反向代理,将客户请求根据权重随机分配给三台tomcat服务器,redis做为三台tomcat的共享session数据服务器。

2.规划

redis

localhost:6379
nginx

localhost:80
tomcat

localhost:8081
localhost:8082
localhost:8083


3.配置

tomcat:

修改tomcat文件夹中conf/context.xml文件,在context节点下添加如下配置:


<!-- 单点配置-->

<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"
host="192.168.1.249"
port="6379"
password="123456"
database="0"
maxInactiveInterval="60" />



host为redis所在服务的ip,port为redis的端口,password为redis密码,高版本的redis要设置密码,不然就会报连接池打开异常。



<!-- 集群配置-->

<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"
maxInactiveInterval="60"
password="123456"
sentinelMaster="mymaster"
sentinels="192.168.1.249:7000,192.168.1.249:7001,192.168.1.249:7002,192.168.1.248:7003,192.168.1.248:7004,192.168.1.248:7005"/>


conf/server.xml文件中的端口根据规划依次修改。

另外要在tomcat的lib文件夹下分别添加三个jar文件,这个地方jar文件的版本有可能会有冲突,配置的时候需要多尝试。我这里的版本如下,是验证过可以使用的,通过maven的库都可以下载到。

tomcat-redis-session-manager-1.2-tomcat-7.jar

jedis-2.2.0.jar

commons-pool-1.6.jar

nginx:

修改nginx文件目中的conf/nginx.conf文件为:

#user  nobody;
worker_processes  1;

error_log  logs/error.log;

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  localhost   { 
          server   localhost:8081 weight=1; 
          server   localhost:8082 weight=2; 
  server   localhost:8083 weight=3;


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://localhost; 
       proxy_set_header  X-Real-IP  $remote_addr; 
        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;
    }

}
}

redis:

vi /usr/local/redis-3.2.4/redis.conf


找到# requirepass foobared ,修改为  requirepass 123456

123456为redis密码


记录操作命令[ Append-only file(缩写aof)的方式](较安全持久化)

appendonly yes #启用aof 持久化方式

# appendfsync always //收到写命令就立即写入磁盘,最慢,但是保证完全的持久化

appendfsync everysec //每秒钟写入磁盘一次,在性能和持久化方面做了很好的折中

# appendfsync no //完全依赖os,性能最好,持久化没保证


# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52  ###重定义命令,例如将CONFIG命令更名为一个很复杂的名字: 

# rename-command CONFIG ""  取消这个命令;

以上两行,任意打开一个,即去掉前面的#


#daemonize no  默认情况下, redis 不是在后台运行的,生成模式时一般需要在后台运行,把该项的值更改为 yes
daemonize yes



4.运行

分别启动redis、nginx和三台tomcat。


5.测试

在三个tomcat的webapps/ROOT目录下,分别添加session.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>shared session</title>
</head>
<body>
<br>session id=<%=session.getId()%>
<br>tomcat 3
</body>
</html>


注:每个tomcat下的标示不同

tomcat1:




tomcat2:




tomcat3:





从截图中,可以看出,分别访问了不同的tomcat,但是得到的session却是相同的,说明达到了集群的目的。










  • 大小: 19.7 KB
  • 大小: 20.3 KB
  • 大小: 19.8 KB
  • 大小: 19.7 KB
  • 大小: 4.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics