最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot整合RabbitMQ實(shí)現(xiàn)通配符模式

 更新時(shí)間:2025年06月12日 09:26:01   作者:新綠MEHO  
本文主要介紹了SpringBoot整合RabbitMQ實(shí)現(xiàn)通配符模式,包括依賴添加、配置、隊(duì)列與交換機(jī)聲明及綁定,生產(chǎn)者發(fā)送消息,兩個(gè)消費(fèi)者分別接收并處理,驗(yàn)證消息正確分發(fā)至不同隊(duì)列,感興趣的可以了解一下

通配符模式

引入依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<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.springframework.amqp</groupId>
    <artifactId>spring-rabbit-test</artifactId>
    <scope>test</scope>
</dependency>

添加配置

spring:
  application:
    name: rabbitmq-springboot
  rabbitmq:
    addresses: amqp://study:study@47.98.109.138:5672/aaa

常量類

public class Constants {

    //通配符模式
    public static final String TOPIC_QUEUE1 = "topic.queue1";
    public static final String TOPIC_QUEUE2 = "topic.queue2";
    public static final String TOPIC_EXCHANGE = "topic.exchange";
}

聲明隊(duì)列和交換機(jī)并綁定二者關(guān)系

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import rabbitmq.constant.Constants;

@Configuration
public class RabbitMQConfig {
    //通配符模式
    @Bean("topicQueue1")
    public Queue topicQueue1(){
        return QueueBuilder.durable(Constants.TOPIC_QUEUE1).build();
    }
    @Bean("topicQueue2")
    public Queue topicQueue2(){
        return QueueBuilder.durable(Constants.TOPIC_QUEUE2).build();
    }
    @Bean("topicExchange")
    public TopicExchange topicExchange(){
        return ExchangeBuilder.topicExchange(Constants.TOPIC_EXCHANGE).durable(true).build();
    }

    @Bean("topicQueueBinding1")
    public Binding topicQueueBinding1(@Qualifier("topicExchange") TopicExchange topicExchange, @Qualifier("topicQueue1") Queue queue){
        return BindingBuilder.bind(queue).to(topicExchange).with("*.orange.*");
    }
    @Bean("topicQueueBinding2")
    public Binding topicQueueBinding2(@Qualifier("topicExchange") TopicExchange topicExchange, @Qualifier("topicQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(topicExchange).with("*.*.rabbit");
    }
    @Bean("topicQueueBinding3")
    public Binding topicQueueBinding3(@Qualifier("topicExchange") TopicExchange topicExchange, @Qualifier("topicQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(topicExchange).with("lazy.#");
    }
}

編寫生產(chǎn)者代碼

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import rabbitmq.constant.Constants;

@RequestMapping("/producer")
@RestController
public class ProducerController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/topic/{routingKey}")
    public String topic(@PathVariable("routingKey") String routingKey){
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE, routingKey, "hello spring amqp:topic, my routing key is "+routingKey);
        return "發(fā)送成功";
    }
}

編寫消費(fèi)者代碼(含兩個(gè)隊(duì)列)

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import rabbitmq.constant.Constants;

@Component
public class TopicListener {

    @RabbitListener(queues = Constants.TOPIC_QUEUE1)
    public void queueListener1(String message){
        System.out.println("隊(duì)列["+Constants.TOPIC_QUEUE1+"] 接收到消息:" +message);
    }

    @RabbitListener(queues = Constants.TOPIC_QUEUE2)
    public void queueListener2(String message){
        System.out.println("隊(duì)列["+Constants.TOPIC_QUEUE2+"] 接收到消息:" +message);
    }
}

生產(chǎn)消息

消費(fèi)消息

兩個(gè)隊(duì)列都收到并消費(fèi)了消息,且結(jié)果符合預(yù)期。

到此這篇關(guān)于SpringBoot整合RabbitMQ實(shí)現(xiàn)通配符模式的文章就介紹到這了,更多相關(guān)SpringBoot RabbitMQ通配符模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

桃源县| 城口县| 前郭尔| 华容县| 元谋县| 土默特左旗| 莱芜市| 民乐县| 合川市| 黎城县| 梁平县| 甘孜| 呼玛县| 康马县| 郓城县| 交口县| 九寨沟县| 工布江达县| 莱州市| 长乐市| 商水县| 廉江市| 唐河县| 同心县| 汾西县| 陇南市| 乌兰县| 普格县| 沂南县| 会泽县| 纳雍县| 土默特右旗| 河南省| 萝北县| 芒康县| 明水县| 泽普县| 大关县| 衡东县| 松潘县| 邹平县|