SpringBoot+RabbitMQ集成(自动创建队列)

pom

pom 复制代码
<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>

yml配置文件

yml 复制代码
spring:
  #rabbitmq配置信息
  rabbitmq:
    host: 192.168.183.130
    port: 5672
    username: admin
    password: admin
###############自定义properties配置信息###############
properties:
  #rabbitMQ消息队列
  queue:
    insure:
      add: insureadd
      update: insureupdate
      del: insuredel
    money:
      add: moneyadd
      update: moneyupdate
      del: moneydel
    financing:
      add: financingadd
      update: financingupdate
      del: financingdel

配置类

java 复制代码
package com.example.higerpoint.config;

import lombok.Data;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author xx
 * @date 2019/12/24 星期二
 * @time 14:12
 * @description higerpoint
 * springboot自动读取yml文件自动配置,这里可删
 */
@Data
@Configuration(value = "rabbitConfig")
public class RabbitConfig {

    @Value("${spring.rabbitmq.host}")
    private String host;

    @Value("${spring.rabbitmq.port}")
    private int port;

    @Value("${spring.rabbitmq.username}")
    private String username;

    @Value("${spring.rabbitmq.password}")
    private String password;

    /**
     * 消息队列名
     */
    @Value("${properties.queue.insure.add}")
    private String queueInsureAdd;
    @Value("${properties.queue.insure.update}")
    private String queueInsureUpdate;
    @Value("${properties.queue.insure.del}")
    private String queueInsureDel;
    @Value("${properties.queue.money.add}")
    private String queueMoneyAdd;
    @Value("${properties.queue.money.update}")
    private String queueMoneyUpdate;
    @Value("${properties.queue.money.del}")
    private String queueMoneyDel;
    @Value("${properties.queue.financing.add}")
    private String queueFinancingAdd;
    @Value("${properties.queue.financing.update}")
    private String queueFinancingUpdate;
    @Value("${properties.queue.financing.del}")
    private String queueFinancingDel;

    /**
     * 交换空间名称
     */
    public static final String EXCHANGE = "microboot.exchange";

    /**
     * 设置路由key
     */
    public static final String ROUTINGKEY = "microboot.routingkey";

    /**
     * @author chezhenqi
     * @date 14:42 2019/12/24
     * @description: 创建mq连接
     * @param:
     * @return:
     */
    @Bean(name = "connectionFactory")
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host, port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost("/");
        connectionFactory.setPublisherConfirms(true);
        return connectionFactory;
    }

    /**
     * @author chezhenqi
     * @date 16:36 2019/12/24
     * @description: 创建交换空间
     * @params: No such property: code for class: Script1
     * @return:
     */
    @Bean
    public DirectExchange exchange() { // 使用直连的模式
        return new DirectExchange(EXCHANGE, true, true);
    }

    /**
     * @author chezhenqi
     * @date 16:36 2019/12/24
     * @description: 创建队列
     * @params: No such property: code for class: Script1
     * @return:
     */
    @Bean
    public Binding bindingAueueInsureAdd() {
        return BindingBuilder.bind(queueInsureAdd()).to(exchange()).with(ROUTINGKEY);
    }

    @Bean
    public Binding bindingQueueInsureUpdate() {
        return BindingBuilder.bind(queueInsureUpdate()).to(exchange()).with(ROUTINGKEY);
    }

    @Bean
    public Binding bindingQueueInsureDel() {
        return BindingBuilder.bind(queueInsureDel()).to(exchange()).with(ROUTINGKEY);
    }

    @Bean
    public Binding bindingQueueMoneyAdd() {
        return BindingBuilder.bind(queueMoneyAdd()).to(exchange()).with(ROUTINGKEY);
    }

    @Bean
    public Binding bindingQueueMoneyUpdate() {
        return BindingBuilder.bind(queueMoneyUpdate()).to(exchange()).with(ROUTINGKEY);
    }

    @Bean
    public Binding bindingQueueMoneyDel() {
        return BindingBuilder.bind(queueMoneyDel()).to(exchange()).with(ROUTINGKEY);
    }

    @Bean
    public Binding bindingQueueFinancingAdd() {
        return BindingBuilder.bind(queueFinancingAdd()).to(exchange()).with(ROUTINGKEY);
    }

    @Bean
    public Binding bindingQueueFinancingUpdate() {
        return BindingBuilder.bind(queueFinancingUpdate()).to(exchange()).with(ROUTINGKEY);
    }

    @Bean
    public Binding bindingQueueFinancingDel() {
        return BindingBuilder.bind(queueFinancingDel()).to(exchange()).with(ROUTINGKEY);
    }


    /**
     * 将需要创建的消息队列实例化
     */
    @Bean
    public Queue queueInsureAdd() {
        return new Queue(queueInsureAdd, true);
    }

    @Bean
    public Queue queueInsureUpdate() {
        return new Queue(queueInsureUpdate, true);
    }

    @Bean
    public Queue queueInsureDel() {
        return new Queue(queueInsureDel, true);
    }

    @Bean
    public Queue queueMoneyAdd() {
        return new Queue(queueMoneyAdd, true);
    }

    @Bean
    public Queue queueMoneyUpdate() {
        return new Queue(queueMoneyUpdate, true);
    }

    @Bean
    public Queue queueMoneyDel() {
        return new Queue(queueMoneyDel, true);
    }

    @Bean
    public Queue queueFinancingAdd() {
        return new Queue(queueFinancingAdd, true);
    }

    @Bean
    public Queue queueFinancingUpdate() {
        return new Queue(queueFinancingUpdate, true);
    }

    @Bean
    public Queue queueFinancingDel() {
        return new Queue(queueFinancingDel, true);
    }

}

消息发送者

java 复制代码
package com.example.higerpoint.producer;

import com.example.higerpoint.config.RabbitConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author chezhenqi
 * @date 2019/12/24 星期二
 * @time 15:28
 * @description higerpoint
 */
@Component
public class FinancingProducer {
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RabbitConfig rabbitConfig;

    public void sendMessageAdd() {
        rabbitTemplate.convertAndSend(rabbitConfig.getQueueFinancingAdd(), "financingAdd");
    }

    public void sendMessageUpdate() {
        rabbitTemplate.convertAndSend(rabbitConfig.getQueueFinancingUpdate(), "financingUpdate");
    }

    public void sendMessageDel() {
        rabbitTemplate.convertAndSend(rabbitConfig.getQueueFinancingDel(), "financingDel");
    }
}

消息消费者

java 复制代码
package com.example.higerpoint.consumer;

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

/**
 * @author chezhenqi
 * @date 2019/12/24 星期二
 * @time 15:35
 * @description higerpoint
 */
@Component
@Slf4j
public class FinancingConsumer {

    @RabbitListener(queues = "#{rabbitConfig.getQueueFinancingAdd()}")
    public void add(String content) {
        log.info("financingConsumer.add():{}", content);
    }

    @RabbitListener(queues = "#{rabbitConfig.getQueueFinancingUpdate()}")
    public void update(String content) {
        log.info("financingConsumer.update():{}", content);
    }

    @RabbitListener(queues = "#{rabbitConfig.getQueueFinancingDel()}")
    public void del(String content) {
        log.info("financingConsumer.del():{}", content);
    }
}

测试方法

java 复制代码
    @Autowired
    private FinancingProducer financingProducer;
	/**
     * 测试方法
     *
     * @return
     */
    @GetMapping("/rabbit/testSendMsg")
    public String testSendMsg() {
        financingProducer.sendMessageAdd();
        financingProducer.sendMessageUpdate();
        financingProducer.sendMessageDel();
        return "发送成功!";
    }
相关推荐
Q_19284999065 小时前
基于Spring Boot的电影售票系统
java·spring boot·后端
陈无左耳、6 小时前
Spring Boot应用开发实战:从入门到精通
spring boot
烟波人长安吖~6 小时前
【目标跟踪+人流计数+人流热图(Web界面)】基于YOLOV11+Vue+SpringBoot+Flask+MySQL
vue.js·pytorch·spring boot·深度学习·yolo·目标跟踪
张铁铁是个小胖子10 小时前
消息中间件RabbitMQ和kafka
分布式·kafka·rabbitmq
Mr.朱鹏10 小时前
操作002:HelloWorld
java·后端·spring·rabbitmq·maven·intellij-idea·java-rabbitmq
顽疲11 小时前
从零用java实现 小红书 springboot vue uniapp (6)用户登录鉴权及发布笔记
java·vue.js·spring boot·uni-app
编程洪同学12 小时前
Spring Boot 中实现自定义注解记录接口日志功能
android·java·spring boot·后端
GraduationDesign12 小时前
基于SpringBoot的蜗牛兼职网的设计与实现
java·spring boot·后端
customer0813 小时前
【开源免费】基于SpringBoot+Vue.JS安康旅游网站(JAVA毕业设计)
java·vue.js·spring boot·后端·kafka·开源·旅游
罗政16 小时前
PDF书籍《手写调用链监控APM系统-Java版》第10章 插件与链路的结合:SpringBoot环境插件获取应用名
java·spring boot·pdf