Hyperledger Fabric入門實戰(四)——手動組建Fabric網絡

我們可以自己組建一個Fabric網路, 網絡結構如下:

  • 排序節點 1 個
  • 組織個數 2 個, 分別爲go和cpp, 每個組織分別有兩個peer節點, 用戶個數爲3
機構名稱 組織標識符 組織ID
Go學科 org_go OrgGoMSP
CPP org_cpp OrgCppMSP

一些理論基礎:

  • 域名
    • baidu.com
    • jd.com
    • taobao.com
  • msp
    • Membership service provider (MSP)是一個提供虛擬成員操作的管理框架的組件。
    • 賬號
      • 都誰有msp
        • 每個節點都有一個msp賬號
        • 每個用戶都有msp賬號
  • 錨節點
    • 代表所屬組織和其他組織進行通信的節點

1. 生成fabric證書

1.1 命令介紹

$cryptogen --help

1.2 證書的文件的生成 - yaml

  1. 配置文件的模板介紹
# ---------------------------------------------------------------------------
# "OrdererOrgs" - Definition of organizations managing orderer nodes
# ---------------------------------------------------------------------------
OrdererOrgs:	# 排序節點組織信息
  # ---------------------------------------------------------------------------
  # Orderer
  # ---------------------------------------------------------------------------
  - Name: Orderer	# 排序節點組織的名字
    Domain: example.com	# 根域名, 排序節點組織的根域名
    Specs:
      - Hostname: orderer # 訪問這臺orderer對應的域名爲: orderer.example.com
      - Hostname: order2 # 訪問這臺orderer對應的域名爲: order2.example.com
# ---------------------------------------------------------------------------
# "PeerOrgs" - Definition of organizations managing peer nodes
# ---------------------------------------------------------------------------
PeerOrgs:
  # ---------------------------------------------------------------------------
  # Org1
  # ---------------------------------------------------------------------------
  - Name: Org1	# 第一個組織的名字, 自己指定
    Domain: org1.example.com	# 訪問第一個組織用到的根域名
    EnableNodeOUs: true			# 是否支持node.js
    Template:					# 模板, 根據默認的規則生成2個peer存儲數據的節點
      Count: 2 # 1. peer0.org1.example.com 2. peer1.org1.example.com
    Users:	   # 創建的普通用戶的個數
      Count: 3
      
  # ---------------------------------------------------------------------------
  # Org2: See "Org1" for full specification
  # ---------------------------------------------------------------------------
  - Name: Org2
    Domain: org2.example.com
    EnableNodeOUs: true
    Template:
      Count: 2
    Specs:
      - Hostname: hello
    Users:
      Count: 1

上邊使用的域名, 在真實的生成環境中需要註冊備案, 測試環境, 域名自己隨便指定就可以

  1. 根據要求編寫好的配置文件, 配置文件名: crypto-config.yaml
    步驟:
    (1)創建文件夾
    在這裏插入圖片描述
    (2)根據模版生成配置文件

在這裏插入圖片描述

# crypto-config.yaml
# ---------------------------------------------------------------------------
# "OrdererOrgs" - Definition of organizations managing orderer nodes
# ---------------------------------------------------------------------------
OrdererOrgs:
  # ---------------------------------------------------------------------------
  # Orderer
  # ---------------------------------------------------------------------------
  - Name: Orderer
    Domain: zjnu.com
    Specs:
      - Hostname: orderer

# ---------------------------------------------------------------------------
# "PeerOrgs" - Definition of organizations managing peer nodes
# ---------------------------------------------------------------------------
PeerOrgs:
  # ---------------------------------------------------------------------------
  # Org1
  # ---------------------------------------------------------------------------
  - Name: OrgGo
    Domain: orggo.zjnu.com
    EnableNodeOUs: true
    Template:
      Count: 2
    Users:
      Count: 3

  # ---------------------------------------------------------------------------
  # Org2: See "Org1" for full specification
  # ---------------------------------------------------------------------------
  - Name: OrgCpp
    Domain: orgcpp.zjnu.com
    EnableNodeOUs: true
    Template:
      Count: 2
    Users:
      Count: 3

  • 通過命令生成證書文件

    $ cryptogen generate --config=crypto-config.yaml
    

在這裏插入圖片描述

注:specs和Template的作用是一樣的,區別是specs可以指定域名,而template是默認生成的。

2. 創始塊文件和通道文件的生成

2.1 命令介紹

$ configtxgen --help 
  # 輸出創始塊區塊文件的路徑和名字
  `-outputBlock string`
  # 指定創建的channel的名字, 如果沒指定系統會提供一個默認的名字.
  `-channelID string`
  # 表示輸通道文件路徑和名字
  `-outputCreateChannelTx string`
  # 指定配置文件中的節點
  `-profile string`
  # 更新channel的配置信息
  `-outputAnchorPeersUpdate string`
  # 指定所屬的組織名稱
  `-asOrg string`
  # 要想執行這個命令, 需要一個配置文件 configtx.yaml

2.2 創始塊/通道文件的生成

  • 配置文件的編寫 - 複製例子中的configtx.yaml
    在這裏插入圖片描述

    
    ---
    ################################################################################
    #
    #   Section: Organizations
    #
    #   - This section defines the different organizational identities which will
    #   be referenced later in the configuration.
    #
    ################################################################################
    Organizations:			# 固定的不能改
        - &OrdererOrg		# 排序節點組織, 自己起個名字
            Name: OrdererOrg	# 排序節點的組織名
            ID: OrdererMSP		# 排序節點組織的ID
            MSPDir: crypto-config/ordererOrganizations/example.com/msp # 組織的msp賬號信息
    
        - &Org1			# 第一個組織, 名字自己起
            Name: Org1MSP # 第一個組織的名字
            ID: Org1MSP		# 第一個組織的ID
            MSPDir: crypto-config/peerOrganizations/org1.example.com/msp
            AnchorPeers: # 錨節點
                - Host: peer0.org1.example.com  # 指定一個peer節點的域名
                  Port: 7051					# 端口不要改
    
        - &Org2
            Name: Org2MSP
            ID: Org2MSP
            MSPDir: crypto-config/peerOrganizations/org2.example.com/msp
            AnchorPeers:
                - Host: peer0.org2.example.com
                  Port: 7051
    
    ################################################################################
    #
    #   SECTION: Capabilities, 在fabric1.1之前沒有, 設置的時候全部設置爲true
    #   
    ################################################################################
    Capabilities:
        Global: &ChannelCapabilities
            V1_1: true
        Orderer: &OrdererCapabilities
            V1_1: true
        Application: &ApplicationCapabilities
            V1_2: true
    
    ################################################################################
    #
    #   SECTION: Application
    #
    ################################################################################
    Application: &ApplicationDefaults
        Organizations:
    
    ################################################################################
    #
    #   SECTION: Orderer
    #
    ################################################################################
    Orderer: &OrdererDefaults
        # Available types are "solo" and "kafka"
        # 共識機制 == 排序算法
        OrdererType: solo	# 排序方式
        Addresses:			# orderer節點的地址
            - orderer.example.com:7050	# 端口不要改
    
    	# BatchTimeout,MaxMessageCount,AbsoluteMaxBytes只要一個滿足, 區塊就會產生
        BatchTimeout: 2s	# 多長時間產生一個區塊
        BatchSize:
            MaxMessageCount: 10		# 交易的最大數據量, 數量達到之後會產生區塊, 建議100左右
            AbsoluteMaxBytes: 99 MB # 數據量達到這個值, 會產生一個區塊, 32M/64M
            PreferredMaxBytes: 512 KB
        Kafka:
            Brokers:
                - 127.0.0.1:9092
        Organizations:
    
    ################################################################################
    #
    #   Profile
    #
    ################################################################################
    Profiles:	# 不能改
        TwoOrgsOrdererGenesis:	# 區塊名字, 隨便改
            Capabilities:
                <<: *ChannelCapabilities
            Orderer:
                <<: *OrdererDefaults
                Organizations:
                    - *OrdererOrg
                Capabilities:
                    <<: *OrdererCapabilities
            Consortiums:
                SampleConsortium:	# 這個名字可以改
                    Organizations:
                        - *Org1
                        - *Org2
        TwoOrgsChannel:	# 通道名字, 可以改
            Consortium: SampleConsortium	# 這個名字對應93行
            Application:
                <<: *ApplicationDefaults
                Organizations:
                    - *Org1
                    - *Org2
                Capabilities:
                    <<: *ApplicationCapabilities
    
    
  • 按照要求編寫的配置文件

    # configtx.yaml
    ---
    ################################################################################
    #
    #   Section: Organizations
    #
    ################################################################################
    Organizations:
        - &OrdererOrg
            Name: OrdererOrg
            ID: OrdererMSP
            MSPDir: crypto-config/ordererOrganizations/zjnu.com/msp
    
        - &org_go
            Name: OrgGoMSP
            ID: OrgGoMSP
            MSPDir: crypto-config/peerOrganizations/orggo.zjnu.com/msp
            AnchorPeers:
                - Host: peer0.orggo.zjnu.com
                  Port: 7051
    
        - &org_cpp
            Name: OrgCppMSP
            ID: OrgCppMSP
            MSPDir: crypto-config/peerOrganizations/orgcpp.zjnu.com/msp
            AnchorPeers:
                - Host: peer0.orgcpp.zjnu.com
                  Port: 7051
    
    ################################################################################
    #
    #   SECTION: Capabilities
    #
    ################################################################################
    Capabilities:
        Global: &ChannelCapabilities
            V1_1: true
        Orderer: &OrdererCapabilities
            V1_1: true
        Application: &ApplicationCapabilities
            V1_2: true
    
    ################################################################################
    #
    #   SECTION: Application
    #
    ################################################################################
    Application: &ApplicationDefaults
        Organizations:
    
    ################################################################################
    #
    #   SECTION: Orderer
    #
    ################################################################################
    Orderer: &OrdererDefaults
        # Available types are "solo" and "kafka"
        OrdererType: solo
        Addresses:
            - orderer.itcast.com:7050
        BatchTimeout: 2s
        BatchSize:
            MaxMessageCount: 100
            AbsoluteMaxBytes: 32 MB
            PreferredMaxBytes: 512 KB
        Kafka:
            Brokers:
                - 127.0.0.1:9092
        Organizations:
    
    ################################################################################
    #
    #   Profile
    #
    ################################################################################
    Profiles:
        ZjnuOrgsOrdererGenesis:
            Capabilities:
                <<: *ChannelCapabilities
            Orderer:
                <<: *OrdererDefaults
                Organizations:
                    - *OrdererOrg
                Capabilities:
                    <<: *OrdererCapabilities
            Consortiums:
                SampleConsortium:
                    Organizations:
                        - *org_go
                        - *org_cpp
        ZjnuOrgsChannel:
            Consortium: SampleConsortium
            Application:
                <<: *ApplicationDefaults
                Organizations:
                    - *org_go
                    - *org_cpp
                Capabilities:
                    <<: *ApplicationCapabilities
    
    
  • 執行命令生成文件

    -profile 後邊的參數從configtx.yaml中的Profiles 裏邊的配置項

    • 生成創始塊文件

      $ configtxgen -profile ZjnuOrgsOrdererGenesis -outputBlock ./genesis.block
      - 在當前目錄下得到一個文件: genesis.block
      
    • 生成通道文件

      $ configtxgen -profile ZjnuOrgsChannel -outputCreateChannelTx channel.tx -channelID zjnuchannel
      
    • 生成錨節點更新文件

      這個操作是可選的

      # 每個組織都對應一個錨節點的更新文件
      # go組織錨節點文件
      $ configtxgen -profile ZjnuOrgsChannel -outputAnchorPeersUpdate GoMSPanchors.tx -channelID zjnuchannel -asOrg OrgGoMSP
      # cpp組織錨節點文件
      $ configtxgen -profile ZjnuOrgsChannel -outputAnchorPeersUpdate CppMSPanchors.tx -channelID zjnuchannel -asOrg OrgCppMSP
      
      # 查看生成的文件
      $ tree -L 1
      .
      ├── channel-artifacts
      ├── channel.tx	----------> 生成的通道文件
      ├── configtx.yaml
      ├── CppMSPanchors.tx -----> 生成的cpp組織錨節點文件
      ├── crypto-config
      ├── crypto-config.yaml
      ├── genesis.block --------> 生成的創始塊文件
      └── GoMSPanchors.tx	------> 生成的go組織錨節點文件
      

將genesis.block、GoMSPanchors.tx、CppMSPanchors.tx、genesis.block移動到channel-artifacts目錄下。

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章