当前位置:网站首页>[redis]redis6 master-slave replication

[redis]redis6 master-slave replication

2022-06-22 21:10:00 fate _ zore

Redis6 Master-slave replication of

brief introduction

After the host data is updated, according to the configuration and Policy , Automatic synchronization to the standby machine master/slaver Mechanism ,Master Write first ,Slave Mainly reading

effect

  • Read / write separation , Performance expansion
  • Rapid disaster recovery

image-20220621112217034

Specific operation

  1. Copy multiple redis.conf file include( Write absolute path )
  2. Turn on daemonize yes
  3. Pid File name pidfile
  4. Designated port port
  5. Log File name
  6. dump.rdb name dbfilename
  7. Appendonly Turn it off or change the name

newly build redis****.conf

include /myRedis/redis.conf
pidfile /var/run/redis_6379.pid
port 6379
dbfilename dump6379.rdb

Start three services

image-20220621135348219

perform slaveof host ip Port number

image-20220621141106758

Commonly used 3 recruit

One master and two servants

  • When you hang up from the service , Start again , It will not inherit the previous master-slave state , At this time, the service is master, It needs to be reset , And after resetting , The data of the main service will be copied
  • When the main service hangs up , The state of the slave service does not change , When the main service is started again , It will automatically continue the previous master-slave status , The data will also be copied to the main service , Everything remains the same.

It's passed down from generation to generation

the previous Slave It could be the next slave Of Master,Slave Can also receive other slaves Connection and synchronization requests for , Then the slave As the next in the chain master, Can effectively reduce master The pressure of writing , Decentralization reduces risk .

  • use slaveof
  • Change direction in the middle : Will clear the previous data , Recreate the copy of the latest
  • The risk is that once something slave Downtime , hinder slave No backup
  • The mainframe is down , Slave or slave , Unable to write data

image-20220621144809090

Going to

When one master After downtime , hinder slave Can be promoted to master, Behind it slave You don't have to make any changes .

use slaveof no one From slave to host .

Master slave replication principle

  1. After the server is linked to the primary server , Send a data synchronization message to the primary server
  2. The primary server receives a message , Pass the data through rdb Way to persist , take rdb File transfer to the slave server , From the server through rdb File read
  3. Whenever the primary server performs a write operation , Will notify the server to synchronize data
  4. The slave server only actively requests data synchronization during the first link , In other cases, the master server is responsible for data synchronization

image-20220621143739606

Sentinel mode

brief introduction

The automatic version of anti guest oriented , Be able to monitor the failure of the host in the background , If it fails, it will automatically convert from the library to the main library according to the number of votes

image-20220621150358229

Implementation steps

  1. One servant and two masters

  2. Self defined /myRedis New under the directory sentinel.conf file

  3. Deploy sentinels , Fill in the content

    sentinel monitor mymaster 127.0.0.1 6379 1

    among mymaster Server name for the monitored object , 1 For at least how many sentinels agree to move the number .

  4. Activate the sentry

    /usr/local/bin

    redis For pressure measurement, you can use your own re dis-benchmark Tools

    perform redis-sentinel /myredis/sentinel.conf

  5. When the host goes down , A new host is generated from the election of the slave computer

    ( Probably 10 You can see the sentry window log in seconds , Switched to a new host )

    Which is elected as the host from the opportunity ? According to priority :slave-priority

    After the original host is restarted, it will become a slave .

  6. Replication delay

    Because all the writing operations are first in Master On the operation , Then sync update to Slave On , So from Master Synchronize to Slave The machine has a certain delay , When the system is busy , The delay problem will be more serious ,Slave The increase in the number of machines will make the problem more serious .

Fault recovery

image-20220621152330751

The priority is redis.conf The default :slave-priority 100, The lower the value, the higher the priority

Offset refers to the most complete data of the original host

Every redis After the instance is started, it will generate one randomly 40 Bit runid

java Set up

private static JedisSentinelPool jedisSentinelPool=null;

public static  Jedis getJedisFromSentinel(){
    
if(jedisSentinelPool==null){
    
            Set<String> sentinelSet=new HashSet<>();
            sentinelSet.add("192.168.11.103:26379");

            JedisPoolConfig jedisPoolConfig =new JedisPoolConfig();
            jedisPoolConfig.setMaxTotal(10); // Maximum number of connections available 
jedisPoolConfig.setMaxIdle(5); // Maximum number of idle connections 
jedisPoolConfig.setMinIdle(5); // Minimum number of idle connections 
jedisPoolConfig.setBlockWhenExhausted(true); // Whether the connection is exhausted and wait 
jedisPoolConfig.setMaxWaitMillis(2000); // Waiting time 
jedisPoolConfig.setTestOnBorrow(true); // Test the connection  ping pong

jedisSentinelPool=new JedisSentinelPool("mymaster",sentinelSet,jedisPoolConfig);
return jedisSentinelPool.getResource();
  }else{
    
return jedisSentinelPool.getResource();
        }
}

原网站

版权声明
本文为[fate _ zore]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221944519951.html