Nginx+Memcached+Tomcat cluster configuration

WBOY
Release: 2016-08-08 09:21:36
Original
767 people have browsed it

This time we mainly demonstrate the operation on win7 64.

    • web application build
    • Memcached installation configuration startup
    • Tomcat configuration
      • required jar package
        • memcached-session-manager
        • serialization
      • contextxml configuration
    • Nginx installation Configuration startup
    • Test
    • Reference

Web application construction

Simple construction of a web application, I will not describe it here. For details, you can visit my Github.
For the convenience of display, make a difference in success.jsp, add 8080 to one and 8081 to the other.


Memcached installation, configuration, startup

This example uses 2 memcached nodes, both on one machine.

Operation on win7 64. The resource can be downloaded at this link I am link.
After unzipping, enter cmd to enter this directory. Remember to run it as an administrator.

Install

<code>memcached  <span>-d</span> install</code>
Copy after login

Run

<code>memcached <span>-d</span> start</code>
Copy after login

You can also start multiple memcached services, you can refer to this link.

Create

<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>
Copy after login

Pay attention to modify the port, the default is 11211
Can be accessed via Telnet: telnet 127.0.0.1 11211
Telnet interface quit can exit

Commands to start, stop, and uninstall 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>
Copy after login

You can see the memcached service on the task manager


Tomcat configuration

This time I use 2 tomcat7.0.62 examples

Tomcat cluster configuration, each node in the cluster realizes session sharing by sharing the session stored in the cache Memcached: if the Tomcat service on one machine is stopped, the session data on other peer servers can still be cached from Memcached Read in, so that the problem of session loss will not occur.

Use memcached-session-manager, official documentation.

The configuration of tomcat mainly includes two aspects. First, you need to download the required packages and put them in the lib directory under the tomcat installation directory (strictly speaking, it should be $CATALINA_HOME/lib/)
The jar required for the first msm, the jar required for serialization,
The second configuration context.xml

required jar package

memcached-session-manager

maven configuration corresponding to tomcat 7
The following 3 jar packages are required;
memcached-session-manager-version.jar,
memcached-session-manager-tc7-version.jar,
spymemcached-version.jar

can be downloaded from maven below

<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>
Copy after login

serialization

4 kinds of serialization

<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>
Copy after login

The jar packages required for various serializations are as follows:
- 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/The situation of adding jar is as follows

context.xml configuration

That is, modify the content under the Context node in the $CATALINA_HOME/conf/context.xml file , submit the MSM configuration.
It mainly includes using memcached to manage sticky sessions and non-sticky sessions and using membase to manage non-sticky sessions.
The example is based on two memcached instances on one host. The serialization method used in the example is kryo.

This time, sticky sessions are used for management. As for non-sticky ones, you can configure them yourself or add them next time. The configuration is as follows. The two tomcat configurations are the same.

<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>
Copy after login

Nginx installation, configuration, startup

download the stable version from the official website,

decompress it and enter the directory,

<code>start nginx  <span>// 启动</span></code>
Copy after login

In this way, the nginx service will be started. Open the task manager and check the nginx.exe process. Two processes will be displayed and occupy system resources, which is quite small. Then open the browser and enter http://127.0.0.1/ to see the nginx welcome page, which is very friendly.

<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>
Copy after login

By default, Nginx uses the default configuration file nginx.conf, modify it as follows:

<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>
Copy after login

Test

Login page

Success Page
success1

success2

When tomcat (8080) is turned off, call tomcat (8081) to display success2;
When a memcached service is closed, the display is normal.


Reference

Nginx+Memcached+Tomcat cluster configuration practice (Sticky Session)
Start multiple Memcached services on the same Windows machine
MSM–Memcached_Session_Manager introduction and use
memcached-session-manager configuration
Installation and configuration of Nginx under Windows

Copyright statement: This article is an original article by the blogger and may not be reproduced without the permission of the blogger.

The above introduces the Nginx+Memcached+Tomcat cluster configuration, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!