適合只面對 Kafka, 不涵蓋進階議題
- 一個 application 的架構: 外部系統(middleware) -> input -> application process -> output -> 外部系統
- 在 Spring Cloud Stream
- 透過 Binder 來處理外部系統的細節. ex. spring-cloud-stream-binder-kafka
- 透過 Binding 來處理 input.
- Setup
- Spring Boot Application
- Empty application.properties
- Consumer method (注意! 這裡的 method name: status 就是 Binding name, 會影響 application.properties)
- 指定 input topic application.properties
- 再來就可以發訊息給 status 這個 topic
- Function: 收到一個訊息後, 在尾巴貼上 random suffix, 然後回傳
- 指定 input & output topic in application.properties
- 發訊息給 randomNumberSuffix topic
- Supplier
- 指定 output topic 給 mydate 這個 Supplier binding
- Supplier 只要打開 app 就會自動被執行
- Consumer Group
- 跟 Kafka Consumer Group 的概念一樣
- 同一個 group 裡面只會有一個 consumer 收到 message
- 不同 group 則都會收到訊息
- 預設每個 consumer 都是不同的 group (anomymous group)
- 透過 {binding}.group=xxx 來指定 groupName
- Durability
- 有指定 group, 則對 consumer 的 subscription 就會被保留, 即使這個 group 目前沒有 consumer, 等 consumer 回來, 就會接著收到訊息
- anonymous group 的 subscription 就不會被保留, 因此 anonymous group 容易收到 duplicated message
- Partition
- 一個 topic 可以被切成多個 partition, 每個 partition 會由固定的一個 consumer 接收資料