zoukankan      html  css  js  c++  java
  • CentOS7 搭建Fabric 1.0

    1.环境搭建

    1.1 go的按装及配置

    1.1.1下载go压缩包

    wget https://dl.google.com/go/go1.9.2.linux-amd64.tar.gz

    1.1.2 解压

    tar -C /usr/local -zxvf go1.9.2.linux-amd64.tar.gz

    1.1.3配置环境变量

    vi ~/.bash_profile
    

    添加如下内容:

    PATH=$PATH:$HOME/bin
    export PATH
    export PATH=$PATH:/usr/local/go/bin
    export GOROOT=/usr/local/go
    export GOPATH=$HOME/go
    export PATH=$PATH:$HOME/go/bin
    

    使环境变量生效

    source  ~/.bash_profile
    

    1.2 安装Docker

    1.2.1 检查是否已经安装

    rpm -qa|grep docker

    1.2.2 卸载旧版本

    yum remove docker docker-client docker-common docker-latest docker-latest-logrotate docker-logrotate docker-selinux docker-engine-selinux docker-engine
    

    1.2.3 安装需要的软件包

    yum install -y yum-utils device-mapper-persistent-data lvm2
    

    1.2.4设置 stable 源

    yum-config-manager --add-repo http://download.docker.com/linux/centos/docker-ce.repo
    

    1.2.5 启用 启用 docker-ce-edge和docker-ce-test(可选)

    yum-config-manager --enable docker-ce-edge
    yum-config-manager --enable docker-ce-tes
    

    1.2.6 安装 Docker CE

    yum install docker-ce
    

    1.2.7 启动docker

    service docker start
    

    1.2.8 加入开机启动

    chkconfig docker on
    

    1.2.9 测试是否安装成功

    docker --version
    

    1.2.10 安装docker-compose

    curl -L https://github.com/docker/compose/releases/download/1.20.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
    

    1.2.11 测试docker-compose是否安装成功

    docker-compose version
    

    1.3 安装python-pip

    yum -y install epel-release
    yum -y install python-pip
    

    1.4 安装node.js

    ## root 
    curl -sL https://rpm.nodesource.com/setup_10.1 | bash -
    ## no root
    curl -sL https://rpm.nodesource.com/setup_10.1 | sudo bash -
    yum install -y nodejs
    

    1.5 升级gcc

    wget http://ftp.gnu.org/gnu/gcc/gcc-9.1.0/gcc-9.1.0.tar.gz
    tar -C /usr/local -xzf gcc-9.1.0.tar.gz
     cd /usr/local/gcc-9.1.0
     ./contrib/download_prerequisites 
     mkdir build
      cd build/
     ../configure --enable-checking=release --enable-languages=c,c++ --disable-multilib
      yum groupinstall "Development Tools" 
     make
     make install
    

    1.6 安装npm

    $ npm install npm -g
    $ npm -v
    

    1.7 fabric下载

    1.7.1 下载fabric源码

    ## 下载源码
    $ mkdir -p ~/go/src/github.com/hyperledger 
    $ cd ~/go/src/github.com/hyperledger 
    $ git clone https://github.com/hyperledger/fabric.git
    ## 切换到v1.0.0版本
    $ cd ~/go/src/github.com/hyperledger/fabric
    $ git checkout v1.0.0
    

    1.7.2 下载 Fabric Docker镜像

    $ cd ~/go/src/github.com/hyperledger/fabric/examples/e2e_cli/
    $ source download-dockerimages.sh -c x86_64-1.0.0 -f x86_64-1.0.0
    

    1.7.3 检查下载的镜像列表

    docker images
    

    1.7.4 测试Fabric环境是否成功

    cd ~/go/src/github.com/hyperledger/fabric/examples/e2e_cli
    ./network_setup.sh up
    

    该指令做了一下操作

    1.编译生成Fabric公私钥、证书的程序,程序在目录:fabric/release/linux-amd64/bin
    2.基于configtx.yaml生成创世区块和通道相关信息,并保存在channel-artifacts文件夹。
    3.基于crypto-config.yaml生成公私钥和证书信息,并保存在crypto-config文件夹中。
    4.基于docker-compose-cli.yaml启动1Orderer+4Peer+1CLI的Fabric容器。
    5.在CLI启动的时候,会运行scripts/script.sh文件,这个脚本文件包含了创建Channel,加入Channel,安装Example02,运行Example02等功能。

    运行完如果出现下图所示,说明整个Fabric网络已经通了.

    2.测试Fabric网络

    Fabric提供了SDK和CLI两种交互方式,我们使用的是CLI 。

    使用官方提供的小例子进行测试,在官方例子中,channel名字是mychannel,链码(智能合约)的名字是mycc。
    首先要登录到CLI这个容器中,才能执行Fabric的CLI命令:

    docker exec -it cli bash
    

    这时用户名变为root@44084971aff7,当前目录变为/opt/gopath/src/github.com/hyperledger/fabric/peer#,接着可执行peer命令,体验区块链的命令行使用方式。

    2.1 查看账户余额

    root@44084971aff7:/opt/gopath/src/github.com/hyperledger/fabric/peer#  peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
    

    结果:

    Query Result: 90
    

    这里90就是a账户的余额

    2.2 调用链码,转账

    让b账户向a账户转账10:

    peer chaincode invoke -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 mycc -c '{"Args":["invoke","a","b","20"]}'
    

    再次执行查询(因为转账操作了2次所以此处是50)

    root@c259da3de4d1:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
    

    2.3 退出cli容器

    直接执行

    exit
    

    关闭Fabric

    cd ~/go/src/github.com/hyperledger/fabric/examples/e2e_cli
    ./network_setup.sh down
    

    3.遇到的问题

    3.1 使用wget、curl、git报command not found

    • 原因:未安装wget、curl

    • 解决

      yum install -y wget
      yum install -y curl
      yum install -y git
      

    3.2 安装docker-compose问题

    Failed connect to github-production-release-asset-2e65be.s3.amazonaws.com:443; Connection timed out

    • 原因: 该地址(github-production-release-asset-2e65be.s3.amazonaws.com) 需要的一些下载的访问被 国内屏蔽了

    • 解决:在hosts中加配置

      ping github-production-release-asset-2e65be.s3.amazonaws.com
       vi /etc/hosts
      

      新增如下配置:

      52.216.128.51 github-production-release-asset-2e65be.s3.amazonaws.com

    3.3 gcc升级问题

    configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+.,mpfr2.4.0

     yum install  gmp  gmp-devel  mpfr  mpfr-devel  libmpc  libmpc-devel
    

    3.4 gcc动态库没有替换问题

    node: /usr/lib64/libstdc++.so.6: version GLIBCXX_3.4.21' not found (required by node) node: /usr/lib64/libstdc++.so.6: version GLIBCXX_3.4.15' not found (required by node)
    node: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by node)

    运行检查命令

    strings /usr/lib64/libstdc++.so.6 | grep GLIBC
    

    查找编译gcc时生成的最新动态库

    find / -name "libstdc++.so*"
    cd /usr/lib64
    rm -rf libstdc++.so.6
    ln -s libstdc++.so.6.0.26 libstdc++.so.6
    strings /usr/lib64/libstdc++.so.6 | grep GLIBC
    

    3.5 Fabric启动报错1

    can't load package: package github.com/hyperledger/fabric/core/chaincode/shim: cannot find package

    源码路径不对

    go env
    

    把fabric源码放在~/go/src/github.com/hyperledger 目录下

    3.6 Fabric启动报错2

    Error response from daemon: Container is not running

    ##查看运行的容器
    docker ps -a
    ## 把失败的重新启动
    docker start 44084971aff7 
    

    3.7 Fabric启动报错3

    Error: Error getting endorser client chaincode: PER:404 - Error trying to connect to local peer

    重新关闭网络

    ./network_setup.sh down mychannel
    

    3.8 测试转账no such file or directory

    root@b103ad898190:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /root/go/src/github.com/hyperledger/fabric/examples/e2e_cli/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc -c '{"Args":["invoke","b","a","10"]}'
    2020-01-21 10:19:15.752 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
    2020-01-21 10:19:15.752 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
    Error: Error getting broadcast client: Error connecting to orderer.example.com:7050 due to open /root/go/src/github.com/hyperledger/fabric/examples/e2e_cli/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem: no such file or directory

    根证书不对

    参考文档

    1.联盟链初识以及Fabric环境搭建流程

    2.centos7安装fabric

  • 相关阅读:
    创建数据库的那些事
    同步、异步、阻塞、非阻塞我的理解
    Openfire MultiUserChat 多用户聊天 消息发送
    JAVA 随机字符串
    OF 同步异步问题的改进
    Openfire S2S 监听与消息处理
    MySQL FEDERATED 存储引擎
    一个S2S通信中的同步、异步问题的解决
    Openfire Monitoring/jinglenodes plugin error
    Java Cache System JCS(一) 使用方法
  • 原文地址:https://www.cnblogs.com/vicosong/p/12226132.html
Copyright © 2011-2022 走看看