What is a Redis

Redis is an open source in-memory database written in ANSI C language. It stores data in the form of key-value pairs. It provides high performance, fast reading speed, and persistent storage mechanism.

The most common feature Redis uses in projects is caching, but today we will introduce another feature of Redis, a lightweight message queue.

Redis publishes subscriptions

mechanism

Redis provides a publish/subscribe function that can be used for the transmission of messages. Redis’s publish/subscribe mechanism consists of three parts, publisher, subscriber and Channel (called Channel or topic).

A publisher publishes a message to a specific channel, and subscribers to that channel receive the message, or if there are multiple subscribers, they receive the same message.

Function implementation

news

Redis sends a message using a PUBLISH command and returns the number of subscribers who have received the message.

Subscribe to the channel

Redis uses SUBSCRIBE command to SUBSCRIBE to a channel, its return value includes the client subscribed to the channel, the number of channels have been subscribed to, and received messages, where SUBSCRIBE means has been successfully subscribed to a channel.

Pattern matching

Pattern matching allows clients to subscribe to channels that match a certain pattern. Redis uses PSUBSCRIBE to all channels that match a certain pattern. “*” is used to represent the pattern, and “*” can be replaced by any value.

If the client subscribes to both a pattern and a channel that conforms to the pattern, the message sent to the channel will be received twice by the client, but the two messages are of the same type, one is of the message type, the other is of the pMessage type, but the content is the same.

unsubscribe

Redi s UNSUBSCRIBE using the UNSUBSCRIBE and PUNSUBSCRIBE commands. The return value is similar to that of a subscription. Since Redis subscribing is blocking, once a client subscribing to a channel or mode, it stays subscribed until it exits. In the SUBSCRIBE, PSUBSCRIBE, UNSUBSCRIBE and PUNSUBSCRIBE commands, the return value contains the number of channels and modes that the client is currently subscribed to. When this number becomes zero, the client will automatically exit the subscription status.

SpringBoot+Redis implements publish-and-subscribe

I won’t go into how Springboot integrates with Redis here, and I’m sure it will be no problem for you. Let’s go straight to the code

Message Listening Configuration

@Configuration public class RedisSubConfig { public static final String SUB_KEY = "chat"; // channel channel /** * Redis message listener container * you can add multiple Redis listeners that listen on different topics by binding the listener to the corresponding message subscription handler. The message listener * does some business by invoking the relevant methods of the message subscription handler via reflection technology * @param connectionFactory * @param listenerAdapter * @return */ @bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) { RedisMessageListenerContainer container = new RedisMessageListenerContainer();  container.setConnectionFactory(connectionFactory); / / subscribed to a channel container. AddMessageListener (listenerAdapter, new PatternTopic (RedisSubConfig. SUB_KEY)); return container; } /** * the message listener adapter is bound to the message handler, @param receiver * @return */ @bean MessageListenerAdapter listenerAdapter(RedisReceiver Receiver) {  return new MessageListenerAdapter(receiver, "receiveMessage"); } /** * redis reads the contents of the template * @param connectionFactory * @return */ @bean StringRedisTemplate template(RedisConnectionFactory connectionFactory) { return new StringRedisTemplate(connectionFactory); }}Copy the code

Receives the message

@service public class RedisReceiver {public void receiveMessage(String message) {system.out.println (" receiveMessage "); " + message); }}Copy the code

Use timers to publish messages

@Component public class MessageSender {@autowired private StringRedisTemplate stringRedisTemplate; @scheduled (fixedRate = 5000) // Post a message to the Redis message queue chat channel with a StringRedisTemplate object at an interval of 5s public void sendMessage(){ stringRedisTemplate.convertAndSend("chat", "hello "+ new Date()); }}Copy the code

The results

Compare the RabbitMQ publish-subscribe model

  • Redis: Lightweight, low latency, high concurrency, low reliability;
  • RabbitMQ: heavyweight, highly reliable, asynchronous, no real-time guarantee
  • RabbitMQ is a dedicated AMQP protocol queue. The advantage of RabbitMQ is that it provides reliable queuing services and can be asynchronous, while Redis is mainly used for caching. Redis’ publish-and-subscribe mode can be used for timely and unreliable functions.

Phase to recommend

The most detailed text parsing Java lock (ultimate)

Be sure to bookmark 5 excellent SpringCloud open source projects

Be sure to collect the front end framework of the 5 back end management systems

How much do you know about MySQL’s three major logs?

Elaborate description of 23 design patterns