成都创新互联网站制作重庆分公司

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

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


    org.springframework.boot
	
    spring-boot-starter-parent
	
    2.1.6.RELEASE
	
     
	


com.mao

spring-01-amqp

0.0.1-SNAPSHOT

spring-01-amqp

Demo 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配置

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 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消息组件就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


文章名称:SpringBoot中怎么使用RabbitMQ消息组件
转载源于:http://cxhlcq.com/article/jdsdjg.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部