要集群tomcat主要是解决SESSION共享的问题,因此我利用memcached来保存session,多台TOMCAT服务器即可共享SESSION了。
你可以自己写tomcat的扩展来保存SESSION到memcached。
这里推荐使用memcached-session-manager这个开源项目(http://code.google.com/p/memcached-session-manager/ ),下面简称msm。
如何安装nginx、memcached、tomcat这些就不多说了。
先说明一下测试环境:
tomcat1、nginx、memcached安装在192.168.1.11
tomcat2安装在192.168.1.101
下面分步实现基于nginx的tomcat负载均衡和集群配置
一,tomcat集群
1,先下载msm及其依赖包
http://memcached-session-manager.googlecode.com/files/memcached-session-manager-1.3.0.jar
http://memcached-session-manager.googlecode.com/files/msm-javolution-serializer-jodatime-1.3.0.jar
http://memcached-session-manager.googlecode.com/files/msm-javolution-serializer-cglib-1.3.0.jar
http://spymemcached.googlecode.com/files/memcached-2.4.2.jar
http://memcached-session-manager.googlecode.com/files/javolution-5.4.3.1.jar
2,将这5个包放到$TOMCAT_HOME/lib目录下
3,修改$TOMCAT_HOME/conf/server.xml
- <Context docBase="E:/java_codes/TestSession/WebContent" path="" reloadable="true" >
- <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
- memcachedNodes="n1:localhost:11211"
- requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"
- sessionBackupAsync="false"
- sessionBackupTimeout="100"
- transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
- copyCollectionsForSerialization="false"
- />
- </Context>
<Context docBase="E:/java_codes/TestSession/WebContent" path="" reloadable="true" >
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:localhost:11211"
requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"
sessionBackupAsync="false"
sessionBackupTimeout="100"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
copyCollectionsForSerialization="false"
/>
</Context>
这里的memcachedNodes是填写memcached节点,多个节点时可以以空隔分开,如:
n1:localhost:11211 n2:localhost:11212
sessionBackupTimeout的单位为分钟
E:/java_codes/TestSession/WebContent 替换成你的WEB目录
修改后重启两个TOMCAT即可,这个时候已经解决SESSION的共享问题.
二,配置nginx实现负载均衡
以我的nginx.conf为例
- #user nobody;
- worker_processes 1;
- error_log logs/error.log;
- events {
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- #gzip on;
- upstream www.docyeah.com {
- server 192.168.1.11:8080;
- server 192.168.1.101:8080;
- }
- server {
- listen 80;
- server_name www.docyeah.com;
- charset utf-8;
- location / {
- root html;
- index index.html index.htm;
- proxy_pass http://www.docyeah.com;
- proxy_set_header X-Real-IP $remote_addr;
- client_max_body_size 100m;
- }
- location ~ ^/(WEB-INF)/ {
- deny all;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
- }
#user nobody;
worker_processes 1; error_log logs/error.log; events {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream; sendfile on;
keepalive_timeout 65; #gzip on;
upstream www.docyeah.com {
server 192.168.1.11:8080;
server 192.168.1.101:8080;
}
server {
listen 80;
server_name www.docyeah.com;
charset utf-8;
location / {
root html;
index index.html index.htm;
proxy_pass http://www.docyeah.com;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
} location ~ ^/(WEB-INF)/ {
deny all;
} error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} }
}
将www.docyeah.com替换成你的域名
192.168.1.11和192.168.1.101替换成你服务器的IP
OK,已经完成。启动nginx即可。
这是我采用的负载均衡及集群方案,希望大家拍砖.
ps:javaeye的编辑器有问题,代码里加颜色后居然变乱了