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 use RabbitMQ message component in SpringBoot

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

Share

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

This article will explain in detail how to use RabbitMQ message components in SpringBoot. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

1 install rabbitmq in the virtual machine

2 enable rabbitmq

3. View rabbitmq on page

3 explanation of rabbitmq-related knowledge points [1]

MQ is called Message Queue, and message queuing (MQ) is a method of application-to-application communication. MQ is a typical representative of the consumer-producer model, in which one end continuously writes messages to the message queue, while the other end can read the messages in the queue.

1. Queues, producers, consumers

Queues: producers of information stored internally in mq: consumers who generate information: consumers who consume messages

2. Exchange, Binding exchange: exchangers. The exchange binds to the column according to certain rules, so that the message can be sent to the queue for Binding using routing key.

There are four kinds of Exchange Type

There are three kinds of Exchange Type commonly used in RabbitMQ: fanout, direct, and topic. Headers is not commonly used. Fanout: delivers all messages sent to this Exchange to all queues bound to it. To send a message to all the queues bound to the exchange, there is no need to compare the routing key direct: deliver the message to queues where the binding key matches the routing key exactly. Require the routing key to exactly match the column name topic: route the message to a queue where the binding key matches the routing key pattern. Match 0 or more with #, and match one with *

4. Simply implement the results of the following figure.

1 create a switch of type direct

2 create a switch of type fanout

3 create a switch of type topic

4 results created

5 create alignment. The following figure is an example

Queue creation completed

6 example of binding queue direct type

7 example of binding fanout type

8 example of binding topic type

9 Test switches of direct type

Test result

10 Test switches of fanout type

Test result

11 Test switches of topic type

5 use RabbitMq under the springBoot project

Pom.xml

4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.6.RELEASE com.maospring-01-amqp0.0.1-SNAPSHOTspring-01-amqpDemo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-amqp Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot Spring-boot-maven-plugin

Applicaton.properties configuration

Spring.rabbitmq.host=192.168.1.139

Spring.datasource.password=guest

Spring.datasource.username=guest

Book entity

Public class book {

Private String bookName;private String author;public book (String bookName, String author) {this.bookName = bookName; this.author = author;} public book () {} public String getAuthor () {return author;} public void setAuthor (String author) {this.author = author;} public String getBookName () {return bookName;} public void setBookName (String bookName) {this.bookName = bookName } [@ Override] (https://my.oschina.net/u/1162528)public String toString () {return "book {" + "bookName='" + bookName +'\'+ ", author='" + author +'\'+'}';}

}

Test send data code @ Test public void contextLoads () {

/ / message needs to construct one to define the message body content and header / / rabbitTemplate.send (exchange,roteKey,message); / / only need to pass in the object to be sent, automatically serialize and send it to rabbitmq, and send Map map= new HashMap () after the object is serialized by default; map.put ("msg", "this is the first message") Map.put ("data", Arrays.asList ("hello", "wored", "rabbitmq"); rabbitTemplate.convertAndSend ("exchange.direct", "mao.news", new book ("teenager", "genius")) } / * * broadcast method * * / [@ Test] (https://my.oschina.net/azibug)public void senMsg () {rabbitTemplate.convertAndSend ("exchange.fanout", "new book");} receive data test / / receive data [@ Test] (https://my.oschina.net/azibug)public void receive () {Object o=rabbitTemplate.receiveAndConvert ("mao.news") System.out.println (o.getClass ()); System.out.println (o);} there is a problem in the above test, which is solved under the serialized data displayed in the management interface.

The following can be displayed in json format, similar to a previous blog, configuring a custom redis configuration

@ Configuration

Public class myamqpconfig {@ Bean public MessageConverter messageConverter () {

Return new Jackson2JsonMessageConverter ();}

}

You can receive a message using the following

@ Service

Public class BookService {

@ RabbitListener (queues = "mao.news") public void receive (book book1) {System.out.println ("received message:" + book1.toString ());} this is the end of sharing about how to use the RabbitMQ message component in SpringBoot. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.

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