Nginx+tomcat+memcached

Source: Internet
Author: User
Tags memcached serialization

nginx+tomcat+memcached

System Environment : Rhel6 x64 SELinux and iptables disabled

Host role :

Server1:192.168.122.11:nginx

Server2:192.168.122.12:tomcat memcached

Server3:192.168.122.13:tomcat memcached


Tomcat-1 (T1) stores the session on Memcached-2 (T2). T1 stores the session on Memcached-1 only if M2 is not available (M1 is T1 failovernode). The advantage of using this configuration is that when both T1 and M1 crash, the session sessions are not lost and the single point of failure is avoided.

One, Tomcat installation


First configure the Java environment

[[Email protected] ~]# tar zxf jdk-7u79-linux-x64.tar.gz-c/usr/local/-C Specify the decompression path

[Email protected] local]# cd/usr/local/
[[email protected] local]# ln-s Jdk1.7.0_79/java do the connection


[[email protected] local]# vim/etc/profile Modify global variables

Export Java_home=/usr/local/java

Export classpath=.: $JAVA _home/lib: $JAVA _home/jre/lib

Export path= $PATH: $JAVA _home/bin


[[email protected] local]# source/etc/profile make it effective


Testing the Java Environment


[[email protected] ~]# vim Test.java write test script

public class Test {

public static void Main (string[] args)

{

System.out.println ("Hello World");

}

}


[[email protected] ~]# javac Test.java Compilation
[[Email protected] ~]# java test
Hello World
At this point, theserver2 on The Java Environment was built , server3 the same


Deploying Tomcat

[Email protected] ~]# tar zxf apache-tomcat-7.0.37.tar.gz-c/usr/local/
[Email protected] local]# cd/usr/local/

[Email protected] local]# ln-s Apache-tomcat-7.0.37/tomcat
[Email protected] tomcat]# Cd/usr/local/tomcat
[[email protected] tomcat]# bin/startup.sh open tomcat, port defaults to 8080

[Email protected] tomcat]# NETSTAT-ANTLP




[Email protected] tomcat]# CD webapps/root/
[Email protected] root]# vim test.jsp
[email protected] root]# cat test.jsp
The time is: <%=new java.util.Date ()%>

Server3 Ibid.


Second, install Nginx

[Email protected] ~]# tar zxf nginx-1.10.1.tar.gz
[Email protected] ~]# tar zxf nginx-sticky-module-ng.tar.gz

Note: Nginx-sticky-module for nginx third-party module, so that nginx support sticky mode, so-called sticky mode refers to the same user's access request is sent to the same Tomcat instance processing.

[Email protected] ~]# CD nginx-1.10.1
[Email protected] nginx-1.10.1]# vim AUTO/CC/GCC

#CFLAGS = "$CFLAGS-G" commented out

[[email protected] nginx-1.10.1]# Yum install gcc pcre-devel openssl-devel-y installation dependent

[Email protected] nginx-1.10.1]#/configure--prefix=/usr/local/lnmp/nginx--with-http_ssl_module--with-http_stub       _status_module--with-file-aio--with-threads--user=nginx--group=nginx--add-module=/root/nginx-sticky-module-ng Install related modules

[[email protected] nginx-1.10.1]# make && make install build installation

[Email protected] ~]# ln-s/usr/local/lnmp/nginx/sbin/nginx/usr/sbin/
[[email protected] ~]# useradd-s/sbin/nologin nginx Create Nginx User

[Email protected] sbin]# Cd/usr/sbin

[[email protected] sbin]# Nginx open Nginx



deploying Tomcat on Nginx Load Balancing

Modify Location File

[Email protected] conf]# pwd
/usr/local/lnmp/nginx/conf

[Email protected] conf]# vim nginx.conf

+ HTTP {
Upstream Tomcat {#负载均衡模块
Sticky;
Server 192.168.122.12:8080; #后端tomcat
Server 192.168.122.13:8080;
22}
23
Include Mime.types;
Default_type Application/octet-stream;

From location ~ \.jsp$ {#所有jsp页面交给tomcat处理, static and dynamic separation
Proxy_pass Http://tomcat;
70}


[[email protected] logs]# nginx-t can be used to detect errors in syntax

[Email protected] logs]# Nginx-s Reload
Nginx: [ERROR] Invalid PID number "in"/usr/local/lnmp/nginx/logs/nginx.pid "


Error, the Internet to find solutions, as follows:
[Email protected] logs]# nginx-c/usr/local/lnmp/nginx/conf/nginx.conf
[[email protected] logs]# nginx-s reload okay, no error.


The test surface on the Server2,server3 is modified slightly, the experimental effect is obvious

Test:


Configure session

server2,3 on


There are 4 official recommendations for the session's serialization scheme:
1. Java serialization
2. Msm-kryo-serializer
3. Msm-javolution-serializer
4. Msm-xstream-serializer
One of the best-performing serialization schemes is Kryo, which we use kryo approach.
Place the following packages in the/usr/local/tomcat/lib directory
Kryo-1.03.jar
Kryo-serializers-0.8.jar
Memcached-2.5.jar
Memcached-session-manager-1.5.1.jarmemcached-session-manager-tc7-1.5.1.jar
Minlog-1.2.jar
Msm-kryo-serializer-1.5.1.jar
Reflectasm-0.9.jar

[Email protected] memsoft]# MV */usr/local/tomcat/lib/

[Email protected] conf]# pwd
/usr/local/tomcat/conf


[Email protected] conf]# vim context.xml

<?xml version= ' 1.0 ' encoding= ' utf-8 '?>
<Context>

<WatchedResource>WEB-INF/web.xml</WatchedResource>
<valve classname= "Org.apache.catalina.valves.CometConnectionManagerValve"/>

<manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager"
Memcachednodes= "n1:192.168.122.12:11211,n2:192.168.122.13:11211"

Failovernodes= "N1" on Node2 (SERVER3) This item is set to "N2 (Find Yourself bad)

Requesturiignorepattern= ". *\. (ICO|PNG|GIF|JPG|CSS|JS) $ "
Transcoderfactoryclass= "De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
</Context>


[[email protected] conf]# Yum install-y memcached Installation
[[email protected] conf]#/etc/init.d/memcached start
[Email protected] conf]# vim/usr/local/tomcat/webapps/root/test.jsp

<%@ page contenttype= "text/html; CHARSET=GBK "%>
<%@ page import= "java.util.*"%>
<body>
Server Info:
<%
Out.println (REQUEST.GETLOCALADDR () + ":" + request.getlocalport () + "<br>");%>
<%
Out.println ("<br> ID" + session.getid () + "<br>");
String dataname = Request.getparameter ("Dataname");
if (dataname! = null && dataname.length () > 0) {
String DataValue = Request.getparameter ("DataValue");
Session.setattribute (Dataname, DataValue);
}
Out.print ("<b>session list</b>");
Enumeration E = Session.getattributenames ();
while (E.hasmoreelements ()) {
String name = (string) e.nextelement ();
String value = Session.getattribute (name). toString ();
OUT.PRINTLN (name + "=" + value+ "<br>");
SYSTEM.OUT.PRINTLN (name + "=" + value);
}
%>

Server3 same configuration


Third, testing


Access/ http192.168.122.11/test.jsp,different host accesses are dispatched to differentTomcatprocessing requests from the same host on the instance will be given to the sameTomcatinstance Processing,at this time you Downthe current response is droppedTomcatExample, Nginxautomatically dispatches the user's request to anotherTomcatinstance on,alsoSessiondidn't throw it away.

Note: You must pay attention to the boot order before testing Tomcat>memcached>nginx


When the browser accesses:

http://192.168.122.11/test.jsp

At this point, you drop the tomcat instance that is currently responding

[Email protected] ~]#/usr/local/tomcat/bin/shutdown.sh

nginx  The user's request is automatically dispatched to another  , instance ,   at the same time; SESSION&NBSP,

You can also view the session information in the terminal by viewing the log

[[Email protected] ~] #tail-F/usr/local/tomcat/log/catalina.out




Nginx+tomcat+memcached

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.