当前位置:网站首页>Redis cluster

Redis cluster

2022-07-08 01:26:00 beginnerDZ

Redis colony

Concept

Not enough capacity ,redis How to expand ?

1. Changing one set to multiple sets can solve colony

Concurrent write operations , redis How to apportion ?

1. colony

in addition , A master-slave mode , Pass on mode , Host down , Lead to ip The address has changed , The corresponding host address needs to be modified for configuration in the application 、 Port and other information .

Previously, it was solved by proxy host , however redis3.0 The solution . That is, decentralized cluster configuration .

Proxy host : A proxy is used between the client and the server It is different from the agent Redis The server sends the request receive data

 Please add a picture description

shortcoming : There are many servers Inconvenient maintenance

No center :

Request to call the cluster , Automatically transfer to the target server

3 Lord 3 from

 Please add a picture description

Redis The cluster has realized to Redis Horizontal expansion of , Start now N individual redis node , The entire database is distributed and stored here N A node in the , Each node stores the total data 1/N.

Redis Clusters are partitioned (partition) To provide a certain degree of usability (availability): Even if some nodes in the cluster fail or fail to communicate , The cluster can also continue to process command requests .

To configure

Turn on daemonize yes

Pid File name

Designated port

Log File name

Dump.rdb name

Appendonly Turn it off or change the name

In profile :

cluster-enabled yes Open cluster mode

cluster-config-file nodes-6379.conf Set the node profile name

cluster-node-timeout 15000 Set the node loss time , Beyond that time ( millisecond ), The cluster automatically switches between master and slave .

With 6 For example 6 Ports simulation 6 Servers

3678 Port of , Other ports are the same as the following, just change the number

 Please add a picture description

start-up 6 individual redis Before service composition , Please make sure that all redis After the instance is started ,nodes-xxxx.conf All files are generated normally

The new version (6.2) There is no need to install ruby Environmental Science

Go to start installation redis Of src Next

 Please add a picture description

Execute... Under this path

redis-cli --cluster create --cluster-replicas 1 192.168.200.130:6379 192.168.200.130:6380 192.168.200.130:6381 192.168.200.130:6389 192.168.200.130:6390 192.168.200.130:6391

6 A cluster of nodes

  • –cluste Indicates cluster operation
  • –cluster-replicas The way of clustering 1 It means the simplest way to configure the cluster , A mainframe , One slave , At present, there are exactly three groups .2 Namely A mainframe , Two slaves

Don't use it here 127.0.0.1, Please use real IP Address

 Please add a picture description

Master and slave have been assigned . Accept or not

[OK] All 16384 slots covered.

redis-cli -c -p 6379 -c Use cluster policy to connect , The setting data will be automatically switched to the corresponding write host

cluster nodes View cluster information
 Please add a picture description

What is? slots

Cluster building completed There's a hint

 Please add a picture description

One Redis The cluster contains 16384 Slots (hash slot), Every key in the database belongs here 16384 One of the slots ,

The cluster uses the formula CRC16(key) % 16384 To calculate the key key Which slot does it belong to , among CRC16(key) Statements are used to evaluate keys key Of CRC16 The checksum .

Each node in the cluster is responsible for processing a portion of the slots . for instance , If a cluster can have a master node , among :

node A Responsible for handling 0 No. to 5460 Slot number .

node B Responsible for handling 5461 No. to 10922 Slot number .set

node C Responsible for handling 10923 No. to 16383 Slot number .

Not in one slot Key values under , Can't use mget,mset Wait for multi key operation .

Multiple values added Braces are required . Can pass {} To define the concept of a group , So that key in {} Key value pairs with the same content in a slot In the middle .

mset <key1><value1><key2><value2>

 mset name{user} aa age{user} 20 

CLUSTER keyslot X Calculation x Slot value for

CLUSTER countkeysinslot <slot> Calculate the number of keys in this card slot ( Only those within the corresponding range of the current library , If it's another library , Returns the 0)

CLUSTER GETKEYSINSLOT <slot><count> return count individual slot Key in groove .

 Please add a picture description

Fault recovery

  • If the master node goes offline G 了 The slave node is automatically promoted to the master node Be careful :15 Second timeout

  • When the master node comes back, it will become a slave .

  • If the master and slave of a certain slot (6 Hang up 2 individual ) All hang up , and cluster-require-full-coverage by yes , that , The whole cluster is down

    If the master and slave of a certain slot hang up , and cluster-require-full-coverage by no , that , None of the slot data is available , Can't store .

Jedis Related operations of

Even if the host is not connected , The cluster automatically switches host storage . Host write , Read from machine .

Decentralized master-slave cluster . No matter which host the data is written from , Data can be read on other hosts .

public class JedisClusterTest {
    
  public static void main(String[] args) {
    
// Set<HostAndPort> set =new HashSet<HostAndPort>();
// //HostAndPort  Any one will do   Each can be used as the entrance of the cluster 
// set.add();
     JedisCluster jedisCluster=new JedisCluster(new HostAndPort("192.168.200.130",6379)); // Pass in set Or a HostAndPort  Fine 
     jedisCluster.set("bbb1", "v111");
     System.out.println(jedisCluster.get("bbb1"));
  }
}

Advantages and disadvantages

Redis Clustering provides the following benefits

  • Realize capacity expansion

  • Apportionment pressure

  • No central configuration is relatively simple

Redis The lack of clustering

  • Multi key operation is not supported

  • Multibond Redis Transactions are not supported .lua Scripts are not supported

  • Due to the late emergence of the cluster scheme , Many companies have adopted other clustering schemes , The proxy or client partition scheme wants to migrate to redis cluster, Overall migration is required rather than gradual transition , High complexity .

原网站

版权声明
本文为[beginnerDZ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/189/202207072320416530.html

随机推荐