HyperLedger Fabric 1.4.4搭建基于多节点kafka共识集群

修改configtx.yaml配置文件

SampleDevModeKafka下增加kafkabroker节点,我增加后的内容如下

SampleDevModeKafka:
        <<: *ChannelDefaults
        Capabilities:
            <<: *ChannelCapabilities
        Orderer:
            <<: *OrdererDefaults
            OrdererType: kafka
            Kafka:
                Brokers:
                - kafka1.example.com:9082
                - kafka2.example.com:9083
                - kafka3.example.com:9084

            Organizations:
            - *OrdererOrg
            Capabilities:
                <<: *OrdererCapabilities
        Application:
            <<: *ApplicationDefaults
            Organizations:
            - <<: *OrdererOrg
        Consortiums:
            SampleConsortium:
                Organizations:
                - *Org1
                - *Org2

生成通道的创世区块

将原来solo共识的命令改为如下命令
configtxgen -profile SampleDevModeKafka -channelID kafka-channel -outputBlock ./channel-artifacts/genesis.block

docker-compose-kafka.yaml

新建docker-compose-kafka.yaml文件,内容如下:

# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#


# NOTE: This is not the way a Kafka cluster would normally be deployed in production, as it is not secure
# and is not fault tolerant. This example is a toy deployment that is only meant to exercise the Kafka code path
# of the ordering service.

version: '2'

services:
  zookeeper1.example.com:
    container_name: zookeeper1.example.com
    hostname: zookeeper1.example.com
    image: hyperledger/fabric-zookeeper:latest
    ports:
      - "2171:2181"
      - "2878:2878"
      - "3878:3878"
    environment:
      # 这里是zookeeper客户端的端口号,一般无用
      # 需要注意的是zookeeper的端口号依然是2181
      # 所以一个主机装多个zookeeper的时候,需要将2181映射到外部的其他端口上
      ZOOKEEPER_CLIENT_PORT: 32171
      ZOOKEEPER_TICK_TIME: 2000
      ZOO_MY_ID: 1
      ZOO_SERVERS: server.1=zookeeper1.example.com:2878:3878 server.2=zookeeper2.example.com:2879:3879 server.3=zookeeper3.example.com:2880:3880
    extra_hosts:
      - "kafka1.example.com:10.1.24.225"
      - "zookeeper2.example.com:10.1.24.225"
      - "kafka2.example.com:10.1.24.225"
      - "zookeeper3.example.com:10.1.24.225"
      - "kafka3.example.com:10.1.24.225"

  kafka1.example.com:
    container_name: kafka1.example.com
    image: hyperledger/fabric-kafka:latest
    depends_on:
      - zookeeper1.example.com
      - zookeeper2.example.com
      - zookeeper3.example.com
    ports:
      - "9082:9082"
    environment:
      - KAFKA_BROKER_ID=1
      - KAFKA_ZOOKEEPER_CONNECT=zookeeper1.example.com:2171,zookeeper2.example.com:2172,zookeeper3.example.com:2173
      # 如果你的orderer节点和kafka节点处于一个docker网络中,或者处在一台主机上,那么下面两个配置是不必须的,可以直接注释掉
      # 虽然我们是内网环境,但是因为给这个docker容器分配的ip地址是不确定的,
      # 所以需要通过这样的方式来实现监听所有转到这个端口的请求,
      # 一定要将端口号也修改成映射出去的端口,否则还是监听不到
      - KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9082
      # 这个是kafka外网监听配置,我们正常使用过程中其实是用不到这个配置的,只是为了可以使用上面的0.0.0.0的配置
      # 所以这里写任何hostname都是可以的,但是为了明确性,还是监听了kafka1.example.com,即容器名
      - KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://kafka1.example.com:9082
      - KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=3
      - KAFKA_MESSAGE_MAX_BYTES=1048576 # 1 * 1024 * 1024 B
      - KAFKA_REPLICA_FETCH_MAX_BYTES=1048576 # 1 * 1024 * 1024 B
      - KAFKA_UNCLEAN_LEADER_ELECTION_ENABLE=false
      - KAFKA_LOG_RETENTION_MS=-1
      - KAFKA_MIN_INSYNC_REPLICAS=1
      - KAFKA_DEFAULT_REPLICATION_FACTOR=3
    extra_hosts:
      - "zookeeper1.example.com:10.1.24.225"
      - "zookeeper2.example.com:10.1.24.225"
      - "kafka2.example.com:10.1.24.225"
      - "zookeeper3.example.com:10.1.24.225"
      - "kafka3.example.com:10.1.24.225"




  zookeeper2.example.com:
    container_name: zookeeper2.example.com
    hostname: zookeeper2.example.com
    image: hyperledger/fabric-zookeeper:latest
    ports:
      - "2172:2181"
      - "2879:2879"
      - "3879:3879"
    environment:
      ZOOKEEPER_CLIENT_PORT: 32172
      ZOOKEEPER_TICK_TIME: 2000
      ZOO_MY_ID: 2
      ZOO_SERVERS: server.1=zookeeper1.example.com:2878:3878 server.2=zookeeper2.example.com:2879:3879 server.3=zookeeper3.example.com:2880:3880
    extra_hosts:
      - "zookeeper1.example.com:10.1.24.225"
      - "kafka1.example.com:10.1.24.225"
      - "kafka2.example.com:10.1.24.225"
      - "zookeeper3.example.com:10.1.24.225"
      - "kafka3.example.com:10.1.24.225"

  kafka2.example.com:
    container_name: kafka2.example.com
    image: hyperledger/fabric-kafka:latest
    depends_on:
      - zookeeper1.example.com
      - zookeeper2.example.com
      - zookeeper3.example.com
    ports:
      - "9083:9083"
    environment:
      - KAFKA_BROKER_ID=2
      - KAFKA_ZOOKEEPER_CONNECT=zookeeper1.example.com:2171,zookeeper2.example.com:2172,zookeeper3.example.com:2173
      - KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9083
      - KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://kafka2.example.com:9083
      - KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=3
      - KAFKA_MESSAGE_MAX_BYTES=1048576 # 1 * 1024 * 1024 B
      - KAFKA_REPLICA_FETCH_MAX_BYTES=1048576 # 1 * 1024 * 1024 B
      - KAFKA_UNCLEAN_LEADER_ELECTION_ENABLE=false
      - KAFKA_LOG_RETENTION_MS=-1
      - KAFKA_MIN_INSYNC_REPLICAS=1
      - KAFKA_DEFAULT_REPLICATION_FACTOR=3
    extra_hosts:
      - "zookeeper1.example.com:10.1.24.225"
      - "kafka1.example.com:10.1.24.225"
      - "zookeeper2.example.com:10.1.24.225"
      - "zookeeper3.example.com:10.1.24.225"
      - "kafka3.example.com:10.1.24.225"



  zookeeper3.example.com:
    container_name: zookeeper3.example.com
    hostname: zookeeper3.example.com
    image: hyperledger/fabric-zookeeper:latest
    ports:
      - "2173:2181"
      - "2880:2880"
      - "3880:3880"
    environment:
      ZOOKEEPER_CLIENT_PORT: 32173
      ZOOKEEPER_TICK_TIME: 2000
      ZOO_MY_ID: 3
      ZOO_SERVERS: server.1=zookeeper1.example.com:2878:3878 server.2=zookeeper2.example.com:2879:3879 server.3=zookeeper3.example.com:2880:3880
    extra_hosts:
      - "zookeeper1.example.com:10.1.24.225"
      - "kafka1.example.com:10.1.24.225"
      - "zookeeper2.example.com:10.1.24.225"
      - "kafka2.example.com:10.1.24.225"
      - "kafka3.example.com:10.1.24.225"

  kafka3.example.com:
    container_name: kafka3.example.com
    image: hyperledger/fabric-kafka:latest
    depends_on:
      - zookeeper1.example.com
      - zookeeper2.example.com
      - zookeeper3.example.com
    ports:
      - "9084:9084"
    environment:
      - KAFKA_BROKER_ID=3
      - KAFKA_ZOOKEEPER_CONNECT=zookeeper1.example.com:2171,zookeeper2.example.com:2172,zookeeper3.example.com:2173
      - KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9084
      - KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://kafka3.example.com:9084
      - KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=3
      - KAFKA_MESSAGE_MAX_BYTES=1048576 # 1 * 1024 * 1024 B
      - KAFKA_REPLICA_FETCH_MAX_BYTES=1048576 # 1 * 1024 * 1024 B
      - KAFKA_UNCLEAN_LEADER_ELECTION_ENABLE=false
      - KAFKA_LOG_RETENTION_MS=-1
      - KAFKA_MIN_INSYNC_REPLICAS=1
      - KAFKA_DEFAULT_REPLICATION_FACTOR=3
    extra_hosts:
      - "zookeeper1.example.com:10.1.24.225"
      - "kafka1.example.com:10.1.24.225"
      - "zookeeper2.example.com:10.1.24.225"
      - "kafka2.example.com:10.1.24.225"
      - "zookeeper3.example.com:10.1.24.225"

启动

启动的时候带上-f docker-compose-kafka.yaml即可

你可能感兴趣的:(区块链)