Redis master-slave replication, sentinel, cluster mode

Redis master-slave replication, sentinel, cluster mode

Single node mode

SpringBoot integration
@Bean(name = "singleClient")
public RedissonClient singleRedissonClient() {
    
    
    try {
    
    
        Config config = new Config();
        config.useSingleServer().setAddress("redis://" + redisProperties.getUrl() + ":" + redisProperties.getPort()).setDatabase(1);
        // 设置序列化方式
        Codec codec = new JsonJacksonCodec();
        config.setCodec(codec);
        RedissonClient redissonClient = Redisson.create(config);
        redissonClient.getBucket("one").set("one");
        logger.info("==========> singleRedissonClient()配置完成!");
        return redissonClient;
    } catch (Exception e) {
    
    
        logger.info("==========> singleRedissonClient() 出错:" + e.getMessage());
        return null;
    }
}

Master-slave replication

Configuration method

The configuration of the master node remains unchanged, and the slaveof 127.0.0.1 9000server address and port are added to the configuration information of the slave node .

Features
  • A master can have multiple slaves.
  • Slaves can also be synchronized with each other. To put it simply, the database is backed up to prevent data loss and so on.
  • Can be used to read and write separation and restore lost data.
  • You need to manually configure the master and slave nodes, add the master node and hang up, you need to redefine the master node.
SpringBoot integration
/**
  * 主从模式
  */
@Bean(name = "masterSlaveClient")
@Primary
public RedissonClient masterSlaveClient() {
    
    
    try {
    
    
        Config config = new Config();
        config.useMasterSlaveServers().setMasterAddress("redis://127.0.0.1:6379")
            .addSlaveAddress("redis://127.0.0.1:6380");
        RedissonClient redissonClient = Redisson.create(config);
        redissonClient.getBucket("masterSlaveClient").set("masterSlaveClient");
        logger.info("==========> masterSlaveClient()配置完成!");
        return redissonClient;
    } catch (Exception e) {
    
    
        logger.info("==========> masterSlaveClient() 出错:" + e.getMessage());
        return null;
    }
}

Sentinel mode

The sentinel mode is a special mode. First of all, Redis provides the command of the sentinel. The sentinel is an independent process. As a process, it will run independently. The principle is that the sentinel can monitor multiple Redis instances running by sending commands and waiting for the response from the Redis server. **In simple terms, it replaces the manual operation of switching the master node.

The role of the sentinel
  • By sending a command, the Redis server returns to monitor its running status, including the master server and the slave server.

  • When the sentinel detects that the master is down, it will automatically switch the slave to the master, and then notify other slave servers through the publish and subscribe mode , modify the configuration file, and let them switch the master.

/**
  * 哨兵模式
  */
@Bean(name = "sentryClient")
public RedissonClient sentryClient(){
    
    
    try{
    
    
        Config config = new Config();
        // 需要启动/redis-sentinel
        config.useSentinelServers().setMasterName("mymaster")
            .addSentinelAddress("redis://127.0.0.1:6379")
            .addSentinelAddress("redis://127.0.0.1:6380");
        RedissonClient redissonClient = Redisson.create(config);
        redissonClient.getBucket("sentryClient").set("sentryClient");
        logger.info("==========> sentryClient()配置完成!");
        return redissonClient;
    }catch (Exception e){
    
    
        logger.info("==========> sentryClient() 出错:" + e.getMessage());
    }
    return null;
}

Cluster mode

You need to enable Redis cluster mode, otherwise an error will be reported. The use of clusters is distributed storage. That is, each redis stores different content. The cluster requires at least 3 masters and 3 slaves, and each instance uses a different configuration file. The master and slave do not need to be configured, and the cluster will choose by itself. Multiple master nodes are provided through the cluster mode, each node can read and write, and the nodes can communicate with each other; the entire cluster has no central node.

Cluster mode characteristics
  • The data is automatically fragmented, and a part of the data is placed on each master.
  • Provides built-in high-availability support. When some masters are unavailable, they can still continue to work.
/**
  * 集群模式
  */
@Bean(name = "clustersClient")
public RedissonClient  clustersClient(){
    
    
    try{
    
    
        Config config = new Config();
        config.useClusterServers()
            .setScanInterval(2000)
            .addNodeAddress("redis://127.0.0.1:6379")
            .addNodeAddress("redis://127.0.0.1:6382")
            // ..
            // ..
            // ..
            .addNodeAddress("redis://127.0.0.1:6381");
        RedissonClient redissonClient = Redisson.create(config);
        redissonClient.getBucket("clustersClient").set("clustersClient");
        logger.info("==========> clustersClient()配置完成!");
        return redissonClient;
    }catch (Exception e){
    
    
        logger.info("==========> clustersClient() 出错:" + e.getMessage());
    }
    return null;
}

Guess you like

Origin blog.csdn.net/qq_37248504/article/details/113861502