Spring Boot笔记-发送消息给RabbitMQ

目录

 

 

基本概念

代码及演示


 

基本概念

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;

@Component
public 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=8001

spring.rabbitmq.addresses=192.168.164.141:5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.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)
@SpringBootTest
public 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

<?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>

    <groupId>com.sxw</groupId>
    <artifactId>springboot-rabbitmq</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>

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

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

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

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.29</version>
        </dependency>
        <dependency>
            <groupId>com.github.miemiedev</groupId>
            <artifactId>mybatis-paginator</artifactId>
            <version>1.2.17</version>
            <exclusions>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--工具类依赖包-->
        <dependency>
            <groupId>org.apache-commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.49</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>

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

</project>

 

相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页