本次主要是在win7 64 上示範操作。
本次範例使用了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服務Tomcat2個配置
組設定。
Tomcat 叢集配置,叢集中各個結點透過共享儲存在快取Memcached中session來實現session的共享:如果有一台機器上的Tomcat服務停掉了,對於其他對等伺服器上的session資料仍然可以從Memcached緩存中讀取,從而不會發生session丟失的問題。
使用memcached-session-manager,官方文件。
第二個配置context.xml
所需jar包
memcached-session-manager
tomcat 7對應的maven配置
需要如下3個jar包;
memcached-session-manager-version.jar,
memcached-session-manager-tc7-version.jar,
spymemcached-version.jar
可以在下面maven中下載
<code><span><<span>dependency</span>></span><span><<span>groupId</span>></span>de.javakaffee.msm<span></<span>groupId</span>></span><span><<span>artifactId</span>></span>memcached-session-manager-tc7<span></<span>artifactId</span>></span><span><<span>version</span>></span>1.8.3<span></<span>version</span>></span><span></<span>dependency</span>></span></code>
4種序列化
<code>kryo-serializer: <span><<span>dependency</span>></span><span><<span>groupId</span>></span>de.javakaffee.msm<span></<span>groupId</span>></span><span><<span>artifactId</span>></span>msm-kryo-serializer<span></<span>artifactId</span>></span><span><<span>version</span>></span>version<span></<span>version</span>></span><span></<span>dependency</span>></span>javolution: <span><<span>dependency</span>></span><span><<span>groupId</span>></span>de.javakaffee.msm<span></<span>groupId</span>></span><span><<span>artifactId</span>></span>msm-javolution-serializer<span></<span>artifactId</span>></span><span><<span>version</span>></span>version<span></<span>version</span>></span><span></<span>dependency</span>></span>xstream: <span><<span>dependency</span>></span><span><<span>groupId</span>></span>de.javakaffee.msm<span></<span>groupId</span>></span><span><<span>artifactId</span>></span>msm-xstream-serializer<span></<span>artifactId</span>></span><span><<span>version</span>></span>version<span></<span>version</span>></span><span></<span>dependency</span>></span>flexjson: <span><<span>dependency</span>></span><span><<span>groupId</span>></span>de.javakaffee.msm<span></<span>groupId</span>></span><span><<span>artifactId</span>></span>msm-flexjson-serializer<span></<span>artifactId</span>></span><span><<span>version</span>></span>version<span></<span>version</span>></span><span></<span>dependency</span>></span></code>
- flexjson-serializer: msm-flexjson-serializer, flexjson
context.xml配置
text
_HO$CATApATA22S2/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"</span> memcachedNodes=<span>"n1:localhost:11211,n2:localhost:11311"</span> sticky=<span>"false"</span> sessi>"false"</span> lockingMode=<span>"auto"</span> requestUriIgnorePattern=<span>".*\.(ico|png|gif|jpg|css|js)$"</span>transcoderFactoryClass=<span>"de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"</span>/> </Context> </code>Nginx安裝、設定、啟動
官網下載穩定版本,
解壓縮後進入目錄後,
<code>start nginx <span>// 启动</span></code>
<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介紹及使用
Windows下Nginx的安裝與設定
版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。
以上就介紹了Nginx+Memcached+Tomcat集群配置,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。