2020+SpringBoot+RabbitMQ

SpringBoot+RabbitMQ

一、topic模式下product and consumer 手动确认消息(MQ各名词已熟悉)

boot集成RabbitMQ,只讲topic模式,因为它几乎涵盖了其他所有模式。

二、配置文件

tips:测试手动确认和自动确认之前,记得改一下yml配置

spring:
  rabbitmq:
    host: xxxx
    port: 5672
    username: xxxx
    password: xxxx
    #确认消息已发送到交换机(Exchange) 版本较低用 publisher-confirms: true
    publisher-confirm-type: correlated
    #指定消息确认模式为手动确认
    listener:
      simple:
        acknowledge-mode: manual
        #acknowledge-mode: none
    #发送返回监听回调
    publisher-returns: true
<dependency>
		<groupId>org.springframework.amqp</groupId>
		<artifactId>spring-rabbit</artifactId>
		<version>2.2.9.RELEASE</version>
</dependency>

三、TopicRabbitConfig、MQ初始化Broker-Exchange-RoutingKey-Queue

tips:绑定通配符topic.A 、topic.# ( .# 代表匹配任意长度字符,.* 代表匹配一个)

package com.sdzn.flxt.commons.configs;

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

/**
 * rabbit innit
 */
@Configuration
public class TopicRabbitConfig {
   
    /*
        exchange - name
     */
    private final static String exchange_name = "topicExchange";
    /*
        routingKey - name
     */
    private final static String routingKey1 = "topic.A";
    private final static String routingKey2 = "topic.#";

    /*
       Queue - name
     */
    private static String QueueName_A = "QueueA";
    private static String QueueName_B = "QueueB";

    /*
        Queue - matchRule
     */
    @Bean
    public Queue firstQueue() {
   
        return new Queue(TopicRabbitConfig.QueueName_A);
    }

    @Bean
    public Queue secondQueue() {
   
        return new Queue(TopicRabbitConfig.QueueName_B);
    }

    /*
        exchange - set
     */
    @Bean
    TopicExchange exchange() {
   
        return new TopicExchange(exchange_name);
    }


    /*
        bind - queue->exchange->routingKey
     */
    @Bean
    Binding bindingExchangeMessage() {
   
        return BindingBuilder.bind(
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值