基于nginx的tomcat负载均衡和集群

要集群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

  1. <Context docBase="E:/java_codes/TestSession/WebContent" path="" reloadable="true" >
  2. <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
  3. memcachedNodes="n1:localhost:11211"
  4. requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"
  5. sessionBackupAsync="false"
  6. sessionBackupTimeout="100"
  7. transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
  8. copyCollectionsForSerialization="false"
  9. />
  10. </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为例

  1. #user  nobody;
  2. worker_processes  1;
  3. error_log  logs/error.log;
  4. events {
  5. worker_connections  1024;
  6. }
  7. http {
  8. include       mime.types;
  9. default_type  application/octet-stream;
  10. sendfile        on;
  11. keepalive_timeout  65;
  12. #gzip  on;
  13. upstream  www.docyeah.com   {
  14. server   192.168.1.11:8080;
  15. server   192.168.1.101:8080;
  16. }
  17. server {
  18. listen       80;
  19. server_name  www.docyeah.com;
  20. charset utf-8;
  21. location / {
  22. root   html;
  23. index  index.html index.htm;
  24. proxy_pass        http://www.docyeah.com;
  25. proxy_set_header  X-Real-IP  $remote_addr;
  26. client_max_body_size  100m;
  27. }
  28. location ~ ^/(WEB-INF)/ {
  29. deny all;
  30. }
  31. error_page   500 502 503 504  /50x.html;
  32. location = /50x.html {
  33. root   html;
  34. }
  35. }
  36. }
#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的编辑器有问题,代码里加颜色后居然变乱了

上一篇:php实现input输入框失去焦点自动保存输入框的数据


下一篇:mac ssd开启trim