RabbitMQ (四) --------- SpringBoot 集成 RabbitMQ


一、创建消息生产者工程

创建模块 rabbitmq-springboot-send

在这里插入图片描述

在这里插入图片描述

配置模块 rabbitmq-springboot-send 的 application.properties 文件添加对 RabbitMQ 的集成

#配置RabbitMQ链接信息
#配置RabbitMQ服务器的IP地址
spring.rabbitmq.host=192.168.160.133
#配置RabbitMQ服务器的端口
spring.rabbitmq.port=5672
#配置RabbitMQ服务器的访问账号
spring.rabbitmq.username=root
#配置RabbitMQ服务器的访问密码
spring.rabbitmq.password=aszhuo

二、创建消息接收者工程

创建模块 rabbitmq-springboot-receive

在这里插入图片描述
在这里插入图片描述
配置模块 rabbitmq-springboot-receive 的 application.properties 文件添加对 RabbitMQ 的集成

在这里插入图片描述

三、Direct 模式消息发送和接收

1. 编写 Direct 模式的消息发送

在 rabbitmq-springboot-send 模块中创建类,com.fancy.rabbitmq.direct.Send

package com.fancy.rabbitmqspringbootsend.direct;

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

import javax.annotation.Resource;

@Service
public class Send {

    // 自动注入 Amqp 的模板对象

    /*
    * Spring AMQP 是基于 Spring 框架的AMQP消息解决方案,提供模板化的发送和接收消息的抽象层,提供基于消息驱动的 POJO 的消息监听等,很大方便我们使用 RabbitMQ 程序的相关开发。
    * Spring AMQP包含一些模块,如:spring-amqp, spring-rabbit and spring-erlang等,每个模块分别由独立的一些Jar包组成。
      Spring AMQP模块主要包含org.springframework.amqp.core这个包中。这个包定义的相关类主要是与前面讲的AMQP模型相对应。
      Spring AMQP的目的是提供不依赖于任何特定的AMQP代理实现或客户端库通用的抽象。最终用户代码将很容易实现更易替换、添加和删除AMQP,因为它可以只针对抽象层来开发。
      这可以很方便我们选择和使用哪一个具体的broker实现,如sping-rabbit实现。
    */
    @Resource
    private AmqpTemplate template;

    public void send() {
        //发送消息到队列
        //参数 1 为消息存放的交换机名称 (需要事前创建)
        //参数 2 为RoutingKey,接收者需要根据这个key精准接收消息
        //参数 3 为具体存入队列中的消息数据
        template.convertAndSend("BootDirectExchange", "BootRouting", "SpringBootDirect");
    }
}

创建 Amqp 配置类 com.fancy.rabbitmq.config.AmqpConfig

package com.fancy.rabbitmqspringbootsend.config;

import org.springframework.amqp.core.DirectExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AmqpConfig {
    // @Bean 用于模拟 Spring 配置文件中的 <bean> 标签 , 用于创建 名字为
    // BootDirectExchange 的交换机
    @Bean
    public DirectExchange myChange() {
        return new DirectExchange("BootDirectExchange");
    }
}

配置主启动类,测试运行 Direct 消息发送

package com.fancy.rabbitmqspringbootsend;

import com.fancy.rabbitmqspringbootsend.direct.Send;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class RabbitmqSpringbootSendApplication {

    public static void main(String[] args) {
        ApplicationContext ac = SpringApplication.run(RabbitmqSpringbootSendApplication.class, args);
        Send send = (Send)ac.getBean("send");
        send.send();
    }

}

2. 编写 Direct 模式的消息接收

在 rabbitmq-springboot-receive 模块中创建类,com.fancy.rabbitmq.direct.Receive

package com.fancy.rabbitmqspringbootsend.direct;

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

import javax.annotation.Resource;

@Service
public class Send {

    // 自动注入 Amqp 的模板对象

    /*
    * Spring AMQP 是基于 Spring 框架的AMQP消息解决方案,提供模板化的发送和接收消息的抽象层,提供基于消息驱动的 POJO 的消息监听等,很大方便我们使用 RabbitMQ 程序的相关开发。
    * Spring AMQP包含一些模块,如:spring-amqp, spring-rabbit and spring-erlang等,每个模块分别由独立的一些Jar包组成。
      Spring AMQP模块主要包含org.springframework.amqp.core这个包中。这个包定义的相关类主要是与前面讲的AMQP模型相对应。
      Spring AMQP的目的是提供不依赖于任何特定的AMQP代理实现或客户端库通用的抽象。最终用户代码将很容易实现更易替换、添加和删除AMQP,因为它可以只针对抽象层来开发。
      这可以很方便我们选择和使用哪一个具体的broker实现,如sping-rabbit实现。
    */
    @Resource
    private AmqpTemplate template;

    public void send() {
        //发送消息到队列
        //参数 1 为消息存放的交换机名称 (需要事前创建)
        //参数 2 为RoutingKey,接收者需要根据这个key精准接收消息
        //参数 3 为具体存入队列中的消息数据
        template.convertAndSend("BootDirectExchange", "BootRouting", "SpringBootDirect");
    }
}

创建 Amqp 配置类 com.fancy.rabbitmq.config.AmqpConfig

package com.fancy.rabbitmqspringbootreceive.config;


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



@Configuration
public class AmqpConfig {

    //创建一个名字为  myQueueDirect 的队列
    @Bean
    public Queue queue() {
        return new Queue("myQueueDirect");
    }

    //创建一个名字为 BootDirectExchange 的交换机
    @Bean
    public Exchange myChange() {
        return new DirectExchange("BootDirectExchange");
    }

    // 将队列绑定到交换机
    @Bean("binding")
    // 参数1 为 自定义队列对象, 参数名为 queue 为 自定义队列对象 bean 的 id
    // 参数2 为 自定义的交换机, 参数名为 myChange 为自定义交换机 bean 的 id
    public Binding binding(Queue queue, Exchange exchange) {
        // 将队列绑定到交换机, 参数 BootRouting 为 RoutingKey
        return BindingBuilder.bind(queue).to(exchange).with("BootRouting").noargs();

    }
    
}

运行测试 Receive 消息接收,编写 Application.java 类

package com.fancy.rabbitmqspringbootreceive;

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

@SpringBootApplication
public class RabbitmqSpringbootReceiveApplication {

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

}

四、Fanout 模式消息发送和接收

1. 编写 Fanout 模式的消息发送

在 rabbitmq-springboot-send模 块中创建类,com.fancy.rabbitmq.fanout.Send

package com.fancy.rabbitmqspringbootsend.fanout;

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

import javax.annotation.Resource;

@Service
public class Send {

    @Resource
    private AmqpTemplate template;

    public void fanoutSend() {
        template.convertAndSend("BootFanoutExchange", "", "SpringBootFanout");
    }
}

修改 Amqp 配置类, com.fancy.rabbitmq.config.AmqpConfig,增加以下内容

@Bean
public FanoutExchange fanoutExchange() {
    // 创建一个基于 Fanout 的交换机, 名字为 BootFanoutExchange 
    return new FanoutExchange("BootFanoutExchange");
}  

运行测试 Direct 消息发送,编写 Application.java 类

package com.fancy.rabbitmqspringbootsend;

import com.fancy.rabbitmqspringbootsend.fanout.Send;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class RabbitmqSpringbootSendApplication {

    public static void main(String[] args) {
        ApplicationContext ac = SpringApplication.run(RabbitmqSpringbootSendApplication.class, args);
        Send send = (Send)ac.getBean("send");
        send.fanoutSend();
    }

}

2. 编写 Fanout 模式的消息接收

在 rabbitmq-springboot-send 模块中创建类,com.fancy.fanout.Send

package com.fancy.rabbitmqspringbootreceive.fanout;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class Receive {

    @RabbitListener(queues = "fanoutQueue")
    public void fanoutRecevie(String message) {
        System.out.println("Boot 的 Fanout 消息 ----" + message);
    }
    
}

修改 Amqp 配置类 com.fancy.rabbitmq.config.AmqpConfig,增加以下内容

@Bean 
public Queue fanoutQueue() {
    return new Queue("fanoutQueue");
}

@Bean 
public FanoutExchange fanoutExchange() {
    return new FanoutExchange("BootFanoutExchange");
}

@Bean 
public Binding fanoutBinding(Queue fanoutQueue, FanoutExchange fanoutExchange) {
    // 将队列绑定到指定的交换机上
    // 参数 1 为 指定的队列对象
    // 参数 2 为 指定的交换机对象
    return BindingBuilder.bind(fanoutQueue).to(fanoutExchange);
}

五、Topic 模式消息发送和接收

1. 编写 Topic 模式消息发送

在 rabbitmq-springboot-send 模块中创建类,com.fancy.rabbitmq.topic.Send

package com.fancy.rabbitmqspringbootsend.topic;

import org.springframework.amqp.core.AmqpTemplate;

import javax.annotation.Resource;

public class Send {
    
    @Resource
    private AmqpTemplate template;
    
    public void topicSend() {
        template.convertAndSend("BootTopicExchange", "Boot.text", "SpringBootTopic");
    }
}

修改 Amqp 配置类com.fancy.rabbitmq.config.AmqpConfig,增加以下内容

@Bean 
public TopicExchange topicExchange() {
    return new TopicExchange("BootTopicExchange");
}

2. 编写 Topic 模式消息接收

在 rabbitmq-springboot-receive 模块中创建类,com.fancy.rabbitmq.topic.Receive

package com.fancy.rabbitmqspringbootreceive.topic;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

@Service
public class Receive {

    @RabbitListener(queues = "topicQueue")
    public void topicReceive(String message) {
        System.out.println("Boot 的 topic 消息 1" + message);
    }

    @RabbitListener(queues = "topicQueue2")
    public void topicReceive2(String message) {
        System.out.println("Boot 的 topic 消息 2 " + message);
    }
}

修改 Amqp 配置类 com.fancy.rabbitmq.config.AmqpConfig,增加以下内容

@Bean
public TopicExchange TopicExchange() {
    return new TopicExchange("BootTopicExchange");
}

@Bean
public Queue topicQueue() {
    return new Queue("topicQueue");
}

@Bean
public Queue topicQueue2() {
    return new Queue("topicQueue2");
}

@Bean
public Binding topicBinding(Queue topicQueue, TopicExchange topicExchange) {
    // 为RoutingKey的匹配规则, #.test表示 可以接收以任意路径靠头的但是必须以 test 结尾的队列
    return BindingBuilder.bind(topicQueue).to(TopicExchange()).with("#.text");
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
单片微型计算机(MCU)经过多年的发展,在性能上有很大的进步,在型号上发展到上千种类,已经广泛应用于人类社会生活的各个领域。单片机课程已经成为高校计算机、自动化、测控以及电子信息工程等专业的重要课程。该课程是一门理论性和实践性都很强的课程,在实际教学中,应将理论教学和实验教学紧密结合。学生在掌握理论知识之余,必须通过编写程序、设计硬件电路、仿真、调试这一系列的实验过程,才能更好地掌握单片机的结构原理和应用技能。随着单片机及其接口技术的飞速发展,目前市场上供应的编程仿真实验资源并不能完全满足高校单片机课程教与学的需求,构建低成本、技术先进、源码公开的单片机编程仿真实验系统,对我国单片机课程的教学和单片机领域人才的培养具有重要的现实意义。 本论文结合目前教学中对单片机编程仿真实验系统的实际需求,采用模块化结构设计思想,精心设计和开发了单片机编程仿真实验系统。该单片机编程仿真实验系统由PC机端单片机编程控制软件和单片机编程仿真实验板两部分组成。PC机端的单片机编程控制软件可以自动检测到连接到单片机编程仿真实验板上的单片机,控制单片机编程器擦除、写入、读出、校验目标单片机ROM中的程序,以十六进制文件(.HEX文件)格式显示在控制界面内;单片机仿真实验系统能够把写入单片机的程序实时地运行,并呈现实际运行效果。单片机编程控制软件和单片机仿真实验板组成一个完整的单片机编程仿真实验系统。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

在森林中麋了鹿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值