About Nginx+Tomcat+Memcached load sharing session equilibrium

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
First of all, to ensure that the machine has been installed on nginx, Tomcat, memcached, and can successfully start service

Nginx can start normally, we can 80 port to access nginx through domain + port number.
As a local example
localhost

Nginx can access, we began to configure the nginx configuration file, to do the mapping with Tomcat

location / {
            root   html;
            index  index.html index.htm;
            proxy_pass    http://tomcat_115;
 }


This is the configuration of the nginx default parsing only intercepted a section of code, do not represent the entire meaning
The proxy_pass is configured as agents to



So we need to add in the nginx configuration file
upstream tomcat_115 {
          server 192.168.1.110:8080 weight=1;  
          server 192.168.1.111:8080 weight=2;  
          server 192.168.1.112:8080 weight=3;  
}

Mapping of this code is configured local tomcat, behind the weight configuration of the weight, the higher the weight, the probability is assigned to the higher

The above code is nginx responsible for the distribution of incoming requests
Configured, can through the nginx -t to verify that the configuration file is correct

Configure the memcached
The following is a description of the official installation, see the
https://code.google.com/p/memcached-session-manager/wiki/SetupAndConfiguration
The official document which said, first of all to the memcached jar package download, and added to the Tomcat website, the path to the $CATALINA_HOME/lib/, in the TOMCAT_HOME/lib/ should be
Be careful
The corresponding version of the jar package inside Tomcat jar
In addition
If you use memcached, you also need to download the spymemcached-2.8.12.jar and couchbase-client-1.1.4.jar. the two jar package
If you use couchbase, you also need to download the ***jar package, here but many shows

Then the configuration file to configure the tomcat, can be added to server.xml, can also be added to the context.xml.,
The following code

<Context>
  ...
  <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
    memcachedNodes="n1:host1.yourdomain.com:11211,n2:host2.yourdomain.com:11211"
    requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
    />
</Context>

This is the website for the code, there are two kinds of writing, reference website, in addition, the configuration instructions on the website there are, here only briefly to run through
MemcachedNodes means the node that points to memcached, if there are multiple machines installed memcached, you need to this configuration, the format for the domain + port number, domain and port number it is referring to the installation of the memcached domain and the port, the default port number for memcached is 11211

In front of the N1, and N2, can be understood as the programming language which can be used in the namespace, add other configuration
Official website said, if only a single point, can not write in front of the N1.

Configure these, restart tomcat, reload the nginx, you can try to access nginx port 80, multi access several times, are assigned to the access of Tomcat is random.
You can then go to the Tomcat access log, if each Tomcat has been access to records, successful.

Through the use of JSP code to access the sessionId, make sure that the session is shared, when to refresh the page's sessionId is unchanged

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>sessionID:<%=session.getId() %></h1>  
</body>
</html>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Osmond at January 23, 2014 - 5:02 PM