首页 > 代码库 > nginx+tomcat的集群和session复制
nginx+tomcat的集群和session复制
前端服务器采用nginx,后端应用服务器采用tomcat。nginx负责负载均衡,session复制在tomcat上处理。
1、nginx安装(略)
2、nginx配置负载均衡
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#设置负载均衡列表
upstream backend
{
#ip_hash;
server localhost:8081;
server localhost:8082;
}
#设置虚拟主机
server {
listen 8888;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root "E:\test";
index index.html index.htm;
proxy_redirect off;
#保留用户真实信息
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://backend;
}
}
}
2、tomcat安装(略)
3、tomcat负载均衡配置
分别修改两个tomcat中的server.xml文件,找到<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">类似这句话
1)两个tomcat分别设置为jvmRoute="jvm1",jvmRoute="jvm2";
2)在这句话加入以下内容(直接复制使用即可)
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
3、在你的项目WEB-INF下的web.xml中添加
<distributable/>
4、测试
建立文件夹testCluster,在文件夹下建立WEB-INF文件夹,在WEB-INF文件夹下放入web.xml(web.xml内容中记得加入<distributable/>),在testCluster文件夹中放入testCluster.jsp,testCluster.jsp内容如下:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster Test</title></head>
<body>
<%
//HttpSession session = request.getSession(true);
System.out.println(session.getId());
out.println("<br> SESSION ID:" + session.getId()+"<br>");
// 如果有新的请求,则添加session属性
String name = request.getParameter("name");
if (name != null && name.length() > 0) {
String value = http://www.mamicode.com/request.getParameter("value");
session.setAttribute(name, value);
}
out.print("<b>Session List:</b>");
Enumeration<String> names = session.getAttributeNames();
while (names.hasMoreElements()) {
String sname = names.nextElement();
String value = http://www.mamicode.com/session.getAttribute(sname).toString();
out.println( sname + " = " + value+"<br>");
System.out.println( sname + " = " + value);
}
%>
<form action="testCluster.jsp" method="post">
名称:<input type=text size=20 name="name">
<br>
值:<input type=text size=20 name="value">
<br>
<input type=submit value="http://www.mamicode.com/提交">
</form>
</body>
</html>
启动nginx、tomcat1、tomcat2,通过nginx端口访问testCluster.jsp。输入名称和值,不断刷新页面。
现象:
1、tomcat1、tomcat2后台轮询打印出信息。【达到负载均衡】
2、tomcat1端口访问tomcat1端口访问testCluster.jsp输入名称和值,提交。tomcat2端口访问testCluster.jsp能看到tomcat1提交的数据。【达到session复制】
nginx+tomcat的集群和session复制