nginx+tomcat7+redis实现tomcat集群session共享
2016-06-27 23:08
477 查看
Linux环境下使用Nginx+Tomcat7+Redis实现tomcat集群的session共享,分为如下几个步骤:
(1)安装jdk环境,此处省略;
(2)下载tomcat7.x,下载地址为:http://tomcat.apache.org/download-70.cgi
(3)下载nginx服务器,下载地址为:http://nginx.org/en/download.html
(4)下载redis缓存数据库,下载地址为:http://redis.io/download
(5)下载完毕后,分别安装,安装步骤省略;
(6)安装完毕后,首先对redis进行配置,配置中注意一下几个问题:
① 如果不是本地连接,需要注意redis.conf中的bind属性的配置,将 bind localhost 这一行注释掉,否则可能会造成redis无法正常连接;
② 出现redis protected model类似的错误时,将redis.conf中的protected-model属性设置为no即可解决;
③ 如果redis数据库设置有密码,则需要在tomcat中的context.xml中配置,下面会讲到;
(7)对nginx进行配置,配置文件示例如下:此处测试时使用的主机ip为192.168.137.223
keepalive_timeout 65;
upstream wb.com {
server 192.168.137.223:8080;
server 192.168.137.223:8081;
server 192.168.137.223:8082;
}
#gzip on;
server {
listen 80;
server_name wb.com;
root /opt/tomcat7_1/webapps/test;
#charset koi8-r;
access_log /usr/logs/nginx.access.log main;
error_log /usr/logs/nginx.error.log warn;
location / {
proxy_pass http://wb.com;
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#root html; #local website
index index.html index.htm;
}
error_page 403 404 /40x.html;
location = /40x.html {
root html;
}
location /nginx_status {
stub_status on;
access_log off;
}
}
注意:upstream wb.com中的三个url地址分别对应集群中三个tomcat服务器;
root /opt/tomcat7.x.1/webapps/test;表示默认指向第一个tomcat中的test目录,test目录是一个放在webapps中的war包或者文件夹;
(8)为了在一台机器上同时启动多个tomcat,需要对三个tomcat进行配置:需要配置的地方有两处,分别是tomcat中conf/server.xml和conf/context.xml;
① 对第一个tomcat进行配置,第一台可以使用默认配置,需要修改server.xml和context.xml;
② 对第二个tomcat的server.xml进行配置,server.xml的配置中需要修改的地方如下:
a.第一处修改:<Server port="8006" shutdown="SHUTDOWN"> //默认端口是8005,此处改为了8006;
b.第二处修改:<Connector port="8081" protocol="HTTP/1.1 connectionTimeout="20000" redirectPort="8443" /> //port默认是8080,此处修改为8081;
c.第三处修改:<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" /> //默认是8009,此处改为了8010;
d*.地四处修改(可以不配置):<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2"> //默认没有jvmRoute属性,此处加上这个属性,值可以自定义,在使用http-server做集群时会用到;
③ 对第二个tomcat的context.xml进行配置,context.xml的配置中需要添加的内容如下:
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="192.168.137.223"
port="6379"
database="0"
maxInactiveInterval="60" />
注意:上述的host为redis服务器所在的服务器的ip地址,port为redis服务开启后监听的端口,database为redis默认使用的数据库,maxInactiveInterval为session在redis中的缓存时间,默认是60秒;如果redis数据库有登陆密码验证,则需要在上述的<Manager ../>中添加password="xxxxx"属性,值为redis密码;
④ 对第三个tomcat进行配置:
a.第一处修改:<Server port="8007" shutdown="SHUTDOWN"> //默认端口是8005,此处改为了8007;
b.第二处修改:<Connector port="8082" protocol="HTTP/1.1 connectionTimeout="20000" redirectPort="8443" /> //port默认是8080,此处修改为8082;
c.第三处修改:<Connector port="8011" protocol="AJP/1.3" redirectPort="8443" /> //默认是8009,此处改为了8011;
d*.地四处修改(可以不配置):<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2"> //默认没有jvmRoute属性,此处加上这个属性,值可以自定义,在使用http-server做集群时会用到;
⑤ 对第二个tomcat的context.xml进行配置,context.xml的配置中需要添加的内容如下:
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="192.168.137.223"
port="6379"
database="0"
maxInactiveInterval="60" />
(9)将session共享的依赖包分别放入tomcat根目录中的lib目录下,依赖的jar包有:commons-pool2-2.3.jar,jedis-2.7.3.jar,tomcat-redis-session-manager-master-2.0.0.jar,tomcat-juli-7.0.61.jar,其中tomcat-redis-session-manager-master-2.0.0.jar可以自行下载编译为jar,下载地址为:https://github.com/jcoleman/tomcat-redis-session-manager
(10)编写jsp页面进行session共享的测试,步骤如下:
4000
① 新建一个web工程,并添加一个jsp页面,命名为: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的session共享</title>
</head>
<body>
<table align="centre" border="1">
<h1>Tomcat1</h1>
<tr>
<td>Session ID</td>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
② 将该工程打包为一个war包,同时修改上述页面中的<h1>Tomcat1</h1>为<h1>Tomcat2</h1>和<h1>Tomcat3</h1>,并分别打包为另外的两个war包,为了部署好以后可以明确区分页面;
③ 将打包后的三个war包分别复制到三个tomcat的webapps目录下;
④ 先启动redis缓存数据库,命令为:# redis-server /etc/redis.conf
再启动nginx服务器,命令为:# /etc/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
最后分别启动三个tomcat服务器,命令为:# sh /opt/tomcat7.x.1/bin/startup.sh && /opt/tomcat7.x.2/bin/startup.sh && /opt/tomcat7.x.3/bin/startup.sh
⑤ 测试,在浏览器中输入:http://192.168.137.223/test/index.jsp,然后刷新页面,会发现sesson相同,同时使用# keys * 命令查看redis数据库,发现session已经被存储到了redis中,经过此过程,已经实现了nginx+tomcat+redis集群的session共享。
(1)安装jdk环境,此处省略;
(2)下载tomcat7.x,下载地址为:http://tomcat.apache.org/download-70.cgi
(3)下载nginx服务器,下载地址为:http://nginx.org/en/download.html
(4)下载redis缓存数据库,下载地址为:http://redis.io/download
(5)下载完毕后,分别安装,安装步骤省略;
(6)安装完毕后,首先对redis进行配置,配置中注意一下几个问题:
① 如果不是本地连接,需要注意redis.conf中的bind属性的配置,将 bind localhost 这一行注释掉,否则可能会造成redis无法正常连接;
② 出现redis protected model类似的错误时,将redis.conf中的protected-model属性设置为no即可解决;
③ 如果redis数据库设置有密码,则需要在tomcat中的context.xml中配置,下面会讲到;
(7)对nginx进行配置,配置文件示例如下:此处测试时使用的主机ip为192.168.137.223
keepalive_timeout 65;
upstream wb.com {
server 192.168.137.223:8080;
server 192.168.137.223:8081;
server 192.168.137.223:8082;
}
#gzip on;
server {
listen 80;
server_name wb.com;
root /opt/tomcat7_1/webapps/test;
#charset koi8-r;
access_log /usr/logs/nginx.access.log main;
error_log /usr/logs/nginx.error.log warn;
location / {
proxy_pass http://wb.com;
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#root html; #local website
index index.html index.htm;
}
error_page 403 404 /40x.html;
location = /40x.html {
root html;
}
location /nginx_status {
stub_status on;
access_log off;
}
}
注意:upstream wb.com中的三个url地址分别对应集群中三个tomcat服务器;
root /opt/tomcat7.x.1/webapps/test;表示默认指向第一个tomcat中的test目录,test目录是一个放在webapps中的war包或者文件夹;
(8)为了在一台机器上同时启动多个tomcat,需要对三个tomcat进行配置:需要配置的地方有两处,分别是tomcat中conf/server.xml和conf/context.xml;
① 对第一个tomcat进行配置,第一台可以使用默认配置,需要修改server.xml和context.xml;
② 对第二个tomcat的server.xml进行配置,server.xml的配置中需要修改的地方如下:
a.第一处修改:<Server port="8006" shutdown="SHUTDOWN"> //默认端口是8005,此处改为了8006;
b.第二处修改:<Connector port="8081" protocol="HTTP/1.1 connectionTimeout="20000" redirectPort="8443" /> //port默认是8080,此处修改为8081;
c.第三处修改:<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" /> //默认是8009,此处改为了8010;
d*.地四处修改(可以不配置):<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2"> //默认没有jvmRoute属性,此处加上这个属性,值可以自定义,在使用http-server做集群时会用到;
③ 对第二个tomcat的context.xml进行配置,context.xml的配置中需要添加的内容如下:
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="192.168.137.223"
port="6379"
database="0"
maxInactiveInterval="60" />
注意:上述的host为redis服务器所在的服务器的ip地址,port为redis服务开启后监听的端口,database为redis默认使用的数据库,maxInactiveInterval为session在redis中的缓存时间,默认是60秒;如果redis数据库有登陆密码验证,则需要在上述的<Manager ../>中添加password="xxxxx"属性,值为redis密码;
④ 对第三个tomcat进行配置:
a.第一处修改:<Server port="8007" shutdown="SHUTDOWN"> //默认端口是8005,此处改为了8007;
b.第二处修改:<Connector port="8082" protocol="HTTP/1.1 connectionTimeout="20000" redirectPort="8443" /> //port默认是8080,此处修改为8082;
c.第三处修改:<Connector port="8011" protocol="AJP/1.3" redirectPort="8443" /> //默认是8009,此处改为了8011;
d*.地四处修改(可以不配置):<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2"> //默认没有jvmRoute属性,此处加上这个属性,值可以自定义,在使用http-server做集群时会用到;
⑤ 对第二个tomcat的context.xml进行配置,context.xml的配置中需要添加的内容如下:
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="192.168.137.223"
port="6379"
database="0"
maxInactiveInterval="60" />
(9)将session共享的依赖包分别放入tomcat根目录中的lib目录下,依赖的jar包有:commons-pool2-2.3.jar,jedis-2.7.3.jar,tomcat-redis-session-manager-master-2.0.0.jar,tomcat-juli-7.0.61.jar,其中tomcat-redis-session-manager-master-2.0.0.jar可以自行下载编译为jar,下载地址为:https://github.com/jcoleman/tomcat-redis-session-manager
(10)编写jsp页面进行session共享的测试,步骤如下:
4000
① 新建一个web工程,并添加一个jsp页面,命名为: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的session共享</title>
</head>
<body>
<table align="centre" border="1">
<h1>Tomcat1</h1>
<tr>
<td>Session ID</td>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
② 将该工程打包为一个war包,同时修改上述页面中的<h1>Tomcat1</h1>为<h1>Tomcat2</h1>和<h1>Tomcat3</h1>,并分别打包为另外的两个war包,为了部署好以后可以明确区分页面;
③ 将打包后的三个war包分别复制到三个tomcat的webapps目录下;
④ 先启动redis缓存数据库,命令为:# redis-server /etc/redis.conf
再启动nginx服务器,命令为:# /etc/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
最后分别启动三个tomcat服务器,命令为:# sh /opt/tomcat7.x.1/bin/startup.sh && /opt/tomcat7.x.2/bin/startup.sh && /opt/tomcat7.x.3/bin/startup.sh
⑤ 测试,在浏览器中输入:http://192.168.137.223/test/index.jsp,然后刷新页面,会发现sesson相同,同时使用# keys * 命令查看redis数据库,发现session已经被存储到了redis中,经过此过程,已经实现了nginx+tomcat+redis集群的session共享。
相关文章推荐
- java-模拟tomcat服务器
- i-jetty环境搭配与编译
- nginx代理指定目录
- 实现单Tomcat多Server配置
- 生产环境下的Tomcat配置
- 访问Nginx发生SSL connection error的一种情况
- redis安装问题小结
- Linux部署Tomcat服务器
- Nginx+Naxsi部署专业级Web应用防火墙
- jenkins------结合maven将svn项目自动部署到tomcat下
- 如何搞定tomcat这只喵~
- CentOS 6.2实战部署Nginx+MySQL+PHP
- nginx中http核心模块的配置指令2
- nginx中http核心模块的配置指令3
- nginx中http核心模块的配置指令4
- nginx中http的fastcgi模块的配置指令1
- Nginx 学习笔记(一)
- 使用 Redis 和 Python 构建一个共享单车的应用程序