22 Ekim 2018 Pazartesi

SpringKafka Consumer @EnableKafka Anotasyonu - Kullanmak Şart Değil

Giriş
Spring Boot kullanıyorsak bu anotasyon şart değil. Açıklaması şöyle
... the @EnableKafka annotation is not necessary if you are using Spring Boot with the auto-configuration feature.

When using Spring Boot, the Kafka configuration and setup are automatically performed based on the presence of the Kafka dependencies in your project's classpath. Spring Boot's auto-configuration will detect the Kafka dependencies and configure the necessary Kafka components, including Kafka listeners.
Örnek
Elimizde şöyle bir application.yaml olsun
spring:
  ###
  #   Kafka Settings
  ###
  kafka:
    consumer:
      bootstrap-servers: localhost:9092
      key-deserializer: com.panbet.externalbet.history.report.support
        .ReportsBetKeyJsonDeserializer
      value-deserializer: com.panbet.externalbet.history.report.support
        .ReportsBetJsonDeserializer
      group-id: external.history.group
Şöyle yaparız.
@EnableKafka
@Configuration
public class KafkaConfig {
  private final KafkaProperties properties;


  public KafkaConfig(KafkaProperties properties) {
    this.properties = properties;
  }


  @Bean
  public ConsumerFactory<ReportsBetKeyDto, ReportsBetDto> kafkaConsumerFactory() {
    return new DefaultKafkaConsumerFactory<>(properties.buildConsumerProperties());
  }


  @Bean
  public ConcurrentKafkaListenerContainerFactory<ReportsBetKeyDto, ReportsBetDto>
    kafkaListenerContainerFactory() {
    ConcurrentKafkaListenerContainerFactory<ReportsBetKeyDto, ReportsBetDto> factory
      = new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(kafkaConsumerFactory());
    return factory;
  }
}

Hiç yorum yok:

Yorum Gönder