编程知识 cdmana.com

Hyperledger fabric 共识机制-Kafka集群篇(五)

Hyperledger fabric 共识机制-Kafka集群篇(五)

1、准备工作

最小的kafka集群网络配置

为了实现机器的可复用性,不同类型的节点可以部署到同一台服务器上,同一类型节点需要部署在不同服务器中。

zookeeper服务器:奇数个,且最小为3台,最多为7台

kafka服务器:最小4台

orderer节点:最小3个

名称 Hostname IP地址
zk1 zookeeper1 1
zk2 zookeeper2 2
zk3 zookeeper3 3
kafka1 kafka1 1
kafka2 kafka2 2
kafka3 kafka3 3
kafka4 kafka4 4
orderer0 orderer0.example.com 1
orderer1 orderer1.example.com 2
orderer2 orderer2.example.com 3

为了保证整个集群的正常工作,需要给集群中各个节点设置工作目录,必须要保证各个节点工作目录是相同的

mkdir ~/kafka

2、生成证书文件

修改crypto-config.yaml文件,添加2个orderer节点

OrdererOrgs:
  # ---------------------------------------------------------------------------
  # Orderer
  # ---------------------------------------------------------------------------
  - Name: Orderer
    Domain: test.com
    EnableNodeOUs: true

    # ---------------------------------------------------------------------------
    # "Specs" - See PeerOrgs below for complete description
    # ---------------------------------------------------------------------------
    Specs:
      - Hostname: orderer0
      - Hostname: orderer1
      - Hostname: orderer2

3、生成创始块文件和通道文件

修改configtx.yaml文件,首先需要在Orderer.Addresses中声明3个orderer节点信息,将Orderer.OrdererType的值由默认的solo修改为kafka,在Orderer.Kafka.Brokers中添加Kafka集群服务器的配置信息。

Orderer: &OrdererDefaults

    # Orderer Type: The orderer implementation to start
    # Available types are "solo","kafka"  and "etcdraft"
    OrdererType: kafka

    Addresses:
        - orderer0.test.com:7050
        - orderer1.test.com:7050
        - orderer2.test.com:7050

    # Batch Timeout: The amount of time to wait before creating a batch
    BatchTimeout: 2s

    # Batch Size: Controls the number of messages batched into a block
    BatchSize:

        # Max Message Count: The maximum number of messages to permit in a batch
        MaxMessageCount: 10

        # Absolute Max Bytes: The absolute maximum number of bytes allowed for
        # the serialized messages in a batch.
        AbsoluteMaxBytes: 99 MB

        # Preferred Max Bytes: The preferred maximum number of bytes allowed for
        # the serialized messages in a batch. A message larger than the preferred
        # max bytes will result in a batch larger than preferred max bytes.
        PreferredMaxBytes: 512 KB

    Kafka:
        # Brokers: A list of Kafka brokers to which the orderer connects
        # NOTE: Use IP:port notation
        Brokers:
            - kafka0:9092
            - kafka1:9092
            - kafka2:9092
            - kafka3:9092

4、配置docker-compose.yml文件

4.1、配置zookeeper服务器

ZooKeeper部分:(如果为多机多节点,则此部分单独为一个zookeeper.yaml文件)

version: '2'

services:
  zookeeper1:
    container_name: zookeeper1
    hostname:zookeeper1
    image: hyperledger/fabric-zookeeper:latest
    #restart:always
    environment:
    #ID在集合中必须是唯一且应该有一个值,在1-255之间(服务器在集群内的当前zookeerper服务器节点的ID)
      -  ZOO_MY_ID=1
      #server.x = hostname:port1:port2
      -  ZOO_SERVERS=server.1=zookeeper1:2888:3888 server.2=zookeeper2:2888:3888 server.3=zookeeper3:2888:3888
    port:
      - '2181'
      - '2888'
      - '3888'
      extra_hosts:
        - "zookeeper1:192.168.1.1"
        - "zookeeper2:192.168.1.2"
        - "zookeeper3:192.168.1.3"
        - "kafka1:127.0.0.1"
        - "kafka2:127.0.0.2"
        - "kafka1:127.0.0.3"
        - "kafka2:127.0.0.4"
        
   zookeeper2:
    container_name: zookeeper2
    hostname:zookeeper2
    environment:
    #ID在集合中必须是唯一且应该有一个值,在1-255之间(服务器在集群内的当前zookeerper服务器节点的ID)
      -  ZOO_MY_ID=2
 

配置项解释:

ZOO_SERVERS:

  • zookeeper集群服务器列表
  • 两个端口号
    • 第一个:追随者用来连接leader使用
    • 第二个:用户选举leader

三个端口port:

  • 访问zookeeper的端口:2181
  • zookeeper集群中追随者连接leader的端口:2888
  • zookeeper集群中选举leader的端口:3888

4.2、kafka集群配置及orderer节点

#
# Copyright IBM Corp All Rights Reserved
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2'

networks:
  default:

services:
  ca.innovation.com:
    image: hyperledger/fabric-ca
    environment:
      - FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
      - FABRIC_CA_SERVER_CA_NAME=ca.innovation.com
      - FABRIC_CA_SERVER_CA_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca.org1.innovation.com-cert.pem
      - FABRIC_CA_SERVER_CA_KEYFILE=/etc/hyperledger/fabric-ca-server-config/fc0d22c49db02855470082d05e7933353bb06cf3c1b899164843b0e2de1e674e_sk
      - FABRIC_CA_SERVER_TLS_ENABLED=true
      - FABRIC_CA_SERVER_TLS_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca.org1.innovation.com-cert.pem
      - FABRIC_CA_SERVER_TLS_KEYFILE=/etc/hyperledger/fabric-ca-server-config/fc0d22c49db02855470082d05e7933353bb06cf3c1b899164843b0e2de1e674e_sk
    ports:
      - "7054:7054"
    command: sh -c 'fabric-ca-server start -b admin:adminpw'
    volumes:
      - ./crypto-config/peerOrganizations/org1.innovation.com/ca/:/etc/hyperledger/fabric-ca-server-config
    container_name: ca.innovation.com
    networks:
      default:
        aliases:
          - ca.innovation.com

  orderer1.innovation.com:
    container_name: orderer1.innovation.com
    image: hyperledger/fabric-orderer
    environment:
      - ORDERER_GENERAL_LOGLEVEL=debug
      - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
      - ORDERER_GENERAL_LISTENPORT=7050
      - ORDERER_GENERAL_GENESISPROFILE=business
      - ORDERER_GENERAL_GENESISMETHOD=file
      - ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/genesis.block
      - ORDERER_GENERAL_LOCALMSPID=OrdererMSP
      - ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
      #kafka
      - CONFIGTX_ORDERER_ORDERERTYPE=kafka
      - CONFIGTX_ORDERER_KAFKA_BROKERS=[kafka:9092,kafka1:9092,kafka2:9092,kafka3:9092]
      #第一阶段
      #orderer节点连接kafka可能会失败,ORDERER_KAFKA_RETRY_SHORTINTERVAL为重试的间隔时间
      - ORDERER_KAFKA_RETRY_SHORTINTERVAL=1s
      #总共重试的时间
      - ORDERER_KAFKA_RETRY_SHORTTOTAL=30s
      #第二阶段
      #间隔ORDERER_KAFKA_RETRY_LONGINTERVAL开始重新尝试连接
      - ORDERER_KAFKA_RETRY_LONGINTERVAL=10s
      #一共尝试的时间
      -ORDERER_KAFKA_RETRY_LONGTOTAL=100s
      - ORDERER_KAFKA_VERBOSE=true
      #TLS
      - ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
      - ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
      - ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/orderer
    command: orderer
    depends_on:
      - kafka0
      - kafka1
      - kafka2
      - kafka3
    ports:
      - 7050:7050
    volumes:
        - ./channel-artifacts/genesis.block:/var/hyperledger/orderer/genesis.block
        - ./crypto-config/ordererOrganizations/innovation.com/orderers/orderer.innovation.com/msp:/var/hyperledger/orderer/msp
        - ./crypto-config/ordererOrganizations/innovation.com/orderers/orderer.innovation.com/tls:/var/hyperledger/orderer/tls
    networks:
      default:
        aliases:
          - orderer1.innovation.com

  orderer2.innovation.com:
    container_name: orderer2.innovation.com
    image: hyperledger/fabric-orderer
    environment:
      - ORDERER_GENERAL_LOGLEVEL=debug
      - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
      - ORDERER_GENERAL_LISTENPORT=7050
      - ORDERER_GENERAL_GENESISPROFILE=business
      - ORDERER_GENERAL_GENESISMETHOD=file
      - ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/genesis.block
      - ORDERER_GENERAL_LOCALMSPID=OrdererMSP
      - ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
      #kafka
      - CONFIGTX_ORDERER_ORDERERTYPE=kafka
      - CONFIGTX_ORDERER_KAFKA_BROKERS=[kafka:9092,kafka1:9092,kafka2:9092,kafka3:9092]
      - ORDERER_KAFKA_RETRY_SHORTINTERVAL=1s
      - ORDERER_KAFKA_RETRY_SHORTTOTAL=30s
      - ORDERER_KAFKA_VERBOSE=true
      #TLS
      - ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
      - ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
      - ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/orderer
    command: orderer
    depends_on:
      - kafka0
      - kafka1
      - kafka2
      - kafka3
    ports:
      - 7050:7050
    volumes:
      - ./channel-artifacts/genesis.block:/var/hyperledger/orderer/genesis.block
      - ./crypto-config/ordererOrganizations/innovation.com/orderers/orderer.innovation.com/msp:/var/hyperledger/orderer/msp
      - ./crypto-config/ordererOrganizations/innovation.com/orderers/orderer.innovation.com/tls:/var/hyperledger/orderer/tls
    networks:
      default:
        aliases:
          - orderer2.innovation.com

  orderer0.innovation.com:
    container_name: orderer0.innovation.com
    image: hyperledger/fabric-orderer
    environment:
      - ORDERER_GENERAL_LOGLEVEL=debug
      - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
      - ORDERER_GENERAL_LISTENPORT=7050
      - ORDERER_GENERAL_GENESISPROFILE=business
      - ORDERER_GENERAL_GENESISMETHOD=file
      - ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/genesis.block
      - ORDERER_GENERAL_LOCALMSPID=OrdererMSP
      - ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
      #kafka
      - CONFIGTX_ORDERER_ORDERERTYPE=kafka
      - CONFIGTX_ORDERER_KAFKA_BROKERS=[kafka:9092,kafka1:9092,kafka2:9092,kafka3:9092]
      - ORDERER_KAFKA_RETRY_SHORTINTERVAL=1s
      - ORDERER_KAFKA_RETRY_SHORTTOTAL=30s
      - ORDERER_KAFKA_VERBOSE=true
      #TLS
      - ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
      - ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
      - ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/orderer
    command: orderer
    depends_on:
      - kafka0
      - kafka1
      - kafka2
      - kafka3
    ports:
      - 7050:7050
    volumes:
      - ./channel-artifacts/genesis.block:/var/hyperledger/orderer/genesis.block
      - ./crypto-config/ordererOrganizations/innovation.com/orderers/orderer.innovation.com/msp:/var/hyperledger/orderer/msp
      - ./crypto-config/ordererOrganizations/innovation.com/orderers/orderer.innovation.com/tls:/var/hyperledger/orderer/tls
    networks:
      default:
        aliases:
          - orderer0.innovation.com

  peer0.org1.innovation.com:
    container_name: peer0.org1.innovation.com
    image: hyperledger/fabric-peer
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_VM_DOCKER_ATTACHSTDOUT=true
      - CORE_LOGGING_LEVEL=DEBUG
      - CORE_CHAINCODE_LOGGING_LEVEL=info
      - CORE_PEER_LOCALMSPID=Org1MSP
      - CORE_PEER_NETWORKID=business
      - CORE_PEER_PROFILE_ENABLED=true
      - CORE_PEER_TLS_ENABLED=true
      - CORE_PEER_TLS_CERT_FILE=/var/hyperledger/tls/server.crt
      - CORE_PEER_TLS_KEY_FILE=/var/hyperledger/tls/server.key
      - CORE_PEER_TLS_ROOTCERT_FILE=/var/hyperledger/tls/ca.crt
      - CORE_PEER_ID=peer0.org1.innovation.com
      - CORE_PEER_ADDRESSAUTODETECT=true
      - CORE_PEER_ADDRESS=peer0.org1.innovation.com:7051
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.innovation.com:7051
      - CORE_PEER_GOSSIP_USELEADERELECTION=true
      - CORE_PEER_GOSSIP_ORGLEADER=false
      - CORE_PEER_GOSSIP_SKIPHANDSHAKE=true
      - CORE_PEER_LOCALMSPID=org1.innovation.com
      - CORE_PEER_MSPCONFIGPATH=/var/hyperledger/msp
      - CORE_PEER_TLS_SERVERHOSTOVERRIDE=peer0.org1.innovation.com
      - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
      - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984
      - CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
      - CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: peer node start
    ports:
      - 7051:7051
      - 7053:7053
    volumes:
        - /var/run/:/host/var/run/
        - ./crypto-config/peerOrganizations/org1.innovation.com/peers/peer0.org1.innovation.com/msp:/var/hyperledger/msp
        - ./crypto-config/peerOrganizations/org1.innovation.com/peers/peer0.org1.innovation.com/tls:/var/hyperledger/tls
    depends_on:
      - orderer.innovation.com
      - couchdb
    networks:
      default:
        aliases:
          - peer0.org1.innovation.com

  peer1.org1.innovation.com:
    container_name: peer1.org1.innovation.com
    image: hyperledger/fabric-peer
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_VM_DOCKER_ATTACHSTDOUT=true
      - CORE_LOGGING_LEVEL=DEBUG
      - CORE_CHAINCODE_LOGGING_LEVEL=info
      - CORE_PEER_LOCALMSPID=Org1MSP
      - CORE_PEER_NETWORKID=business
      - CORE_PEER_PROFILE_ENABLED=true
      - CORE_PEER_TLS_ENABLED=true
      - CORE_PEER_TLS_CERT_FILE=/var/hyperledger/tls/server.crt
      - CORE_PEER_TLS_KEY_FILE=/var/hyperledger/tls/server.key
      - CORE_PEER_TLS_ROOTCERT_FILE=/var/hyperledger/tls/ca.crt
      - CORE_PEER_ID=peer1.org1.innovation.com
      - CORE_PEER_ADDRESSAUTODETECT=true
      - CORE_PEER_ADDRESS=peer1.org1.innovation.com:7151
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org1.innovation.com:7151
      - CORE_PEER_GOSSIP_USELEADERELECTION=true
      - CORE_PEER_GOSSIP_ORGLEADER=false
      - CORE_PEER_GOSSIP_SKIPHANDSHAKE=true
      - CORE_PEER_LOCALMSPID=org1.innovation.com
      - CORE_PEER_MSPCONFIGPATH=/var/hyperledger/msp
      - CORE_PEER_TLS_SERVERHOSTOVERRIDE=peer1.org1.innovation.com
      - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
      - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984
      - CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
      - CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: peer node start
    ports:
      - 7151:7151
      - 7153:7153
    volumes:
        - /var/run/:/host/var/run/
        - ./crypto-config/peerOrganizations/org1.innovation.com/peers/peer1.org1.innovation.com/msp:/var/hyperledger/msp
        - ./crypto-config/peerOrganizations/org1.innovation.com/peers/peer1.org1.innovation.com/tls:/var/hyperledger/tls
    depends_on:
      - orderer.innovation.com
      - couchdb
    networks:
      default:
        aliases:
          - peer1.org1.innovation.com


  peer0.org2.innovation.com:
    container_name: peer0.org2.innovation.com
    image: hyperledger/fabric-peer
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_VM_DOCKER_ATTACHSTDOUT=true
      - CORE_LOGGING_LEVEL=DEBUG
      - CORE_CHAINCODE_LOGGING_LEVEL=info
      - CORE_PEER_LOCALMSPID=Org2MSP
      - CORE_PEER_NETWORKID=business
      - CORE_PEER_PROFILE_ENABLED=true
      - CORE_PEER_TLS_ENABLED=true
      - CORE_PEER_TLS_CERT_FILE=/var/hyperledger/tls/server.crt
      - CORE_PEER_TLS_KEY_FILE=/var/hyperledger/tls/server.key
      - CORE_PEER_TLS_ROOTCERT_FILE=/var/hyperledger/tls/ca.crt
      - CORE_PEER_ID=peer0.org2.innovation.com
      - CORE_PEER_ADDRESSAUTODETECT=true
      - CORE_PEER_ADDRESS=peer0.org2.innovation.com:7251
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.innovation.com:7251
      - CORE_PEER_GOSSIP_USELEADERELECTION=true
      - CORE_PEER_GOSSIP_ORGLEADER=false
      - CORE_PEER_GOSSIP_SKIPHANDSHAKE=true
      - CORE_PEER_LOCALMSPID=org2.innovation.com
      - CORE_PEER_MSPCONFIGPATH=/var/hyperledger/msp
      - CORE_PEER_TLS_SERVERHOSTOVERRIDE=peer0.org2.innovation.com
      - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
      - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984
      - CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
      - CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: peer node start
    ports:
      - 7251:7251
      - 7253:7253
    volumes:
        - /var/run/:/host/var/run/
        - ./crypto-config/peerOrganizations/org2.innovation.com/peers/peer0.org2.innovation.com/msp:/var/hyperledger/msp
        - ./crypto-config/peerOrganizations/org2.innovation.com/peers/peer0.org2.innovation.com/tls:/var/hyperledger/tls
    depends_on:
      - orderer.innovation.com
      - couchdb
    networks:
      default:
        aliases:
          - peer0.org2.innovation.com

  peer1.org2.innovation.com:
    container_name: peer1.org2.innovation.com
    image: hyperledger/fabric-peer
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_VM_DOCKER_ATTACHSTDOUT=true
      - CORE_LOGGING_LEVEL=DEBUG
      - CORE_CHAINCODE_LOGGING_LEVEL=info
      - CORE_PEER_LOCALMSPID=Org2MSP
      - CORE_PEER_NETWORKID=business
      - CORE_PEER_PROFILE_ENABLED=true
      - CORE_PEER_TLS_ENABLED=true
      - CORE_PEER_TLS_CERT_FILE=/var/hyperledger/tls/server.crt
      - CORE_PEER_TLS_KEY_FILE=/var/hyperledger/tls/server.key
      - CORE_PEER_TLS_ROOTCERT_FILE=/var/hyperledger/tls/ca.crt
      - CORE_PEER_ID=peer1.org2.innovation.com
      - CORE_PEER_ADDRESSAUTODETECT=true
      - CORE_PEER_ADDRESS=peer1.org2.innovation.com:7351
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org2.innovation.com:7351
      - CORE_PEER_GOSSIP_USELEADERELECTION=true
      - CORE_PEER_GOSSIP_ORGLEADER=false
      - CORE_PEER_GOSSIP_SKIPHANDSHAKE=true
      - CORE_PEER_LOCALMSPID=org2.innovation.com
      - CORE_PEER_MSPCONFIGPATH=/var/hyperledger/msp
      - CORE_PEER_TLS_SERVERHOSTOVERRIDE=peer1.org2.innovation.com
      - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
      - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984
      - CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
      - CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: peer node start
    ports:
      - 7351:7351
      - 7353:7353
    volumes:
        - /var/run/:/host/var/run/
        - ./crypto-config/peerOrganizations/org2.innovation.com/peers/peer1.org2.innovation.com/msp:/var/hyperledger/msp
        - ./crypto-config/peerOrganizations/org2.innovation.com/peers/peer1.org2.innovation.com/tls:/var/hyperledger/tls
    depends_on:
      - orderer.innovation.com
      - couchdb
    networks:
      default:
        aliases:
          - peer1.org2.innovation.com

  couchdb:
    container_name: couchdb
    image: hyperledger/fabric-couchdb
    # Populate the COUCHDB_USER and COUCHDB_PASSWORD to set an admin user and password
    # for CouchDB.  This will prevent CouchDB from operating in an "Admin Party" mode.
    environment:
      - COUCHDB_USER=
      - COUCHDB_PASSWORD=
    ports:
      - 5984:5984


  zookeeper1:
    container_name: zookeeper1
    environment:
      - ZOO_MY_ID=1
      - ZOO_SERVERS=server.1=zookeeper1:2888:3888 server.2=zookeeper2:2888:3888 server.3=zookeeper3:2888:3888
    ports:
    - '2181'
    - '2888'
    - '3888'
    networks:
      default:
        - aliases:
            - zookeeper1

  zookeeper2:
    container_name: zookeeper2
    environment:
      - ZOO_MY_ID=2
      - ZOO_SERVERS=server.1=zookeeper1:2888:3888 server.2=zookeeper2:2888:3888 server.3=zookeeper3:2888:3888
    ports:
      - '2181'
      - '2888'
      - '3888'
    networks:
      default:
        - aliases:
            - zookeeper2

  zookeeper3:
    container_name: zookeeper3
    environment:
      - ZOO_MY_ID=3
      - ZOO_SERVERS=server.1=zookeeper1:2888:3888 server.2=zookeeper2:2888:3888 server.3=zookeeper3:2888:3888
    ports:
      - '2181'
      - '2888'
      - '3888'
    networks:
      default:
        - aliases:
            - zookeeper3


  kafka0:
    container_name: kafka0
    environment:
    #当前kafka服务器在集群中的ID,非负数,不能重复
      - KAFKA_BROKER_ID=0
      #标识log日志保持时间,官方默认关闭,为-1
      - KAFKA_LOG_RETENTION_MS=-1
      #消息的最大字节数,和configtx.yaml中的Orderer.BatchSize.AbsoluteMaxBytes对应
      #由于消息都有头信息,所以这个值要比计算的值稍大,多加1M就够了
      - KAFKA_MESSAGE_MAX_BYTES=103809024
      #副本最大字节数,为每个channel获取的消息的字节数
      #AbsoluteMaxBytes<KAFKA_REPLICA_FETCH_MAX_BYTES<= KAFKA_MESSAGE_MAX_BYTES
      - KAFKA_REPLICA_FETCH_MAX_BYTES=103809024
      #不共识选取领导节点,设置为false:采取共识选取leader节点
      - KAFKA_UNCLEAN_LEADER_ELECTION_ENABLE=false
      #最小同步备份,该值要小于环境变量KAFKA_DEFAULT_REPLICATION_FACTOR
      - KAFKA_MIN_INSYNC_REPLICAS=2
      #默认同步备份,该值要小于kafka集群数量
      - KAFKA_DEFAULT_REPLICATION_FACTOR=3
      #指向zookeeper节点的集合
      - KAFKA_ZOOKEEPER_CONNECT=zookeeper1:2181,zookeeper2:2181,zookeeper3:2181
    depends_on:
      - zookeeper1
      - zookeeper2
      - zookeeper3
    ports:
    - '9092'
    networks:
      default:
        - aliases:
            - kafka0


  kafka1:
    container_name: kafka1
    environment:
      - KAFKA_BROKER_ID=0
      - KAFKA_LOG_RETENTION_MS=-1
      - KAFKA_MESSAGE_MAX_BYTES=103809024
      - KAFKA_REPLICA_FETCH_MAX_BYTES=103809024
      - KAFKA_UNCLEAN_LEADER_ELECTION_ENABLE=false
      - KAFKA_MIN_INSYNC_REPLICAS=2
      - KAFKA_DEFAULT_REPLICATION_FACTOR=3
      - KAFKA_ZOOKEEPER_CONNECT=zookeeper1:2181,zookeeper2:2181,zookeeper3:2181
    depends_on:
      - zookeeper1
      - zookeeper2
      - zookeeper3
    ports:
      - '9092'
    networks:
      default:
        - aliases:
            - kafka1

  kafka2:
    container_name: kafka2
    environment:
      - KAFKA_BROKER_ID=0
      - KAFKA_LOG_RETENTION_MS=-1
      - KAFKA_MESSAGE_MAX_BYTES=103809024
      - KAFKA_REPLICA_FETCH_MAX_BYTES=103809024
      - KAFKA_UNCLEAN_LEADER_ELECTION_ENABLE=false
      - KAFKA_MIN_INSYNC_REPLICAS=2
      - KAFKA_DEFAULT_REPLICATION_FACTOR=3
      - KAFKA_ZOOKEEPER_CONNECT=zookeeper1:2181,zookeeper2:2181,zookeeper3:2181
    depends_on:
      - zookeeper1
      - zookeeper2
      - zookeeper3
    ports:
      - '9092'
    networks:
      default:
        - aliases:
            - kafka2

  kafka3:
    container_name: kafka3
    environment:
      - KAFKA_BROKER_ID=0
      - KAFKA_LOG_RETENTION_MS=-1
      - KAFKA_MESSAGE_MAX_BYTES=103809024
      - KAFKA_REPLICA_FETCH_MAX_BYTES=103809024
      - KAFKA_UNCLEAN_LEADER_ELECTION_ENABLE=false
      - KAFKA_MIN_INSYNC_REPLICAS=2
      - KAFKA_DEFAULT_REPLICATION_FACTOR=3
      - KAFKA_ZOOKEEPER_CONNECT=zookeeper1:2181,zookeeper2:2181,zookeeper3:2181
    depends_on:
      - zookeeper1
      - zookeeper2
      - zookeeper3
    ports:
      - '9092'
    networks:
      default:
        - aliases:
            - kafka3

配置项解释

kafka0:
    container_name: kafka0
    environment:
    #当前kafka服务器在集群中的ID,非负数,不能重复
      - KAFKA_BROKER_ID=0
      #标识log日志保持时间,官方默认关闭,为-1
      - KAFKA_LOG_RETENTION_MS=-1
      #消息的最大字节数,和configtx.yaml中的Orderer.BatchSize.AbsoluteMaxBytes对应
      #由于消息都有头信息,所以这个值要比计算的值稍大,多加1M就够了
      - KAFKA_MESSAGE_MAX_BYTES=103809024
      #副本最大字节数,为每个channel获取的消息的字节数
      #AbsoluteMaxBytes<KAFKA_REPLICA_FETCH_MAX_BYTES<= KAFKA_MESSAGE_MAX_BYTES
      - KAFKA_REPLICA_FETCH_MAX_BYTES=103809024
      #不共识选取领导节点,设置为false:采取共识选取leader节点
      - KAFKA_UNCLEAN_LEADER_ELECTION_ENABLE=false
      #最小同步备份,该值要小于环境变量KAFKA_DEFAULT_REPLICATION_FACTOR
      - KAFKA_MIN_INSYNC_REPLICAS=2
      #默认同步备份,该值要小于kafka集群数量
      - KAFKA_DEFAULT_REPLICATION_FACTOR=3
      #指向zookeeper节点的集合
      - KAFKA_ZOOKEEPER_CONNECT=zookeeper1:2181,zookeeper2:2181,zookeeper3:2181
    depends_on:
      - zookeeper1
      - zookeeper2
      - zookeeper3
    ports:
    - '9092'
    networks:
      default:
        - aliases:
            - kafka0
            
            
            
orderer1.innovation.com:
    container_name: orderer1.innovation.com
    image: hyperledger/fabric-orderer
    environment:
      - ORDERER_GENERAL_LOGLEVEL=debug
      - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
      - ORDERER_GENERAL_LISTENPORT=7050
      - ORDERER_GENERAL_GENESISPROFILE=business
      - ORDERER_GENERAL_GENESISMETHOD=file
      - ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/genesis.block
      - ORDERER_GENERAL_LOCALMSPID=OrdererMSP
      - ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
      #kafka
      - CONFIGTX_ORDERER_ORDERERTYPE=kafka
      - CONFIGTX_ORDERER_KAFKA_BROKERS=[kafka:9092,kafka1:9092,kafka2:9092,kafka3:9092]
      #第一阶段
      #orderer节点连接kafka可能会失败,ORDERER_KAFKA_RETRY_SHORTINTERVAL为重试的间隔时间
      - ORDERER_KAFKA_RETRY_SHORTINTERVAL=1s
      #总共重试的时间
      - ORDERER_KAFKA_RETRY_SHORTTOTAL=30s
      #第二阶段
      #间隔ORDERER_KAFKA_RETRY_LONGINTERVAL开始重新尝试连接
      - ORDERER_KAFKA_RETRY_LONGINTERVAL=10s
      #一共尝试的时间
      -ORDERER_KAFKA_RETRY_LONGTOTAL=100s
      #orderer与kafka通信是否写log日志
      - ORDERER_KAFKA_VERBOSE=true
      #TLS
      - ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
      - ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
      - ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/orderer
    command: orderer
    depends_on:
      - kafka0
      - kafka1
      - kafka2
      - kafka3
    ports:
      - 7050:7050
    volumes:
        - ./channel-artifacts/genesis.block:/var/hyperledger/orderer/genesis.block
        - ./crypto-config/ordererOrganizations/innovation.com/orderers/orderer.innovation.com/msp:/var/hyperledger/orderer/msp
        - ./crypto-config/ordererOrganizations/innovation.com/orderers/orderer.innovation.com/tls:/var/hyperledger/orderer/tls
    networks:
      default:
        aliases:
          - orderer1.innovation.com

5、集群的启动

启动顺序:zookeeper集群,kafka集群,orderer集群

启动顺序不能错,在启动过程中可以通过查看节点日志是否启动成功,例如查看zookeeper1

docker logs zookeeper1

5.1、zookeeper集群启动

  • zookeeper1

1、进入当前节点的工作目录,~/kafka

2、将写好的配置文件部署到当前主机~/kafka目录 zookeeper.yaml

3、启动docker-compose

docker-compose -f zookeeper1.yaml up -d

  • zookeeper2

1、进入当前节点的工作目录,~/kafka

2、将写好的配置文件部署到当前主机~/kafka目录 zookeeper.yaml

3、启动docker-compose

docker-compose -f zookeeper2.yaml up -d

  • zookeeper3

1、进入当前节点的工作目录,~/kafka

2、将写好的配置文件部署到当前主机~/kafka目录 zookeeper.yaml

3、启动docker-compose

docker-compose -f zookeeper3.yaml up -d

5.2、kafka集群启动

第一台kafka,之后依次启动

1、进入当前节点的工作目录,~/kafka

2、将写好的配置文件部署到当前主机~/kafka目录 kafka0.yaml

3、启动docker-compose

docker-compose -f kafka0.yaml up -d

5.3、orderer集群启动

  • orderer0

    1、进入当前节点的工作目录,~/kafka

    2、将写好的配置文件部署到当前主机~/kafka目录 kafka0.yaml

    3、首先把生成的证书文件和创始块文件部署到主机

    4、启动docker-compose

    docker-compose -f orderer0.yaml up -d

版权声明
本文为[风云傲天]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/liuhui5599/p/14187642.html

Scroll to Top