亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

通配符模式

引入依賴

<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";
}

聲明隊列和交換機(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ā)送成功";
    }
}

編寫消費者代碼(含兩個隊列)

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("隊列["+Constants.TOPIC_QUEUE1+"] 接收到消息:" +message);
    }

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

生產(chǎn)消息

消費消息

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

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

相關(guān)文章

  • Java POI讀取excel中數(shù)值精度損失問題解決

    Java POI讀取excel中數(shù)值精度損失問題解決

    這篇文章主要介紹了Java POI讀取excel中數(shù)值精度損失問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • Dubbo服務(wù)無法注冊到ZK上問題

    Dubbo服務(wù)無法注冊到ZK上問題

    這篇文章主要介紹了Dubbo服務(wù)無法注冊到ZK上問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • spring?boot集成loback日志配置的示例代碼

    spring?boot集成loback日志配置的示例代碼

    這篇文章主要介紹了spring?boot集成loback日志配置的示例代碼,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-01-01
  • java拋出異常的幾種情況小結(jié)

    java拋出異常的幾種情況小結(jié)

    這篇文章主要介紹了java拋出異常的幾種情況小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Spring Boot 集成 Kafka的詳細(xì)步驟

    Spring Boot 集成 Kafka的詳細(xì)步驟

    Spring Boot與Kafka的集成使得消息隊列的使用變得更加簡單和高效,可以配置 Kafka、實現(xiàn)生產(chǎn)者和消費者,并利用 Spring Boot 提供的功能處理消息流,以下是 Spring Boot 集成 Kafka 的詳細(xì)步驟,包括配置、生產(chǎn)者和消費者的實現(xiàn)以及一些高級特性,感興趣的朋友一起看看吧
    2024-07-07
  • Spring的AOP極簡入門

    Spring的AOP極簡入門

    今天小編就為大家分享一篇關(guān)于Spring的AOP極簡入門,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • IDEA啟動報錯Internal?error.?Please?refer?to?https://jb.gg/ide/critical-startup-errors解決辦法

    IDEA啟動報錯Internal?error.?Please?refer?to?https://jb.gg/i

    這篇文章主要介紹了IDEA啟動報錯Internal?error.?Please?refer?to?https://jb.gg/ide/critical-startup-errors解決辦法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • 關(guān)于jdk環(huán)境變量的配置方式解讀

    關(guān)于jdk環(huán)境變量的配置方式解讀

    這篇文章主要介紹了關(guān)于jdk環(huán)境變量的配置方式解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Springboot輕量級的監(jiān)控組件SpringbootAdmin

    Springboot輕量級的監(jiān)控組件SpringbootAdmin

    這篇文章主要為大家介紹了Springboot輕量級的監(jiān)控組件SpringbootAdmin使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • java編程實現(xiàn)多人聊天室功能

    java編程實現(xiàn)多人聊天室功能

    這篇文章主要為大家詳細(xì)介紹了java編程實現(xiàn)多人聊天室功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07

最新評論