美文网首页
Hyperledger Fabric Samples在Mac部署

Hyperledger Fabric Samples在Mac部署

作者: 一钱科技 | 来源:发表于2019-11-05 15:58 被阅读0次

Hyperledger Fabric 整理体系:

 以下操作在fabric-samples 1.4.4版本上进行。

第一步

 下载bin目录 hyperledger fabric,拷贝到fabric-sample目录下。

  • configtxgen:生成初始区块及通道交易配置文件的工具;
  • cryptogen:生成组织结构及相应的身份文件的工具;
  • configtxlator:将指定的文件在二进制格式与json格式之间进行切换

构建第一个网络

 构建你的第一个网络(build your first network - BYFN),提供了一个fabric网络示例。该示例网络由两个组织组成,每个组织维护两个节点,默认使用solo共识服务。

  1. 切换到first-network目录
  2. 生成证书及创世区块
./byfn.sh generate

 运行该命令会为fabric网络各种实体生成所需的证书和秘钥,保存在crypto-config目录下。
ordererOrganizations子目录下包括构成Orderer组织的身份信息
peerOrganizations子目录为所有Peer节点组织的相关身份信息,其中最关键的是MSP目录,代表了实体的身份信息。一般包括

  • admincerts:管理员的身份证书文件
  • cacerts:信任的根证书文件
  • keystore:节点的签名,私钥文件
  • signcerts:节点的签名,身份证书文件
  • tlscacerts:TLS连接用的证书
  • intermediatercerts:信任的中间证书
  • crls:证书撤销列表
  • config.yaml:记录OrganizationalUnitldentifiers信息,包括跟证书位置和ID信息
$ tree crypto-config -L 4 -d
crypto-config
├── ordererOrganizations
│   └── example.com
│       ├── ca
│       ├── msp
│       │   ├── admincerts
│       │   ├── cacerts
│       │   └── tlscacerts
│       ├── orderers
│       │   └── orderer.example.com
│       ├── tlsca
│       └── users
│           └── Admin@example.com
└── peerOrganizations
    ├── org1.example.com
    │   ├── ca
    │   ├── msp
    │   │   ├── admincerts
    │   │   ├── cacerts
    │   │   └── tlscacerts
    │   ├── peers
    │   │   ├── peer0.org1.example.com
    │   │   └── peer1.org1.example.com
    │   ├── tlsca
    │   └── users
    │       ├── Admin@org1.example.com
    │       └── User1@org1.example.com
    └── org2.example.com
        ├── ca
        ├── msp
        │   ├── admincerts
        │   ├── cacerts
        │   └── tlscacerts
        ├── peers
        │   ├── peer0.org2.example.com
        │   └── peer1.org2.example.com
        ├── tlsca
        └── users
            ├── Admin@org2.example.com
            └── User1@org2.example.com

 如果报错:cryptogen tool not found. exiting
解决方案:拉取fabric 1.4,拷贝fabric/scripts/bootstrap.sh文件到fabric-samples目录下,直接执行该脚本。最后会生成所需bin目录。

2.1 与组织结构和身份证书关联的配置文件
 生成过程中依赖cryto-config.yaml配置文件,包括如下内容:

OrdererOrgs:   
 - Name: Orderer    # 组织名称
   Domain: example.com    # 域名
   Specs:
     - Hostname: orderer    # hostname + Domain的值组成Orderer节点的完整域名

PeerOrgs:
 - Name: Org1
   Domain: org1.example.com
   EnableNodeOUs: true        # 在msp下生成config.yaml文件
   Template:
     Count: 2     # 节点数量
   Users:
     Count: 1     # 新增用户数量

 - Name: Org2
   Domain: org2.example.com
   EnableNodeOUs: true
   Template:
     Count: 2
   Users:
     Count: 1
  1. 启动网络
./byfn.sh up

日志详情:日志总结
逐步详解:byfn 手动创建

 fabric-sample-1.4.0版本在执行first-network时,会出现一些问题,切换到1.4.4版本就OK。

2019-11-20 09:31:53.970 UTC [main] InitCmd -> ERRO 001 Cannot run peer because cannot init crypto, folder "/Users/macserver/Documents/BlockChain/src/github.com/hyperledger/fabric-samples/first-network/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" does not exist
!!!!!!!!!!!!!!! Channel creation failed !!!!!!!!!!!!!!!!
========= ERROR !!! FAILED to execute End-2-End Scenario ===========

3.1 关联的docker-compose-base.yaml文件

services:
  orderer.example.com:
    container_name: orderer.example.com
    image: hyperledger/fabric-orderer:$IMAGE_TAG
    environment:
      - ORDERER_GENERAL_LOGLEVEL=INFO  # 日志级别
      - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0  # 监听地址
      - ORDERER_GENERAL_GENESISMETHOD=file  # 生成初始区块的提供方式
      - ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block  # 初始区块配置文件路径
      - ORDERER_GENERAL_LOCALMSPID=OrdererMSP  # 本地MSP ID
      - ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp  # 本地MSP路径
      # enabled TLS
      - ORDERER_GENERAL_TLS_ENABLED=true  # 开启TLS验证
      - ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key  # TLS 私钥
      - ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt  # TLS 证书
      - ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]  # TLS 根证书
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric  # 进入容器后,默认工作目录
    command: orderer
    volumes:
    - ../channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block  # 初始区块配置文件
    - ../crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp:/var/hyperledger/orderer/msp  # MSP目录
    - ../crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/:/var/hyperledger/orderer/tls  # TLS目录
    - orderer.example.com:/var/hyperledger/production/orderer
    ports:
      - 7050:7050

  peer0.org1.example.com:
    container_name: peer0.org1.example.com
    extends:   # 基本信息来源于哪个文件
      file: peer-base.yaml
      service: peer-base
    environment:
      - CORE_PEER_ID=peer0.org1.example.com
      - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
      - CORE_PEER_GOSSIP_BOOTSTRAP=peer1.org1.example.com:7051
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.example.com:7051
      - CORE_PEER_LOCALMSPID=Org1MSP
    volumes:
        - /var/run/:/host/var/run/
        - ../crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp:/etc/hyperledger/fabric/msp
        - ../crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls:/etc/hyperledger/fabric/tls
        - peer0.org1.example.com:/var/hyperledger/production
    ports:
      - 7051:7051
      - 7053:7053

3.2 关联的peer-base.yaml文件

services:
  peer-base:
    image: hyperledger/fabric-peer:$IMAGE_TAG
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=${COMPOSE_PROJECT_NAME}_byfn
      - CORE_LOGGING_LEVEL=INFO  # 日志级别
      - CORE_PEER_TLS_ENABLED=true  # 是否开启TLS验证
      - CORE_PEER_GOSSIP_USELEADERELECTION=true  # 是否采用leader选举
      - CORE_PEER_GOSSIP_ORGLEADER=false  # 是否将当前节点设为leader
      - CORE_PEER_PROFILE_ENABLED=true
      - CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt  # TLS 证书文件
      - CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key  # TLS 秘钥文件
      - CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt  # TLS 根证书文件
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer  # 进入容器默认工作目录
    command: peer node start   # 容器启动命令

第一个简单应用:fabcar

  1. 在fabcar目录下执行脚本,启动网络:
./startFabric.sh

日志详情:日志总结

1.1 configtx.yaml配置文件

Organizations:
    - &OrdererOrg   # 组织
        Name: OrdererOrg  # 组织的名称
        ID: OrdererMSP  # 组织ID
        MSPDir: crypto-config/ordererOrganizations/example.com/msp  # MSP路径

    - &Org1
        Name: Org1MSP
        ID: Org1MSP
        MSPDir: crypto-config/peerOrganizations/org1.example.com/msp

        AnchorPeers:
            - Host: peer0.org1.example.com
              Port: 7051

    - &Org2
        Name: Org2MSP
        ID: Org2MSP
        MSPDir: crypto-config/peerOrganizations/org2.example.com/msp

        AnchorPeers:
            - Host: peer0.org2.example.com
              Port: 7051

Orderer: &OrdererDefaults
    OrdererType: solo  # 共识排序服务的实现方式,有两种选择(solo和kafka)
    Addresses:
        - orderer.example.com:7050  # orderer节点的服务地址和端口号

    BatchTimeout: 2s
    BatchSize:    # 批处理大小,如最大交易数量,最大字节数及建议字节数
        MaxMessageCount: 10
        AbsoluteMaxBytes: 99 MB
        PreferredMaxBytes: 512 KB

    Kafka:
        Brokers:
            - 127.0.0.1:9092

    Organizations:

Profiles:
    TwoOrgsOrdererGenesis:  # 生成Orderer服务的初始区块文件,
        Capabilities:  # 通道的权限信息
            <<: *ChannelCapabilities
        Orderer:  # Orderer服务的信息及权限信息
            <<: *OrdererDefaults
            Organizations:
                - *OrdererOrg
            Capabilities:
                <<: *OrdererCapabilities
        Consortiums:  # 联盟组成成员
            SampleConsortium:
                Organizations:
                    - *Org1
                    - *Org2
    TwoOrgsChannel:  # 生成应用通道交易配置文件
        Consortium: SampleConsortium  # 联盟信息
        Application:  # 组织及权限信息
            <<: *ApplicationDefaults
            Organizations:
                - *Org1
                - *Org2
            Capabilities:
                <<: *ApplicationCapabilities
  1. 在javascript文件内,运行脚本实现用户、数据更新或查询:
# 安装环境
npm install
# 加入管理员 admin
node enrollAdmin
# 注册新用户 user1
node registerUser
# 查询所有车辆信息
node query.js
# 插入新的车辆信息,car12
node invoke

相关文章

网友评论

      本文标题:Hyperledger Fabric Samples在Mac部署

      本文链接:https://www.haomeiwen.com/subject/refebctx.html