Home > Database > Redis > Example analysis of high availability in Redis sentry mode

Example analysis of high availability in Redis sentry mode

WBOY
Release: 2023-06-02 22:38:04
forward
914 people have browsed it

    1. Preface

    Redis high availability has two modes: Sentinel mode and Cluster mode, this article builds one master, two slaves and three sentinelsRedis high availability service based on the sentinel mode.

    1. Goals and gains

    One master, two slaves and three sentinelsRedis 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:

    • Sentinel mode service planning and construction

    Sentinel mode service is compared to a single machine The version service is more reliable and suitable for scenarios where reading and writing are separated, the amount of data is not large, and reliability and stability are required.

    • Client integration and read-write separation

    Connect to the sentinel mode through the Spring framework to complete the production environment Common operations.

    2. Port planning

    Port planning is the first step to complete this solution.

    Example analysis of high availability in Redis sentry mode

    2. Single-machine simulation

    Single-machine simulation is to simulate operations on a physical machine or virtual machine to restore the intermediate process of the original solution as much as possible. Usually Used during the learning or development phase.

    In order to simplify the operation, the Redis service makes the following conventions: data is not persisted to disk; service instances run as foreground processes; node configuration files use the default configuration file as a template; there is no password verification.

    (1) Service planning

    1. Redis instance

    When the service is started for the first time, it clearly knows which node is the master node. When the service is running for a long time and When a master-slave switch occurs, it is not possible to display which node is the master node and needs to be queried indirectly through the command line.

    Node Host Port Role Additional configuration
    node01 127.0.0.1 6380 As the master service when started for the first time
    node02 127.0.0.1 6381 As a slave service when started for the first time replicaof 127.0.0.1 6380
    node03 127.0.0.1 6382 As a slave service when started for the first time replicaof 127.0. 0.1 6380

    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 master-slave distinction between Sentinel service nodes, and all nodes are on an equal footing. When an exception occurs in the main service, the sentinel service will trigger the voting strategy and select candidates from the slave nodes of the Redis instance as the new main service.

    Node Host Port Additional configuration
    node01 127.0.0.1 26380 sentinel monitor mymaster 127.0.0.1 6380 2
    node02 127.0.0.1 26381 sentinel monitor mymaster 127.0.0.1 6380 2
    node03 127.0.0.1 26382 sentinel monitor mymaster 127.0.0.1 6380 2

    (2) Service configuration

    1. Redis instance

    The initial configuration file of the node uses the default configuration file as a template.

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

    replicaof 127.0.0.1 6380
    Copy after login
    2. Sentinel service

    The initial configuration file of the node is as follows: The default configuration file is a template.

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

    sentinel monitor mymaster 127.0.0.1 6381 2
    Copy after login

    (3) Service management

    When testing or learning, it is recommended to use the foreground process to manage services. It is convenient to simulate a single point of failure, view logs and observe master-slave switching.

    Under production conditions, it is recommended to use Supervisor to manage the service, which is not only easy to manage but also can automatically restart the service after abnormal termination. 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 no
    Copy after login
    2. 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
    Copy after login

    3. Client integration

    Client implementation refers to the integration based on SpringBoot. Two-step implementation: first, complete the integration as the basis; second, add new features based on production needs.

    (1) Basic integration

    The content of basic integration is to use Java client to connect to the high-availability sentinel mode Redis service to achieve the requirements for normal operation of single-node failure services.

    1. Global configuration file

    The configuration information added to the global configuration file is: masterThe parameter is the sentinel service name, here is the default value;nodesThe parameter is the sentinel service list (not the Redis instance service list); databaseThe parameter is the database.

    spring:
      redis:
        database: 0
        sentinel:
          nodes: 192.168.181.171:26380,192.168.181.171:26381,192.168.181.171:26382
          master: mymaster
    Copy after login
    2. Integrated configuration

    is integrated into the SpringBoot system. The core is to create a LettuceConnectionFactory connection factory. Through the Redis connection factory, it can be smoothly inherited into other parts of the Spring system. frame.

    @Configuration
    public class RedisSentinelConfig {
        @Autowired
        private RedisProperties redisProperties;
        
        @Bean
        public RedisConnectionFactory lettuceConnectionFactory() {
            RedisProperties.Sentinel sentinel = redisProperties.getSentinel();
            HashSet<String> nodes = new HashSet<>(sentinel.getNodes());
            String master = sentinel.getMaster();
            RedisSentinelConfiguration config = new RedisSentinelConfiguration(master, nodes);
            config.setDatabase(redisProperties.getDatabase());
            return new LettuceConnectionFactory(config);
        }
    }
    Copy after login

    (2) Read-write separation

    Basic integration only implements the process of high-availability Redis service. In the production environment, other configurations still need to be added: modify the custom connection database serial number; authorize the connection ;Connection pool configuration; read-write separation.

    Under the premise of high availability, the feature of separation of reading and writing is derived. The main library completes the write request; the slave library completes the read request (the slave library does not allow writing).

    @Bean
    public LettuceClientConfigurationBuilderCustomizer lettuceClientCustomizer() {
        // 配置读写分离
        return builder -> builder.readFrom(ReadFrom.REPLICA);
    }
    Copy after login

    The above is the detailed content of Example analysis of high availability in Redis sentry mode. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template