[b]1.准备[/b]
apache tomcat 7.0.55
nginx 1.7.2
redis 2.8.9
配置环境使用三个tomcat, 三台tomcat、redis和nginx都在一台机器上,为了方便测试和部署。
大致的整个配置的架构:
[img]http://dl2.iteye.com/upload/attachment/0130/6093/09721a5b-daab-3ec8-b123-ef47232a9937.jpg[/img]
在这个图中,nginx做为反向代理,将客户请求根据权重随机分配给三台tomcat服务器,redis做为三台tomcat的共享session数据服务器。
[b]2.规划[/b]
redis
localhost:6379
nginx
localhost:80
tomcat
localhost:8081
localhost:8082
localhost:8083
[b]3.配置[/b]
[b]tomcat:[/b]
修改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
[b]nginx:[/b]
修改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;
}
}
}
[b]redis:[/b]
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
[b]4.运行[/b]
分别启动redis、nginx和三台tomcat。
[b]5.测试[/b]
在三个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:
[img]http://dl2.iteye.com/upload/attachment/0130/6087/623467d2-cb81-38dc-8000-8f763e9adb2f.jpg[/img]
tomcat2:
[img]http://dl2.iteye.com/upload/attachment/0130/6089/766ac669-b55d-352b-a041-8638f8ad116b.jpg[/img]
tomcat3:
[img]http://dl2.iteye.com/upload/attachment/0130/6091/26507806-bb89-3069-87b3-b60d42cbcf3f.jpg[/img]
从截图中,可以看出,分别访问了不同的tomcat,但是得到的session却是相同的,说明达到了集群的目的。
apache tomcat 7.0.55
nginx 1.7.2
redis 2.8.9
配置环境使用三个tomcat, 三台tomcat、redis和nginx都在一台机器上,为了方便测试和部署。
大致的整个配置的架构:
[img]http://dl2.iteye.com/upload/attachment/0130/6093/09721a5b-daab-3ec8-b123-ef47232a9937.jpg[/img]
在这个图中,nginx做为反向代理,将客户请求根据权重随机分配给三台tomcat服务器,redis做为三台tomcat的共享session数据服务器。
[b]2.规划[/b]
redis
localhost:6379
nginx
localhost:80
tomcat
localhost:8081
localhost:8082
localhost:8083
[b]3.配置[/b]
[b]tomcat:[/b]
修改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
[b]nginx:[/b]
修改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;
}
}
}
[b]redis:[/b]
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
[b]4.运行[/b]
分别启动redis、nginx和三台tomcat。
[b]5.测试[/b]
在三个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:
[img]http://dl2.iteye.com/upload/attachment/0130/6087/623467d2-cb81-38dc-8000-8f763e9adb2f.jpg[/img]
tomcat2:
[img]http://dl2.iteye.com/upload/attachment/0130/6089/766ac669-b55d-352b-a041-8638f8ad116b.jpg[/img]
tomcat3:
[img]http://dl2.iteye.com/upload/attachment/0130/6091/26507806-bb89-3069-87b3-b60d42cbcf3f.jpg[/img]
从截图中,可以看出,分别访问了不同的tomcat,但是得到的session却是相同的,说明达到了集群的目的。