CnetOS 7.4 部署 Hyperledger Fabric 1.4

VMware 安装 CentOS 7.4 请参考 https://blog.csdn.net/qq_39135287/article/details/83993574 前两部分即可,其中第二部分步骤20可省略

1. 关闭CentOS防火墙

systemctl stop firewalld && systemctl disable firewalld

2. 安装环境

  • curl、git、Python系统自带,无需安装

  • Docker安装,官方示例 https://docs.docker.com/install/linux/docker-ce/centos/

    yum install -y yum-utils device-mapper-persistent-data lvm2
    
    yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    
    yum makecache fast
    
    yum -y install docker-ce
    
    mkdir -p  /etc/docker/
    
    echo -e '{\n  "data-root":"/home/docker",\n  "registry-mirrors": ["https://1e7waog4.mirror.aliyuncs.com"]\n}' > /etc/docker/daemon.json
    
    systemctl start docker && systemctl enable docker
    
    # 验证版本
    docker --version
    
  • Docker-Compose安装,官方示例 https://github.com/docker/compose/releases

    sudo curl -L https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
    
    sudo chmod +x /usr/local/bin/docker-compose
    
    # 验证版本
    docker-compose --version
    
  • GO语言安装,资源路径 https://golang.google.cn/dl/

    wget https://studygolang.com/dl/golang/go1.11.13.linux-amd64.tar.gz -O /opt/go1.11.13.linux-amd64.tar.gz
    
    cd /opt/
    
    tar -zxvf go1.11.13.linux-amd64.tar.gz
    
    rm -rf /opt/go1.11.13.linux-amd64.tar.gz
    
    mkdir /opt/gopath
    
    echo -e 'export GOROOT=/opt/go \n export GOBIN=$GOROOT/bin \n export PATH=$PATH:$GOBIN \n export GOPATH=/opt/gopath' >> /etc/profile
    
    source /etc/profile
    
    # 验证版本
    go version
    
  • Node.js安装

    wget https://npm.taobao.org/mirrors/node/v8.9.4/node-v8.9.4-linux-x64.tar.xz -O /opt/node-v8.9.4-linux-x64.tar.xz
    
    cd /opt/
    
    tar -xvf node-v8.9.4-linux-x64.tar.xz
    
    rm -rf /opt/node-v8.9.4-linux-x64.tar.xz
    
    ln -s /opt/node-v8.9.4-linux-x64/bin/npm /usr/local/bin/npm
    
    ln -s /opt/node-v8.9.4-linux-x64/bin/node /usr/local/bin/node
    
    # 验证版本
    node -v
    

3. 安装Fabric

mkdir -p $GOPATH/src/github.com/hyperledger

cd $GOPATH/src/github.com/hyperledger

git clone https://github.com/hyperledger/fabric.git

cd fabric

git checkout v1.4.0

echo 'export PATH=$GOPATH/src/github.com/hyperledger/fabric/scripts/fabric-samples/bin:$PATH' >> /etc/profile

source /etc/profile

cd scripts

./bootstrap.sh

4. 构建及管理Fabric网络

官方示例:https://hyperledgercn.github.io/hyperledgerDocs/build_network_zh/

  • 运行fabric
    cd $GOPATH/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network
    
    ./byfn.sh generate
    
    ./byfn.sh up
    
  • 查看到节点启动情况
    docker ps
    
  • 停止网络
    ./byfn.sh down
    

至此Fabric已在CentOS部署完毕


部署ChainCode

  • 在下方目录内新建文件夹chaincode_test,并将编写好的ChainCode放在其中(重点)
    # 网上很多示例说是把 chaincode 放在
    # 	/opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode 里面
    # 但是我试了很多次都不行
    # 后来看了第4步 ./byfn.sh up 时打印的信息
    # 	peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
    # 发现如果路径不是 github.com/chaincode/... 就会报错
    
    /opt/gopath/src/github.com/hyperledger/fabric/scripts/fabric/fabric-samples/chaincode
    
  • 切换节点
    docker exec -it cli bash
    
    CORE_PEER_LOCALMSPID="Org1MSP" 
    CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt 
    CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp 
    CORE_PEER_ADDRESS=peer0.org1.example.com:7051
    
  • 安装ChainCode,可参照第4步 ./byfn.sh up 时的打印信息
    peer chaincode install -n testcc -v 1.0 -l golang -p github.com/chaincode/chaincode_test
    
  • 实例化ChainCode,可参照第4步 ./byfn.sh up 时的打印信息
    peer chaincode instantiate -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n testcc -l golang -v 1.0 -c '{"Args":[""]}' -P "OR ('Org1MSP.member','Org2MSP.member')"
    
  • 切换至节点org2,再次安装ChainCode,此次无需实例化
    CORE_PEER_LOCALMSPID="Org2MSP" 
    CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt 
    CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp 
    CORE_PEER_ADDRESS=peer0.org2.example.com:7051
    
  • 可根据ChainCode执行query和invoke

安装区块链浏览器可参照上篇文章

  • 直接安装jq会报错,需按以下步骤操作
    yum install epel-release
    
    yum install jq
    
  • 构建Hyperledger Explorer ./main.sh install 报错
    gyp WARN EACCES user "root" does not have permission to access the dev dir "/opt/gopath/src/github.com/hyperledger/blockchain-explorer/node_modules/pkcs11js/.node-gyp/8.9.4"
    gyp WARN EACCES attempting to reinstall using temporary dev dir "/opt/gopath/src/github.com/hyperledger/blockchain-explorer/node_modules/pkcs11js/.node-gyp"
    
    # 修改 main.sh 文件
    # 在所有 npm install $VERBOSE 后面添加 --unsafe-perm 跳过权限验证
    # function do_install () {
    #		...
    #		npm install $VERBOSE --unsafe-perm=true --allow-root
    # }	
    
  • Hyperledger Explorer ./start.sh 报错
    # error 信息
    ['Default client peer is down and no channel details available database']
    
    # 解决方案
    # 修改 blockchain-explorer/app/platform/fabric/connection-profile/first-network.json
    # true 改为 false
    "tlsEnable" : false
    # grpcs 改为 grpc
    "url" : "grpc://localhost:7051",
    "eventUrl" : "grpc://localhost:7053"
    

你可能感兴趣的:(区块链,#,Hyperledger,Fabric)