Kafka集群模式端口配置

时间:2019-03-01 17:34:14

标签: docker apache-kafka apache-zookeeper confluent

我正在尝试部署具有3个zookeeper和3个kafka节点的docker kafka集群。 kafka节点一直死于打印以下错误:

private void showFileChooser () {

        mHandler.post(new Runnable() {
            @Override
            public void run() {

                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
            }
        });
}

private void showFileChooser2 () {

    mHandler.post(new Runnable() {
        @Override
        public void run() {

            Intent intent2 = new Intent();
            intent2.setType("image/*");
            intent2.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent2, "Select Picture"), PICK_IMAGE_REQUEST2);

        }
    });
}


 @Override
        protected void onActivityResult ( int requestCode, int resultCode, Intent data){
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
                Uri filePath = data.getData();
                try {
                    //Getting the Bitmap from Gallery
                    bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                    rbitmap = getResizedBitmap(bitmap, 1000);//Setting the Bitmap to ImageView
                    imageViewUserImage.setImageBitmap(rbitmap);
                    bitmap.recycle;
                    imageViewUserImage.requestFocus();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else if (requestCode == PICK_IMAGE_REQUEST2 && resultCode == RESULT_OK && data != null && data.getData() != null) {
                Uri filePath2 = data.getData();
                try {
                    //Getting the Bitmap from Gallery
                    bitmap2 = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath2);
                    rbitmap2 = getResizedBitmap(bitmap2, 1000);//Setting the Bitmap to ImageView
                    imageViewUserImage2.setImageBitmap(rbitmap2);
                    bitmap2.recycle;
                    imageViewUserImage2.requestFocus();
                } catch (IOException e) {
                    e.printStackTrace();
                }
}
}

当Zookeeper节点继续打印时:

[main-SendThread(zookeeper-1:2181)] INFO org.apache.zookeeper.ClientCnxn - Opening socket connection to server zookeeper-1/10.0.0.5:2181. Will not attempt to authenticate using SASL (unknown error)
[main-SendThread(zookeeper-1:2181)] INFO org.apache.zookeeper.ClientCnxn - Socket connection established to zookeeper-1/10.0.0.5:2181, initiating session
[main-SendThread(zookeeper-1:2181)] INFO org.apache.zookeeper.ClientCnxn - Unable to read additional data from server sessionid 0x0, likely server has closed socket, closing socket connection and attempting reconnect

看起来Zookeeper节点无法相互通信,但是我检查了docker-compose yaml几次,但找不到奇数。有人可以帮我吗?

docker-compose.yaml:

WARN Cannot open channel to 1 at election address
zookeeper-1/10.0.0.5:3888 (org.apache.zookeeper.server.quorum.QuorumCnxManager)
    java.net.ConnectException: Connection refused (Connection refused)
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:558)
        at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectAll(QuorumCnxManager.java:610)
        at org.apache.zookeeper.server.quorum.FastLeaderElection.lookForLeader(FastLeaderElection.java:838)
        at org.apache.zookeeper.server.quorum.QuorumPeer.run(QuorumPeer.java:957)
    [2019-03-01 17:24:30,930] INFO Resolved hostname: zookeeper-1 to address: zookeeper-1/10.0.0.5 (org.apache.zookeeper.server.quorum.QuorumPeer)
    [2019-03-01 17:24:30,931] WARN Cannot open channel to 2 at election address zookeeper-2/10.0.0.7:3888 (org.apache.zookeeper.server.quorum.QuorumCnxManager)
    java.net.ConnectException: Connection refused (Connection refused)
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:558)
        at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectAll(QuorumCnxManager.java:610)
        at org.apache.zookeeper.server.quorum.FastLeaderElection.lookForLeader(FastLeaderElection.java:838)
        at org.apache.zookeeper.server.quorum.QuorumPeer.run(QuorumPeer.java:957)

2 个答案:

答案 0 :(得分:3)

最后我在这里很奇怪:https://forums.docker.com/t/cannot-get-zookeeper-to-work-running-in-docker-using-swarm-mode/27109/2

似乎要使Zookeeper群集正常工作,当KAFKA_BROKER_ID为x时,您需要将0.0.0.0指定为主机名x。似乎端口号也没有关系,因为它们实际上是不同的机器。

这是我的新撰写文件:

version: '3.3'

networks:
  kafka_example:
      driver: overlay

services:
  zookeeper-1:
      image: confluentinc/cp-zookeeper:5.0.1
      volumes:
        - '/volumedockerkafka/zookeeper-1/zookeeper_data:/var/lib/zookeeper:rw'
        - '/volumedockerkafka/zookeeper-1/zookeeper_etc:/etc/zookeeper:rw'
      environment:
        ZOOKEEPER_SERVER_ID: 1
        ZOOKEEPER_CLIENT_PORT: 2181
        ZOOKEEPER_SERVERS: 0.0.0.0:2888:3888;zookeeper-2:2888:3888;zookeeper-3:2888:3888
        ZOOKEEPER_TICK_TIME: 2000
        ZOOKEEPER_INIT_LIMIT: 5
        ZOOKEEPER_SYNC_LIMIT: 2
        ZOOKEEPER_MAX_CLIENT_CNXNS: 200
      networks:
      - kafka_example

  zookeeper-2:
      image: confluentinc/cp-zookeeper:5.0.1
      volumes:
        - '/volumedockerkafka/zookeeper-2/zookeeper_data:/var/lib/zookeeper:rw'
        - '/volumedockerkafka/zookeeper-2/zookeeper_etc:/etc/zookeeper:rw'
      environment:
        ZOOKEEPER_SERVER_ID: 2
        ZOOKEEPER_CLIENT_PORT: 2181
        ZOOKEEPER_SERVERS: zookeeper-1:2888:3888;0.0.0.0:2888:3888;zookeeper-3:2888:3888
        ZOOKEEPER_TICK_TIME: 2000
        ZOOKEEPER_INIT_LIMIT: 5
        ZOOKEEPER_SYNC_LIMIT: 2
        ZOOKEEPER_MAX_CLIENT_CNXNS: 200
      networks:
        - kafka_example

  zookeeper-3:
      image: confluentinc/cp-zookeeper:5.0.1
      volumes:
        - '/volumedockerkafka/zookeeper-3/zookeeper_data:/var/lib/zookeeper:rw'
        - '/volumedockerkafka/zookeeper-3/zookeeper_etc:/etc/zookeeper:rw'
      environment:
        ZOOKEEPER_SERVER_ID: 3
        ZOOKEEPER_CLIENT_PORT: 2181
        ZOOKEEPER_SERVERS: zookeeper-1:2888:3888;zookeeper-2:2888:3888;0.0.0.0:2888:3888
        ZOOKEEPER_TICK_TIME: 2000
        ZOOKEEPER_INIT_LIMIT: 5
        ZOOKEEPER_SYNC_LIMIT: 2
        ZOOKEEPER_MAX_CLIENT_CNXNS: 200
      networks:
        - kafka_example

  message_queue_kafka-1:
      image: confluentinc/cp-kafka:5.0.1
      volumes:
        - '/volumedockerkafka/kafka-1/kafka_data:/var/lib/kafka/data:rw'
        - '/volumedockerkafka/kafka-1/kafka_etc:/etc/kafka:rw'
      environment:
        KAFKA_BROKER_ID: 1
        KAFKA_ZOOKEEPER_CONNECT: zookeeper-1:2181,zookeeper-2:2181,zookeeper-3:2181
        KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://message_queue_kafka-1:9092
        KAFKA_SESSION_TIMEOUT_MS: 60000
        KAFKA_HEARTBEAT_INTERVAL_MS: 20000
        KAFKA_REBALANCE_TIMEOUT_MS: 300000
      depends_on:
        - zookeeper-1
        - zookeeper-2
        - zookeeper-3
      networks:
        - kafka_example

  message_queue_kafka-2:
      image: confluentinc/cp-kafka:5.0.1
      volumes:
        - '/volumedockerkafka/kafka-2/kafka_data:/var/lib/kafka/data:rw'
        - '/volumedockerkafka/kafka-2/kafka_etc:/etc/kafka:rw'
      environment:
        KAFKA_BROKER_ID: 2
        KAFKA_ZOOKEEPER_CONNECT: zookeeper-1:2181,zookeeper-2:2181,zookeeper-3:2181
        KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://message_queue_kafka-2:9092
        KAFKA_SESSION_TIMEOUT_MS: 60000
        KAFKA_HEARTBEAT_INTERVAL_MS: 20000
        KAFKA_REBALANCE_TIMEOUT_MS: 300000
      depends_on:
        - zookeeper-1
        - zookeeper-2
        - zookeeper-3
      networks:
        - kafka_example

  message_queue_kafka-3:
      image: confluentinc/cp-kafka:5.0.1
      volumes:
        - '/volumedockerkafka/kafka-3/kafka_data:/var/lib/kafka/data:rw'
        - '/volumedockerkafka/kafka-3/kafka_etc:/etc/kafka:rw'
      environment:
        KAFKA_BROKER_ID: 3
        KAFKA_ZOOKEEPER_CONNECT: zookeeper-1:2181,zookeeper-2:2181,zookeeper-3:2181
        KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://message_queue_kafka-3:9092
        KAFKA_SESSION_TIMEOUT_MS: 60000
        KAFKA_HEARTBEAT_INTERVAL_MS: 20000
        KAFKA_REBALANCE_TIMEOUT_MS: 300000
      depends_on:
        - zookeeper-1
        - zookeeper-2
        - zookeeper-3
      networks:
        - kafka_example

答案 1 :(得分:0)

我有一个类似的设置,3个动物园管理员和3个经纪人,并且运行良好。我注意到从您的设置中发现的三件事与我的不同,而我肯定知道的第二件事是一个问题(我做了同样的事情但没有工作)

  1. 对于ZOOKEEPER_SERVERS,所有动物园管理员2888:3888使用相同的端口组合。据我了解,您应该有不同的端口check this example as a reference
  2. 您为所有Zookeeper实例使用相同的ZOOKEEPER_CLIENT_PORT,考虑为每个实例使用不同的实例,并且与ZOOKEEPER_SERVERS中使用的端口也不同。
  3. 对于kafka服务,环境变量KAFKA_ZOOKEEPER_CONNECT的值用单引号引起来,我认为这不是问题,但与其余部分不一致您的配置。

考虑然后将ZOOKEEPER_SERVERSZOOKEEPER_CLIENT_PORT的值更改为例如以下内容:

ZOOKEEPER_CLIENT_PORT: 22181
ZOOKEEPER_SERVERS: zookeeper-1:22888:23888;zookeeper-2:32888:33888;zookeeper-3:42888:43888

还要记住,要相应地调整KAFKA_ZOOKEEPER_CONNECT的值:

KAFKA_ZOOKEEPER_CONNECT: zookeeper-1:22181,zookeeper-2:32181,zookeeper-3:42181 
  

clientPort 用于侦听客户端连接的端口

     

server.X ...,请注意每个服务器名称后的两个端口号:“ 2888”和“ 3888”。对等方使用前一个端口连接到其他对等方。这种连接是必需的,以便对等方可以进行通信,例如,以商定更新顺序。更具体地说,ZooKeeper服务器使用此端口将关注者连接到领导者。当出现新的领导者时,跟随者使用此端口打开与领导者的TCP连接。因为默认的领导者选举也使用TCP,所以我们当前需要另一个端口来进行领导者选举。这是服务器条目中的第二个端口。

Zookeeper Docs提取

所有看起来都应该像这样(尚未测试):

version: '3.3'

networks:
kafka_example:
    driver: overlay

services:
zookeeper-1:
    image: confluentinc/cp-zookeeper:5.0.1
    volumes:
    - '/volumedockerkafka/zookeeper-1/zookeeper_data:/var/lib/zookeeper:rw'  # mount point
    - '/volumedockerkafka/zookeeper-1/zookeeper_etc:/etc/zookeeper:rw'  # mount point
    environment:
    ZOOKEEPER_SERVER_ID: 1
    ZOOKEEPER_CLIENT_PORT: 22181
    ZOOKEEPER_SERVERS: zookeeper-1:22888:23888;zookeeper-2:32888:33888;zookeeper-3:42888:43888
    ZOOKEEPER_TICK_TIME: 2000
    ZOOKEEPER_INIT_LIMIT: 5
    ZOOKEEPER_SYNC_LIMIT: 2
    networks:
    - kafka_example

zookeeper-2:
    image: confluentinc/cp-zookeeper:5.0.1
    volumes:
    - '/volumedockerkafka/zookeeper-2/zookeeper_data:/var/lib/zookeeper:rw'  # mount point
    - '/volumedockerkafka/zookeeper-2/zookeeper_etc:/etc/zookeeper:rw'  # mount point
    environment:
    ZOOKEEPER_SERVER_ID: 2
    ZOOKEEPER_CLIENT_PORT: 32181
    ZOOKEEPER_SERVERS: zookeeper-1:22888:23888;zookeeper-2:32888:33888;zookeeper-3:42888:43888
    ZOOKEEPER_TICK_TIME: 2000
    ZOOKEEPER_INIT_LIMIT: 5
    ZOOKEEPER_SYNC_LIMIT: 2
    networks:
    - kafka_example

zookeeper-3:
    image: confluentinc/cp-zookeeper:5.0.1
    volumes:
    - '/volumedockerkafka/zookeeper-3/zookeeper_data:/var/lib/zookeeper:rw'  # mount point
    - '/volumedockerkafka/zookeeper-3/zookeeper_etc:/etc/zookeeper:rw'  # mount point
    environment:
    ZOOKEEPER_SERVER_ID: 3
    ZOOKEEPER_CLIENT_PORT: 42181
    ZOOKEEPER_SERVERS: zookeeper-1:22888:23888;zookeeper-2:32888:33888;zookeeper-3:42888:43888
    ZOOKEEPER_TICK_TIME: 2000
    ZOOKEEPER_INIT_LIMIT: 5
    ZOOKEEPER_SYNC_LIMIT: 2
    networks:
    - kafka_example

message_queue_kafka-1:
    image: confluentinc/cp-kafka:5.0.1
    volumes:
    - '/volumedockerkafka/kafka-1/kafka_data:/var/lib/kafka/data:rw'  # mount point
    - '/volumedockerkafka/kafka-1/kafka_etc:/etc/kafka:rw'
    environment:
    KAFKA_LOG4J_OPTS: '-Dlog4j.configuration=file:/etc/kafka/json_log4j.properties'
    KAFKA_ZOOKEEPER_CONNECT: zookeeper-1:22181,zookeeper-2:32181,zookeeper-3:42181
    KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://message_queue_kafka-1:9092
    KAFKA_BROKER_ID: 1
    KAFKA_SESSION_TIMEOUT_MS: 60000
    KAFKA_HEARTBEAT_INTERVAL_MS: 20000
    KAFKA_REBALANCE_TIMEOUT_MS: 300000
    depends_on:
    - zookeeper-1
    - zookeeper-2
    - zookeeper-3
    networks:
    - kafka_example

message_queue_kafka-2:
    image: confluentinc/cp-kafka:5.0.1
    volumes:
    - '/volumedockerkafka/kafka-2/kafka_data:/var/lib/kafka/data:rw'  # mount point
    - '/volumedockerkafka/kafka-2/kafka_etc:/etc/kafka:rw'
    environment:
    KAFKA_LOG4J_OPTS: '-Dlog4j.configuration=file:/etc/kafka/json_log4j.properties'
    KAFKA_ZOOKEEPER_CONNECT: zookeeper-1:22181,zookeeper-2:32181,zookeeper-3:42181
    KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://message_queue_kafka-2:9092
    KAFKA_BROKER_ID: 2ori
    KAFKA_SESSION_TIMEOUT_MS: 60000
    KAFKA_HEARTBEAT_INTERVAL_MS: 20000
    KAFKA_REBALANCE_TIMEOUT_MS: 300000
    depends_on:
    - zookeeper-1
    - zookeeper-2
    - zookeeper-3
    networks:
    - kafka_example

message_queue_kafka-3:
    image: confluentinc/cp-kafka:5.0.1
    volumes:
    - '/volumedockerkafka/kafka-3/kafka_data:/var/lib/kafka/data:rw'  # mount point
    - '/volumedockerkafka/kafka-3/kafka_etc:/etc/kafka:rw'
    environment:
    KAFKA_LOG4J_OPTS: '-Dlog4j.configuration=file:/etc/kafka/json_log4j.properties'
    KAFKA_ZOOKEEPER_CONNECT: zookeeper-1:22181,zookeeper-2:32181,zookeeper-3:42181
    KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://message_queue_kafka-3:9092
    KAFKA_BROKER_ID: 3
    KAFKA_SESSION_TIMEOUT_MS: 60000
    KAFKA_HEARTBEAT_INTERVAL_MS: 20000
    KAFKA_REBALANCE_TIMEOUT_MS: 300000
    depends_on:
    - zookeeper-1
    - zookeeper-2
    - zookeeper-3
    networks:
    - kafka_example

更新

这是我正在使用的docker-compose,在我的设置中,容器可以位于两个物理主机中的任何一个中。

version: '2'
services:
zookeeper-3:
    image: confluentinc/cp-zookeeper:5.1.1
    environment:
    ZOOKEEPER_CLIENT_PORT: '42181'
    ZOOKEEPER_INIT_LIMIT: '5'
    ZOOKEEPER_SERVERS: zookeeper-1:22888:23888;zookeeper-2:32888:33888;zookeeper-3:42888:43888
    ZOOKEEPER_SERVER_ID: '3'
    ZOOKEEPER_SYNC_LIMIT: '2'
    ZOOKEEPER_TICK_TIME: '2000'
zookeeper-2:
    image: confluentinc/cp-zookeeper:5.1.1
    environment:
    ZOOKEEPER_CLIENT_PORT: '32181'
    ZOOKEEPER_INIT_LIMIT: '5'
    ZOOKEEPER_SERVERS: zookeeper-1:22888:23888;zookeeper-2:32888:33888;zookeeper-3:42888:43888
    ZOOKEEPER_SERVER_ID: '2'
    ZOOKEEPER_SYNC_LIMIT: '2'
    ZOOKEEPER_TICK_TIME: '2000'
zookeeper-1:
    image: confluentinc/cp-zookeeper:5.1.1
    environment:
    ZOOKEEPER_CLIENT_PORT: '22181'
    ZOOKEEPER_INIT_LIMIT: '5'
    ZOOKEEPER_SERVERS: zookeeper-1:22888:23888;zookeeper-2:32888:33888;zookeeper-3:42888:43888
    ZOOKEEPER_SERVER_ID: '1'
    ZOOKEEPER_SYNC_LIMIT: '2'
    ZOOKEEPER_TICK_TIME: '2000'
kafka-1:
    image: confluentinc/cp-enterprise-kafka:5.1.1
    environment:
    KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka-1:19092
    KAFKA_BROKER_ID: '1'
    KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT
    KAFKA_ZOOKEEPER_CONNECT: zookeeper-1:22181,zookeeper-2:32181,zookeeper-3:42181
    KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: '0'
kafka-2:
    image: confluentinc/cp-enterprise-kafka:5.1.1
    environment:
    KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka-2:29092
    KAFKA_BROKER_ID: '2'
    KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT
    KAFKA_ZOOKEEPER_CONNECT: zookeeper-1:22181,zookeeper-2:32181,zookeeper-3:42181
    KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: '0'
kafka-3:
    image: confluentinc/cp-enterprise-kafka:5.1.1
    environment:
    KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka-3:39092
    KAFKA_BROKER_ID: '3'
    KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT
    KAFKA_ZOOKEEPER_CONNECT: zookeeper-1:22181,zookeeper-2:32181,zookeeper-3:42181
    KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: '0'