简体   繁体   English

使用Spring Boot的Kafka集群中的容错

[英]Fault tolerance in Kafka Cluster using Spring Boot

I started work with Spring Boot Kafka and kafka cluster on docker. 我开始在docker上使用Spring Boot Kafka和kafka集群工作。 I've faced the problem when I started to check fault tolerance. 我开始检查容错能力时就遇到了问题。 Generally I created one topic (replication factor = 2) through Kafka Admin in Spring Boot. 通常,我在Spring Boot中通过Kafka Admin创建了一个主题(复制因子= 2)。 I was able to store some messages and consume them from Kafka. 我能够存储一些消息并从Kafka中使用它们。 But after stopping one of the kafka container (I have two kafka instances and one zookeeper) my consumer was not able to communicate which remaining one. 但是在停止了一个kafka容器(我有两个kafka实例和一个Zookeeper)之后,我的使用者无法传达剩下的一个。

Already I provide to bootstrap.servers property: 我已经提供了bootstrap.servers属性:

SPRING_KAFKA_BOOTSTRAP_SERVERS: kafka:9092,kafka2:9093

My kafka admin bean which create topic looks like: 我创建主题的kafka admin bean如下所示:

@Bean
public NewTopic adviceTopic() {
    return new NewTopic(topicName, 1, (short) 2);
}

When I'm using only one instance (it's doesn't matter which one I choose) it works well (so it's not case of wrong configuration kafka listeners/host). 当我只使用一个实例时(选择哪个实例都没有关系),它可以很好地工作(因此,不会出现配置错误的Kafka监听器/主机的情况)。 When I'm removing one instance my application is not able to use the second one kafka instance. 当我删除一个实例时,我的应用程序无法使用第二个kafka实例。

My docker compose looks like: 我的码头工人组成看起来像:

version: '2'
services:
  zookeeper:
    image: wurstmeister/zookeeper
    ports:
    - "2181:2181"
  kafka:
    container_name: kafka
    image: wurstmeister/kafka
    expose:
    - "9092"
    environment:
      KAFKA_LISTENERS: PLAINTEXT://kafka:9092
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'false'
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    depends_on:
      - zookeeper
  kafka2:
    container_name: kafka2
    image: wurstmeister/kafka
    expose:
      - "9093"
    environment:
      KAFKA_LISTENERS: PLAINTEXT://kafka2:9093
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_AUTO_CREATE_TOPICS_ENABLE:
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    depends_on:
      - zookeeper
  kafka-service:
     container_name: kafka_service
     build: .
     ports:
     - "8080:8080"
     environment:
       SPRING_KAFKA_BOOTSTRAP_SERVERS: kafka:9092,kafka2:9093
     depends_on:
       - zookeeper
       - kafka
       - kafka2

The result I've got when I used "docker stop kafka_container": 使用“ docker stop kafka_container”时得到的结果是:

2019-07-19 09:19:16.836 WARN 1 --- [| 2019-07-19 09:19:16.836警告1 --- [| consumers] org.apache.kafka.clients.NetworkClient : [Consumer clientId=json-0, groupId=qpm-consumers] Connection to node 1002 could not be established. 消费者] org.apache.kafka.clients.NetworkClient:[消费者clientId = json-0,groupId = qpm-消费者]无法建立到节点1002的连接。 Broker may not be available. 经纪人可能不可用。

And after that I was not able to produce any messages using my Spring Boot Example. 之后,我无法使用Spring Boot Example生成任何消息。 Could you provide me some tips, why my app was not able to use second one kafka instance? 您能否提供一些提示,为什么我的应用程序无法使用第二个kafka实例? Even if I set properly replication factor? 即使我设置正确的复制因子?

If you have a topic with replication factor 2, the you need at least two nodes running or kafka will complain. 如果您的主题的复制因子为2,则至少需要运行两个节点,否则kafka会抱怨。 To test you may need say 5 nodes (a,b,c,d,e) then set a topic with replication factor of 2 and check which nodes it is using then kill one of it. 要测试您可能需要说5个节点(a,b,c,d,e),然后设置一个复制因子为2的主题,并检查它正在使用的节点,然后杀死其中一个。

I found the solution. 我找到了解决方案。 I need to add to my docker-compose for kafka: KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 2 KAFKA_DEFAULT_REPLICATION_FACTOR: 2 我需要在docker-compose中添加kafka:KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR:2 KAFKA_DEFAULT_REPLICATION_FACTOR:2

Problem was related which __consumer_offsets topic, which has 1 replication factor. 问题与哪个__consumer_offsets主题相关,该主题具有1个复制因子。 Beacuse I'm using two kafka instances, I had to set these properties to 2. 因为我使用了两个kafka实例,所以我不得不将这些属性设置为2。

Detailed answer for this question could be found here: 有关此问题的详细答案,请参见此处:

Kafka - Broker: Group coordinator not available Kafka-经纪人:小组协调员不可用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM