SpringBoot中怎么使用RabbitMQ消息组件

这篇文章将为大家详细讲解有关SpringBoot中怎么使用RabbitMQ消息组件,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

1 在虚拟机中安装rabbitmq

SpringBoot中怎么使用RabbitMQ消息组件

2 开启rabbitmq 

SpringBoot中怎么使用RabbitMQ消息组件 

3 页面查看rabbitmq SpringBoot中怎么使用RabbitMQ消息组件

3 rabbitmq相关知识点讲解 [1]

MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法。MQ是消费-生产者模型的一个典型的代表,一端往消息队列中不断写入消息,而另一端则可以读取队列中的消息。

1、队列、生产者、消费者

队列:mq内部存储信息的
生产者:产生信息的
消费者:消费消息的

2、Exchange、Binding exchange:交换器,交换按照一定规则与对列绑定,消息才能到queue中 Binding用routing key

3、Exchange Type有四种

  RabbitMQ常用的Exchange Type有三种:fanout、direct、topic。headers不常用。

  fanout:把所有发送到该Exchange的消息投递到所有与它绑定的队列中。将消息发送与该exchange绑定的所有的对列,不需要比较路由键

  direct:把消息投递到那些binding key与routing key完全匹配的队列中。要求路由键与对列名完全匹配

  topic:将消息路由到binding key与routing key模式匹配的队列中。用#匹配0或者多个,*匹配一个

4 下面简单实现下图的结果

SpringBoot中怎么使用RabbitMQ消息组件

1 创建direct类型的交换器

SpringBoot中怎么使用RabbitMQ消息组件

2 创建 fanout类型的交换器 SpringBoot中怎么使用RabbitMQ消息组件

3 创建 topic类型的交换器

SpringBoot中怎么使用RabbitMQ消息组件

4 创建的结果

SpringBoot中怎么使用RabbitMQ消息组件

5 创建对列,以下图为例子

SpringBoot中怎么使用RabbitMQ消息组件

队列创建完成

SpringBoot中怎么使用RabbitMQ消息组件

6 绑定队列direct类型例子

SpringBoot中怎么使用RabbitMQ消息组件

7 绑定fanout类型的例子

SpringBoot中怎么使用RabbitMQ消息组件

8 绑定topic类型的例子

SpringBoot中怎么使用RabbitMQ消息组件

SpringBoot中怎么使用RabbitMQ消息组件

9 测试direct类型的交换器

SpringBoot中怎么使用RabbitMQ消息组件

测试结果 SpringBoot中怎么使用RabbitMQ消息组件

10 测试 fanout类型的交换器

SpringBoot中怎么使用RabbitMQ消息组件

测试结果

SpringBoot中怎么使用RabbitMQ消息组件

11 测试topic 类型的交换器

SpringBoot中怎么使用RabbitMQ消息组件

SpringBoot中怎么使用RabbitMQ消息组件

5 在springBoot项目下使用RabbitMq

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	 
<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>org.springframework.boot</groupId>
	
    <artifactId>spring-boot-starter-parent</artifactId>
	
    <version>2.1.6.RELEASE</version>
	
    <relativePath/> <!-- lookup parent from repository -->
	
</parent>

<groupId>com.mao</groupId>

<artifactId>spring-01-amqp</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>spring-01-amqp</name>

<description>Demo project for Spring Boot</description>

<properties>

    <java.version>1.8</java.version>
	
</properties>

<dependencies>

    <dependency>
	
        <groupId>org.springframework.boot</groupId>
		
        <artifactId>spring-boot-starter-amqp</artifactId>
		
    </dependency>
	
    <dependency>
	
        <groupId>org.springframework.boot</groupId>
		
        <artifactId>spring-boot-starter-web</artifactId>
		
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
		
        <artifactId>spring-boot-starter-test</artifactId>
		
        <scope>test</scope>
		
    </dependency>
	
</dependencies>

<build>
    <plugins>
        <plugin>
		
            <groupId>org.springframework.boot</groupId>
			
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>

applicaton.properties配置

spring.rabbitmq.host=192.168.1.139

spring.datasource.password=guest

spring.datasource.username=guest

book实体

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 public void contextLoads() {

    //message需要自己构造一个,定义消息体内容和消息头
	
  //  rabbitTemplate.send(exchange,roteKey,message);
  
    //只需要传入要发送的对象,自动序列化发送给rabbitmq,对象被默认序列化后发送
	
    Map<String,Object> map= new HashMap<>();
	
    map.put("msg","这是第一个消息");
	
    map.put("data", Arrays.asList("hello","wored","rabbitmq"));
	
    rabbitTemplate.convertAndSend("exchange.direct","mao.news",new book("少年","天机"));
}

  /**
 采用广播的方式
 **/
[@Test](https://my.oschina.net/azibug)
public void senMsg(){

    rabbitTemplate.convertAndSend("exchange.fanout","",new book("少年","落非"));
}

接收数据测试

  //接收数据
  
[@Test](https://my.oschina.net/azibug)

public void receive(){

    Object o=rabbitTemplate.receiveAndConvert("mao.news");
	
    System.out.println(o.getClass());
	
    System.out.println(o);
	
}
上面测试有个问题,在管理界面显示的是序列化的数据

下面来解决该问题

下面这样就可以以json格式显示,与之前的一篇博客类似,配置自定义redis配置

@Configuration

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

    return new Jackson2JsonMessageConverter();
}

}

使用下面的就可以收到消息

@Service

public class BookService {

@RabbitListener(queues = "mao.news")

public  void receive(book book1){

    System.out.println("收到消息:"+book1.toString());
	

}

关于SpringBoot中怎么使用RabbitMQ消息组件就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。