本文使用到nginx1.5.4、tomcat6.0.35。
1、配置nginx
~/conf/nginx.conf
#user guzicheng; worker_processes 1; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; upstream localhost { server 127.0.0.1:8081; server 127.0.0.1:8082; } server { listen 80; server_name localhost; charset utf-8; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; proxy_pass http://localhost; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }主要修改内容:
……
http{
upstream localhost {
server 127.0.0.1:8081;
server 127.0.0.1:8082;
}
server {
listen 80;
server_name localhost;
……
location / {
……
proxy_pass http://localhost;
}
……
}
}
……
测试修改内容:
/usr/local/nginx/sbin/nginx -t启动nginx
/usr/local/nginx/sbin/nginx在浏览器中输入http://localhost,测试nginx是否启动成功。
2、配置Tomcat
分别打开tomcat1和tomcat2下conf中server.xml。
注意端口号不要重复
<Server port="8005" shutdown="SHUTDOWN"> ... <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> ... <!-- Define an AJP 1.3 Connector on port 8009 --> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
修改<Engine>节点
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
<Engine name="Catalina" defaultHost="localhost"jvmRoute="tomcat2">
(注:至此,完成了tomcat负载均衡。下面进行session同步配置,通过tomcat自身实现session同步。)
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
打开web.xml,在</web-app>节点前添加:
<distributable/>
a、在tomcat下创建test工程,分别建立测试页面index.jsp
tomcat1:
SessionID:<%=session.getId()%> <BR> SessionIP:<%=request.getServerName()%> <BR> SessionPort:<%=request.getServerPort()%> <BR> <% String name=(String)session.getAttribute("name"); if(name==null||name.equals("")){ session.setAttribute("name","Hello Host1!"); out.println(session.getAttribute("name")); }else{ out.println(name); } %> <BR> This is Tomcat Server 111111!
tomcat2:
SessionID:<%=session.getId()%> <BR> SessionIP:<%=request.getServerName()%> <BR> SessionPort:<%=request.getServerPort()%> <BR> <% String name=(String)session.getAttribute("name"); if(name==null||name.equals("")){ session.setAttribute("name","Hello Host2!"); out.println(session.getAttribute("name")); }else{ out.println(name); } %> <BR> This is Tomcat Server 222222!
b、启动tomcat1、tomcat2、nginx
c、在浏览器中输入http://localhost/test/index.jsp
如果SessionID、CreateTime、“Hello Host”不变,最后易行“This is Tomcat Server ...”不停变化,说明session复制成功。
这种方式实现的session同步存在缺陷:当一个节点的session数据发生变化,要把整个session数据全部序列化,广播给其他节点,造成大量网络通信,导致网络阻塞。下面一篇介绍通过terracotta实现tomcat集群的session同步。
参考:
http://www.blogjava.net/Alpha/archive/2011/06/21/352745.html
http://blog.csdn.net/shimiso/article/details/8979044
http://blog.163.com/comfort_122/blog/static/48904409201131145149530