Redis (9), redis Cluster use of redis cluster

redis technical catalog

 

Premise: redis cluster sharding, currently there are two types of redis solutions

  • Hash slot (hash slot), representing the scheme: redis cluster
  • Consistent hash, representative scheme: twemproxy, codis
  This article is for redis cluster configuration implementation, the principle will be analyzed in another article. By haoran-10.iteye.com

 

 
First understand a few key words:
Node: When a single redis is running, it is isolated. Once a failure occurs, the data will not be lost when the master-slave replication occurs at most, but the redis is unavailable. Redis stand-alone, one node.
Cluster: Many nodes are connected by some scheme to provide redis services together, which is called redis cluster. When a single node fails, the data will be transferred (no strong consistency is guaranteed) to a safe backup node to provide highly available redis services. The disadvantage is that commands that do not support processing multiple keys cannot be used.
Sharding: When there is a lot of redis data, a single redis cannot fit. At this time, the redis cluster distributes the data evenly to different nodes through the sharding technology.
Hash slot: Redis cluster distributes data to different nodes . Redis Cluster has 16384 hash slots, we just use the key's CRC16 encoding to modulo 16384 to calculate the hash slot to which a given key belongs. 
 
 
1. Key configuration of redis node
daemonize yes
pidfile /mytest/redis_dev/9001/redis.pid
port 9001
appendonly yes
appendfsync everysec
cluster-enabled yes
cluster-config-file nodes.conf  
cluster-node-timeout 15000
 
 For the first configuration, it is strongly recommended to use 6 nodes, so that the configuration and advantages of the redis cluster can be more intuitively understood.
 Copy into 6 points, 9001, 9002, 9003, 9004, 9005, 9006 (note to change to different ports, different pid files, ignore nodes.conf, redis will automatically create and manage)
2. Start each node


 
3. Use the redis-trib tool to create a cluster 
After the startup is successful, use the tools that come with redis to create a cluster
Note, here to install tools such as ruby
(2), install ruby ​​gem
(3), install gem redis
The installation process is relatively simple, skip it.


 
 Type "yes" to continue

 
Hash slots have been allocated to 9001, 9002, 9003 nodes. 
4. Test with redis-cli

 
Note: here is to use the src/redis-cli -c command to start the client cluster operation mode
Get operational tests:


 
As can be seen from the data, the client enters from port 9002. When set age 27 is entered, it is automatically transferred to node 9001.
When entering get age, data is also obtained from node 9001
 
5. Fault-tolerant processing test
Manually kill the 9001 node

 

 

 
Continue to operate the redis cluster at this point

 
Automatically obtain data from the original 9001 slave node 9004 node (9001 is the master node, 9004 is the slave node of 9001), indicating that 9004 automatically becomes the master node
 
 
6. Add node test
Create 9007 node, start 9007
 
And set the 9007 node as the slave node of 9004
src/redis-trib.rb add-node --slave 127.0.0.1:9007 127.0.0.1:9004
 
From the output, you can see that 9007 is a replica node of 9004, that is, the slave node.
 
 
7. Add a master node and re-shard the cluster
redis-trib.rb reshard 127.0.0.1:9008
 Mainly to reallocate the hash slot 
 
8. java client 
It seems that only sharedjedis can be used. Spring-data-redis does not support the new cluster features of redis. I don't know if the posture is wrong.
package wang.conge.init;

import java.util.HashSet;
import java.util.Set;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

public class SharedRedisClient {
	public static void main(String[] args) {
		Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();  
        jedisClusterNodes.add(new HostAndPort("127.0.0.1", 9001));  
        
        JedisCluster jedis = new JedisCluster(jedisClusterNodes);
        
        jedis.set("nginx", "yes");
		jedis.set("tomcat", "yes");
		jedis.set("keepalived", "yes");
		jedis.set("vip", "yes");
		jedis.set("redis", "yes");
		jedis.set("mysql", "yes");
		
		String key = jedis.get("redis");
		System.out.println(key);
	}
}
 
9. Summary
The configuration of the cluster solution officially provided by redis is too cumbersome, but the performance is relatively strong. The company has not yet used it in production, so we will wait and see.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326270200&siteId=291194637