Tomcat + Nginx + Memcached integrated case

Tomcat + Nginx + Memcached integrated case

Explanation

By Nginxparsing static pages and dynamic load balancing scheduling to the back of multiple Tomcat, Tomcatanalytical javadynamic program.

Since http is a stateless protocol, you visit a page A, then visit B, http unable to determine these two visits from a person, so use to track user session cookie or to display different pages based on user identity and authorization . A user such as landing, then be able to see their personal information, and B have not logged in, you can not see your personal information. A possible also in shopping, the items in their shopping cart, then B also have this process, you can not determine the identity and shopping information A, B, and so need a session ID to maintain this process. So we used the session management.

Document official website

Environmental Planning

Host computer hostname surroundings
192.168.1.31 nginx.cluster.com nginx (yum installed)
192.168.1.32 tomcat1.cluster.com tomcat-9.0
192.168.1.33 tomcat2.cluster.com tomcat-9.0
192.168.1.34 memcached.cluster.com memcached (yum installed)

Turn off the firewall, selinux; time synchronization; host binding basic configuration (step omitted)

Specific steps

In this case used tomcat, jdkand tomcat-sessionthe associated jar package Download
link: https://pan.baidu.com/s/1ESm_RSrFx77kWObmCt1DQw
extraction code: f64n

memcached deployment

Description: memcached no need to configure here too, can be installed, and then check whether the port is in listening in. tomcat server can successfully connect to

[root@memcached ~]# yum -y install memcached
[root@memcached ~]# systemctl enable memcached
[root@memcached ~]# systemctl start memcached

[root@memcached ~]# lsof -i:11211
COMMAND    PID      USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
memcached 4419 memcached   26u  IPv4  51021      0t0  TCP *:memcache (LISTEN)
memcached 4419 memcached   27u  IPv6  51022      0t0  TCP *:memcache (LISTEN)
memcached 4419 memcached   28u  IPv4  51025      0t0  UDP *:memcache 
memcached 4419 memcached   29u  IPv6  51026      0t0  UDP *:memcache

nginx deployment

1) install nginx

[root@nginx ~]# yum -y install nginx
[root@nginx ~]# nginx -v
nginx version: nginx/1.12.2

2) Profile Configuration

# Create a virtual host 
[Nginx the root @ ~] # Vim /etc/nginx/conf.d/ www.conf 
upstream Tomcat { 
    Server 192.168 . 1.32 : 8080 weight = . 1 ; 
    Server 192.168 . 1.33 : 8080 weight = . 1 ; 
} 

Server { 
    the listen        88 default_server; 
    server_name localhost; 
    root          / opt / Project; 

# dynamic program has been scheduled to the end of the jsp tomcat to handle 
    LOCATION ~ *. \ .jsp $ { 
        proxy_pass HTTP: // tomcat;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}


# 创建测试文件
[root@nginx ~]# mkdir /opt/project
[root@nginx ~]# echo "<h1>Nginx IP:192.168.1.31</h1>" >> /opt/project/index.html

tomcat deployment

Detailed installation Reference: https://www.cnblogs.com/yanjieli/p/11092350.html
two tomcat servers must perform all the following operations can be performed in a top, and then copy the past.
1) package upload to the server, write a script to install temporary use

[root@tomcat1 ~]# cat install_tomcat.sh 
#!/bin/bash

#----安装java环境
function InstallJava (){
    tar xf jdk-8u211-linux-x64.tar.gz -C /usr/local/
    ln -s /usr/local/jdk1.8.0_211 /usr/local/java
    sed -i.ori '$a export JAVA_HOME=/usr/local/java \nexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH \nexport CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar' /etc/profile
    source /etc/profile
    java -version
}

#----安装tomcat环境
function InstallTomcat (){
    tar xf apache-tomcat-9.0.21.tar.gz -C /usr/local/
    ln -s /usr/local/apache-tomcat-9.0.21 /usr/local/tomcat
    echo "export TOMCAT_HOME=/usr/local/tomcat" >> /etc/profile
    source /etc/profile
}
InstallJava
InstallTomcat

/usr/local/tomcat/bin/startup.sh

2) execute the script

[root@tomcat1 ~]# bash install_tomcat.sh 
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.

[root@tomcat1 ~]# ss -nltp |grep :80
LISTEN     0      100         :::8080                    :::*                   users:(("java",pid=2993,fd=54))
LISTEN     0      1         ::ffff:127.0.0.1:8005                    :::*                   users:(("java",pid=2993,fd=74))
LISTEN     0      100         :::8009                    :::*                   users:(("java",pid=2993,fd=59))

3) Download memcached-session-managerother relevant software packages and copythe tomcatinstallation directory libdirectory

[root@tomcat1 ~]# mkdir tools && cd tools

[root@tomcat1 ~]# wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/2.3.0/memcached-session-manager-2.3.0.jar
[root@tomcat1 ~]# wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc9/2.3.0/memcached-session-manager-tc9-2.3.0.jar
[root@tomcat1 ~]# wget http://repo1.maven.org/maven2/de/javakaffee/msm/msm-kryo-serializer/2.3.0/msm-kryo-serializer-2.3.0.jar
[root@tomcat1 ~]# wget http://repo1.maven.org/maven2/net/spy/spymemcached/2.12.2/spymemcached-2.12.2.jar
[root@tomcat1 ~]# wget https://repo1.maven.org/maven2/de/javakaffee/kryo-serializers/0.42/kryo-serializers-0.42.jar
[root@tomcat1 ~]# wget https://repo1.maven.org/maven2/com/esotericsoftware/reflectasm/1.11.0/reflectasm-1.11.0.jar
[root@tomcat1 ~]# wget https://repo1.maven.org/maven2/com/esotericsoftware/minlog/1.3.0/minlog-1.3.0.jar
[root@tomcat1 ~]# wget https://repo1.maven.org/maven2/com/esotericsoftware/kryo/4.0.0/kryo-4.0.0.jar
[root@tomcat1 ~]# wget https://repo1.maven.org/maven2/org/ow2/asm/asm/7.0/asm-7.0.jar
[root@tomcat1 ~]# wget http://repo1.maven.org/maven2/org/objenesis/objenesis/3.0.1/objenesis-3.0.1.jar

[root@tomcat1 tools]# cp ./* /usr/local/tomcat/lib/

4) edit the configuration file, add connectionsmemcached

No- # Stick mode 
[tomcat1 the root @ ~] # Vim / usr / local / Tomcat / the conf / context.xml 
# in <Context> and </ Context> inside with the following paragraph
 <-! Ip server here is memcached the IP, if you have multiple memcached servers, separated by commas -> 
    <Manager className = " de.javakaffee.web.msm.MemcachedBackupSessionManager " 
        memcachedNodes = " N1: 192.168.1.34: 11211 "  
        lockingMode = " Auto " 
        Sticky = " false " 
        requestUriIgnorePattern = " * \.. (PNG | GIF | JPG | CSS | JS) $ "  
        sessionBackupAsync= "false"  
        sessionBackupTimeout= "100"  
        copyCollectionsForSerialization="true"  
        transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />

5) Prepare the test file

[root@tomcat1 ~]# rm -rf /usr/local/tomcat/webapps/*
[root@tomcat1 ~]# mkdir /usr/local/tomcat/webapps/ROOT
[root@tomcat1 ~]# vim /usr/local/tomcat/webapps/ROOT/index.jsp
SessionID:<%=session.getId()%> <BR>
SessionIP:<%=request.getServerName()%> <BR>
SessionPort:<%=request.getServerPort()%>

6) Restarttomcat

[root@tomcat1 ~]# /usr/local/tomcat/bin/shutdown.sh
[root@tomcat1 ~]# /usr/local/tomcat/bin/startup.sh
[root@tomcat1 ~]# lsof -i:8080
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
java    4672 root   63u  IPv6  52084      0t0  TCP *:webcache (LISTEN)

7) Test

Access tomcat1:

Access tomcat2:

Nginx access the default page:

Jsp dynamic program visit:

As can be seen from the above visit, nginx handle itself when accessing static pages, when accessing dynamic will be dispatched to jsp tomcat to deal with. And when a browser visits, will have access to this one, so, this session reached the session remains.

 

supplement

memcached-session-manager Parameters:

Required memcachedNodes, memcached node information, memcached plurality of nodes, intermediate spaces are required 

failoverNodes = " N2 "   represents the current session held on memcached node n1 
failoverNodes option can not be used in non - Sticky Sessions mode. Node failover configuration, multiple use spaces or commas to separate, a node configured as a backup node, 
when other nodes are not available will be stored in a backup node, the official recommended configuration node and same tomcat server. 
The following reasons: 
if there are two servers m1, m2, where m1 tomcat and deploy memcached node n1, m2 deploy memcached node n2. 
If you configure the tomcat failoverNodes value n2 or not configured, when the server hang m1 n1 and save the tomcat session will be lost, and n2 not save or save only a part of the session, 
which resulted in the loss of some users state. 
If you configure the tomcat failoverNodes value n1, then hang up because when n2 m1 in stores all session, so when users restart tomcat state is not lost. 
Why n2 in stores all session ? Because the value of failoverNodes configuration is n1, n2 node only when the session will not be available when stored to n1, so this time n1 is not stored in any session.
lockingMode optional and defaults none, only non - Sticky effective.
requestUriIgnorePattern optional values, ignored those requests session to develop operations, the general development of static resources such as css, js a class. 
sessionBackupAsync optional and defaults true, whether stored in an asynchronous manner to memcached. 
sessionBackupTimeout options, default timeout 100 ms, asynchronous store the session.

Download the relevant package jar package:

memcached-session-manager Download: HTTP: // repo1.maven.org/maven2/de/javakaffee/msm/ 
HTTP: // repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/ 2.3.0 / memcached-Manager-2.3.0.jar the session- 
HTTP: // repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc9/2.3.0/memcached-session-manager -tc9-2.3.0.jar 

MSM -kryo-the Serializer Download: HTTP: // repo1.maven.org/maven2/de/javakaffee/msm/msm-kryo-serializer/ 
HTTP: // repo1.maven.org/ Maven2 / de / javakaffee / MSM / MSM-Kryo-the Serializer / 2.3.0 / MSM-Kryo-the Serializer-2.3.0.jar 

Spymemcached Download: HTTP: // repo1.maven.org/maven2/net/spy/spymemcached / 
HTTP:// repo1.maven.org/maven2/net/spy/spymemcached/2.12.2/spymemcached-2.12.2.jar 

serializers Download: HTTPS: // repo1.maven.org/maven2/de/javakaffee/kryo-serializers / 
HTTPS: // repo1.maven.org/maven2/de/javakaffee/kryo-serializers/0.42/kryo-serializers-0.42.jar 

reflectasm Download: HTTPS: // repo1.maven.org/maven2/com/esotericsoftware/ reflectasm 
HTTPS: // repo1.maven.org/maven2/com/esotericsoftware/reflectasm/1.11.0/reflectasm-1.11.0.jar 

minlog Download: HTTPS: // repo1.maven.org/maven2/com/esotericsoftware/ minlog 
HTTPS: // repo1.maven.org/maven2/com/esotericsoftware/minlog/1.3.0/minlog-1.3.0.jar

kryo Download: HTTPS: // repo1.maven.org/maven2/com/esotericsoftware/kryo 
HTTPS: // repo1.maven.org/maven2/com/esotericsoftware/kryo/4.0.0/kryo-4.0.0.jar 

asm Download: HTTPS: // repo1.maven.org/maven2/org/ow2/asm/asm/ 
HTTPS: // repo1.maven.org/maven2/org/ow2/asm/asm/7.0/asm-7.0. JAR      

objenesis Download: HTTP: // repo1.maven.org/maven2/org/objenesis/objenesis/ 
HTTP: // repo1.maven.org/maven2/org/objenesis/objenesis/3.0.1/objenesis-3.0.1 .jar

 

Guess you like

Origin www.cnblogs.com/yanjieli/p/11100951.html