In Ubuntu1404, Tomcat 8 + Nginx + memcache configures server cluster session sharing

Source: Internet
Author: User
Tags nginx reverse proxy
: This article mainly introduces Tomcat8 + Nginx + memcache server cluster session sharing in Ubuntu1404. if you are interested in the PHP Tutorial, refer to it. The previously configured Tomcat cluster uses the simple cluster management method provided by Tomcat: The cluster session contributes to org. apache. catalina. ha. session. deltaManager, which copies the session of a node to all nodes in the cluster. according to the official Tomcat documents, this session sharing method does not work well when the cluster grows. Therefore, I tried to use memcached to share the session with the cluster.

The configuration reference documents include: click the open link, find the jar package that matches your Tomcat, and the Tomcat configuration method. Below is a record of my configuration process.

1. configure the Nginx Reverse proxy based on the content of the previous blog;

2. install memcache in ubuntu:

You can directly use the following command to install

sudo apt-get install memcached

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

This file contains the configuration of parameters. here we need to comment out the-l parameter, because memcache listens to all addresses by default, or change 127.0.0.1 to 0.0.0.0 to enable memcache to listen to the default address.

If you do not do this, you can only use memcached on the local machine. Otherwise, it makes no sense.

After configuration, run the following command to check whether memcache can be connected. the ip address is the ip address installed on the memcache host, and 11211 is the listening port of memcache.

telnet ip 11211

4. edit the CATALINA_HOME/conf/context. xml file on all Tomcat nodes. Add the following configuration under the tag.

 
MemcachedNodes is the node where memcache is configured and installed. cloud2 and cloud3 are host names, which must be recognized by the local machine. n1 and n2 are the names of the memcached nodes we have obtained, and 11211 is the port on which memcache is listened, 11211 is the default port.

5. add some jar packages to CATALINA_HOME/lib.

The packages I used 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.

The test logic here is as follows: Use the following code to create a Servlet and deploy it on two tomcat servers. Start only one server and access the server load balancer server through a browser. you can view and write down the SessionID in the browser. Close the server, start another tomcat server, access the server load balancer server with the same address, and view the SessionID in the browser. if the SessionID is consistent twice, the 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, 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 (); pw. println ("request from:" + remoteIpString); request. getSession (). setAttribute ("5", 5); String sessionID = request. getSession (). getId (); pw. println ("The SESSIONID assigned to it is:" + sessionID); Inet4Address inet4Address = (Inet4Address) Inet4Address. getLocalHost (); pw. println ("The server IP address is:" + inet4Address. getHostAddress ());}}

There is still a lot of work that can be done, such as efficient serialization configuration, which will be done later.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

In Ubuntu1404, Tomcat 8 + Nginx + memcache is used to configure server cluster session sharing, which includes some content. if you are interested in the PHP Tutorial, please help.

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.