首页 > 其他分享 >RabbitMQ交换机类型

RabbitMQ交换机类型

时间:2024-11-06 22:16:28浏览次数:4  
标签:spring boot springframework 交换机 RabbitMQ import 类型 org public

RabbitMQ交换机类型

1、RabbitMQ工作模型

  • broker 相当于mysql服务器;
  • virtual host相当于数据库(可以有多个数据库);
  • queue相当于表;
  • 消息相当于记录。

在这里插入图片描述

消息队列有三个核心要素: 消息生产者、消息队列、消息消费者;
生产者(Producer):发送消息的应用;(java程序,也可能是别的语言写的程序)
消费者(Consumer):接收消息的应用;(java程序,也可能是别的语言写的程序)
代理(Broker):就是消息服务器,RabbitMQ Server就是Message Broker;
连接(Connection):连接RabbitMQ服务器的TCP长连接;
信道(Channel):连接中的一个虚拟通道,消息队列发送或者接收消息时,都是通过信道进行的;
虚拟主机(Virtual host):一个虚拟分组,在代码中就是一个字符串,当多个不同的用户使用同一个RabbitMQ服务时,可以划分出多个Virtual host,每个用户在自己的Virtual host创建exchange/queue等;(分类比较清晰、相互隔离)
交换机(Exchange):交换机负责从生产者接收消息,并根据交换机类型分发到对应的消息队列中,起到一个路由的作用;
路由键(Routing Key):交换机根据路由键来决定消息分发到哪个队列,路由键是消息的目的地址;
绑定(Binding):绑定是队列和交换机的一个关联连接(关联关系);
队列(Queue):存储消息的缓存;
消息(Message):由生产者通过RabbitMQ发送给消费者的信息;(消息可以任何数据,字符串、user对象,json串等等)

2、RabbitMQ交换机类型

Exchange(X) 可翻译成交换机/交换器/路由器

  • Fanout Exchange(扇形)
  • Direct Exchange(直连)
  • Topic Exchange(主题)
  • Headers Exchange(头部)

2.1、Fanout Exchange(扇形)

2.1.1、介绍

Fanout 扇形的,散开的; 扇形交换机
投递到所有绑定的队列,不需要路由键,不需要进行路由键的匹配,相当于广播、群发;
在这里插入图片描述

2.1.2、示例

在这里插入图片描述

2.1.2.1、生产者

1、配置类

package com.power.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    //rabbitmq三部曲
    //1、定义交换机
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("exchange.fanout");
    }

    //2、定义队列
    @Bean
    public Queue queueA() {
        return new Queue("queue.fanout.a");
    }

    @Bean
    public Queue queueB() {
        return new Queue("queue.fanout.b");
    }

    //3、绑定交换机何队列
    @Bean
    public Binding bingingA(FanoutExchange fanoutExchange, Queue queueA) {
        //将队列A绑定到扇形交换机
        return BindingBuilder.bind(queueA).to(fanoutExchange);
    }

    @Bean
    public Binding bingingB(FanoutExchange fanoutExchange, Queue queueB) {
        //将队列B绑定到扇形交换机
        return BindingBuilder.bind(queueB).to(fanoutExchange);
    }
}

2、生产者

package com.power.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
@Slf4j
public class MessageService {
    @Resource
    private RabbitTemplate rabbitTemplate;

    public void sendMsg() {
        //定义要发送的消息
        String msg = "hello world";
        Message message = new Message(msg.getBytes());
        rabbitTemplate.convertAndSend("exchange.fanout", "", message);
        log.info("消息发送完毕,发送时间为:" + new Date());
    }
}

2、启动类

package com.power;

import com.power.service.MessageService;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.Resource;

@SpringBootApplication
public class FanoutApplication implements ApplicationRunner {

    @Resource
    private MessageService messageService;

    public static void main(String[] args) {
        SpringApplication.run(FanoutApplication.class,args);
    }

    /**
     * 程序一启动就运行该方法
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        messageService.sendMsg();
    }
}

4、配置文件application.yml

server:
  port: 8080

spring:
  application:
    name: fanout-learn

  rabbitmq:
    host: 你的RabbitMQ服务器主机IP #rabbitmq的主机
    port: 5672
    username: 登录账号
    password: 登录密码
    virtual-host: power #虚拟主机

5、项目配置文件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>

    <groupId>com.power</groupId>
    <artifactId>rabbit_01_fanout</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.13</version>
        <relativePath/>
    </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-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
    </dependencies>

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

</project>
2.1.2.2、消费者

在这里插入图片描述
1、消费者

package com.power.message;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ReceiveMessage {
    //接收两个队列的消息
    @RabbitListener(queues = {"queue.fanout.a","queue.fanout.b"})
    public void receiveMsg(Message message){
        byte[] body = message.getBody();
        String msg = new String(body);
        log.info("接收到的消息为:"+msg);

    }
}

2、配置文件application.yml

server:
  port: 9090

spring:
  application:
    name: receive-msg

  rabbitmq:
    host: 你的RabbitMQ服务器主机IP #rabbitmq的主机
    port: 5672
    username: 登录账号
    password: 登录密码
    virtual-host: power #虚拟主机

3、配置文件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>

    <groupId>com.power</groupId>
    <artifactId>rabbit_01_receiveMessage</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.13</version>
        <relativePath/>
    </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-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
    </dependencies>

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

</project>

4、启动类

package com.power;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ReceiveFanoutMsgApplication {
    public static void main(String[] args) {
        SpringApplication.run(ReceiveFanoutMsgApplication.class,args);
    }
}
2.1.2.3、测试

先启动生产者:

在这里插入图片描述

再启动消费者:

在这里插入图片描述消费者启动会会一直监听

2.2、Direct Exchange(直连)

2.2.1、介绍

根据路由键精确匹配(一模一样)进行路由消息队列;
在这里插入图片描述

2.2.2、示例

在这里插入图片描述

2.2.2.1、生产者

1、配置类

package com.power.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
//@ConfigurationProperties(prefix = "my")
public class RabbitConfig {
    //交换机的名字
    @Value("${my.exchangeName}")
    private String exchangeName;
    //队列A的名字
    @Value("${my.queueAName}")
    private String queueAName;
    //队列B的名字
    @Value("${my.queueBName}")
    private String queueBName;

    //1、定义交换机
    @Bean
    public DirectExchange directExchange() {
        //使用建造者模式创建
        return ExchangeBuilder.directExchange(exchangeName).build();
    }

    //2、定义队列
    @Bean
    public Queue queueA() {
        return QueueBuilder.durable(queueAName).build();
    }

    @Bean
    public Queue queueB() {
        return QueueBuilder.durable(queueBName).build();
    }

    //3、交换机和队列A进行绑定
    @Bean
    public Binding bindingA(DirectExchange directExchange, Queue queueA) {
        return BindingBuilder.bind(queueA).to(directExchange).with("error");
    }

    //交换机和队列B进行绑定
    @Bean
    public Binding bindingB1(DirectExchange directExchange, Queue queueB) {
        return BindingBuilder.bind(queueB).to(directExchange).with("error");
    }

    @Bean
    public Binding bindingB2(DirectExchange directExchange, Queue queueB) {
        return BindingBuilder.bind(queueB).to(directExchange).with("info");
    }

    @Bean
    public Binding bindingB3(DirectExchange directExchange, Queue queueB) {
        return BindingBuilder.bind(queueB).to(directExchange).with("warning");
    }

}

2、发送消息

package com.power.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Slf4j
public class MessageService {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Bean
    public void sendMsg() {
        //使用建造者模式创建消息
        Message message = MessageBuilder.withBody("hello world".getBytes()).build();
        //参数1:交换机   参数2:路由key    参数3:消息
        rabbitTemplate.convertAndSend("exchange.direct", "info", message);
        log.info("消息发送完毕,发送时间:"+new Date());
    }
}

3、配置文件application.yml

server:
  port: 8080

spring:
  application:
    name: receive-msg

  rabbitmq:
    host: <你的RqaabitMQ服务器IP> #rabbitmq的主机
    port: 5672
    username: 登录名
    password: 登录密码
    virtual-host: power #虚拟主机

my:
  exchangeName: exchange.direct
  queueAName: queue.direct.a
  queueBName: queue.direct.b

4、启动类

package com.power;

import com.power.service.MessageService;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.Resource;

@SpringBootApplication
public class DirectApplication implements ApplicationRunner {
    @Resource
    private MessageService messageService;

    public static void main(String[] args) {
        SpringApplication.run(DirectApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        messageService.sendMsg();
    }
}

5、配置文件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>

    <groupId>com.power</groupId>
    <artifactId>rabbit_02_direct</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.13</version>
        <relativePath/>
    </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-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
    </dependencies>

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


</project>
2.2.2.2、测试

运行启动类。
登录服务器查看Exchange:
在这里插入图片描述

登录服务器查Queues:
在这里插入图片描述

2.3、Topic Exchange(主题交换机)

2.3.1 介绍

通配符匹配,相当于模糊匹配;
# 匹配多个单词,用来表示任意数量(零个或多个)单词
* 匹配一个单词(必须有一个,而且只有一个),用.隔开的为一个单词:
beijing.# == beijing.queue.abc, beijing.queue.xyz.xxx
beijing.* == beijing.queue, beijing.xyz

在这里插入图片描述
案例:发送时指定的路由键:lazy.orange.rabbit,可以进入上图的那个队列
:可以进入Q1和Q2队列,其中Q2队列只能进入一条消息

2.3.2 示例

在这里插入图片描述

2.3.2.1 配置文件appication.yml
server:
  port: 8080
spring:
  application:
    name: topic-exchange

  rabbitmq:
    host: 你的主机IP
    port: 5672
    username: 你的管理员账号
    password: 你的管理员密码
    virtual-host: power

my:
  exchangeName: exchange.topic
  queueAName: queue.topic.a
  queueBName: queue.topic.b
2.3.2.2 配置类RabbitConfig
package com.power.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    //交换机的名字
    @Value("${my.exchangeName}")
    private String exchangeName;
    //队列A的名字
    @Value("${my.queueAName}")
    private String queueAName;
    //队列B的名字
    @Value("${my.queueBName}")
    private String queueBName;

    //创建交换机
    @Bean
    public TopicExchange topicExchange(){
        return ExchangeBuilder.topicExchange(exchangeName).build();
    }

    //创建队列
    @Bean
    public Queue queueA(){
        return QueueBuilder.durable(queueAName).build();
    }
    @Bean
    public Queue queueB(){
        return QueueBuilder.durable(queueBName).build();
    }

    //创建绑定
    @Bean
    public Binding bindingA(TopicExchange topicExchange,Queue queueA){
        return BindingBuilder.bind(queueA).to(topicExchange).with("*.orange.*");
    }
    @Bean
    public Binding bindingB1(TopicExchange topicExchange,Queue queueB){
        return BindingBuilder.bind(queueB).to(topicExchange).with("*.*.rabbit");
    }
    @Bean
    public Binding bindingB2(TopicExchange topicExchange,Queue queueB){
        return BindingBuilder.bind(queueB).to(topicExchange).with("lazy.#");
    }
}
2.3.2.3 发送消息MessageService
package com.power.service;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class MessageService {

    @Resource
    private AmqpTemplate amqpTemplate;

    public void sendMsg(){
        Message message = MessageBuilder.withBody("hello world".getBytes()).build();
        //参数1:交换机,参数2:发送路由key,参数3:消息
        amqpTemplate.convertAndSend("exchange.topic","hello.world",message);
    }
}
2.3.2.4 启动类Application
package com.power;

import com.power.service.MessageService;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.Resource;

@SpringBootApplication
public class Application implements ApplicationRunner {

    @Resource
    private MessageService messageService;

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        messageService.sendMsg();
    }
}
2.3.2.5 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>

    <groupId>com.power</groupId>
    <artifactId>rabbit_03_topic</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>rabbit_03_topic</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.13</version>
        <relativePath/>
    </parent>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <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>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
    </dependencies>

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

</project>
2.3.2.6 测试

在这里插入图片描述

2.4、Headers Exchange(头部交换机)

2.4.1、介绍

基于消息内容中的headers属性进行匹配;
在这里插入图片描述

2.4.2、示例

2.4.2.1 配置文件appication.yml
server:
  port: 8080
spring:
  application:
    name: topic-exchange

  rabbitmq:
    host: 你的服务器IP
    port: 5672
    username: 你的账号
    password: 你的密码
    virtual-host: power

my:
  exchangeName: exchange.headers
  queueAName: queue.headers.a
  queueBName: queue.headers.b
2.4.2.2 配置类RabbitConfig
package com.power.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {
    //交换机的名字
    @Value("${my.exchangeName}")
    private String exchangeName;
    //队列A的名字
    @Value("${my.queueAName}")
    private String queueAName;
    //队列B的名字
    @Value("${my.queueBName}")
    private String queueBName;

    //创建交换机
    @Bean
    public HeadersExchange headersExchange(){
        return ExchangeBuilder.headersExchange(exchangeName).build();
    }

    //创建队列
    @Bean
    public Queue queueA(){
        return QueueBuilder.durable(queueAName).build();
    }
    @Bean
    public Queue queueB(){
        return QueueBuilder.durable(queueBName).build();
    }

    //创建绑定
    @Bean
    public Binding bindingA(HeadersExchange headersExchange,Queue queueA){
        Map<String, Object> headerValues = new HashMap<>();
        headerValues.put("type","m");
        headerValues.put("status",1);
        return BindingBuilder.bind(queueA).to(headersExchange).whereAll(headerValues).match();
    }

    @Bean
    public Binding bindingB(HeadersExchange headersExchange,Queue queueB){
        Map<String, Object> headerValues = new HashMap<>();
        headerValues.put("type","s");
        headerValues.put("status",0);
        return BindingBuilder.bind(queueB).to(headersExchange).whereAll(headerValues).match();
    }
}
2.4.2.3 发送消息MessageService
package com.power.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class MessageService {

    @Resource
    private RabbitTemplate rabbitTemplate;
    @Value("${my.exchangeName}")
    private String exchangeName;

    public void sendMsg(){
        MessageProperties messageProperties = new MessageProperties();
        Map<String,Object> headers = new HashMap<>();
        headers.put("type","s");
        headers.put("status",0);
        //设置消息头
        messageProperties.setHeaders(headers);
        Message message = MessageBuilder.withBody("hello world".getBytes())
                .andProperties(messageProperties).build();
        rabbitTemplate.convertAndSend(exchangeName,"",message);
        log.info("消息发送完毕");
    }

}
2.4.2.4 启动类Application
package com.power;

import com.power.service.MessageService;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.Resource;

@SpringBootApplication
public class HeadersExchangeApplication implements ApplicationRunner {

    @Resource
    private MessageService messageService;


    public static void main(String[] args) {
        SpringApplication.run(HeadersExchangeApplication.class,args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        messageService.sendMsg();
    }
}
2.4.2.5 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>

  <groupId>com.power</groupId>
  <artifactId>rabbit_04_headers</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>rabbit_04_headers</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.13</version>
    <relativePath/>
  </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-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.24</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
  </dependencies>

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

</project>
2.4.2.6 测试

在这里插入图片描述

标签:spring,boot,springframework,交换机,RabbitMQ,import,类型,org,public
From: https://blog.csdn.net/qq_46112274/article/details/142621583

相关文章

  • RabbitMQ如何保证发送的消息可靠(RabbitMQ的Confirm模式和2.Return模式)
    RabbitMQ如何保证发送的消息可靠(RabbitMQ的Confirm模式和2.Return模式)1、RabbitMQ消息Confirm模式(保证从生产者到交换机的消息可靠)1.1、Confirm模式简介1.2、具体代码实现1.2.1、application.yml开启确认模式1.2.2、生产者方式1:实现RabbitTemplate.ConfirmCallback生产......
  • Object.assign(), 一道关于基本类型合并的面试题
    vars1="abc";vars2=true;vars3=10;vars4=Symbol("foo")varobj=Object.assign(s1,null,s2,undefined,s3,s4);console.log(obj);//{"0":"a","1":"b","2":"c&quo......
  • 1.python的基本语法和简单数据类型
    1.1语法特点1.1.1注释语句单行注释在所需注释内容前加'#'号#我是一个注释多行注释'''(单引号/或双引号)注释代码''''''我是代码我是代码我是代码我是代码'''1.2.1缩进先看代码if<我是条件1>:<我是代码><我是代码>else:<我是......
  • 【JAVA】Java基础—基础语法:数据类型(基本数据类型与引用数据类型)
    在Java编程中,数据类型是一个非常重要的概念,它决定了变量能够存储什么样的数据以及如何在内存中处理这些数据。Java是一种强类型语言,这意味着每个变量都必须声明其数据类型,并且在使用时必须遵循该类型的规则。数据类型主要分为两大类:基本数据类型和引用数据类型。理解这两种数......
  • 第一章 TypeScript 基础数据类型详解
    在TypeScript中,正确理解和使用各种数据类型是构建强大、类型安全的应用程序的关键。让我们深入了解TypeScript中的一些主要数据类型。一、变量声明使用`let`关键字可以定义变量。例如:leta:number=100;console.log(a);这里明确指定变量`a`的类型为`number`......
  • 数据类型转换和Ascii表常用的几个数值
    1.数据类型转换:当数据类型不一致时,会发生数据类型转换(1)自动类型转换(隐式):数据范围从较小到较大时,代码不需做特殊处理,自动完成(2)强制类型转换(显式):数据范围从较大到较小时,代码需要特殊处理处理格式:范围小的类型范围小的变量名=(范围较小的类型)范围较大的数据;注意:<1>.强制类......
  • Spring-Converter类型转换器
    Converter在Spring3中引入了一个Converter接口,它支持从一个Object转为另一个Object。除了Converter接口之外,实现ConverterFactory接口和GenericConverter接口也可以实现我们自己的类型转换逻辑。Converter接口Converter<S,T>接口定义了从类型S转换为类型T的转换器。packa......
  • 什么是C++模板,有哪些类型的模板?
    模板C++模板是一种强大的语言特性,允许开发者编写与类型无关的代码,从而实现代码的复用和灵活性。通过模板,可以定义函数和类,具体实现将由具体的类型实例化决定。函数模板函数模板(FunctionTemplates):函数模板用于定义一个通用的函数,该函数可以接受任意类型的参数。通过使用模......
  • IO 多路复用技术:原理、类型及 Go 实现
    文章目录1.引言IO多路复用的应用场景与重要性高并发下的IO处理挑战2.IO多路复用概述什么是IO多路复用IO多路复用的优点与适用场景3.IO多路复用的三种主要实现3.1`select`3.2`poll`3.3`epoll`三者对比4.深入理解epoll4.1epoll的三大操作4.2epoll的......
  • Go 语言变量类型:从入门到精通,一篇搞定所有知识点!
    Go语言变量类型1.基本类型1.1数值类型1.2布尔类型1.3字符串类型2.复合类型2.1数组2.2切片2.3字典(map)2.4结构体2.5接口3.类型转换4.零值5.示例1.基本类型Go语言中的基本类型主要包括数值类型、布尔类型和字符串类型。1.1数值类型整型:int:根据......