Tomcat+nginx+redis Cluster test

Source: Internet
Author: User
Tags redis cluster install redis nginx load balancing

Nginx Load Balancing + Tomcat + Session storage Redis Configuration essentials

Using Nginx as a load balancer for Tomcat, Tomcat's sessions session data is stored in Redis, enabling 7x24 operation with 0. Because the session is stored in Redis, Nginx does not have to be configured to stick a Tomcat mode to actually implement multiple Tomcat load balancing in the background, user requests can be sent to any Tomcat host, and when we need to deploy new application code, As soon as you stop any tomcat, all current online users will be directed to the running Tomcat instance, because session data is serialized to Redis, online users are unaffected, and once the deactivated Tomcat instance is on-line, there are additional recurring deployment processes.

Nginx

First, we install Nginx:

$ sudo rpm-ivh nginx-1.4.2-1.el6.ngx.x86_64.rpm

To edit the/etc/nginx/nginx.conf, join the load Balancing configuration:

HTTP {

Upstream Tomcat {

Server localhost:8080;

Server localhost:8081;

}

Include/etc/nginx/mime.types;

Default_type Application/octet-stream;

To update the/etc/nginx/conf.d/default.conf, replace the original with the following location configuration:

Location/{

Proxy_pass Http://tomcat;

}

Restart Nginx:

sudo service nginx restart

Tomcat instance

Let's start by installing two Tomcat instances, changing the second server port to prevent them from conflicting, and if you enter Https://localhost in the browser, we access to the default Tomcat page because we don't have the session sticky sticky sessions set, Each request distribution uses the load-balanced round robin algorithm, which means that each request creates a new session that can be easily seen through the Tomcat built-in case and into the http://localhost/examples/servlets/ Servlet/sessionexample, after a while to refresh this page, notice that each session ID will change, let us solve the problem.

To download and install Redis, the Http://redis.io/download has documented how to install and start, and after starting the server, use the client program to check whether it is working.

Finally, we need to configure the Tomcat storage session to Redis, we need to use the Tomcat-redis-session-manager Open Source Library package, specifically need some tweaks, download the project's source project, rebuild, This article uses versions Commons-pool2-2.2.jar and Jedis-2.6.1.jar. Copy these jars to the Lib directory of Tomcat.

Configure your Commons-pool, Jedis, and Tomcat versions in Tomcat-redis-session-manager Build.gradle and rebuild the project to build the results Tomcat-redis-session-manager-1.2.jar copied to the Tomcat Lib directory, two instances are replicated and configured in Tomcat's context.xml:

<valve classname= "Com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/>

<manager classname= "Com.orangefunction.tomcat.redissessions.RedisSessionManager"

host= "localhost"

Port= "6379"

database= "0"

Maxinactiveinterval= "/>"

Reboot tomcat Everything is done, you can now see that the tomcat session is stored in Redis, using a previous tomcat built-in case, experimenting with two tomcat boot and shutdown scenarios for testing, session data will not be affected.

Tomcat+nginx+redis Cluster test2014-08-01 15:10:06 This site (1432)

Tomcat+nginx+redis cluster test, there is a need for friends can refer to the next.


The Redis installation doesn't talk.

Just talk about dealing with the session.

The first thing to download is three packages: Tomcat-redis-session-manager.jar and Jedis-2.0.0.jar, And Commons-pool-1.6.jar, the first use of the package is Commons-pool-2 point, but is not the start of Tomcat when the error, said can not find what org.apache.commons.pool what class, and then I put commons-pool-2 dot more that The jar package was unpacked and saw that the package name was org.apache.commons.pool2, so the above error occurred, so I switched to Commons-pool-1.6.jar. And then start the success.

Then configure the following in the host node in Tomcat's Conf/server.xml:

<context path= "" docbase= "/usr/local/tomcat/webapps/web" debug= "0" reloadable= "true" >        <valve className = "Com.radiadesign.catalina.session.RedisSessionHandlerValve"/>        <manager classname= " Com.radiadesign.catalina.session.RedisSessionManager "            host=" localhost "            port=" 6379 "            database=" 0 "            maxinactiveinterval= "/>"         
</Context>



Originally according to the official website Https://github.com/jcoleman/tomcat-redis-session-manager, this above configuration, but error, XML parsing errors, remove the comments just fine.

Nginx installation before the blog has, the following to say the configuration:


In the HTTP node, add the following:

Upstream MyProject {
<span style= "White-space:pre" ></span> #这个是配置后端的tomcat, I started two tomcat locally, one port 8080, one for 8081       server localhost:8080;       Server localhost:8081;
<span style= "White-space:pre" ></span> #下面这三行可以注释掉, this is because prior to using Nginx configuration based on the JVM load Balancer     #  Server localhost:8080 srun_id:jvm1;     #  Server localhost:8081 srun_id:jvm2;     #  Jvm_route $cookie _jsessionid|sessionid reverse;    }


Configure under the location node:
Location/{        #     root   html;        #    Index  index.html index.htm;        Add_header Cache-control ' No-store ';         Client_max_body_size 100m;         Proxy_set_header Host $http _host;         Proxy_set_header x-real-ip $remote _addr;         Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;        #         Proxy_cache Z;         Proxy_cache_valid 1m;
<span style= "White-space:pre" ></span> #重要的是这里, here the MyProject and the above configuration in the MyProject name of the same can be, very simple         Proxy_pass   http://myproject;        }


Two tomcat, Port set to different, then each tomcat inside a project, of course, the project is very simple, tomcat1 inside a index.jsp, a test.jsp,tomcat2 inside a index.jsp

TOMCAT1 in the index.jsp:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "  pageencoding=" UTF-8 "%>

The test.jsp in TOMCAT1

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "  pageencoding=" UTF-8 "%>

The index.jsp in TOMCAT2

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "  pageencoding=" UTF-8 "%>

All two tomcat server.xml need to be configured, and two tomcat will need to place the three packages under the Lib directory of the Tomcat directory. Once you're done, you can start two Tomcat separately.


And then into the Nginx Bin directory, directly./nginx start Nginx


Then run http://YourIP/,

Will find basically tomcat1-tomcat2-tomcat1-tomcat2, the frequency of entering each server is basically the same. Then run http://YourIP/test.jsp, the page comes out and then run http://YourIP/, and you can see that each Tomcat index.jsp page prints out the user's value.

So far, the test is successful!


Because I work relatively busy, every time I write a blog is very messy, mainly to see for themselves. If you have any questions, or I write the wrong, can be submitted through the comments, I see the timely response, and everyone to communicate.

Tomcat+nginx+redis Cluster test

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.