一:所用到的工具
tomcat7 、nginx、tomcat7(2个)
nginx下载地址http://nginx.org/en/download.html
redis下载安装可以参考我写的一篇博客地址:
二:工具版本
这里我用的工具版本为:
apache-tomcat-7.0.70
nginx-1.11.2
redis直接用的是虚拟机上的3.0的
三:集群架构图
四、进行配置
1.使用nginx进行对多台服务器进行代理
#gzip on;
upstream localhost {
server localhost:8080 weight=1;
server localhost:8081 weight=2;
}
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;
}
nginx.conf修改的地方有两处:
---.#gzip on;
upstream localhost {
server localhost:8080 weight=1;
server localhost:8081 weight=2;
}
添加两台要代理的服务器
---proxy_pass http://localhost;
更改访问地址
2.tomcat配置
tomcat的lib目录中添加所用到的jar包
tomcat-redis-session-manager-1.2-tomcat-7.jar
jedis-2.1.0.jar
commons-pool-1.6.jar
3.更改tomcat端口
这里不多说。如果在同一台机器上测试tomcat,端口占用了tomcat是启动不了的。意思就是只要让tomcat起来就行了。
4.在多台tomcat服务器上修改改context.xml
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!-- <Manager pathname="" /> -->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve"
/> -->
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"
host="192.168.29.129" port="6379" database="0" maxInactiveInterval="60" />
</Context>
添加以下代码:
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"
host="192.168.29.129" port="6379" database="0" maxInactiveInterval="60" />
5.在每台tomcat的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>SessionPort:<%=request.getServerPort()%>
<br>tomcat7 ——2——8081
</body>
</html>
用于识别各自的端口号和sessionId是否共享成功。
<%@ 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>SessionPort:<%=request.getServerPort()%>
<br>tomcat7 ——1——8080
</body>
</html>
5.测试地址:localhost/session.jsp
localhost:nginx代理地址,session.jsp :多台tomcat下同名session.jsp,nginx会进行轮回调用。
==================================================================================
好了。不知道有没遗漏的0.0反正我测试成功了!
现成tomcat配置和nginx配置下载地址:tomcat7集群,里面包含了nginx配置
群号:276520216共同探讨。
架构图借鉴:http://my.oschina.net/liting/blog/535273