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 quickly integrate Redis Sentinel with Spring Boot

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

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to quickly integrate Redis Sentinel with Spring Boot". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to quickly integrate Redis Sentinel with Spring Boot".

Redis Sentinel introduction

Redis Sentinel: Sentinel, Sentinel, look at the Chinese name, you can see that it is a Redis highly available solution, mainly for Redis master-slave mode to achieve master-slave node monitoring and automatic fault switching.

Before the Redis Sentinel architecture, if the master node dies, the OPS staff is required to manually switch between master and slave, and then update all the Redis IP address parameters used before restarting the system. All recovery operations require human intervention. If the system is hung up in the middle of the night, if there are many systems, if a certain operation is wrong, and so on, this is simply a nightmare for OPS personnel.

With Redis Sentinel, the master and slave node failures are switched automatically, and the application parameters do not need to be changed. For the client, the switch is transparent and seamless, and the operation and maintenance staff no longer have to worry.

For example, a 1-master-3-slave Redis architecture is as follows:

The structure after joining the Redis Sentinel is as follows:

In order to ensure the high availability of the Redis Sentinel architecture itself, it cannot have a single point. It is generally composed of 3 or more Sentinel nodes, which are responsible for monitoring the master and slave nodes. When most Sentinel nodes think that the master node is not available, they will choose a Sentinel node for failover.

The construction of the sentry architecture will not be carried out here. You can go to the official account Java technology stack, and reply to redis in the official account Java technology stack to read about the construction of Redis stand-alone, outpost and cluster, as well as the previous Redis and Spring Boot integration and distributed locking tutorials.

Spring Boot & Redis Sentinel actual combat

Now that you understand the usefulness of Redis Sentinels, let's take a look at how Spring Boot quickly integrates Redis Sentinel.

To know how to configure Redis Sentinel automatically, in addition to the official tutorial (not necessarily detailed), the best way is to look at the source code.

Anyone who has read the previous article knows that the default client of Spring Boot Redis is Lettuce. Let's take a look at the automatic configuration source code of LettuceConnectionFactory:

Org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration

As shown in the source code, we can see that the priority for automatic configuration of Redis connections is:

Redis Sentinel (Sentinel) > Redis Cluster (Cluster) > Standalone (standalone)

Sentinel mode has the highest priority, so let's take a look at the source code of the getSentinelConfig method:

Master and sentinels are required parameters, password and SentinelPassword are optional, and database is the 0th database by default.

Configuration parameter source code:

Org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel

Therefore, we only need to provide the basic configuration parameters of Redis Sentinel.

The application.yml configuration is as follows:

# follow the official account Java technology stack to see more spring: profiles: active: sentinel---spring: profiles: standalone redis: host: 192.168.1.110 port: 6379 password: redis2020 database: 1---spring: profiles: sentinel redis: password: redis2020 sentinel: master: mymaster nodes:-192.168.1.110RV 26379-192.168.1.11Rule 26379-192.168.1.112RV 26379

This allows you to switch between stand-alone and Sentinel modes, which is the advantage of yaml configuration. If you are not familiar with the official account Java technology stack, you can read the Spring Boot series articles I wrote. Of course, the configuration of two sets here is just for testing, which is meaningless in the actual project.

After the configuration is successful, Redis Sentinel is transparent to the client as to how to use it or how to use it.

Thank you for reading, the above is the content of "how to quickly integrate Redis Sentinel with Spring Boot". After the study of this article, I believe you have a deeper understanding of how Spring Boot can quickly integrate Redis Sentinel, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report