您的位置:首页 > 运维架构 > Tomcat

Nginx + Tomcat + Session

2014-12-10 09:58 381 查看
分别下载

tomcathttp://archive.apache.org/dist/tomcat/tomcat-7/v7.0.22/bin/apache-tomcat-7.0.22.tar.gz

nginxhttp://nginx.org/download/nginx-1.1.14.tar.gz

准备两个虚拟机:

server1192.168.1.112

server2192.168.1.64

Tomcat直接解压,运行,使用默认的8080端口

访问http://192.168.1.112:8080和http://192.168.1.64:8080出现Tomcat首页即可

接下来安装nginx,nginx我就直接安装在server1上

nginx_upstream_jvm_route是一个Nginx的扩展模块,用来实现基于Cookie的SessionSticky的功能,去SVN下载最新版

svncheckouthttp://nginx-upstream-jvm-route.googlecode.com/svn/trunk//root/dev/nginx-upstream-jvm-route-read-only


解压nginx

tarzxvfnginx-1.1.14.tar.gz

cdnginx-1.1.14


运行

patch-p0</root/dev/nginx-upstream-jvm-route-read-only/jvm_route.patch

./configure--prefix=/etc/nginx--with-http_stub_status_module--add-module=/root/dev/nginx-upstream-jvm-route-read-only/

make

makeinstall


在nginx安装目录下的conf/目录新建一个文件proxy.conf(/etc/nginx/conf/proxy.conf),内容如下:

proxy_redirectoff;
proxy_set_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
client_max_body_size10m;
client_body_buffer_size128k;
proxy_connect_timeout90;
proxy_send_timeout90;
proxy_read_timeout90;
proxy_buffer_size4k;
proxy_buffers432k;
proxy_busy_buffers_size64k;
proxy_temp_file_write_size64k;


修改nginx配置文件/etc/nginx/conf/nginx.conf

完整配置如下

http{
includemime.types;

#反向代理配置
includeproxy.conf;

default_typeapplication/octet-stream;
sendfileon;

keepalive_timeout65;

upstreamserver1{
server192.168.1.112:8080srun_id=tomcat1;
server192.168.1.64:8080srun_id=tomcat2;
jvm_route$cookie_JSESSIONID|sessionidreverse;
}

server{
listen80;
server_namelocalhost;
access_log/var/log/nginx/access.log;

location~^/NginxStatus/{
stub_statuson;#Nginx状态监控配置
access_logoff;
}

location~^/(WEB-INF)/{
denyall;
}

location/{
roothtml;
indexindex.htmlindex.htm;
proxy_passhttp://server1;}

location/doc{
root/usr/share;
autoindexon;
allow127.0.0.1;
denyall;
}

location/images{
root/usr/share;
autoindexon;
}


具体配置说明请参考《轻量级WEB服务器Nginx》

然后运行nginx

/etc/nginx/sbin/nginx


访问http://192.168.1.112/NginxStatus,可以看到nginx状态

然后修改tomcat配置文件,打开apache-tomcat-7.0.22/conf/server.xml,找到最下面的<Enginename="Catalina"defaultHost="localhost">节点,修改为

server1

<Enginename="Catalina"defaultHost="localhost"jvmRoute="tomcat1">


并插入如下配置

<ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<ManagerclassName="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<ChannelclassName="org.apache.catalina.tribes.group.GroupChannel">
<MembershipclassName="org.apache.catalina.tribes.membership.McastService"
address="224.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<ReceiverclassName="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="192.168.1.112"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<SenderclassName="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<TransportclassName="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<InterceptorclassName="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<InterceptorclassName="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
<InterceptorclassName="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</Channel>
<ValveclassName="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<ValveclassName="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<ClusterListenerclassName="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListenerclassName="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>


server2

...

<Enginename="Catalina"defaultHost="localhost"jvmRoute="tomcat2">
<ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<ManagerclassName="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<ChannelclassName="org.apache.catalina.tribes.group.GroupChannel">
<MembershipclassName="org.apache.catalina.tribes.membership.McastService"
address="224.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<ReceiverclassName="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="192.168.1.64"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<SenderclassName="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<TransportclassName="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<InterceptorclassName="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<InterceptorclassName="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
<InterceptorclassName="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</Channel>
<ValveclassName="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<ValveclassName="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<ClusterListenerclassName="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListenerclassName="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>

...


jvmRoute="tomcat1"和nginx配置upstream中的srun_id对应

membership中的address=224.0.0.4为组播IP,集群中的tomcat通信之用

receiver中的address设为本机IP,或auto,如果多个Tomcat在同一台电脑上,则要保证port端口不重复


在两个tomcat的webapps目录下分别新建一个项目test,

/test

/test/index.jsp

/test/WEB-INF/

/test/WEB-INF/web.xml

index.jsp内容

<%@pagelanguage="java"contentType="text/html;charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">

<title>Inserttitlehere</title>

</head>

<body>

<%=session.getId()%><br>
<%

Stringmsg=(String)session.getAttribute("msg");

if(null==msg){

session.setAttribute("msg","Hello!");

}else{

session.setAttribute("msg",msg+0);

}

%>

<%=session.getAttribute("msg")%>

</body>

</html>


web.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<distributable/>

</web-app>


注意上面的distributable节点,表示该应用是在集群环境下的。

重启tomcat后在两台机子上访问http://192.168.1.112/test

从SessionId后面带的服务器名可以看到负载均衡的效果。

然后将server1上的tomcat停掉,再刷新页面,可以看到原来访问server1的页面成功地切换到了server2,而sessionId没有变,session中的msg也和原来一样。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: