最新国产好看的视频,伊人天堂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.#");
    }
}

編寫(xiě)生產(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ā)送成功";
    }
}

編寫(xiě)消費(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

夹江县| 双桥区| 云和县| 禹城市| 贵南县| 德阳市| 新河县| 新巴尔虎左旗| 虹口区| 临湘市| 三江| 长子县| 常州市| 金华市| 忻州市| 平泉县| 顺义区| 丹棱县| 井研县| 黔西县| 周宁县| 常德市| 肥城市| 碌曲县| 杭锦后旗| 巴南区| 玛多县| 怀集县| 广宁县| 榕江县| 青浦区| 屏南县| 新安县| 新晃| 皮山县| 上饶县| 苏尼特右旗| 平江县| 桑植县| 谷城县| 会泽县|