smilebestSun 2014-05-21
转载自: http://blog.sina.com.cn/s/blog_5f53615f0100w299.html
本例用到的软件和相应版本如下:
Nginx for windows V1.1.8,下载地址:http://nginx.org/en/download.html
Membase for windows V1.7.2,下载地址:http://www.couchbase.com/downloads/membase-server/community
Tomcat for windows V7
Memcached session manager V1.5.1,下载地址:http://code.google.com/p/memcached-session-manager/
Memcached java client: spymemcached-2.7.3.jar,下载地址:http://code.google.com/p/spymemcached/
集群结构图如下:
搭建步骤:
一、 部署两个tomcat7,端口分别改为5080和6080
二、 下载nginx,并解压,修改配置:
打开nginx-1.1.8\conf\nginx.conf配置文件
#user nobody; #cpu个数,与机器有关 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 { #连接数,一般在windows系统下不能设置太大,否则会造成没有相应 worker_connections 200; } 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; client_header_buffer_size 1k; large_client_header_buffers 4 4k; #gzip on; upstream myserver { #tomcat访问url,权重 server localhost:6080 weight=1 fail_timeout=10; server localhost:5080 weight=1 fail_timeout=10; #采用共享session不能用ip_hash #ip_hash; } server { #监听端口 listen 7080; #监听服务器 server_name 172.18.17.130; charset utf-8; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; #请求转发到前面定义的myserver proxy_pass http://myserver; #html头加上7080端口 proxy_set_header Host $host:7080; } #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; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } } |
三、 下载并安装Membase,安装后会自动启动membase服务,进入控制台显示:
登录,进入配置。
四、 Copy jar包到tomcat\lib目录下
将Memcached session manager的几个jar包:
memcached-session-manager-1.5.1.jar、memcached-session-manager-tc7-1.5.1.jar、msm-javolution-serializer-1.5.1.jar、msm-kryo-serializer-1.5.1.jar、msm-xstream-serializer-1.5.1.jar
和spymemcached-2.7.3.jar,copy到两个tomcat的lib目录下。其中memcached-session-manager-tc7-1.5.1.jar是for tomcat7的,msm-javolution-serializer-1.5.1.jar、msm-kryo-serializer-1.5.1.jar、msm-xstream-serializer-1.5.1.jar是负责session持久化,根据配置文件的持久化策略选择相应的一种就可以。
五、 修改tomcat session存储策略
修改tomcat\conf\context.xml文件,在<context>中加入
<Manager classname="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:172.18.17.130:11211" sticky="false" requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$" /> |
其中,className是session管理类,memcachedNodes="n1:172.18.17.130:11211"是membase cache集群,这里只有一个节点;sticky=”false”表示不采用粘性session,默认值是true,如果要真正的实现负载均衡就要使用session共享方式,而不采用粘性session。Session持久化默认是采用java持久化,但性能比Kryo方式差。如果是kryo方式还需要引入更多的相关jar包。