Nginx + Tomcat + Session學習


分別下載

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

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

 

准備兩個虛擬機:

server1 192.168.1.112

server2 192.168.1.64

 

Tomcat直接解壓,運行,使用默認的8080端口

tar zxvf apache-tomcat-7.0.22.tar.gz
cd apache-tomcat-7.0.22/bin
./startup.sh

訪問http://192.168.1.112:8080和http://192.168.1.64:8080出現Tomcat首頁即可

 

接下來安裝nginx, nginx我就直接安裝在server1上

nginx_upstream_jvm_route是一個Nginx的擴展模塊,用來實現基於Cookie的SessionSticky的功能, 去SVN下載最新版

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

解壓nginx

tar zxvf nginx-1.1.14.tar.gz

cd nginx-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
make install

在nginx安裝目錄下的conf/目錄新建一個文件proxy.conf(/etc/nginx/conf/proxy.conf), 內容如下:

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;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

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

完整配置如下

http {
include mime.types;

#反向代理配置
include proxy.conf;

default_type application/octet-stream;
sendfile on;
    keepalive_timeout  65;

upstream server1{
server 192.168.1.112:8080 srun_id=tomcat1;
server 192.168.1.64:8080 srun_id=tomcat2;
jvm_route $cookie_JSESSIONID|sessionid reverse;
}

server {
listen 80;
server_name localhost;
access_log /var/log/nginx/access.log;

location ~ ^/NginxStatus/ {
stub_status on; #Nginx 狀態監控配置
access_log off;
}

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

location / {
root html;
index index.html index.htm;
proxy_pass http://server1;
}

location /doc {
root /usr/share;
autoindex on;
allow 127.0.0.1;
deny all;
}

location /images {
root /usr/share;
autoindex on;
}

具體配置說明請參考《輕量級WEB服務器Nginx》

然后運行nginx

/etc/nginx/sbin/nginx

訪問http://192.168.1.112/NginxStatus,可以看到nginx狀態

然后修改tomcat配置文件, 打開apache-tomcat-7.0.22/conf/server.xml, 找到最下面的<Engine name="Catalina" defaultHost="localhost">節點, 修改為

server1

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

並插入如下配置

<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
="224.0.0.4"
port
="45564"
frequency
="500"
dropTime
="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address
="192.168.1.112"
port
="4000"
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"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</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>

server2

...
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
<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
="224.0.0.4"
port
="45564"
frequency
="500"
dropTime
="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address
="192.168.1.64"
port
="4000"
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"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</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>
...

 

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內容

<%@ 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>Insert title here</title>

</head>

<body>

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

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

if(null == msg){

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

}
else{

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

}

%>

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

</body>

</html>


web.xml

<?xml version="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也和原來一樣。

 

 

 



 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM