Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize session persistence of Nginx+tomcat load balancing

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Editor to share with you how to achieve Nginx+tomcat load balancing session persistence, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!

What is session persistence?

Session refers to the process of communication between the end user and the interactive system, such as entering the account password into the operating system to exiting the operating system is a session process. Sessions are mostly used on the network. TCP's three-way handshake creates a session, and TCP closes the connection.

Http protocol is a stateless protocol, so we need to use Session mechanism to save the user's state, such as login status, common information and so on.

Session is used to save information about a session, and it is possible to share this data among multiple pages.

For example: you open Taobao and log in to your personal account, even if you browse more stores and switch a lot of pages, the user name remains the same, this is the role of Session. The previous login will not be erased until you close the browser and the Session is destroyed.

To share with you today, "session persistence for load balancing with Nginx+tomcat" is an implementation: if a load node of the website dies, the request is assigned to the new node Session will also be synchronized to achieve session persistence, thus ensuring that users browse without awareness.

Realization principle

Using Manager object to implement HTTP session Management

DeltaManager as the session manager

Users access nginx,nginx load balancing to tomcat, which is used in the session manager of the Tomcat cluster, synchronizing all session session changes to each node in the cluster, and realizing session replication.

Experimental configuration

Nginx configuration

In the test environment, a single server has two tomcat, one corresponding to port 8080 and the other corresponding to port 8090.

Create TomcatA TomcatB and configure tomcat server.xml separately

Copy the following to Engine

Add this module:

Note:

1.classname configures what classes the tomcat cluster uses to transmit information to each other.

Channelsendoptions can be set to 2, 4, 8. 10.

2-acknowledge send 4-synchronous send 8-Asynchronous send

10-in asynchronous mode, reliability can be improved by adding acknowledgment transmission (Acknowledge)

2.manager decides how to manage the session information of the cluster

ClassName- specifies a class that implements the org.apache.catalina.ha.ClusterManager interface for the management of information.

When expireSessionsOnShutdown- is set to true, a node shuts down, which will invalidate all Session under the cluster.

Whether to notify session listeners of Session replication and deletion operations between nodes under the notifyListenersOnReplication- cluster

3.Channel is a tool for communication between Tomcat nodes.

Channel includes five components: Membership, Receiver, Sender, Transport, Interceptor

4.Membership maintains a list of available nodes for the cluster. Used to detect new nodes and offline nodes

Address- Multicast address (arbitrarily defined Multicast address). If you need Aliyun ECS to deploy tomcat, because ECS does not support multicast, it is recommended to use memcache or redis storage session for session persistence instead.

Port- Multicast Port (two tomcat Multicast ports should be consistent)

The interval (in ms) at which frequency- sends heartbeats (UDP packets are sent to multicast addresses). Default value is 500

If dropTime-Membership does not receive a heartbeat from a node within the dropTime (unit: ms), the node is removed from the list of available nodes. Default value is 3000

5.Receiver: receiver

Address where address- receives messages (default auto, but when tomcat is enabled on a virtual machine, an error occurs. It is recommended to configure a native IP address)

The port on which the port- receives messages (if multiple tomcat are configured on a server, the start port needs to be distinguished)

Variation range of autoBind- ports

If port is 4000 and autoBind is 100, the receiver will take a port between 4000 and 4099 to listen.

SelectorTimeout-NioReceiver polling timeout

The number of threads in the maxThreads- thread pool

6.sender: the transmitter is responsible for sending messages

7.Valve: filter

8.Deployer: synchronizes the consistency of all nodes in the cluster

9.ClusterListener: a listener that listens to messages received by Cluster components. When using DeltaManager, the information received by Cluster is passed to DeltaManager through ClusterSessionListener

Configure web.xml

Add attributes to the web.xml to tell the web container that the project is a distributed project

Add tags to the web project WEB-INF directory

Session persistence authentication

Webapps/ROOT/index.jsp creates a test page to get sessionID

TomcatA TomcatA TomcatA Session ID Created on tomcatB TomcatB TomcatB Session ID Created on

Browser access authentication

The above is all the contents of the article "how to achieve session persistence in Nginx+tomcat load balancing". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report