1. 啓動kafka
參考:Kafaka入門(1)- Kafka簡介和安裝與啓動(mac)
- 啓動zookeeper
- 啓動kafka
2. springboot集成kafka
2.1 pom.xml文件,引入依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--kafka-->
<!-- https://mvnrepository.com/artifact/org.springframework.kafka/spring-kafka -->
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.4.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.2 application.properties 配置(生產者)
各個配置的解釋見:spring boot 附錄中的 kafka 配置,搜索kafka 關鍵字定位。
#============== kafka ===================
# 指定kafka server的地址,集羣配多個,中間,逗號隔開
spring.kafka.bootstrap-servers=127.0.0.1:9092
#=============== producer =======================
# 寫入失敗時,重試次數。當leader節點失效,一個repli節點會替代成爲leader節點,此時可能出現寫入失敗,
# 當retris爲0時,produce不會重複。retirs重發,此時repli節點完全成爲leader節點,不會產生消息丟失。
spring.kafka.producer.retries=0
# 每次批量發送消息的數量,produce積累到一定數據,一次發送
spring.kafka.producer.batch-size=16384
# produce積累數據一次發送,緩存大小達到buffer.memory就發送數據
spring.kafka.producer.buffer-memory=33554432
#procedure要求leader在考慮完成請求之前收到的確認數,用於控制發送記錄在服務端的持久化,其值可以爲如下:
#acks = 0 如果設置爲零,則生產者將不會等待來自服務器的任何確認,該記錄將立即添加到套接字緩衝區並視爲已發送。在這種情況下,無法保證服務器已收到記錄,並且重試配置將不會生效(因爲客戶端通常不會知道任何故障),爲每條記錄返回的偏移量始終設置爲-1。
#acks = 1 這意味着leader會將記錄寫入其本地日誌,但無需等待所有副本服務器的完全確認即可做出迴應,在這種情況下,如果leader在確認記錄後立即失敗,但在將數據複製到所有的副本服務器之前,則記錄將會丟失。
#acks = all 這意味着leader將等待完整的同步副本集以確認記錄,這保證了只要至少一個同步副本服務器仍然存活,記錄就不會丟失,這是最強有力的保證,這相當於acks = -1的設置。
#可以設置的值爲:all, -1, 0, 1
spring.kafka.producer.acks=1
# 指定消息key和消息體的編解碼方式
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
2.3生產者向kafka發送消息
@RestController
public class KafkaController {
@Autowired
private KafkaTemplate<String,Object> kafkaTemplate;
@GetMapping("/kafka/send")
public boolean send(@RequestParam String message){
kafkaTemplate.send("sunday",message);
return true;
}
2.4 application.properties配置
#=============== consumer =======================
# 指定默認消費者group id --> 由於在kafka中,同一組中的consumer不會讀取到同一個消息,依靠groud.id設置組名
spring.kafka.consumer.group-id=testGroup
# smallest和largest纔有效,如果smallest重新0開始讀取,如果是largest從logfile的offset讀取。一般情況下我們都是設置smallest
spring.kafka.consumer.auto-offset-reset=earliest
# enable.auto.commit:true --> 設置自動提交offset
spring.kafka.consumer.enable-auto-commit=true
#如果'enable.auto.commit'爲true,則消費者偏移自動提交給Kafka的頻率(以毫秒爲單位),默認值爲5000。
spring.kafka.consumer.auto-commit-interval=100
# 指定消息key和消息體的編解碼方式
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
2.5 消費者監聽topic的消息
@Component
public class ConsumerListener {
@KafkaListener(topics = "sunday")
public void onMessage(String message){
//insertIntoDb(buffer);//這裏爲插入數據庫代碼
System.out.println(message);
}
}
訪問:http://localhost:8080/message/send?message=k1。即可看到kafka生產和消費過程
到此,採用Kafka提供的StringSerializer和StringDeserializer進行序列化和反序列化,此種序列化方式無法序列化實體類