CGB2005-Beijing Amoy 14

1. About Spring Integrating Redis Sharding

1.1 About sharding

The main function of Redis sharding is to achieve the expansion of memory data. Redis sharding cannot achieve high availability if it goes down!
The calculation of Redis sharding occurs in the business server and the data that needs to be saved is stored in redis.
Redis points The execution efficiency of the film is the highest.

1.2 Spring integrates Redis sharding

1.2.1 Edit pro configuration file

#添加redis的配置

#添加单台配置
#redis.host=192.168.126.129
#redis.port=6379

#配置多台的redis信息
redis.nodes=192.168.126.129:6379,192.168.126.129:6380,192.168.126.129:6381


1.2.2 Edit Redis configuration class

package com.jt.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;

import java.util.ArrayList;
import java.util.List;

@Configuration //标识我是一个配置类
@PropertySource("classpath:/properties/redis.properties")
public class JedisConfig {
    
    

    @Value("${redis.nodes}")
    private String nodes; //node,node,node
    /**
     * 添加Redis分片的配置
     * 需求1: 动态的获取IP地址/PORT端口
     *        动态的获取多个的节点信息. 方便以后扩展
     */
    @Bean
    public ShardedJedis shardedJedis(){
    
    
        List<JedisShardInfo> list = new ArrayList<>();
        String[] strArray = nodes.split(","); //[node,node,node]
        for (String node : strArray){
    
      //ip:port
            String host = node.split(":")[0];
            int port = Integer.parseInt(node.split(":")[1]);
            list.add(new JedisShardInfo(host,port ));
        }
        return new ShardedJedis(list);
    }





   /* @Value("${redis.host}") //spel表达式  yml为准
    private String  host;
    @Value("${redis.port}")
    private Integer port;
    *//**
     * 将jedis对象交给spring容器管理
     *//*
    @Bean   //默认条件下是单例对象
    //@Scope("prototype") //设置为多利对象
    public Jedis jedis(){
        //由于将代码写死不利于扩展,所以将固定的配置添加到配置文件中
        return new Jedis(host,port);
    }*/
}

1.2.3 Edit the injection items in CacheAOP

Description: Change the injection item in the AOP cache to a shard object
Insert picture description here

1.3 Redis master-slave implementation

1.3.1 Redis high availability premise

Problem analysis: If you need to realize the high availability of the Redis server, the prerequisite should be the master-slave configuration.

1.3.2 Copy sentinel directory

Prerequisite: first shut down the Redis server.
1). Copy the shards file
Insert picture description here
2). Delete the persistent file
Insert picture description here
3). Run 3 Redis servers

[root@localhost sentinel]# redis-server 6379.conf & redis             -server 6380.conf & redis-server 6381.conf &

Insert picture description here

1.3.3 Implement master-slave mount

Construction rules:
1. 6379 as master
2. 6380/6381 as slave

Check the status of master and slave: info replication
Insert picture description here
3). Realize the mounting of master and slave slaveof 主机IP 主机端口
Insert picture description here
4). About the characteristics of master and slave mounting
Check the status in the slave
Insert picture description here
Check the status of the host:
Insert picture description here

1.3.4 Instructions on mounting errors

Note: Misoperation may cause the master-slave structure to mount abnormally. How to re-mount it?
Operation instructions: You can shut down all the redis servers, and then restart the master-slave mount under the default conditions will fail. Re-mounting is available.
Insert picture description here
Additional information: due to the effective date slaveof in memory If the memory resource is released, the master-slave relationship will fail to achieve permanent, master-slave relationship should write to the configuration file..
a new problem: If the host goes down unexpectedly, who will complete the configuration file modification?

1.4 How the sentry works

Insert picture description here
Description of the working principle of the sentinel
1. When the sentinel is started, it will monitor the current host information. At the same time, obtain the slave information linking the current host.
2. When the sentinel uses the heartbeat detection mechanism (PING-PONG), check whether the host is normal. If continuously 3 Once the host is found to have no response information, the election begins.
3. After the sentinel election is completed, other nodes will be regarded as slaves of the new host.

1.5 Implementation of sentinel mechanism

1.5.1 Copy sentinel configuration file

Insert picture description here

1.5.2 Modify the configuration file

1). Modify the protection mode
Insert picture description here
2). Turn on background operation
Insert picture description here
3). Modify the monitoring of the sentinel, where 1 represents the number of votes in effect.
Insert picture description here
4). The election time after the sentinel is down.
If the host is down for 10 seconds, the election will start.
Insert picture description here
5 .Modify the timeout period of sentinel election
Insert picture description here

1.5.3 Sentinel high availability test

1). Start the sentry

[root@localhost sentinel]# redis-sentinel sentinel.conf

2). Turn off the host first, and then wait for 10 seconds to check whether the slave is elected as the master. Then restart the host (downtime) to check whether it is the slave of the new host
Insert picture description here

1.6 Notes on the tie vote for the sentinel election

If there are multiple Sentinel election, if three consecutive failed vote, may lead to split brain phenomena occur.
Problem: the probability of split-brain phenomenon is how much ?? 1/8 = 12.5%
solution strategy: as long as the election of nodes increases Quantity, can effectively reduce the occurrence of split brain phenomenon. Probability theory

1.7 Starter case of link sentinel

   /**
     * 哨兵的测试
     * 参数说明:  masterName: 主机的变量名称
     *           sentinels:   链接哨兵的集合.
     * 理解: 哨兵虽然链接3台redis. 但是3台redis中存储的数据都是一样的.对于用户而言
     * 就是一台.
     */
    @Test
    public void testSentinel(){
    
    
        Set<String> sets = new HashSet<>();
        sets.add("192.168.126.129:26379");
        JedisSentinelPool sentinelPool =
                new JedisSentinelPool("mymaster",sets);
        Jedis jedis = sentinelPool.getResource();
        jedis.set("aaa", "wt");
        System.out.println(jedis.get("aaa"));
        jedis.close();
    }

1.8 Summary description about Sharding Sentry

Sharding :
1. The main function is to realize the expansion of memory data.
2. Since the operation occurs in the business server, the execution efficiency is higher.
3. The Redis sharding does not have the effect of high availability. If one of the nodes has a problem The
sentinel mechanism:
1. Realize the high availability of Redis. When the redis server is down, the sentinel can monitor flexibly. Automatic election is realized to realize the migration of the fault.
2. The redis node monitored in the sentinel The data in the data is the same. It is impossible to achieve massive data storage.
3. Although the sentinel can achieve high availability of redis, but because the sentinel itself does not achieve high availability. So there is a risk.
If you want to minimize the loss, then It is recommended not to introduce third-party monitoring

2. Redis cluster construction

2.1 Construction steps

Note: For the steps to build a Redis cluster, please refer to the pre-class documentation.

2.2 Explanation of errors in Redis cluster construction

Prerequisites: The redis.conf configuration file should be configured correctly. There is redis configuration in the code cloud.
Construction steps:
1. Close all Redis service items
Insert picture description here
2. Delete the nodes.conf configuration file.
Since the cluster is built, all cluster information will be Write to the nodes.conf file, if the next restart, the configuration information will be read to realize the establishment of the master-slave of the redis cluster. So if you need to rebuild the cluster, you must delete the file and regenerate it.
Insert picture description here
3. Restart the Redis server Build a cluster

redis-cli --cluster create --cluster-replicas 1 192.168.126.129:7000 192.168.126.129:7001 192.168.126.129:7002 192.168.126.129:7003 192.168.126.129:7004 192.168.126.129:7005

2.3 Explanation of the working principle of the cluster

2.3.1 Redis cluster high availability test

1). Check the status of the redis host
Insert picture description here
2). Shut down the host
redis-cli -p 7000 shutdown

3). Check whether the host is switched
Insert picture description here
4). Restart the 7000 server. Check whether it is the slave of 7003
Insert picture description here

2.4 Interview questions about Redis cluster

Principle: Redis memory is missing and the cluster crashes

2.4.1 If there are 3 masters and 3 slaves (1 master and 1 slave), how many clusters will crash at least/downtime? B

	A. 1台      B.2台  C.3台  D.4台

2.4.2 If 3 masters and 6 slaves (1 master and 2 slaves), how many clusters will crash at least? C

	A. 3台      B.4台  C.5台  D.6台
	**说明: 如果没有子节点 则会借用其他主机的多余的从.**

2.5 SpringBoot integrates Redis cluster

2.5.1 Entry Case Test

  /**
     * redis集群的入门案例
     * jedisCluster 操作整个redis集群,链接redis的所有的节点
     */
    @Test
    public void testCluster(){
    
    
        Set<HostAndPort> sets = new HashSet<>();
        sets.add(new HostAndPort("192.168.126.129", 7000));
        sets.add(new HostAndPort("192.168.126.129", 7001));
        sets.add(new HostAndPort("192.168.126.129", 7002));
        sets.add(new HostAndPort("192.168.126.129", 7003));
        sets.add(new HostAndPort("192.168.126.129", 7004));
        sets.add(new HostAndPort("192.168.126.129", 7005));
        JedisCluster jedisCluster = new JedisCluster(sets);
        jedisCluster.set("cluster", "集群测试");
        System.out.println(jedisCluster.get("cluster"));
    }

operation

1. Understand the principle of data storage in the redis cluster (partition algorithm/hash slot algorithm)

2. After the test API of the redis cluster is handed over to the Spring container for management, the AOP cache links the cluster.

3. Complete all the codes of the Jingtao background.

Guess you like

Origin blog.csdn.net/qq_16804847/article/details/108545667