SpringCloud Stream RabbitMQ動(dòng)態(tài)路由Key問(wèn)題
前言
這里有個(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.typepackage 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è)參考,也希望大家多多支持腳本之家。
- SpringCloudStream+RabbitMQ使用中遇到的問(wèn)題及解決
- 解決SpringCloudStream整合Kafka,兩個(gè)通道對(duì)應(yīng)同一個(gè)topic報(bào)錯(cuò)的情況
- SpringCloud?Stream?快速入門(mén)實(shí)例教程
- SpringCloud使用Kafka Streams實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)處理
- SpringCloudStream原理和深入使用小結(jié)
- SpringCloud中的Stream服務(wù)間消息傳遞詳解
- SpringCloudStream中的消息分區(qū)數(shù)詳解
相關(guān)文章
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)求,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
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),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
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
Json讀寫(xiě)本地文件實(shí)現(xiàn)代碼
今天沒(méi)事研究了下Gson,寫(xiě)了個(gè)工具類,需要的朋友可以參考下2014-03-03

