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

Example Analysis of High availability of Redis Sentinel Mode

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail the example analysis of the high availability of Redis Sentinel mode. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

I. Preface

There are two modes of Redis high availability: Sentinel mode and cluster mode. This paper builds an one-master, two-slave, three-sentry Redis high availability service based on the sentinel mode.

1. Goal and harvest

The one-master, two-slave, three-sentry Redis service can basically meet the high availability requirements of small and medium-sized projects. Use Supervisor to monitor and manage Redis instances. Through this article, the following goals will be achieved:

Service Planning and Construction of Sentinel Mode

Compared with the stand-alone service, the Sentinel mode service is more reliable and suitable for scenarios where read and write are separated, the amount of data is not very large, and reliability and stability are required.

Client integration and read-write separation

The Sentinel mode is connected through the Spring framework to complete common operations in the production environment.

2. Port planning

Port planning is the first step to complete this scheme.

Second, stand-alone simulation

Stand-alone simulation refers to simulating the operation on a single physical machine or virtual machine to maximize the intermediate process of the original solution, which is suitable for use in the learning or development stage.

To simplify the operation, the Redis service makes the following conventions: the data is not persisted to disk; the service instance runs as a foreground process; the configuration file of the node is templated with the default configuration file; and there is no password verification.

(1) Service planning 1. Redis instance

When the service starts for the first time, it clearly knows which node is the master node. When the service runs for a long time and the master-slave switch occurs, it cannot show that it knows which node is the master node, so it needs to query indirectly through the command line.

Node host port role additional configuration node01127.0.0.16380 first startup as master service node02127.0.0.16381 first startup as slave service replicaof 127.0.0.1 6380node03127.0.0.16382 first startup as slave service replicaof 127.0.0.16380

Additional configuration refers to the new configuration in the node configuration file when the Redis service instance is started for the first time.

2. Sentinel service

There is no difference between master and slave between sentinel service nodes, and all nodes are in equal status. When the master service is abnormal, the Sentinel service wakes up the voting policy and selects the candidate for the master service from the Redis instance slave node.

Node host port additional configuration node01127.0.0.126380sentinel monitor mymaster 127.0.0.1 6380 2node02127.0.0.126381sentinel monitor mymaster 127.0.0.1 6380 2node03127.0.0.126382sentinel monitor mymaster 127.0.0.1 6380 2 (II) Service configuration 1, Redis instance

The initial configuration file of the node is based on the default configuration file.

After node01 and node02 initialize the configuration file, the master-slave relationship between the specified nodes is displayed, and the following configuration is added:

Replicaof 127.0.0.1 63802, Sentinel Service

The initial configuration file of the node is based on the default configuration file.

After initializing the configuration files of node01, node02, and node03, add the following configuration:

Sentinel monitor mymaster 127.0.0.1 6381 2 (3) Service Management

When testing or learning, it is recommended to use the foreground process management service to simulate a single point of failure and view the log to observe the master-slave switch.

It is recommended to use Supervisor management service under production conditions, which is not only easy to manage, but also can restart automatically after abnormal termination of the service. Three physical machines are used in high availability scenarios.

1. Redis instance / usr/local/redis/bin/redis-server / usr/local/redis/conf/ms/redis80.conf-- port 6380-- save''--daemonize no/usr/local/redis/bin/redis-server / usr/local/redis/conf/ms/redis81.conf-- port 6381-- save''--daemonize no/usr/local/redis/bin/redis-server / usr/local/redis/conf/ms/redis82.conf-- port 6382-- save'--daemonize no2, Sentinel service / usr/local/redis/bin/redis-sentinel / usr/local/redis/conf/ms/sentinel280.conf-port 26380-daemonize no/usr/local/redis/bin/redis-sentinel / usr/local/redis/conf/ms/sentinel281.conf-port 26381-daemonize no/usr/local/redis/bin/redis-sentinel / usr/local/redis/conf/ms/sentinel282.conf-port 26382-daemonize no III, client integration

Client-side implementation refers to the integration based on SpringBoot is divided into two steps: one is to complete the integration as the basis; the other is to add new features according to the needs of production.

(1) basic integration

The content of basic integration is to connect the highly available sentinel mode Redis service with Java client to meet the requirement of normal operation of single node failure service.

1. Global configuration file

The configuration information added to the global configuration file includes: the master parameter is the sentry service name, which is the default value; the nodes parameter is the sentry service list (not the Redis instance service list); and the database parameter is the database.

Spring: redis: database: 0 sentinel: nodes: 192.168.181.171Vl26380192.168.181.171VOL26381192.168.181.171Vl26382 master: mymaster2, integrated configuration

Integrated into the SpringBoot system, the core is to create a LettuceConnectionFactory connection factory, through the Redis connection factory, can be successfully inherited into other frameworks under the Spring system.

@ Configurationpublic class RedisSentinelConfig {@ Autowired private RedisProperties redisProperties; @ Bean public RedisConnectionFactory lettuceConnectionFactory () {RedisProperties.Sentinel sentinel = redisProperties.getSentinel (); HashSet nodes = new HashSet (sentinel.getNodes ()); String master = sentinel.getMaster (); RedisSentinelConfiguration config = new RedisSentinelConfiguration (master, nodes); config.setDatabase (redisProperties.getDatabase ()); return new LettuceConnectionFactory (config);}} (II) Separation of read and write

Basic integration only implements the process of highly available Redis services, and other configurations need to be added in the production environment: modify the custom connection database sequence number; authorize the connection; connection pool configuration; read-write separation.

Under the premise of high availability, the feature of read-write separation is derived, and the master library completes the write request; the slave library completes the read request (write is not allowed from the slave library).

@ Beanpublic LettuceClientConfigurationBuilderCustomizer lettuceClientCustomizer () {/ / configure read-write separation return builder-> builder.readFrom (ReadFrom.REPLICA);} this is the end of the article on "sample Analysis of High availability of Redis Sentinel Mode". I hope the above content can be helpful to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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

Development

Wechat

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

12
Report