本次主要是在win7 64 上演示操作。
web应用构建
简单构造web应用,这里就不做描述了,具体的可以访问我的Github。
为了方便显示,在success.jsp上做区别,一个加上8080,一个加上8081。
Memcached安装、配置、启动
本次示例使用了2个memcached节点,都在一台机子上。
win7 64上的操作。资源可以在这个链接上下载我是链接。
解压后进入cmd进入此目录,记住一定要用已管理员身份运行。
安装
<code>memcached <span>-d</span> install</code>
运行
<code>memcached <span>-d</span> start</code>
也可以启动多个memcached服务,可以参考这个链接。
创建
<code>sc <span><span>create</span><span>"Memcached Server"</span><span>start</span>= auto binPath= <span>"memcached_path -d runservice -m 32 -p 11311 -l 127.0.0.1"</span> DisplayName= <span>"Memcached Server"</span></span></code>
注意修改端口,默认是11211
可以通过Telnet访问:telnet 127.0.0.1 11211
telnet界面 quit可以退出
启动、停止、卸载Memcached Server的命令:
<code>sc start <span>"Memcached Server"</span><span>//启动</span> sc stop <span>"Memcached Server"</span><span>//停止</span> sc <span>delete</span><span>"Memcached Server"</span><span>//卸载该服务</span></code>
可以在任务管理器上看到memcached服务
Tomcat配置
本次以2个tomcat7.0.62示例
Tomcat 集群配置,集群中各个结点通过共享存储在缓存Memcached中session来实现session的共享:如果有一台机器上的Tomcat服务停掉了,对于其他对等服务器上的session数据仍然可以从Memcached缓存中读取,从而不会发生session丢失的问题。
使用memcached-session-manager,官方文档。
tomcat的配置主要包括两个方面,首先需要下载所需要的包,放到tomcat安装目录下的lib目录下(严格来说应该是$CATALINA_HOME/lib/)
第一个msm所需要的jar,序列化所需要的jar,
第二个配置context.xml
所需jar包
tomcat 7对应的maven配置
需要如下3个jar包;
memcached-session-manager-version.jar,
memcached-session-manager-tc7-version.jar,
spymemcached-version.jar
可以在下面maven中下载
<code><span>dependency</span>><span>groupId</span>>de.javakaffee.msm<span><span>groupId</span>></span><span>artifactId</span>>memcached-session-manager-tc7<span><span>artifactId</span>></span><span>version</span>>1.8.3<span><span>version</span>></span><span><span>dependency</span>></span></code>
4种序列化
<code>kryo-serializer: <span>dependency</span>><span>groupId</span>>de.javakaffee.msm<span><span>groupId</span>></span><span>artifactId</span>>msm-kryo-serializer<span><span>artifactId</span>></span><span>version</span>>version<span><span>version</span>></span><span><span>dependency</span>></span>javolution: <span>dependency</span>><span>groupId</span>>de.javakaffee.msm<span><span>groupId</span>></span><span>artifactId</span>>msm-javolution-serializer<span><span>artifactId</span>></span><span>version</span>>version<span><span>version</span>></span><span><span>dependency</span>></span>xstream: <span>dependency</span>><span>groupId</span>>de.javakaffee.msm<span><span>groupId</span>></span><span>artifactId</span>>msm-xstream-serializer<span><span>artifactId</span>></span><span>version</span>>version<span><span>version</span>></span><span><span>dependency</span>></span>flexjson: <span>dependency</span>><span>groupId</span>>de.javakaffee.msm<span><span>groupId</span>></span><span>artifactId</span>>msm-flexjson-serializer<span><span>artifactId</span>></span><span>version</span>>version<span><span>version</span>></span><span><span>dependency</span>></span></code>
各种序列化所需要的jar包如下:
- kryo-serializer: msm-kryo-serializer, kryo-serializers, kryo, minlog, reflectasm, asm
- javolution-serializer: msm-javolution-serializer, javolution
- xstream-serializer: msm-xstream-serializer, xstream, xmlpull, xpp3_min
- flexjson-serializer: msm-flexjson-serializer, flexjson
$CATALINA_HOME/lib/添加jar情况如下
context.xml配置
即修改$CATALINA_HOME/conf/context.xml文件中Context节点下的内容,提交MSM配置。
主要包括使用memcached来管理黏性session和非黏性session以及使用membase来管理非黏性session。
示例基于在一台主机的2个memcached实例,示例使用的序列化方式为kryo。
本次使用黏性session来管理,至于非黏性的可以自行配置或者下次再补充,配置如下,2个tomcat配置一样。
<code><context> <span>...</span><manager classname="<span">"de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes=<span>"n1:localhost:11211,n2:localhost:11311"</span> sticky=<span>"false"</span> sessi>"false" lockingMode=<span>"auto"</span> requestUriIgnorePattern=<span>".*\.(ico|png|gif|jpg|css|js)$"</span>transcoderFactoryClass=<span>"de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"</span>/> </manager></context> </code>
Nginx安装、配置、启动
官网下载稳定版本,
解压后进入该目录后,
<code>start nginx <span>// 启动</span></code>
这样,nginx 服务就启动了。打开任务管理器,查看 nginx.exe 进程,有二个进程会显示,占用系统资源,那是相当的少。然后再打开浏览器,输入 http://127.0.0.1/ 就可以看到nginx的欢迎页面了,非常友好。
<code>nginx <span>-s</span> stop <span>// 停止nginx</span> nginx <span>-s</span> reload <span>// 重新加载配置文件</span> nginx <span>-s</span> quit <span>// 退出nginx</span></code>
默认情况下,Nginx使用默认的配置文件nginx.conf,修改它,如下:
<code><span>#user nobody;</span><span>worker_processes</span><span>2</span>; <span>#error_log logs/error.log;</span><span>#error_log logs/error.log notice;</span><span>#error_log logs/error.log info;</span><span>#pid logs/nginx.pid;</span><span>events</span> { <span>worker_connections</span><span>1024</span>; } <span>http</span> { <span>include</span> mime.types; <span>default_type</span> application/octet-stream; <span>log_format</span> main <span>'<span>$remote_addr</span> - <span>$remote_user</span> [<span>$time_local</span>] "<span>$request</span>" '</span><span>'<span>$status</span><span>$body_bytes_sent</span> "<span>$http_referer</span>" '</span><span>'"<span>$http_user_agent</span>" "<span>$http_x_forwarded_for</span>"'</span>; <span>access_log</span> logs/access.log main; <span>sendfile</span><span>on</span>; <span>#tcp_nopush on;</span><span>#keepalive_timeout 0;</span><span>#keepalive_timeout 65;</span><span>#gzip on;</span><span>upstream</span> localhost { <span>#ip_hash;</span><span>server</span> localhost:<span>8080</span> weight=<span>1</span>; <span>server</span> localhost:<span>8081</span> weight=<span>1</span>; } <span>server</span> { <span>listen</span><span>80</span>; <span>server_name</span> localhost; <span>charset</span> utf-<span>8</span>; <span>#charset koi8-r;</span><span>#access_log logs/host.access.log main;</span><span>location</span> / { <span>root</span> html; <span>index</span> index.html index.htm; <span>proxy_pass</span><span>http://localhost</span>; <span>proxy_connect_timeout</span><span>2s</span>; <span>proxy_set_header</span> X-Real-IP <span>$remote_addr</span>; <span>proxy_set_header</span> X-Forwarded-For <span>$proxy_add_x_forwarded_for</span>; } <span>location</span><span>~ ^/(WEB-INF)/</span> { <span>deny</span> all; } <span>#error_page 404 /404.html;</span><span># redirect server error pages to the static page /50x.html</span><span>#</span><span>error_page</span><span>500</span><span>502</span><span>503</span><span>504</span> /50x.html; <span>location</span> = /50x.html { <span>root</span> html; } <span># proxy the PHP scripts to Apache listening on 127.0.0.1:80</span><span>#</span><span>#location ~ \.php$ {</span><span># proxy_pass http://127.0.0.1;</span><span>#}</span><span># pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000</span><span>#</span><span>#location ~ \.php$ {</span><span># root html;</span><span># fastcgi_pass 127.0.0.1:9000;</span><span># fastcgi_index index.php;</span><span># fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;</span><span># include fastcgi_params;</span><span>#}</span><span># deny access to .htaccess files, if Apache's document root</span><span># concurs with nginx's one</span><span>#</span><span>#location ~ /\.ht {</span><span># deny all;</span><span>#}</span> } <span># another virtual host using mix of IP-, name-, and port-based configuration</span><span>#</span><span>#server {</span><span># listen 8000;</span><span># listen somename:8080;</span><span># server_name somename alias another.alias;</span><span># location / {</span><span># root html;</span><span># index index.html index.htm;</span><span># }</span><span>#}</span><span># HTTPS server</span><span>#</span><span>#server {</span><span># listen 443 ssl;</span><span># server_name localhost;</span><span># ssl_certificate cert.pem;</span><span># ssl_certificate_key cert.key;</span><span># ssl_session_cache shared:SSL:1m;</span><span># ssl_session_timeout 5m;</span><span># ssl_ciphers HIGH:!aNULL:!MD5;</span><span># ssl_prefer_server_ciphers on;</span><span># location / {</span><span># root html;</span><span># index index.html index.htm;</span><span># }</span><span>#}</span>} </code>
测试
登陆页面
成功页面
success1
success2
当把tomcat(8080)关掉后,调用tomcat(8081)显示success2;
当关闭一个memcached服务后,显示正常。
参考
Nginx+Memcached+Tomcat集群配置实践(Sticky Session)
同一台Windows机器中启动多个Memcached服务
MSM–Memcached_Session_Manager介绍及使用
memcached-session-manager配置
Windows下Nginx的安装与配置
版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了Nginx+Memcached+Tomcat集群配置,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。