In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Rabbit MQ message queuing
@ [toc]
Brief introduction benefit
Erlang development, strong concurrency ability.
The community is active, the users are many, and the stability is strong.
Low latency
Shortcoming
Erlang language development, domestic proficiency is not many, customized development is difficult in the future.
Rabbit MQ working mode 1, "Hello World!" Pattern
The simple model is the simplest case of getting started with Rabbit MQ, one producer and one consumer. There is no need to declare the switch (there is actually a default switch). After declaring a queue, the producer sends it here, and the consumer listens to the queue to respond to the message.
Application scenario: such as a chat between two users.
2Jing work queues mode
The work queue pattern distributes tasks among workers (competitive consumer model), which is a simple mode with multiple consumers listening to the queue together, and Rabbit MQ ensures that only one message in the queue is consumed by one consumer.
Application scenario: for example, multiple users scramble for an order (if you compete for the same resource in high concurrency, remember to consider locking)
3PercentPublishPlacement subscribe mode
The publish / subscribe mode sends messages to many consumers at a time, or one producer and multiple consumers, but the mode needs to display and declare that the switch binds the created queues to the switch. the producer sends the message to the switch, and the switch sends the message to the bound queue, and the consumer of the corresponding queue consumes the information in the queue.
Application scenario: send an email in a group and push an advertisement
4Jing routing mode
Whether the route pattern selectively receives the message, or displays the declaration switch, binds the queue with the switch, sends the message to the switch by the producer, sends the message to the corresponding queue by the switch, and the corresponding consumer consumption information. However, a routingkey needs to be specified when the queue is bound to the switch, so that when the producer sends a message to the producer, it will judge the specific routingkey value and send the message that meets the corresponding routingkey value to the corresponding queue.
Application scenarios: encapsulate several types of error type notifications
5Jing Topics model
The topic pattern receives messages according to the pattern (topic), just like the routing pattern, except that it is no longer judged by the specific routingkey value, but is fuzzily matched according to the routingkey value. Among them, the asterisk represents multiple words, and the pound sign represents one word.
For more detailed information, please visit the official website of RabbitMQ
Switch mode
Fanout mode: does not deal with routing keys, that is, there is no routingkey value, much like subnet broadcasting, hosts in each subnet get a copy of the message.
Direct mode: handle the routing key, which needs to match the routing key before it can be forwarded, that is, it exactly matches the routingkey value.
Topic mode: handle routing keys, which can only be forwarded by fuzzy matching of routing keys, that is, the routingkey value on fuzzy matching.
Introducing RabbitMQ queues
RabbitMQ relies on erlang language. Click the download link on the official website to download erlang.
Click the download link on the official website to download RabbitMQ.
When installing both, you can set them all the way by default.
Start the RabbitMQ service, enter it in cmd mode, and enter the default installation path
C:\ Program Files (x86)\ RabbitMQ Server\ rabbitmq_server-3.6.10\ sbin
Enter a command
Rabbitmq-plugins enable rabbitmq_management
The service started successfully, as shown in the figure
Click http://localhost:15672/, as shown in the following figure
New in pom.xml
Org.springframework.boot spring-boot-starter-amqp
Add to application.properties
# initialization of RabbitMQspring.rabbitmq.host=127.0.0.1spring.rabbitmq.port=5672spring.rabbitmq.username=guestspring.rabbitmq.password=guest# concurrent consumers spring.rabbitmq.listener.concurrency=10# maximum of concurrent consumers spring.rabbitmq.listener.max-concurrency=20# number of messages that each consumer can pull and process each time they monitor spring.rabbitmq.listener.prefetch=5 code
Simple mode and work queue mode do not specify switches, the subtleties of rabbitMQ can not be reflected, while route patterns and principal patterns have routingkey values, so choose one. This paper takes publish / subscribe mode and route pattern as examples, and the rest can be compared. Not fully written, because this is a quick to use the series, the shortest time and energy to understand the context, is the purpose of the series, human beings are afraid of obscure and lengthy, like short and direct.
The directory after the new code is as follows
RabbitConfig.java
Package com.example.config;import org.springframework.amqp.core.*;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class RabbitConfig {/ / = publish / subscribe mode = / / queue A @ Bean () public Queue queueA () {return new Queue ("fanoutQueueA");} / / queue B @ Bean () public Queue queueB () {return new Queue ("fanoutQueueB") } / / fanout switch in publish-subscribe mode @ Bean public FanoutExchange fanoutExchange () {return new FanoutExchange ("fanoutExchange");} / / bind queue A to fanout switch @ Bean public Binding bindingWithQueueA () {return BindingBuilder.bind (queueA ()) .to (fanoutExchange ()) } / / bind queue B to fanout switch @ Bean public Binding bindingWithQueueB () {return BindingBuilder.bind (queueB ()) .to (fanoutExchange ());} / / = Route pattern = / / queue C @ Bean () public Queue queueC () {return new Queue ("directQueueC") } / / queue D @ Bean () public Queue queueD () {return new Queue ("directQueueD");} / / direct switch @ Bean public DirectExchange directExchange () {return new DirectExchange ("directExchange") in publish / subscribe mode / / bind queue C to direct switch @ Bean public Binding bindingWithQueueC () {return BindingBuilder.bind (queueC ()) .to (directExchange ()) .with ("directRoutingkey");} / / bind queue B to direct switch @ Bean public Binding bindingWithQueueD () {return BindingBuilder.bind (queueD ()) .to (directExchange ()) .with ("directRoutingkey");}}
RabbitController.java
Package com.example.controller;import com.example.service.IRabbitProducerService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping ("rabbit") public class RabbitController {@ Autowired private IRabbitProducerService rabbitProducerService; @ RequestMapping ("/ fanout") public void fanout () {for (int I = 0; I < 5) ITunes +) {this.rabbitProducerService.producerFanout ("publish / subscribe" message ");}} @ RequestMapping (" / direct ") public void direct () {for (int I = 0; I < 5; iTunes +) {this.rabbitProducerService.producerDirect (" message in routing mode "+ i+");}
IRabbitProducerService.java
Package com.example.service;public interface IRabbitProducerService {void producerFanout (String message); void producerDirect (String message);}
RabbitProducerServiceIml.java
Package com.example.service;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Service ("rabbitProducerService") public class RabbitProducerServiceIml implements IRabbitProducerService {@ Autowired RabbitTemplate rabbitTemplate; public void producerFanout (String message) {rabbitTemplate.convertAndSend ("fanoutExchange", null, message);} public void producerDirect (String message) {rabbitTemplate.convertAndSend ("directExchange", "directRoutingkey", message);}}
IRabbitConsumerService.java
Package com.example.service;public interface IRabbitConsumerService {void consumerFanoutA (String message); void consumerFanoutB (String message); void consumerDirectC (String message); void consumerDirectD (String message);}
RabbitConsumerServiceIml.java
Package com.example.service;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Component;@Componentpublic class RabbitConsumerServiceIml implements IRabbitConsumerService {@ RabbitListener (queues = "fanoutQueueA") public void consumerFanoutA (String message) {System.out.println ("Consumer receives message in queue A:" + message) } @ RabbitListener (queues = "fanoutQueueB") public void consumerFanoutB (String message) {System.out.println ("consumers receive messages in queue B:" + message);} @ RabbitListener (queues = "routingQueueC") public void consumerDirectC (String message) {System.out.println ("consumers receive messages in queue C:" + message) } @ RabbitListener (queues = "routingQueueD") public void consumerDirectD (String message) {System.out.println ("consumers receive messages in queue D:" + message);}}
After starting the project, the browser enters http://localhost:8080/rabbit/fanout, (although the foreground will report an error, because this request only triggers the production of the message, and there is no corresponding interface display. The produced message is sent to the queue through the switch, and the consumer listens to the queue and responds accordingly. ) the effect of using fanout switch to produce and consume messages in publish / subscribe mode is as follows
Enter http://localhost:8080/rabbit/direct in the browser, and the effect of using direct switch to produce consumption messages in routing mode is shown below.
Note: if you are based on this article series, remember to assign this address to the logged-in user because spring security is configured at the beginning. Or open a Super Admin account that can access any directory of the project and use the administrator account to access these addresses.
At this point, the space is already very long, and I was going to finish writing the dead letter queue and the message confirmation mechanism. Considering that this is a quick integration series, put the in-depth content into the pit-in-depth series and look forward to opening up a new series of articles.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.