Verwenden Sie Kafka mit Microservices? Höchstwahrscheinlich behandeln Sie Neuübertragungen falsch







Apache Kafka hat sich zur führenden Plattform für die asynchrone Kommunikation zwischen Microservices entwickelt. Es verfügt über leistungsstarke Funktionen, mit denen Sie fehlertolerante und fehlertolerante asynchrone Architekturen erstellen können.







. , (, ) , , .







: . , , . , , , .







Kafka



, , Kafka. , Kafka , . .







,



Kafka — . Kafka :







(event log), .

(producers, publishers), .

(consumers), .













, RabbitMQ, Kafka , (.. Kafka pull, push ). (offset), ( ) . .









(topics), , . , , , :







  • , .
  • , .




(partitions). . Kafka (partition key). — ( , , ), .













UUID . (, UUID ), - .







, , , .







, . Kafka .







Kafka



Kafka . , . , .









, . (.. ), . - , .







, , , ..







. .







(bounded context) - .







Kafka .













, : ( ) . , : , .







(cross-boundary event publishing).







, (aggregates). — , . «» , , .







UserAccount. , , User ( ID: , , ..). , (EmailAddress, PhoneNumber ..). , ID ID.







UserAccount , (JSON Avro, ) UserAccount. , ID . UserAccount .







, ?



Kafka , . . , , , : .













?









, , . , … , - .







, . , . : ?







(retry)?



, (.. ), . , ?







, . . , .







, ?



. , “ ”, UserAccount, . (, HTTP 400) .







, . , -, . , .







, . , . , , , .







, , .







?



. , , , , . : , .







Retry topic:



, , (retry topic). , :







  1. .
  2. , , .
  3. , , . . , .
  4. , ( (backoff strategy)). , , (dead letter queue, DLQ), .








retry-topic ( ) , . , , 1 , . , , . , , (dead letter queue, DLQ)







?



, . . , . , , .









: , : .







(recoverable errors) — , , , . , . , , . , .







- : , . , , . ( . «» , — — . , - — — .)







, . , . () . - , , ..







(non-recoverable errors) — , , , . , NullPointerException. - .







, , . , , , ..







, , , «» ( «» : !) , , NULLS , .







?







, . , , . , , . , , … , .







? . , (, ).







, , . , …









. Kafka. , ID .







? , .







. ? , . , , , . .







. User , . Zoey ZoZoiee. , (, Login) Zoiee, Zo.







Login User. , Zoiee : « , Zo!»







retry topic. . , Zo, , . , Zoiee, , Zo.







Zoiee/Zo . . , . , - , . , , , .







retry topic ?



, . , , . , , — . :







  • , - .
  • , ( , .
  • , .


.









. . . — , .. — .







. . , . , , . , , .







, 100% , :







  • , , ,
  • , .


?



, , , ?







, , , . , .









, . , , .







, :







void processMessage(KafkaMessage km) {
 try {
   Message m = km.getMessage();
   transformAndSave(m);
 } catch (Throwable t) {
   if (isRecoverable(t)) {
     // ...
   } else {
     // ...
   }
 }
}
      
      





- Java isRecoverable()



(whitelist approach) , t . , t, (, SQL REsT), true, . , false. .







, . , SQLException () (). , , , , stash topic ( -), … , .









, . , . , .







, .







. . -Java - :







void processMessage(KafkaMessage km) {
 try {
   Message m = km.getMessage();
   transformAndSave(m);
 } catch (Throwable t) {
   if (isRecoverable(t)) {
     doWithRetry(m, Backoff.EXPONENTIAL, this::transformAndSave);
   } else {
     // ...
   }
 }
}
      
      





(, , , , ).







,



, , , , , . ? . n , DLQ. ?







, - , . DLQ, . .









, . User/Login. Login , Zo. , . Zoiee .













Zo , Zoiee . .







, , . Zo , .













Zo, . User Zoiee, Login Zo.







, . Zo Login Zoiee, . , .







ID , , ID. , … .







, (: , !). . , -. . , ( , ), .







, , . , :







  • , ;
  • , .


, , -.







?



. , . , .







, ( “ ”) . , .









, , . . Kafka, . , — , — , .







, , , .







, . , , :







  • , Kafka , .
  • .
  • , .
  • Kafka , . ? . , ? .
  • , .


:

, Apache Kafka, Stripe. . , .







:










All Articles