前幾天剛看了別人的教程 學了@Input @OutPut 這種寫法,然后今天看了個開源項目里面已經用了SpringCloud Stream3.0.x新特性,@Input @OutPut 方式已經過時 淘汰了函數式調用...我丟!又要學!
一、yml配置
--- # rocketmq 配置
spring:
cloud:
stream:
function:
# 重點配置 與 binding 名與消費者對應,多個可以使用;
definition: demo
rocketmq:
binder:
# rocketmq 地址
name-server: localhost:9876
bindings:
demo-out-0:
content-type: application/json
destination: stream-test-topic
group: test-group
binder: rocketmq
demo-in-0:
content-type: application/json
destination: stream-test-topic
group: test-group
binder: rocketmq
二、生產者
@Autowired
private StreamBridge streamBridge;
public void streamTestMsg(String msg){
// 構建消息對象
TestMessaging testMessaging = new TestMessaging()
.setMsgId(UUID.randomUUID().toString())
.setMsgText(msg);
streamBridge.send("demo-out-0", MessageBuilder.withPayload(testMessaging).build());
}
三、消費者
@Bean
Consumer<TestMessaging> demo() {
log.error("初始化訂閱");
return msg -> {
log.error("通過stream消費到消息 => {}", msg.toString());
};
}
總結
直接 簡單了10倍,kafka之類全都一樣。