Get the App
SLTechnology News&Howtos  ›  Servers  › 

Implementation of Cluster with Tomcat8+Redis+Nginx

Shulou Source: shulou.com Published: 2022-06-02 18:25:50 10月03日 Update

Realization of Cluster knowledge points by Tomcat8.5.14+Redis+Nginx

1) what is a cluster

A cluster is a group of independent computers interconnected through a high-speed network that form a group and are managed in a single system mode.

To put it bluntly, it is the same business, deployed on multiple servers.

2) problems encountered by the cluster

Session sharing

Session sharing based on NFS

Session sharing based on Database

Session sharing based on Cookie

Cache-based Session sharing (this is the option)

3) advantages of cluster

Improve performanc

Reduce the cost

Improve scalability

Enhance reliability

Test environment

JDK version: JDK8

Tomcat version: apache-tomcat-8.5.14

Redis version: redis-2.4.5

Nginx version: nginx-1.11.6

Required jar package

Put it under the lib folder under Tomcat:

Commons-pool2-2.4.2.jar

Jedis-2.9.0.jar

Javazhan-tomcat-redis-session.jar

Download address: Tomcat8+Redis to implement the Jar rack package http://down.51cto.com/data/2449143 required by the cluster

Modify the configuration file

1) Tomcat

Make two copies of apache-tomcat-8.5.14 and name them apache-tomcat-81 and apache-tomcat-82 respectively. The port of the server.xml needs to be modified in order to start both tomcat.

Service port

two。 Access connection port

The first connector listens on port 8080 and is responsible for establishing a HTTP connection. This connector is used when accessing the Web application of the Tomcat server through a browser.

3. Connect the port to the HTTP server

The second connector listens on port 8009 and is responsible for establishing connections with other HTTP servers. This connector is needed when integrating Tomcat with other HTTP servers.

Configure context.xml files: session shar

WEB-INF/web.xml

${catalina.base} / conf/web.xml

Description:

If you put manager in server.xml in your tomcat configuration, you will fail when you use maven for hot deployment. Therefore, it is recommended to put it in context.xml.

2) Nginx

Modify the nginx.conf file in the conf directory

Upstream javazhan.com {

Server yan.10010.com:8881 weight=5

Server yan.10010.com:8882 weight=5

}

Server {

Listen 80

Server_name api.xxx.com

Location /

{

Proxy_set_header Host $host

Proxy_set_header X-Real-Ip $remote_addr

Proxy_set_header X-Forwarded-For $remote_addr

Proxy_pass http://javazhan.com;

}

}

Create a new web project

Create a new folder ClusterTest, create a new sessiontest.jsp, and put it under webapps under the tomcat folder.

ClusterApp Test

Nginx IP visited:

Tomcat SessionPort:

0) {

String dataValue = request.getParameter ("dataValue")

Session.setAttribute (dataName, dataValue)

}

Out.println ("Session list"

")

System.out.println ("Session list")

Enumeration e = session.getAttributeNames ()

While (e.hasMoreElements ()) {

String name = (String) e.nextElement ()

String value = session.getAttribute (name). ToString ()

Out.println (name + "=" + value + ")

")

System.out.println (name + "=" + value)

}

% >

Name:

Value:

test

1) start nginx

2) start redis

3) start tomcat (two)

4) start the browser

5) enter the address http://yan.10010.com/ClusterTest/sessiontest.jsp

You can see that the same address accesses two tomcat servers respectively, and the session value remains the same, so session sharing is realized.

Tags: Service cluster file server port version connector two address folder configuration that is browser test browse monitor to put it bluntly business advantages scalability Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi OPPO Reno Microsoft Shulou Information Docker