博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot笔记-发送消息给RabbitMQ
阅读量:1952 次
发布时间:2019-04-27

本文共 5452 字,大约阅读时间需要 18 分钟。

目录

 

 


 

基本概念

RabbitMQ

消息中间件是在消息的传输过程中保存消息的容器。消息中间件充当中间人的作用将源消息发送到目标消息。
队列的主要目的是提供路由并保证消息的传递;如果发送消息时接受者不可用,消息队列会保留消息,直到可以成功地传递它为止,当然,消息队列保存消息有期限。

应用程序和应用程序调用关系为松耦合关系

发送者和接受者不必了解对方、只需要确认消息,发送者和接受者不必同时在线;

 

点对点模型:

          1. 每个消息只能用一个消费者;
          2. 发送者和接受者没有时间依赖;
          3. 接受者确认消息接收和处理成功;

发布者/订阅者模型:
发布者需要建立一个订阅(subscription)以便订阅者订阅。订阅者必须保存持续的活动状态以接收消息,除非订阅者建立了持久的订阅。在这种情况下,在订阅者未连接时发布的消息在订阅者重新连接时发布

          1. 每个消息可以有多个订阅者;

          2. 客户端只有订阅后才能接收到消息;
          3. 持久订阅和非持久订阅;

 

代码及演示

RabbitMQ设置如下:

交换机通过绑定队列,并且设置Routing key把获取的消息放到指定的队列中,

order.* 可以匹配order.xxxx的信息,不能匹配order.xxx.xxx

而order.#可以匹配order.xxx.xxx等数据

 

目前的队列如下:

使用Spring Boot发送数据后:

Spring Boot结构如下:

Order.java

package productor.demo.entity;import java.io.Serializable;public class Order implements Serializable {    private static final long serialVersionUID = -1234567891234332445L;    private String id;    private String name;    private String messageId;   //存储消息发送的唯一ID    public Order(){    }    public Order(String id, String name, String messageId) {        this.id = id;        this.name = name;        this.messageId = messageId;    }    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getMessageId() {        return messageId;    }    public void setMessageId(String messageId) {        this.messageId = messageId;    }}

OderSender.java

package productor.demo.produce;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.amqp.rabbit.support.CorrelationData;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import productor.demo.entity.Order;@Componentpublic class OderSender {    @Autowired    private RabbitTemplate rabbitTemplate;    public void send(Order order) throws Exception{        CorrelationData correlationData = new CorrelationData();        correlationData.setId(order.getMessageId());        rabbitTemplate.convertAndSend("order-exchange",     //exchange                "order.abcd",       //routingKey                order,                          //消息体                correlationData);               //correlationData消息唯一ID    }}

application.properties

server.port=8001spring.rabbitmq.addresses=192.168.164.141:5672spring.rabbitmq.username=guestspring.rabbitmq.password=guestspring.rabbitmq.virtual-host=/spring.rabbitmq.connection-timeout=15000

DemoApplicationTests.java

package productor.demo;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import productor.demo.entity.Order;import productor.demo.produce.OderSender;import java.util.UUID;@RunWith(SpringRunner.class)@SpringBootTestpublic class DemoApplicationTests {    @Autowired    private OderSender oderSender;    @Test    public void contextLoads() {    }    @Test    public void testSend1()throws Exception{        Order order = new Order();        order.setId("20180618000000000002");        order.setName("测试订单2");        order.setMessageId(System.currentTimeMillis() + "$" + UUID.randomUUID().toString());        oderSender.send(order);    }}

porn.xml

4.0.0
com.sxw
springboot-rabbitmq
jar
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.0.4.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-amqp
org.springframework.boot
spring-boot-starter-test
test
com.alibaba
druid
1.0.29
com.github.miemiedev
mybatis-paginator
1.2.17
org.mybatis
mybatis
org.apache-commons
commons-lang3
commons-io
commons-io
2.4
com.alibaba
fastjson
1.2.49
javax.servlet
javax.servlet-api
provided
log4j
log4j
1.2.17
org.springframework.boot
spring-boot-maven-plugin

 

转载地址:http://enhif.baihongyu.com/

你可能感兴趣的文章
pip无法安装:换成国内镜像
查看>>
jenkins安装提示Please wait while Jenkins is getting ready to work...(Jenkins访问资源慢的问题)
查看>>
python安装mysqlclient[MySQLdb]
查看>>
性能测试的必要性评估以及评估方法
查看>>
性能测试需求分析
查看>>
性能测试需求评审
查看>>
性能测试实施流程
查看>>
Jmeter在多线程当中对某个http请求进行循环读取配置文件
查看>>
Python读取配置文件中文乱码问题
查看>>
使用Spark读写外部存储介质(Mysql、Hbase、Redis)
查看>>
Spark学习——利用Mleap部署spark pipeline模型
查看>>
手写LogisticRegression
查看>>
SQL经典题目总结
查看>>
剑指offer编程题,一周刷完
查看>>
推荐系统初学
查看>>
Map的遍历和排序
查看>>
约瑟夫环问题
查看>>
springboot+log4j2体验
查看>>
redis集群启动方式
查看>>
ubuntu系统开机自启动方式
查看>>