Nginx+tomcat+memcache for load balancing and session sharing

Source: Internet
Author: User
Tags memcached nginx load balancing

The first part environment introduction

Deployment environment:

Host1:nginx, Memcached, TOMCAT1

Host2:tomcat2

tomcat_version:8.0.38

The second part nginx+tomcat to achieve load balancing

1, Nginx and Tomcat installation omitted

2, Nginx load Balancing configuration:

The tomcat cluster is defined in the HTTP tag:

upstream Tomcat_server {        101.200.229.38:8080;         101.200.162.214:8080;        }

The agent is set up in the server to forward the request to the backend Tomcat cluster processing:

    Server {        listen  ;        server_name     www.xxx.com;         / {            proxy_pass http://tomcat_server;            Proxy_set_header Host $host;            Proxy_set_header X-real-IP $remote _addr;            Proxy_set_header X-forwarded-for$proxy _add_x_forwarded_for;        }    }

There are several ways to do this for nginx load balancing:

2.1 Ip_hash:

Depending on the client address, the same IP address is assigned to the same backend server for processing, which resolves the session problem. However, if the back-end server is down, the session will fail.

Disadvantage: The user in the LAN is using the same public IP, which for Ip_hash, will be considered the same client, so will always be the public IP request to the same back-end server to handle, and therefore does not play a role in load balancing.

2.2 Polling:

Each request, in chronological order, is assigned to a different server on the backend.

Disadvantage: You need to do session sharing to the backend server.

2.3 Weight:

Each request is forwarded according to the weight value of the back-end server. The larger the weight, the higher the server's processing power. It is primarily used in situations where the performance of the backend server is different.

The third part uses the MSM framework to realize session sharing

GitHub Project Address: Https://github.com/magro/memcached-session-manager

1, MSM framework mainly uses the cache (such as memcache, Redis) to store session information, the backend server can read and write the session from the cache.

2. Tomcat serialization (Serializable) Policy introduction:

Serialization refers to the process of transferring, storing, and reading objects as strings on the network.

Java has the following serialization frameworks:

Kryo-serializer

Javolution-serializer

Xstream-serializer

Flexjson-serializer

The dependent jar packages required for each serialization framework are different, and this example uses the Javolution framework

3. Prepare the required jar package:

Download link available on github: https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration

The jar packages and versions required for this example are as follows:

Asm-5.1.jar

Minlog-1.3.0.jar

Javolution-5.5.1.jar

Reflectasm-1.11.3.jar

Msm-javolution-serializer-1.9.5.jar

Memcached-session-manager-1.9.5.jar

Memcached-session-manager-tc8-1.9.5.jar

Note: The version compatibility requirements for each jar are extremely high!

Place the above jar package in the tomcat/lib/directory.

4, install memcached, start memcached:

/usr/local/memcached/bin/memcached-m 512-d-u memcache-p 11011-c 1024

-m indicates allocated memory size

-D indicates background run

-U means running as a user

-P indicates the port number of the listener

-C indicates the number of connections accepted

Tomcat/conf/context.xml configuration:

Add the following to the <Context> tab:

<manager classname="De.javakaffee.web.msm.MemcachedBackupSessionManager"Memcachednodes="n1:127.0.0.1:11011"Sticky="false"Sessionbackupasync="false"Failovernodes=""Sessionbackuptimeout="100000"Lockingmode="uripattern:/path1|/path2"Requesturiignorepattern=".*\. (ICO|PNG|GIF|JPG|CSS|JS) $"Transcoderfactoryclass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"/>

After the above configuration, reboot Tomcat.

Reference: http://blog.csdn.net/remote_roamer/article/details/51133790

Nginx+tomcat+memcache for load balancing and session sharing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.