Table of Contents
memcached-session-manager
serialization
Home Backend Development PHP Tutorial Nginx+Memcached+Tomcat cluster configuration

Nginx+Memcached+Tomcat cluster configuration

Aug 08, 2016 am 09:21 AM
memcached server session

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.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve session failure How to solve session failure Oct 18, 2023 pm 05:19 PM

Session failure is usually caused by the session lifetime expiration or server shutdown. The solutions: 1. Extend the lifetime of the session; 2. Use persistent storage; 3. Use cookies; 4. Update the session asynchronously; 5. Use session management middleware.

Solution to PHP Session cross-domain problem Solution to PHP Session cross-domain problem Oct 12, 2023 pm 03:00 PM

Solution to the cross-domain problem of PHPSession In the development of front-end and back-end separation, cross-domain requests have become the norm. When dealing with cross-domain issues, we usually involve the use and management of sessions. However, due to browser origin policy restrictions, sessions cannot be shared by default across domains. In order to solve this problem, we need to use some techniques and methods to achieve cross-domain sharing of sessions. 1. The most common use of cookies to share sessions across domains

How to install, uninstall, and reset Windows server backup How to install, uninstall, and reset Windows server backup Mar 06, 2024 am 10:37 AM

WindowsServerBackup is a function that comes with the WindowsServer operating system, designed to help users protect important data and system configurations, and provide complete backup and recovery solutions for small, medium and enterprise-level enterprises. Only users running Server2022 and higher can use this feature. In this article, we will explain how to install, uninstall or reset WindowsServerBackup. How to Reset Windows Server Backup If you are experiencing problems with your server backup, the backup is taking too long, or you are unable to access stored files, then you may consider resetting your Windows Server backup settings. To reset Windows

How to implement SMS login in Redis shared session application How to implement SMS login in Redis shared session application Jun 03, 2023 pm 03:11 PM

1. Implementing SMS login based on session 1.1 SMS login flow chart 1.2 Implementing sending SMS verification code Front-end request description: Description of request method POST request path /user/code request parameter phone (phone number) return value No back-end interface implementation: @Slf4j@ ServicepublicclassUserServiceImplextendsServiceImplimplementsIUserService{@OverridepublicResultsendCode(Stringphone,HttpSessionsession){//1. Verify mobile phone number if

What are the differences between JavaScript and PHP cookies? What are the differences between JavaScript and PHP cookies? Sep 02, 2023 pm 12:29 PM

JavaScriptCookies Using JavaScript cookies is the most effective way to remember and track preferences, purchases, commissions and other information. Information needed for a better visitor experience or website statistics. PHPCookieCookies are text files that are stored on client computers and retained for tracking purposes. PHP transparently supports HTTP cookies. How do JavaScript cookies work? Your server sends some data to your visitor's browser in the form of a cookie. Browsers can accept cookies. If present, it will be stored on the visitor's hard drive as a plain text record. Now, when a visitor reaches another page on the site

How to solve the problem that the Springboot2 session timeout setting is invalid How to solve the problem that the Springboot2 session timeout setting is invalid May 22, 2023 pm 01:49 PM

Problem: Today, we encountered a setting timeout problem in our project, and changes to SpringBoot2’s application.properties never took effect. Solution: The server.* properties are used to control the embedded container used by SpringBoot. SpringBoot will create an instance of the servlet container using one of the ServletWebServerFactory instances. These classes use server.* properties to configure the controlled servlet container (tomcat, jetty, etc.). When the application is deployed as a war file to a Tomcat instance, the server.* properties do not apply. They do not apply,

Windows Server 2025 preview version welcomes update, Microsoft improves Insiders testing experience Windows Server 2025 preview version welcomes update, Microsoft improves Insiders testing experience Feb 19, 2024 pm 02:36 PM

On the occasion of releasing the build 26040 version of Windows Server, Microsoft announced the official name of the product: Windows Server 2025. Also launched is the Windows11WindowsInsiderCanaryChannel version build26040. Some friends may still remember that many years ago someone successfully converted Windows NT from workstation mode to server mode, showing the commonalities between various versions of Microsoft operating systems. Although there are clear differences between Microsoft's current version of the server operating system and Windows 11, those who pay attention to the details may be curious: why Windows Server updated the brand,

How to optimize PHP application CPU usage using Memcached caching technology? How to optimize PHP application CPU usage using Memcached caching technology? Jun 21, 2023 pm 05:07 PM

With the development of the Internet, PHP applications have become more and more common in the field of Internet applications. However, high concurrent access by PHP applications can lead to high CPU usage on the server, thus affecting the performance of the application. In order to optimize the performance of PHP applications, Memcached caching technology has become a good choice. This article will introduce how to use Memcached caching technology to optimize the CPU usage of PHP applications. Introduction to Memcached caching technology Memcached is a

See all articles