Nginx + Tomcat + Memcached 的集群至少要考虑到2个方面的问题:
1. 如何实现负载均衡?
2. 如何实现session复制与同步?
本文搜集了网上相关的材料,将Nginx + Tomcat + Memcached 的集群在window下的搭建过程总结如下。
一、开发环境
1. nginx版本:1.8.0
2. tomcat版本:7.0.55
3. memcached版本:1.4.13
4. session复制同步使用memcache-session-manager最新版本:1.6.3
5. 操作系统:Win7
二、Nginx + Tomcat 实现负载均衡
1. nginx 和 tomcat的下载安装
这里就不多说了,具体可以参见 nginx 和 tomcat 的下载安装
2. nginx配置
打开nginx的主配置文件:D:\nginx-1.8.0\conf\nginx.conf, 略作修改,修改后的文件如下(红色加粗部分为我配置的代理转发服务器ip和端口):
#user nobody; 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; <span style="color:#FF0000;"><strong>upstream local_tomcat { server localhost:18080 weight=1; server localhost:18081 weight=1; } </strong></span> server { listen 80; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; <span style="color:#FF0000;"><strong>proxy_pass http://local_tomcat; </strong></span> } #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; # } #} }
三、tomcat + memcached 实现session复制与同步
tomcat session复制与同步的问题通常是采用memcached,或者采用nginx_upstream_jvm_route ,他是一个 Nginx 的扩展模块,用来实现基于 Cookie 的 Session Sticky 的功能。如果tomcat过多不建议session同步,server间相互同步session很耗资源,高并发环境容易引起Session风暴。请根据自己应用情况合理采纳session解决方案。
下文介绍了采用memcached (memcache-session-manager)来管理session
1. memcached 下载安装 (略)
2. 下载相关的jar包
这里有很多jar包需要去下载,各个jar的版本还不能弄错,我这里提供了一整套需要下载的jar包 (已经测试可用),需要下载的请点击 链接 下载。
3. 配置session 共享
好了,把上述jar包放到tomcat/lib 目录下,修改每个tomcat的配置文件tomcat\conf\context.xml,在<context>...</context>标签之间加入如下代码:
a. 配置tomcat
修改tomcat\conf\server.xml ,由于我是本地win7系统下装的2个tomcat,所以要避免端口冲突,以tomcat-node1为例,主要修改下面几处:(node2以此类推)
<Server port="8005" shutdown="SHUTDOWN">
<Connector port="18080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
修改tomcat\conf\context.xml, 在<context>...</context>标签之间加入如下代码:
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="<span style="color:#FF0000;"><strong>n1:127.0.0.1:11211</strong></span>" sticky="false" sessionBackupAsync="false" lockingMode="uriPattern:/path1|/path2" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"/>
(注意2个tomcat的配置里面memecachedNodes 的都是 n1:* )
b. 修改应用jsp
我使用的是tomcat自带的examples project,将下面的jsp直接替换D:\nginx-1.8.0\Tomcat\tomcat-node1\webapps\examples\index.html
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="java.text.SimpleDateFormat"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Tomcat集群测试</title> </head> <body> <% out.print("["+request.getLocalAddr()+":" +request.getLocalPort()+"]" + "<br/>session id:" + session.getId()); %> </body> </html>
四、测试
启动nginx、memcached 和 2个tomcat,使用浏览器访问 http://localhost/examples/ ,比较nginx服务器返回的2次页面,发现2次的端口不一样但是session一样代表Nginx + Tomcat + Memcached 的集群搭建成功。
参考文章:
1. http://www.tuicool.com/articles/I7ryYf
2. http://blog.csdn.net/liuzhigang1237/article/details/8880752
3. http://www.cnblogs.com/phirothing/archive/2013/12/05/3459814.html
版权声明:本文为博主原创文章,未经博主允许不得转载。