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

SpringCloud Stream RabbitMQ動(dòng)態(tài)路由Key問(wèn)題

 更新時(shí)間:2026年05月06日 09:17:26   作者:這個(gè)饕字怎么讀  
文章主要描述了使用消息中間件實(shí)現(xiàn)不同操作后發(fā)送不同郵件的功能,通過(guò)設(shè)置路由key和group,使得生產(chǎn)者能夠根據(jù)郵件類型發(fā)送消息至對(duì)應(yīng)隊(duì)列,消費(fèi)者則通過(guò)路由key篩選消息

前言

這里有個(gè)業(yè)務(wù)是這樣的,我需要在不同的操作后給用戶發(fā)送不同的郵件,由于比較耗時(shí)所以引入消息中間件,不同的郵件對(duì)應(yīng)的消息類型是不一樣的,所以需要生產(chǎn)者往隊(duì)列里發(fā)送數(shù)據(jù)時(shí)綁定好路由key,例如:

圖里表示交換機(jī)根據(jù)路由key綁定了不同的隊(duì)列。

要達(dá)到這種效果,首先消費(fèi)者肯定是可以根據(jù)路由key來(lái)決定消息是不是發(fā)送給自己的,對(duì)于生產(chǎn)者則需要用到routingKeyExpression 來(lái)決定往哪個(gè)路由key發(fā)送數(shù)據(jù)(大概是這個(gè)意思)。

然后就是stream中的group其實(shí)對(duì)應(yīng)到rabbitMQ中就是隊(duì)列的概念,所以我們這里設(shè)置兩個(gè)不同的group來(lái)對(duì)應(yīng)到不同的隊(duì)列,區(qū)分開(kāi)業(yè)務(wù);

例子

這里我定義了兩個(gè)服務(wù)對(duì)應(yīng)消費(fèi)者和生產(chǎn)者。

生產(chǎn)者

spring:
  application:
    name: producer
  cloud:
    stream:
      binders: # 綁定MQ服務(wù)信息(此處我們是RabbitMQ)
        etpmsRabbitMQ: # 給Binder定義的名稱,?于后?的關(guān)聯(lián)
          type: rabbit # MQ類型,如果是Kafka的話,此處配置kafka
          environment: # MQ環(huán)境配置(?戶名、密碼等)
            spring:
              rabbitmq:
                host: localhost
                port: 5672
                username: admin
                password: xxxxxx
      bindings: # 關(guān)聯(lián)整合通道和binder對(duì)象
        output: # output是我們定義的通道名稱,此處不能亂改
          destination: testExchange # 要使?的Exchange名稱(消息隊(duì)列主題名稱)
          content-type: text/plain # application/json # 消息類型設(shè)置,?如json
          binder: etpmsRabbitMQ # 關(guān)聯(lián)MQ服務(wù)
      rabbit:
        bindings:
          output:
            producer:
              # 生產(chǎn)者配置RabbitMq的動(dòng)態(tài)路由鍵
              routingKeyExpression: headers.type
package top.chenyt.producer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

/**
 * @author yantao.chen
 */
@Service
public class ProviderService {
    /**
     * 將MessageChannel的封裝對(duì)象Source注?到這?使?
     */
    @Autowired
    private Source source;

    public void sendMessage(String content, String type) {
        // 向mq中發(fā)送消息(并不是直接操作mq,應(yīng)該操作的是spring cloud stream)
        // 使?通道向外發(fā)出消息(指的是Source??的output通道)
        source.output().send(MessageBuilder.withPayload(content).setHeader("type",type).build());
    }
}
package top.chenyt;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.cloud.stream.messaging.Source;

/**
 * @ClassName etpms-parent
 * @Author Jinondo
 * @Date 2022/1/31 12:42
 */
@SpringBootApplication
@Slf4j
@EnableBinding({Source.class})
public class ProducerApplication {

    public static void main(String[] args)  {
        SpringApplication.run(ProducerApplication.class, args);
    }

}

主要是yml配置添加:routingKeyExpression: headers.type

發(fā)送消息的時(shí)候setHeader一下

消費(fèi)者

spring:
  application:
    name: consumer
  cloud:
    stream:
      binders: # 綁定MQ服務(wù)信息(此處我們是RabbitMQ)
        etpmsRabbitMQ: # 給Binder定義的名稱,?于后?的關(guān)聯(lián)
          type: rabbit # MQ類型,如果是Kafka的話,此處配置kafka
          environment: # MQ環(huán)境配置(?戶名、密碼等)
            spring:
              rabbitmq:
                host: localhost
                port: 5672
                username: admin
                password: xxxxx
      bindings: # 關(guān)聯(lián)整合通道和binder對(duì)象
        input: # input是我們定義的通道名稱,此處不能亂改
          destination: testExchange # 要使?的Exchange名稱(消息隊(duì)列主題名稱)
          content-type: text/plain # application/json # 消息類型設(shè)置,?如json,自動(dòng)將對(duì)象轉(zhuǎn)為json
          binder: etpmsRabbitMQ # 關(guān)聯(lián)MQ服務(wù)
          group: register
        my-input:
          destination: testExchange # 要使?的Exchange名稱(消息隊(duì)列主題名稱)
          content-type: text/plain # application/json # 消息類型設(shè)置,?如json,自動(dòng)將對(duì)象轉(zhuǎn)為json
          binder: etpmsRabbitMQ # 關(guān)聯(lián)MQ服務(wù)
          group: task
      rabbit:
        bindings:
          my-input:
            consumer:
              bindingRoutingKey: task
          input:
            consumer:
              bindingRoutingKey: register

這里我就定義了兩個(gè)通道,一個(gè)是默認(rèn)的input,一個(gè)是自定的

package top.chenyt.consumer;

import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.SubscribableChannel;

public interface MySink {

    String MY_INPUT = "my-input";

    @Input(MY_INPUT)
    SubscribableChannel myinput();

}

package top.chenyt.consumer;

import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Service;

/**
 * @ClassName etpms-parent
 * @Author Jinondo
 * @Date 2022/1/31 12:42
 */
@Service
public class ConsumerMsg {

    @StreamListener(Sink.INPUT)
    public void receiveMessages(Message<String> message) {
        System.out.println("========= input接收到的消息:" + message.getPayload());
    }

    @StreamListener(MySink.MY_INPUT)
    public void receiveMessages02(Message<String> message) {
        System.out.println("========= myinput接收到的消息:" + message.getPayload());
    }
}
package top.chenyt;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Sink;
import top.chenyt.consumer.MySink;

/**
 * @ClassName etpms-parent
 * @Author Jinondo
 * @Date 2022/1/31 12:42
 */
@SpringBootApplication
@Slf4j
@EnableBinding({Sink.class, MySink.class})
public class ConsumerApplication {

    public static void main(String[] args)  {
        SpringApplication.run(ConsumerApplication.class, args);
    }

}

這樣就能實(shí)現(xiàn)根據(jù)不同的消息類型對(duì)應(yīng)到不同的隊(duì)列且不同的路由key去了

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java中創(chuàng)建寫(xiě)入文件的6種方式詳解與源碼實(shí)例

    java中創(chuàng)建寫(xiě)入文件的6種方式詳解與源碼實(shí)例

    這篇文章主要介紹了java中創(chuàng)建寫(xiě)入文件的6種方式詳解與源碼實(shí)例,Files.newBufferedWriter(Java 8),Files.write(Java 7 推薦),PrintWriter,File.createNewFile,FileOutputStream.write(byte[] b) 管道流,需要的朋友可以參考下
    2022-12-12
  • JAVA8發(fā)送帶有Body的HTTP GET請(qǐng)求

    JAVA8發(fā)送帶有Body的HTTP GET請(qǐng)求

    本文主要介紹了JAVA8發(fā)送帶有Body的HTTP GET請(qǐng)求,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • java使用BeanUtils.copyProperties方法對(duì)象復(fù)制同名字段類型不同賦值為空問(wèn)題解決方案

    java使用BeanUtils.copyProperties方法對(duì)象復(fù)制同名字段類型不同賦值為空問(wèn)題解決方案

    這篇文章主要給大家介紹了關(guān)于java使用BeanUtils.copyProperties方法對(duì)象復(fù)制同名字段類型不同賦值為空問(wèn)題的解決方案,文中通過(guò)代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-11-11
  • k8s部署java項(xiàng)目的實(shí)現(xiàn)

    k8s部署java項(xiàng)目的實(shí)現(xiàn)

    本文主要介紹了k8s部署java項(xiàng)目的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • Java?Optional用法面試題精講

    Java?Optional用法面試題精講

    這篇文章主要為大家介紹了Java?Optional用法面試題精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • 淺談Timer和TimerTask與線程的關(guān)系

    淺談Timer和TimerTask與線程的關(guān)系

    下面小編就為大家?guī)?lái)一篇淺談Timer和TimerTask與線程的關(guān)系。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-03-03
  • MyBatis-Plus 中 的動(dòng)態(tài)SQL 片段(sqlSegment)詳解

    MyBatis-Plus 中 的動(dòng)態(tài)SQL 片段(sqlSegment)詳解

    MyBatis-Plus的sqlSegment通過(guò)Wrapper動(dòng)態(tài)生成SQL片段,支持XML中${ew.customSqlSegment}引用,結(jié)合Lambda表達(dá)式避免硬編碼,適用于動(dòng)態(tài)查詢、邏輯刪除等場(chǎng)景,提升代碼可維護(hù)性與靈活性,本文給大家介紹MyBatis-Plus中的動(dòng)態(tài)SQL片段(sqlSegment)講解,感興趣的朋友一起看看吧
    2025-06-06
  • Spark?SQL配置及使用教程

    Spark?SQL配置及使用教程

    SparkSQL是spark的一個(gè)模塊,主入口是SparkSession,將SQL查詢與Spark程序無(wú)縫混合,這篇文章主要介紹了Spark?SQL配置及使用,需要的朋友可以參考下
    2021-12-12
  • java實(shí)現(xiàn)斗地主游戲

    java實(shí)現(xiàn)斗地主游戲

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)斗地主游戲,洗牌、發(fā)牌、看牌,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Json讀寫(xiě)本地文件實(shí)現(xiàn)代碼

    Json讀寫(xiě)本地文件實(shí)現(xiàn)代碼

    今天沒(méi)事研究了下Gson,寫(xiě)了個(gè)工具類,需要的朋友可以參考下
    2014-03-03

最新評(píng)論

瑞昌市| 东乌| 轮台县| 连江县| 奉新县| 赣榆县| 北京市| 襄城县| 长宁县| 抚松县| 天等县| 武鸣县| 巩留县| 汶川县| 诸暨市| 卢湾区| 翁源县| 福泉市| 浦城县| 万安县| 揭东县| 上思县| 昌平区| 灌云县| 合水县| 博野县| 武义县| 西乌珠穆沁旗| 习水县| 秦安县| 上思县| 义乌市| 阳高县| 井冈山市| 陆川县| 云安县| 宜章县| 乳山市| 吴忠市| 平泉县| 仲巴县|