Tomcat cluster sharing session (Nginx+redis) __tomcat

Source: Internet
Author: User

Nginx+tomcat+redis Configure session sharing and load balancing

Using Nginx as a front-end server, the required session is deposited into the Redis, and the session can be taken from the Redis when one of Tomcat hangs.

There are two options here.

Scenario One: Use Tomcat-redis-seesion-manager to put the jar package in Tomcat's lib.


Jar Downloads:

Tomcat-redis-session-manage-tomcat7.jar

Commons-pool2-2.2.jar

Jedis-2.5.2.jar

Jar Package Download Address:http://download.csdn.net/detail/lh2420124680/9904714

Modify Tomcat's context.xml file,

Join:

<valve classname= "Com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/>
<manager classname= "Com.orangefunction.tomcat.redissessions.RedisSessionManager"
host= "localhost"
Port= "6379"
database= "0"
Maxinactiveinterval= "/>"
Then start Tomcat and Nginx according to the Nginx configuration of Scenario two


Scenario Two: Use Redis to handle logic in your code.

Configuration of 1.nginx.conf


Here is a CLU as a cluster name, there are two servers (two Tomcat, modified to different ports), here is the main load balancing, Nginx will forward the request to different servers, reduce the pressure of access.

One of the location ~ (. *\. ( jpg|jpeg|png|ico|gif|css|js|html) is configured because there is a POST request static resource in the project because the Nginx does not allow a POST request for a static resource, so configure it here and ask for 200 if it is 405.

Then add the Redis storage method to the login code


Here, I turn the session data into JSON string type, and then escape into Redis, otherwise Redis will be "" and other special symbols escape, take out is not the original data, so get out with unescape

Here's how to escape and unescape.

public static string Escape (String src) {
int i;
Char J;
StringBuffer tmp = new StringBuffer ();
Tmp.ensurecapacity (Src.length () * 6);
for (i = 0; i < src.length (); i++) {
j = Src.charat (i);
if (Character.isdigit (j) | | Character.islowercase (j)
|| Character.isuppercase (j))
Tmp.append (j);
else if (J < 256) {
Tmp.append ("%");
if (J < 16)
Tmp.append ("0");
Tmp.append (Integer.tostring (J, 16));
} else {
Tmp.append ("%u");
Tmp.append (Integer.tostring (J, 16));
}
}
return tmp.tostring ();
}


public static string unescape (String src) {
StringBuffer tmp = new StringBuffer ();
Tmp.ensurecapacity (Src.length ());
int lastpos = 0, pos = 0;
Char ch;
while (Lastpos < Src.length ()) {
pos = src.indexof ("%", lastpos);
if (pos = = Lastpos) {
if (Src.charat (pos + 1) = = ' U ') {
ch = (char) integer.parseint (src
. substring (pos + 2, pos + 6), 16);
Tmp.append (CH);
Lastpos = pos + 6;
} else {
ch = (char) integer.parseint (src
. substring (pos + 1, pos + 3), 16);
Tmp.append (CH);
Lastpos = pos + 3;
}
} else {
if (pos = = 1) {
Tmp.append (src.substring (Lastpos));
Lastpos = Src.length ();
} else {
Tmp.append (Src.substring (Lastpos, POS));
Lastpos = pos;
}
}
}
return tmp.tostring ();
}

Redis out the data



Then start two tomcat, log in, and then close a tomcat to test whether the user is logged in.

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.