Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement publish and subscribe in redis

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to implement publish and subscribe in redis? in view of this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

1. Whether the publish and subscribe function can be used normally under master-slave synchronization

1. Open five redis clients, including two master nodes and three slave nodes

Master Node 2 subscribes to configserver Channel

Master Node 3 subscribes to configserver Channel

127.0.0.1 purl 6379 > subscribe configserver

Reading messages... (press Ctrl-C to quit)

1) "subscribe"

2) "configserver"

3) (integer) 1

Subscribe to configserver channels from Node 1

Subscribe to configserver channels from Node 2

Subscribe to configserver channels from node 3

127.0.0.1 purl 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, it tests whether other nodes can receive subscriptions

The master node issues a "test subscribe" message

127.0.1 6379 > publish configserver "test subscribe"

(integer) 2

Master Node 2 Master Node 3 print

1) "message"

2) "configserver"

3) "test subscribe"

Message received normally

Print from Node 1 from Node 2 to Node 3

1) "message"

2) "configserver"

3) "test subscribe"

Message received normally

It shows that master-slave synchronization can also support publish and subscribe functions. The specific performance needs to be further tested.

Second, publish and subscribe performance test

1. 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)

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

Info clients# Clientsconnected_clients:1002client_longest_output_list:0client_biggest_input_buf:0blocked_clients:0

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

127.0.0.1 purl 6379 > subscribe test2

Reading messages... (press Ctrl-C to quit)

1) "subscribe"

2) "test2"

3) (integer) 1

1) "message"

2) "test2"

3) "test"

1) "message"

2) "test2"

3) "test"

8. Release 1000 messages for 100 clients in a row

For (int I = 0; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report