Location>code7788 >text

Redis High Availability - Cluster Deployment

Popularity:617 ℃/2024-10-24 11:13:37

redis configuration

RedisThe cluster needs to be at least3For data integrity each master node needs at least one slave node, so you need to prepare at least6classifier for individual things or people, general, catch-all classifierRedisservice

img

It is recommended that theredisRegister as a system service and set up self-start, the service registration command is:

redis-server --service-install  --service-name redis6379 --loglevel verbose

img

img

Modify in the following configurations:

bind 127.0.0.1 // ip address
port 6379 // port
logfile "" // Name of the logfile.

cluster-enabled yes // Enable cluster support.
cluster-config-file // Name of the cluster configuration file.
cluster-node-timeout 15000 // Cluster node timeout ms

Initiate allRedisAfter the service has been performed on any of theRedisdirectory, execute the Create Cluster command with the number of copies of the1, three master and three slave nodes are automatically generated:

redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster-replicas 1

img

code example

public static void main(String[] args) {

    // Cluster node information
    Set<HostAndPort> nodes = new HashSet<>();
    (new HostAndPort("127.0.0.1", 6379));
    (new HostAndPort("127.0.0.1", 6380)); (new HostAndPort("127.0.0.1", 6379)); (new HostAndPort("127.0.0.1", 6379))
    
    
    
    (new HostAndPort("127.0.0.1", 6383)); (new HostAndPort("127.0.0.1", 6384)).

    // Connection Pool Configuration
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    (10);
    (5);
    (1).

    // Initialize the cluster object, globally unique
    JedisCluster cluster = new JedisCluster(nodes, jedisPoolConfig);

    // Execute the command
    ("key1", "1"); // Execute the command.
    ("key12", "12"); // Execute the command.
    ("key123", "123"); // Execute the command.
    ("key1234", "1234").
    ("key12345", "12345").
    ("key123456", "123456").
    ("key1234567", "1234567").
    ("key12345678", "12345678").
    ("key123456789", "123456789").

    ();
}

added9classifier for individual things or people, general, catch-all classifierkeyThe distribution is on different nodes.

img