Nginx+Memcached+Tomcat叢集配置

WBOY
發布: 2016-08-08 09:21:36
原創
801 人瀏覽過

本次主要是在win7 64 上示範操作。

    • web應用建置
    • Memcached安裝設定啟動
    • Tomcat配置
      • .
        • Nginx安裝配置啟動
        測試
      • 參考
    • 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服務



Tomcat2個配置

組設定。

Tomcat 叢集配置,叢集中各個結點透過共享儲存在快取Memcached中session來實現session的共享:如果有一台機器上的Tomcat服務停掉了,對於其他對等伺服器上的session資料仍然可以從Memcached緩存中讀取,從而不會發生session丟失的問題。


使用memcached-session-manager,官方文件。

tomcat的配置主要包括兩個方面,首先需要下載所需的包,放到tomcat安裝目錄下的lib目錄下(嚴格來說應該是$CATALINA_HOME/lib/)
第一個msm所需的jar,序列化所需的jar,

第二個配置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>
登入後複製

各種序列化所需的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配置
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>
登入後複製

這樣,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教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板