Prepare the environment

IP server Redis version Whether the master-slave
10.29.190.24 4.0.8 Yes/Primary node
10.28.36.205  4.0.8 Yes/slave node

As the environment is above, two Redis have been built and master/slave synchronization has been realized.

I. Whether the publish and subscribe function can be used normally under master/slave synchronization

1. Start five Redis clients, including two primary nodes and three secondary nodes

Primary node 2 subscribes to the ConfigServer channel

Primary node 3 subscribes to the ConfigServer channel

127.0.0.1:6379> subscribe configserver

Reading messages… (press Ctrl-C to quit)\

  1. “subscribe”\
  2. “configserver”\
  3. (integer) 1

Subscribe to the ConfigServer channel from node 1

Subscribe to the ConfigServer channel from node 2

Subscribe to the ConfigServer channel from node 3

127.0.0.1:6379> subscribe configserver

Reading messages… (press Ctrl-C to quit)\

  1. “subscribe”\
  2. “configserver”\
  3. (integer) 1

2. As soon as the master node sends a message, test whether other nodes can receive the subscription

The primary node issues a “Test Subscribe” message

127.0.0.1:6379> publish configserver “test subscribe”

(integer) 2

The master node 2The value is printed on primary node 3

  1. “message”\
  2. “configserver”\
  3. “test subscribe”

The message is received normally. Procedure

From the node 1From the node 2From the node 3 print

  1. “message”\
  2. “configserver”\
  3. “test subscribe”

The message is received normally. Procedure

Note Master/slave synchronization also supports publication and subscription. Specific performance needs further testing.

Publish and subscribe performance test

Create 100 threads and subscribe to test2

public class RedisSubScribeTask implements Runnable { private String Name; public RedisSubScribeTask(String name) { Name = name; } @Override public void run() { RedisDaoImpl redis = new RedisDaoImpl(); redis.init(); Jedis jedis = redis.pool.getResource(); if (jedis ! = null) { RedisMsgSubListener redisMsgSubListener = new RedisMsgSubListener(); System.out.println(" thread "+ Name +" start "); jedis.subscribe(redisMsgSubListener, "test2"); } } } public static void main(String[] args) { for (int i = 0; i <= 100; i++) { RedisSubScribeTask redisSubScribeTask = new RedisSubScribeTask(String.valueOf(i)); new Thread(redisSubScribeTask).start(); }}Copy the code

public class RedisMsgSubListener extends JedisPubSub { public void onMessage(String channel, String message) {system.out.println (channel + "is:" + message); } public void onPMessage(String pattern, String channel, String message) { } public void onSubscribe(String channel, int subscribedChannels) { } public void onUnsubscribe(String channel, int subscribedChannels) { } public void onPUnsubscribe(String pattern, int subscribedChannels) { } }Copy the code

2. Define the main method to publish the messageCopy the code

public static void main(String[] args) {
    RedisDaoImpl redis = new RedisDaoImpl();
    redis.init();
    redis.pool.getResource().publish("test2", "test");
}
Copy the code

3, the test result is seconds back

4. Change the number of threads to 300, and the test results are also returned in seconds

5. Increase the number of connections to 500 without delay

` ` `
info clients # Clients connected_clients:500 client_longest_output_list:0 client_biggest_input_buf:0 blocked_clients:0 127.0.0.1:6379 >
` ` `

6. The number of connections is increased to 1000, and the other two are synchronized from master to slave. See if there’s a delay

` ` `
info clients # Clients connected_clients:1002 client_longest_output_list:0 client_biggest_input_buf:0 blocked_clients:0
` ` `

7. There is still no delay. And the message from the master-slave synchronous subscription test2 is also accepted.

127.0.0.1:6379> subscribe test2

Reading messages… (press Ctrl-C to quit)\

  1. “subscribe”\
  2. “test2″\
  3. (integer) 1\
  4. “message”\
  5. “test2″\
  6. “test”\
  7. “message”\
  8. “test2″\
  9. “test”

8. Publish 100 messages in a row for 1000 clients

` ` `
for (int i = 0; i <= 100; i++) { redis.pool.getResource().publish(“test2”, “test” + i); // try { // Thread.sleep(1000); // } catch (InterruptedException e) { // e.printStackTrace(); / /}}
` ` `

It takes about 2s to complete printing from the node, and the performance is ok.

  1. “message”\
  2. “test2″\
  3. “test99″\
  4. “message”\
  5. “test2″\
  6. “test100”  

8. According to the current deployment of redis in three zones, each zone has about 60 virtual machine links. Such performance is enough to meet our business scenarios.

Basic can achieve millisecond level configuration update.