Ubuntu1404, Tomcat8+nginx+memcache configure server cluster session sharing

Source: Internet
Author: User
Tags nginx reverse proxy
The previously configured Tomcat cluster uses a simple cluster management approach provided by Tomcat: , this cluster session contribution uses the following: Org.apache.catalina.ha.session.DeltaManager, which copies a node's session to all nodes in the cluster, which, according to the Tomcat official documentation, becomes larger in the cluster , the effect is not good. So I tried to use memcached to get the cluster to share the session.

The documentation for the configuration reference is: Click the Open link, and in this link you can find the jar package that matches your tomcat, and the method of the Tomcat configuration. The following records my configuration process.

1. Configure the Nginx reverse proxy according to the contents of the previous blog;

2. Install the memcache under Ubuntu:

Can be installed directly using the following command

sudo apt-get install memcached

3. Edit the/etc/memcached.conf file:

This file has the configuration of the parameters, where the-l parameter is commented out, because by default memcache will listen to all addresses, or change 127.0.0.1 to 0.0.0.0来 Let Memcache listen to the default address.

If you do not do so, you can only use memcached on this machine, the other machine connection does not come in, there is no meaning.

After configuration, you can use the following command to see if you can connect to memcache, where IP is the memcache host installed IP address, 11211 is the memcache listening port.

Telnet IP 11211

4. Edit the Catalina_home/conf/context.xml file on all Tomcat nodes and add the following configuration under the tag.

 
  
Memcachednodes is configured to install the Memcache node, CLOUD2 and Cloud3 is the hostname, must be recognized by the function, N1 and N2 is we take the name of the memcached node, 11211 is the memcache listening port, 11211 is the default port.

5. Add some jar packages to Catalina_home/lib

The packages I use here are:

Memcached-session-manager-1.8.3.jarmemcached-session-manager-tc8-1.8.3.jarspymemcached-2.11.1.jar

6. Then you can start Tomcat and nginx for testing.

Here's how I test the idea of building a servlet with the following code and deploying it to two Tomcat servers. Just start one of the servers and then access the Load Balancer server through a browser, and you can view and note the SessionID at this time in your browser. Then shut down the server, start another Tomcat server, then use the same address to access the Load Balancer server, and then view the SessionID on the browser, if two times SessionID consistent, then SessionID is shared.

Package Com.cyber_space. Servlet;import Java.io.ioexception;import Java.io.printwriter;import Java.net.inet4address;import Javax.servlet.servletexception;import Javax.servlet.annotation.webservlet;import Javax.servlet.http.HttpServlet; Import Javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;/** * servlet Implementation class Testservlet */@WebServlet ("/testservlet") public class Testservlet extends HttpServlet {private Static final Long Serialversionuid = 1l;public Testservlet () {super ();} protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Handle (request, response);} protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Handle (request, response);} private void handle (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Response.setheader ("Content-type", "text/html;charset=utf-8"); String REmoteipstring = request.getremoteaddr () + "" "+ request.getremotehost () +" "+ Request.getremoteuser (); PrintWriter pw = Response.getwriter ();p w.println ("Request from:" + remoteipstring); Request.getsession (). SetAttribute ("5", 5) ; String SessionID = Request.getsession (). GetId ();p w.println ("The SessionID given it is:" + SessionID); Inet4address inet4address = (inet4address) Inet4address.getlocalhost ();p w.println ("The IP address of the server is:" + inet4address.gethostaddress ());}}

There are a lot of things that can be done for the time being, such as efficient serialization configuration, and so on.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the Ubuntu1404, Tomcat8+nginx+memcache configuration server cluster session sharing, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.