卡夫卡错误:org.apache.kafka.common.errors.TimeoutException

时间:2018-06-20 08:48:21

标签: java docker spring-boot apache-kafka

我有一个简单的应用程序,正在发送有关该主题的消息。下面是我的代码:

Sender.java

@Component
public class Sender {

 private static final Logger LOGGER =
          LoggerFactory.getLogger(Sender.class);

      @Autowired
      private KafkaTemplate<String, String> kafkaTemplate;

      public void send(String topic, String payload) {
        LOGGER.info("sending payload='{}' to topic='{}'", payload, topic);
        kafkaTemplate.send(topic, payload);
      }
}

SenderConfig.java:

@Configuration
public class SenderConfig {

@Value("${kafka.bootstrap-servers}")
  private String bootstrapServers;

  @Bean
  public Map<String, Object> producerConfigs() {
    Map<String, Object> props = new HashMap<>();
    // list of host:port pairs used for establishing the initial connections to the Kakfa cluster
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,
        bootstrapServers);
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
        StringSerializer.class);
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
        StringSerializer.class);

    return props;
  }

  @Bean
  public ProducerFactory<String, String> producerFactory() {
    return new DefaultKafkaProducerFactory<>(producerConfigs());
  }

  @Bean
  public KafkaTemplate<String, String> kafkaTemplate() {
    return new KafkaTemplate<>(producerFactory());
  }

  @Bean
  public Sender sender() {
    return new Sender();
  }
}

Receiver.java

@Component
 public class Receiver {

private static final Logger LOGGER =
          LoggerFactory.getLogger(Receiver.class);

@Autowired
  private KafkaListenerEndpointRegistry kafkaListenerEndpointRegistry;

      private CountDownLatch latch = new CountDownLatch(1);

      public CountDownLatch getLatch() {
        return latch;
      }

      @KafkaListener(topics = "${kafka.topic.helloworld}")
      public void receive(String payload) {
        LOGGER.info("received payload='{}'", payload);
        latch.countDown();
      }
}

ReceiverConfig.java

@EnableKafka
@Configuration
public class ReceiverConfig {

@Value("${kafka.bootstrap-servers}")
  private String bootstrapServers;

  @Bean
  public Map<String, Object> consumerConfigs() {
    Map<String, Object> props = new HashMap<>();
    // list of host:port pairs used for establishing the initial connections to the Kafka cluster
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
        bootstrapServers);
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
        StringDeserializer.class);
    props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
        StringDeserializer.class);
    // allows a pool of processes to divide the work of consuming and processing records
    props.put(ConsumerConfig.GROUP_ID_CONFIG, "helloworld");
    // automatically reset the offset to the earliest offset
    props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

    return props;
  }

  @Bean
  public ConsumerFactory<String, String> consumerFactory() {
    return new DefaultKafkaConsumerFactory<>(consumerConfigs());
  }

  @Bean
  public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory() {
    ConcurrentKafkaListenerContainerFactory<String, String> factory =
        new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(consumerFactory());

    return factory;
  }

  @Bean
  public Receiver receiver() {
    return new Receiver();
  }
}

application.yml

 kafka:
   bootstrap-servers: localhost:9092
   topic:
      name: myTopic
 server:
    port: 9191

当我发布到不存在的主题时,以上代码可以正常工作。它创建主题。例如,在上述application.yml文件中,如果主题名称为“ myTopic”(不存在),则代码可以正常工作,并且消息已被使用。但是,如果我在docker yml中的哪个地方运行docker,则需要创建一些主题名称。如果我尝试发布有关这些主题的内容,则会收到以下超时异常。

Kafka error: org.apache.kafka.common.errors.TimeoutException: Expiring 1 record(s) for TOPICNAME-0: 30010 ms has passed since batch creation plus linger time

我在Stack Overflow上浏览了一些帖子,但是他们建议增加超时时间,但就我而言,这只是我发布的一条消息。因此,我不确定为什么会收到此错误。

0 个答案:

没有答案