1024programmer Nginx Use Nginx+memcached+tomcat to configure load balancing cluster and session sharing

Use Nginx+memcached+tomcat to configure load balancing cluster and session sharing

Configuration Environment:

under windows xp

jdk1.7.0_10 nginx-1.2.6 (download attached)
memcached-1.2.6-win32-bin.zip (download attached) 
tomcat7.0.12

*(Note: nginxLoad balancing will not have a big problem, but when configuring session sharing with memcached, the version of tomcat may become the key to success or failure,

The tomcat7.0.34 used at the beginning always throws an exception, the reason should be that there is no jar package matching the latest tomcat’s memcached)

1. First, use nginx to build a load balancing environment for three tomcat servers. For how to start three tomcats on one machine, please refer to “Start three tomcats on one machine”

The ports of my tomcat are 8081, 8082, 8083

Unzip the downloaded nginx compressed package, my path is D:\MyServer;

In D:\MyServer\nginx-1.2.6\conf
Next, find nginx.conf, which is the request distribution configuration file for nginx. Open nginx.conf and make the following modifications:

(1) in http {…} – server{…} – location /
Add a line to {…}: “proxy_pass http://127.0.0.1;”

After modification, it is as follows:

location / {
root html;
Index index.html index.htm;
Proxy_pass http://127.0.0.1;
}

(2) Add the following code to http {…}:

#Set load balancing server list
upstream 127.0.0.1 {
The #weigth parameter represents the weight, the higher the weight, the greater the probability of being assigned
Server 127.0.0.1:8081 weight=1;
Server 127.0.0.1:8082 weight=2;
server 127.0.0.1:8082 weight=3;
}

A simple test of request distribution can be performed,

Start nginx, open cmd, enter the path of D:\MyServer\nginx-1.2.6\ and enter nginx
Press Enter, and the task manager at the scheduling office sees nginx in the process, indicating that the startup is successful

Start three tomcat

Use eclipse to create a new web project cluster, create a new clustertest.jsp file under this project, and edit the content of the file as follows:

<%
//HttpSession session = request.getSession(true);
System.out.println(session.getId());
out.println(“
SESSION ID:” + session.getId()+”
“);
// If there is a new request, add the session attribute
String name = request.getParameter(“name”);
if (name != null && name.length() > 0) {
String value = request.getParameter(“value”);
session.setAttribute(name, value);
}
out.print(“Session List:“);
Enumeration names = session.getAttributeNames();
while (names.hasMoreElements()) {
String sname = names.nextElement();
String value = session.getAttribute(sname).toString();
out.println( sname + ” = ” + value+”
“);
System.out.println( sname + ” = ” + value);
}
%>

Modify tomcat\conf\server.xml

The other two corresponding tomcats are

tomcat7-2
tomcat7-3

Package the cluster projectThe war package is placed under the webapps of each tomcat

Enter localhost:8080/cluster/clustertest.jsp in the browser and refresh repeatedly, you can see that the output session value is changing, indicating that the distribution process is successful

2. Use memcached to implement three tomcat shared sessions

Unzip the memcached compressed package downloaded at the beginning to the specified path, mine is D:\MyServer
(Unzip to get a memcached.exe file)

Download the following jar package, click to download spymemcached-2.7.3.jar

Other jar packages can be found from the following link: http://code.google.com/p/memcached-session-manager/downloads/list

*(Note that tomcat7 is used in this test, so the corresponding jar package is memcached-session-manager-tc7-1.6.3.jar,

If you are using tomcat6
Then download memcached-session-manager-tc6-1.6.3.jar)

Ok, put the above jar package into tomcat/lib
directory, and then import the above jar package in the test project, and modify the configuration file tomcat\conf\context.xml of each tomcat

Add the following code between the … tags:

<Manager
className=”de.javakaffee.web.msm.MemcachedBackupSessionManager”
memcachedNodes=”n1:127.0.0.1:11211″
sticky=”false”
sessiOnBackupAsync=”false”
lockingMode=”uriPattern:/path1|/path2″
requestUriIgnorePattern=”.*\.(ico|png|gif|jpg|css|js)$”
transcoderFactoryClass=”de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory”
/>

Close all tomcats, open cmd, enter the path D:\MyServer where memcached is decompressed, and enter: memcached.exe
?d install Enter to install windows service

Enter: memcached.exe ?p 11211 ?d start
Press Enter to start the memcached service and start three tomcats

Enter localhost:8080/cluster/clustertest.jsp in the browser
Refresh repeatedly and find that the value of the session remains unchanged, as shown below, session sharing is successful

SESSION ID: 76AC7C56C4B278ADFDEC6EA782192D15-n1.tomcat7-1

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/use-nginxmemcachedtomcat-to-configure-load-balancing-cluster-and-session-sharing/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索