前几天刚看了别人的教程 学了@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之类全都一样。