Chapter III Redis message queue

The third chapter · Redis message queue

A. Production and consumption model

1. What is the message queue?

In life, there are many examples, are similar to the message queue.

For example: the factory produced bread to supermarkets, shopping malls to sell customers to buy bread by supermarkets, shopping malls, customers will not choose to go for a particular plant, just buy it from the supermarket, the factory also will not control what a customer buy bread, just after produced, to the supermarkets, shopping malls to deal with.

Message Queue (Message Queue) is a communication between an application, you can return immediately after the message is sent, the message system to ensure reliable mail message, the message producer just released the news to the MQ regardless of who is going to take, news consumption who just take messages from the MQ whoever posted, so publishers and users do not know about each other.

2. Why do you want to use the message queue?

First, we can know, is an asynchronous message queue mechanism, such as log collection system, in order to avoid data loss during transmission, as well as ordering system, after an order is generated corresponding documents, inventory deductions transmission of consumer information, a single, much message is generated, is operated by a trigger, then the other message into the message queue, generated sequentially. Then there are many sites, spike activity and the like, before the number of users will be cheaper, are achieved through a message queue.

Examples of these are through the message queue, implemented decoupling operations, the final consistency of the data, broadcast, and so staggering the flow control, thereby completing the service logic.

3. Message Queue product

1) rabbit-MQ (originated in the financial system, distributed system for store and forward message .OpenStack)
2) the MQ-Zero (SaltStack)
. 3) Kafka (the JAVA)
. 4) Redis (Key: value database, cache, message queue)

Two .Redis released two modes message

1. Task Queue Mode (Queuing)

Task Queue: As the name suggests, is the "message delivery queue." Entities that interact with the task queue there are two types, one is the producer (producer), the other is the consumer (consumer). Producers will need to handle the task into the task queue, and consumers continue to read the task information from the task independent and executed.

Benefits task queue
1) loosely coupled.
Producers and consumers simply follow the agreed task description format, were written in code.
2) easy to expand.
Multi-consumer model, consumers may be distributed in a plurality of different servers, thereby reducing the load of a single server.

2. Publish - subscribe model (publish-subscribe)

In fact, from the Pub / Sub mechanism of view, it is more like a broadcast system, multiple subscribers (Subscriber) can subscribe to multiple channels (Channel), multiple publishers (Publisher) can go in multiple channels (Channel) make an announcement.
可以这么简单的理解: 1)Subscriber:收音机,可以收到多个频道,并以队列方式显示 2)Publisher:电台,可以往不同的FM频道中发消息 3)Channel:不同频率的FM频道

3. The more a publisher subscriber model

As shown below, it can be used as a message or a message queue pipeline.
Main applications: notice, announcement

4. Multiple publishers a subscriber model

PubSub may be made independent HTTP interface, the application transmits a respective Publisher to Channel message, executes the corresponding service logic Subscriber terminal after receiving the message, such as writing a database, display and the like.
Main applications: list, vote counting.

5. Multiple publishers more subscribers models

Named Incredibles, that can send messages to different Channel is received by a different Subscriber.
Main applications: group chat, chat.

6.Redis publish subscribe practice

1) PUBLISH channel msg
send information message to the specified channel channel

2) SUBSCRIBE channel [channel ...]
subscribe to channels, you can subscribe to multiple channels at the same time

3) UNSUBSCRIBE [channel ...]
unsubscribe specified channel, if you do not specify a channel, all channels will unsubscribe

4) PSUBSCRIBE pattern [pattern ...]
subscribe to one or more channels match a given pattern, each pattern as to match the symbol *, * matches all such channels (it.news, it.blog beginning to it, it.tweets etc.), news. * matches all news. at the beginning of the channel (news.it, news.global.today etc.), and so on

5) PUNSUBSCRIBE [pattern [pattern ... ]]
unsubscribe specified rules, if no parameters will unsubscribe all rules

6) PUBSUB subcommand [argument [argument ...]]
View subscription and publishing system status

Note: Use publish message queue subscription model to achieve, when the client has subscribed channel you can only receive follow-up release to the news channels, not the cache before sending must Provider and Consumer simultaneously online.

Subscribe to a single channel

#第一个窗口
#登录Redis
[root@db01 ~]# redis-cli -a 123
#在订阅者的服务器上输入订阅zls
127.0.0.1:6379> SUBSCRIBE zls Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "zls" 3) (integer) 1 #第二个窗口 #登录Redis [root@db01 ~]# redis-cli -a 123 #在发布者的服务器上输入信息 127.0.0.1:6379> PUBLISH zls "The Nice Boy Like Me." (integer) 1 #第一个窗口 #在订阅者的服务器上会看到如下信息 1) "message" 2) "zls" 3) "The Nice Boy Like Me."

The results are as follows:

Subscribe to multiple channels

#第一个窗口
#登录Redis
[root@db01 ~]# redis-cli -a 123
#在订阅者服务器上输入订阅所有频道
127.0.0.1:6379> PSUBSCRIBE * Reading messages... (press Ctrl-C to quit) 1) "psubscribe" 2) "*" 3) (integer) 1 #第二个窗口 #在发布者服务器上输入多频道信息 127.0.0.1:6379> PUBLISH zls "The Nice Boy Like Me." (integer) 1 127.0.0.1:6379> PUBLISH bgx "The Ugly Old Man Like Me." (integer) 1 #第一个窗口 #在订阅者的服务器上会看到如下信息 1) "pmessage" 2) "*" 3) "zls" 4) "The Nice Boy Like Me." 1) "pmessage" 2) "*" 3) "bgx" 4) "The Ugly Old Man Like Me."

The results are as follows:

7. Comparative queue messaging system

After executing client subscription command to enter the subscription status can only receive SUBSCRIBE, PSUBSCRIBE, UNSUBSCRIBE, PUNSUBSCRIBE four command. Open subscription client can not receive messages prior to the channel, because Redis news release will not be persistent. Many professional and message queuing systems (such as Kafka, RocketMQ, RabbitMQ) compared to, Redis publish-subscribe slightly rough.

For example: you can not achieve the accumulation of messages and backtracking. But wins in simple enough, these shortcomings can be tolerated if the current scenario, would be a good choice.

A. Production and consumption model

1. What is the message queue?

In life, there are many examples, are similar to the message queue.

For example: the factory produced bread to supermarkets, shopping malls to sell customers to buy bread by supermarkets, shopping malls, customers will not choose to go for a particular plant, just buy it from the supermarket, the factory also will not control what a customer buy bread, just after produced, to the supermarkets, shopping malls to deal with.

Message Queue (Message Queue) is a communication between an application, you can return immediately after the message is sent, the message system to ensure reliable mail message, the message producer just released the news to the MQ regardless of who is going to take, news consumption who just take messages from the MQ whoever posted, so publishers and users do not know about each other.

2. Why do you want to use the message queue?

First, we can know, is an asynchronous message queue mechanism, such as log collection system, in order to avoid data loss during transmission, as well as ordering system, after an order is generated corresponding documents, inventory deductions transmission of consumer information, a single, much message is generated, is operated by a trigger, then the other message into the message queue, generated sequentially. Then there are many sites, spike activity and the like, before the number of users will be cheaper, are achieved through a message queue.

Examples of these are through the message queue, implemented decoupling operations, the final consistency of the data, broadcast, and so staggering the flow control, thereby completing the service logic.

3. Message Queue product

1) rabbit-MQ (originated in the financial system, distributed system for store and forward message .OpenStack)
2) the MQ-Zero (SaltStack)
. 3) Kafka (the JAVA)
. 4) Redis (Key: value database, cache, message queue)

Two .Redis released two modes message

1. Task Queue Mode (Queuing)

Task Queue: As the name suggests, is the "message delivery queue." Entities that interact with the task queue there are two types, one is the producer (producer), the other is the consumer (consumer). Producers will need to handle the task into the task queue, and consumers continue to read the task information from the task independent and executed.

Benefits task queue
1) loosely coupled.
Producers and consumers simply follow the agreed task description format, were written in code.
2) easy to expand.
Multi-consumer model, consumers may be distributed in a plurality of different servers, thereby reducing the load of a single server.

2. Publish - subscribe model (publish-subscribe)

In fact, from the Pub / Sub mechanism of view, it is more like a broadcast system, multiple subscribers (Subscriber) can subscribe to multiple channels (Channel), multiple publishers (Publisher) can go in multiple channels (Channel) make an announcement.
可以这么简单的理解: 1)Subscriber:收音机,可以收到多个频道,并以队列方式显示 2)Publisher:电台,可以往不同的FM频道中发消息 3)Channel:不同频率的FM频道

3. The more a publisher subscriber model

As shown below, it can be used as a message or a message queue pipeline.
Main applications: notice, announcement

4. Multiple publishers a subscriber model

PubSub may be made independent HTTP interface, the application transmits a respective Publisher to Channel message, executes the corresponding service logic Subscriber terminal after receiving the message, such as writing a database, display and the like.
Main applications: list, vote counting.

5. Multiple publishers more subscribers models

Named Incredibles, that can send messages to different Channel is received by a different Subscriber.
Main applications: group chat, chat.

6.Redis publish subscribe practice

1) PUBLISH channel msg
send information message to the specified channel channel

2) SUBSCRIBE channel [channel ...]
subscribe to channels, you can subscribe to multiple channels at the same time

3) UNSUBSCRIBE [channel ...]
unsubscribe specified channel, if you do not specify a channel, all channels will unsubscribe

4) PSUBSCRIBE pattern [pattern ...]
subscribe to one or more channels match a given pattern, each pattern as to match the symbol *, * matches all such channels (it.news, it.blog beginning to it, it.tweets etc.), news. * matches all news. at the beginning of the channel (news.it, news.global.today etc.), and so on

5) PUNSUBSCRIBE [pattern [pattern ... ]]
unsubscribe specified rules, if no parameters will unsubscribe all rules

6) PUBSUB subcommand [argument [argument ...]]
View subscription and publishing system status

Note: Use publish message queue subscription model to achieve, when the client has subscribed channel you can only receive follow-up release to the news channels, not the cache before sending must Provider and Consumer simultaneously online.

Subscribe to a single channel

#第一个窗口
#登录Redis
[root@db01 ~]# redis-cli -a 123
#在订阅者的服务器上输入订阅zls
127.0.0.1:6379> SUBSCRIBE zls Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "zls" 3) (integer) 1 #第二个窗口 #登录Redis [root@db01 ~]# redis-cli -a 123 #在发布者的服务器上输入信息 127.0.0.1:6379> PUBLISH zls "The Nice Boy Like Me." (integer) 1 #第一个窗口 #在订阅者的服务器上会看到如下信息 1) "message" 2) "zls" 3) "The Nice Boy Like Me."

The results are as follows:

Subscribe to multiple channels

#第一个窗口
#登录Redis
[root@db01 ~]# redis-cli -a 123
#在订阅者服务器上输入订阅所有频道
127.0.0.1:6379> PSUBSCRIBE * Reading messages... (press Ctrl-C to quit) 1) "psubscribe" 2) "*" 3) (integer) 1 #第二个窗口 #在发布者服务器上输入多频道信息 127.0.0.1:6379> PUBLISH zls "The Nice Boy Like Me." (integer) 1 127.0.0.1:6379> PUBLISH bgx "The Ugly Old Man Like Me." (integer) 1 #第一个窗口 #在订阅者的服务器上会看到如下信息 1) "pmessage" 2) "*" 3) "zls" 4) "The Nice Boy Like Me." 1) "pmessage" 2) "*" 3) "bgx" 4) "The Ugly Old Man Like Me."

The results are as follows:

7. Comparative queue messaging system

After executing client subscription command to enter the subscription status can only receive SUBSCRIBE, PSUBSCRIBE, UNSUBSCRIBE, PUNSUBSCRIBE four command. Open subscription client can not receive messages prior to the channel, because Redis news release will not be persistent. Many professional and message queuing systems (such as Kafka, RocketMQ, RabbitMQ) compared to, Redis publish-subscribe slightly rough.

For example: you can not achieve the accumulation of messages and backtracking. But wins in simple enough, these shortcomings can be tolerated if the current scenario, would be a good choice.

Guess you like

Origin www.cnblogs.com/ronglianbing/p/11904899.html