NGINX+TOMCAT7 (8) cluster, load balancer and session sharing

Source: Internet
Author: User
Tags manual session id zip nginx server root access tomcat tomcat server nginx load balancing

Reference Document: Install TOMCAT7 on CENTOS7
Reference Document: NGINX+TOMCAT7 's cluster 1. Environment Preparation and configuration

Server IP Operating System Notes
tomcat-001 192.168.1.215 Centos7
tomcat-001 192.168.1.216 Centos7
nginx-001 192.168.1.217 Centos7
nginx-002 192.168.1.218 Centos7

CentOS7 under the keepalived configuration Nginx master-slave mode of dual-machine hot standby configuration, see this blog 2. Install the CENTOS7 install the operating system installation NTP 3. Install TOMCAT7 via yum installation tomcat7 If you are installing TOMCAT8 you can use this document method to install the Centos7 installation Tomcat8 method 4. Install Nginx via Yum 5. Install the Tomcat test program (Nginx Dynamic separation and tomcat cluster) add a Clustertest.war file into the/usr/share/tomcat/webapps directory.

index.jsp files are as follows

<%@ page language= "java"%>

Configuring Tomcat's Server.xml (Vim/usr/share/tomcat/conf/server.xml),
Add the following code, located in the host section

<context path= "" reloadable= "true" docbase= "/usr/share/tomcat/webapps/clustertest"/>

The

makes Tomcat root access to the Clustertest directory
http://192.168.1.215:8080/
http://192.168.1.216:8080/-6. Configure Nginx to enable Tomcat load Balancing vim/etc/nginx/nginx.conf

#修改events的块内容 Events {use Epoll;
Worker_connections 2048;
    } #在http块里面增加如下内容 #开启zip网页压缩 gzip on;
    Gzip_min_length 1k;
    Gzip_buffers 4 8k;
    Gzip_http_version 1.1;

    Gzip_types text/plain application/x-javascript text/css application/xml; #反向代理 # # Meerkat_web upstream meerkat_web {#ip_hash; #每个请求按访问ip的hash结果分配 so that each visitor has fixed access to a back-end server that resolves Sessio
            The problem of N server 192.168.1.215:8080 weight=1 max_fails=2 fail_timeout=30s;
    Server 192.168.1.216:8080 weight=2 max_fails=2 fail_timeout=30s;
                The location section inside the server block is replaced with the following content location/{root HTML;
                Index index.html index.htm index.jsp;
                Proxy_pass Http://meerkat_web;
                Proxy_set_header Host $http _host;
                Proxy_set_header Cookie $http _cookie;
                Proxy_set_header X-real-ip $remote _addr;
               Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for; Proxy_set_header X-forwarded-proto $scheme;
            Client_max_body_size 100m;    }

Reference Document: Nginx Load balancer Several usages of the session sticky post based on Ip_hash -7. Test A. Test auto-Switch
To access the Nginx server represented by http://192.168.1.217/, the effect of index.jsp display will appear. This time, if the corresponding Tomcat service is turned off, it will automatically switch to another stage. B. Test Load Balancing
To access the Nginx server represented by http://192.168.1.217/, the effect of index.jsp display will appear. However, after several refreshes, the same Tomcat service is always displayed. Indicates that load balancing is not implemented. The solution is to upstream inside the Ip_hash this to shield off. It will automatically switch. -8. Several ways to realize session sharing using the cluster method of Tomcat, it is easy to configure the session information automatically in real time between Tomcat. However, this scheme is inefficient and does not perform well in large concurrency. Using Nginx's IP-based hash routing strategy, it is easier to ensure that IP access is always routed to the same tomcat. But if the application is a large number of users of a local area network logged in at the same time, so load balancing does not work. Using memcached to centralize the session of multiple tomcat, the front-end is separated by the Nginx load balancing and dynamic static resources, which can guarantee the high performance while the system level is extended.

We use the third method here -9. Install memcached A. Yum installation memcached on 2 tomcat nodes

Yum-y install memcached
vim/etc/sysconfig/memcached


//reboot, start, boot, status, close
systemctl restart memcached
systemctl start memcached
systemctl enable memcached
systemctl status memcached
systemctl stop memcached


memcached-tool  127.0.0.1:11211 Stats
-10. Install and use Memcached-session-manager on 2 tomcat nodes

Website:
Https://github.com/magro/memcached-session-manager
Installation Manual:
Https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration#add-memcached-session-manager-jars-to-tomcat
MSM supports two modes: Sticky sessions (sticky session) and Non-sticky sessions (non-sticky session). I used a non-sticky session.
Download multiple jar files into the $CATALINA _home\lib directory as per the operating manual:
Due to version issues
This organizes all the required jar files for Memcashed-session-manager that can be used with Tomcat7.0.54, Tomcat 8.0.33, and Tomcat 8.0.36.

Download the jar package for TOMCAT7
Download the available Jar packages for Tomcat 8.0.33 (8.0.36 unavailable)

#tomcat 8.0.36 test down with these jar files Cd/usr/local/src #下载 memcached-session-manager jars to Tomcat wget Http://repo1.maven.org/ma Ven2/de/javakaffee/msm/memcached-session-manager/1.9.5/memcached-session-manager-1.9.5.jar wget/HTTP repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc8/1.9.5/ Memcached-session-manager-tc8-1.9.5.jar # The jar file to use when using memcached wget http://repo1.maven.org/maven2/net/spy/ Spymemcached/2.11.1/spymemcached-2.11.1.jar # The jar file used when using Javolution-serializer to download wget http://repo1.maven.org/ Maven2/de/javakaffee/msm/msm-javolution-serializer/1.9.5/msm-javolution-serializer-1.9.5.jar wget/HTTP Www.java2s.com/Code/JarDownload/javolution/javolution-5.5.1.jar.zip Unzip Javolution-5.5.1.jar.zip # Here is the jar file used when using Kryo-serializer to download wget http://repo1.maven.org/maven2/de/javakaffee/msm/msm-kryo-serializer/1.9.5 /msm-kryo-serializer-1.9.5.jar wget http://repo1.maven.org/maven2/de/javakaffee/kryo-serializers/0.9/ Kryo-serializers-0.9.jar # (compatible Kryo can only be used with 3.+ version.) ) WGet Http://repo1.maven.org/maven2/com/esotericsoftware/kryo/3.0.3/kryo-3.0.3.jar wget http://repo1.maven.org/ Maven2/com/esotericsoftware/minlog/1.3.0/minlog-1.3.0.jar wget http://repo1.maven.org/maven2/com/ Esotericsoftware/reflectasm/1.11.3/reflectasm-1.11.3.jar wget HTTP://REPO1.MAVEN.ORG/MAVEN2/ORG/OW2/ASM/ASM/5.1/ Asm-5.1.jar MV *.jar $CATALINA _home/lib/
-11. Configuring Memcached-session-manager in Tomcat8.0.36Modifying Tomcat's context.xml adds the following:
<Context> ...
        <manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager"
                sticky= "false"
                memcachednodes= "n1:192.168.1.215:11211 n2:192.168.1.216:11211"
                failovernodes= ""
                Requesturiignorepattern =".*\. (Png|gif|jpg|css|js|ico) $ "
                sessionbackupasync=" false "
                sessionbackuptimeout="
                #使用kryo组件做序列化 # transcoderfactoryclass= "De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" 
                #使用javo做序列化
transcoderfactoryclass= "De.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
        />
</Context>

The N1 in the Memcachednodes node here is the alias of the node, which can be used arbitrarily. The subsequent IP must be the IP address of the host where the memcached resides, and the port must be a previously configured port of 11211.
The code snippet found on the Internet other people said can also be put under the server.xml, here I did not try.
At this point, all of the Tomcat sub-nodes are added to the above code, each tomcat inside the content is the same above, no need to make any changes.

Sticky= "false" #非黏性设置, the default is sticky

Memcachednodes #配置memcached节点

Failovernodes #默认为空, if the memcached redundant node is specified here, sticky must be true, otherwise it will error

Requesturiignorepattern #忽略session的正则表达式

Sessionbackupasync #指定Session是否应该被异步保存到Memcached中. If set to True,backupthreadcount setting works, if set to false, by sessionbackuptimeout

Sessionbackuptimeout #设置备份一个Session所用的时间, save fails if the operation exceeds the time. This property only works on Sessionbackupasync= "false". Default 100 milliseconds

Transcoderfactoryclass #默认为 De.javakaffee.web.msm.JavaSerializationTranscoderFactory This property value is the factory class name of the encoding converter that creates serialization and deserialization of the session saved to memcached. The specified class must implement the De.javakaffee.web.msm.TranscoderFactory and provide an argument-free construction method.

Customconverter #可选项, custom converters allow you to provide custom serialization of application-specific types. Multiple custom converter class names are separated by commas (optional spaces after commas). The converter class must be available in the classpath in the WEB application (web-inf/lib), where it is not set.

So far, the session sharing about Memcache has been fully configured.
The specific boot order, first to start the memcached service, then the Nginx service, and finally start the tomcat where each child node service. -12. Test session replication, whether the sharing function succeeded in adding sessiontest.jsp to the/usr/share/tomcat/webapps/clustertest directory

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> <%@ page import=" java.util.* "%> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > Access Nginx Server http://192.168.1.217/sessiontest.jsp View results



Compare the red-framed portions of the above 2 graphs. You can find a switch between 2 tomcat servers, but the SessionID is unchanged and the contents of the session are unchanged. Indicates that session sharing was successful.

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.