Fabric网络搭建流程

Posted mengXi_0718

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Fabric网络搭建流程相关的知识,希望对你有一定的参考价值。

0.虚拟机使用
  • ubuntu16.04.7

  • 安装openssh-server:
    apt install openssh-server

1.安装python
  • ubuntu18默认安装python3.6,查看版本:
    python --version

  • 更新apt-get:
    sudo apt-get update

  • 安装python2.7:
    sudo apt-get install python2.7

  • 安装pip:
    sudo apt install python-pip
    sudo apt install python3-pip

  • 查看pip版本:
    pip --version
    pip3 --version

  • 更新pip:
    sudo pip install --upgrade pip

2.安装go

mkdir go
cd go

  • 下载上传下载工具:
    sudo apt install lrzsz
  • rz 上传
  • 手动安装配置:
    手动下载地址:https://golang.google.cn/dl/
  • 解压:
    tar -xvf go1.15.11.linux-amd64.tar.gz (-C可以指定解压到的目录)
  • 安装vim工具:
    apt install vim
    mkdir go/workspace
  • 配置环境变量:
    vim ~/.bashrc
  • Shfit + G 直接跳到最后一行
    export GOROOT=/home/hallen/go/go
    export GOPATH=/home/hallen/go/workspace
    export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
  • 重新加载配置文件:
    source ~/.bashrc
3.git安装
  • 用来拉取fabric代码:
    sudo apt install git
4.安装docker
  • 更新apt:
    sudo apt-get update
  • 安装docker:
    sudo apt-get install docker
    sudo apt install docker.io
  • 验证:会报权限不够的错误
    docker version
  • 创建docker用户组:
    sudo groupadd docker
  • 将当前用户添加到docker用户组:sudo gpasswd -a 用户名 docker
    sudo gpasswd -a hallen docker
  • 重启docker服务:
    systemctl restart docker
  • 当前用户切换到docker组:
    newgrp docker
  • 验证:
    docker version
5.安装docker-compose

mkdir docker

  • 安装curl:
    sudo apt install curl
  • 下载:
curl -L https://get.daocloud.io/docker/compose/releases/download/1.12.0/docker-compose-`uname -s`-`uname -m` > ~/docker-compose
  • 移动到指定的目录:
    sudo mv ~/docker-compose /usr/local/bin/docker-compose
  • 修改权限:
    chmod +x /usr/local/bin/docker-compose
6.fabric安装:
  • go/workspace目录下:创建三个目录
    mkdir bin pkg src
  • gopath下新建一个目录,存放后期的所有fabric有关的
    mkdir src
  • 进入到目录:
    cd src
    git clone https://github.com/hyperledger/fabric.git
  • 切换分支:
    cd fabric
    git checkout v1.0.0 main分支是没有examples文件夹的
7.安装fabric的docker镜像
  • 进入e2e_cli目录:cd examples/e2e_cli (如果没有examples,需要切换分支到1.0)
  • 在fabric目录下:
  • 批量下载:
    source download-dockerimages.sh -c x86_64-1.0.0 -f x86_64-1.0.0
  • 分开下载:
    docker pull hyperledger/fabric-tools:x86_64-1.0.0
    docker pull hyperledger/fabric-orderer:x86_64-1.0.0
    docker pull hyperledger/fabric-peer:x86_64-1.0.0
    docker pull hyperledger/fabric-couchdb:x86_64-1.0.0
    docker pull hyperledger/fabric-kafka:x86_64-1.0.0
    docker pull hyperledger/fabric-ca:x86_64-1.0.0
    docker pull hyperledger/fabric-ccenv:x86_64-1.0.0
    docker pull hyperledger/fabric-baseimage:x86_64-0.4.7
    docker pull hyperledger/fabric-javaenv:x86_64-1.0.0
    docker pull hyperledger/fabric-zookeeper:x86_64-1.0.0
  • 修改标签为latest:
    docker tag hyperledger/fabric-tools:x86_64-1.0.0 hyperledger/fabric-tools
    docker tag hyperledger/fabric-orderer:x86_64-1.0.0 hyperledger/fabric-orderer
    docker tag hyperledger/fabric-peer:x86_64-1.0.0 hyperledger/fabric-peer
    docker tag hyperledger/fabric-couchdb:x86_64-1.0.0 hyperledger/fabric-couchdb
    docker tag hyperledger/fabric-kafka:x86_64-1.0.0 hyperledger/fabric-kafka
    docker tag hyperledger/fabric-ca:x86_64-1.0.0 hyperledger/fabric-ca
    docker tag hyperledger/fabric-ccenv:x86_64-1.0.0 hyperledger/fabric-ccenv
    docker tag hyperledger/fabric-baseimage:x86_64-0.4.7 hyperledger/fabric-baseimage
    docker tag hyperledger/fabric-javaenv:x86_64-1.0.0 hyperledger/fabric-javaenv
    docker tag hyperledger/fabric-zookeeper:x86_64-1.0.0 hyperledger/fabric-zookeeper
  • 验证:
    docker images
  • 删除镜像:
  • 先删除容器:
    docker container prune
  • 再删除镜像:
    docker rmi –f [IMAGE ID]
8.生成可执行文件:
  • 可在物理机上执行命令编译后上传
  • 也可直接用群里面的上传
  • 自己生成的方式有报错,不提供,有时间自己再试试
9.配置文件及生成证书–cryptogen
  • src目录下
    mkdir hyperledger
    cryptogen showtemplate > conf.yaml
  • 修改内容后
    cryptogen generate --config conf.yaml
  • 生成conf目录,里面会有两个目录:order和peer的organization
10.创世块和通道文件的生成
  • 配置文件必须叫configtx.yaml,配置文件路径:sampleconfig/configtx.yaml,拷贝到src目录下
  • 生成创世块命令:
    configtxgen -profile GenGenesis -outputBlock ./genesis.block
  • 生成通道文件:channelID: 使用小写
    configtxgen -profile GenChannel -channelID zlktchannel -outputCreateChannelTx ./channel.tx
    chmod 775 channel.tx genesis.block
11.生成更新锚节点文件
configtxgen -profile GenChannel -outputAnchorPeersUpdate ./Org1MSPAnchorUpdate.tx -asOrg Org1MSP -channelID zlktChannel
configtxgen -profile GenChannel -outputAnchorPeersUpdate ./Org2MSPAnchorUpdate.tx -asOrg Org2MSP -channelID zlktChannel
12.docker-compose启动
  • 三个文件 docker-compose-cli.yaml docker-compose-base.yaml peer-base.yaml
  • hyperleger目录下:
    mkdir channel-artifacts
  • rz 上传docker-compose-cli.yaml文件
    mv genesis.block channel-artifacts/
    mv channel.tx channel-artifacts/
    mkdir chaincode
    mkdir base
    cd base
  • rz上传docker-compose-base.yaml 和 peer-base.yaml
  • 启动:
    docker-compose -f docker-compose-cli.yaml up -d
  • 查看启动状态:
    docker-compose -f docker-compose-cli.yaml ps
13.创建通道及将节点加入通道
  • 创建通道:
docker exec -it cli /bin/bash peer channel create -o orderer.example.com:7050 -c zlktchannel -f ./channel-artifacts/channel.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/msp/tlscacerts /tlsca.example.com-cert.pem
  • 验证:
    peer channel list
  • 将当前节点加入通道:
    peer channel join -b zlktchannel.block
  • 更换节点:
export CORE_PEER_ADDRESS=peer0.org2.example.com:7051
export CORE_PEER_LOCALMSPID=Org2MSP
export CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/server.crt
export CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/server.key
export 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
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
  • 将当前节点加入通道:
    peer channel join -b zlktchannel.block
14.peer节点安装链码
  • 先将链码上传到挂载路径go/workspace/src/hyperledger/chaincode/go下
  • rz上链码 ,容器会自动挂载
    peer chaincode install -n example02 -v 1.0.0 -p github.com/hyperledger/fabric/examples/chaincode/go
  • 链码实例化:
peer chaincode instantiate -o orderer.example.com:7050 -C zlktchannel -c '{"Args":["init","a","200","b","300"]}' -n example02 -P "OR('Org1MSP.member','Org2MSP.member')" -v 1.0.0 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/msp/tlscacerts/tlsca.example.com-cert.pem
  • 查询:
    peer chaincode query -C zlktchannel -n example02 -c '{"Args":["query","a"]}'
15.安装自己编写链码,测试
  • 重新安装链码,指定-p的路径一定和原来的不在同一个目录,-n 参数不能和原来的一样
  • 用户目录下:hyperledger/chaincode/go目录下 (由于是挂载,所以用户目录与容器目录要一致)
    mkdir mycc
    cd mycc
  • rz 上传链码
  • 重新上传,重新安装,重新实例化
    peer chaincode install -n mycc1 -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/mycc
peer chaincode instantiate -o orderer.example.com:7050 -C zlktchannel -c '{"Args":["init","name","hallen","age","18"]}' -n mycc1 -v 1.0 -P "OR('Org1MSP.member','Org2MSP.member')" --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/msp/tlscacerts/tlsca.example.com-cert.pem

peer chaincode query -C zlktchannel -n mycc1 -c '{"Args":["query","name"]}'

  • 安装交易链码:
  • 重新安装链码,指定-p的路径一定和原来的不在同一个目录,-n 参数不能和原来的一样
  • 用户目录下:hyperledger/chaincode/go目录下 (由于是挂载,所以用户目录与容器目录要一致)
    mkdir mytxcc
    cd mytxcc
  • rz 上传链码
  • 重新上传,重新安装,重新实例化
    peer chaincode install -n mytxcc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/mytxcc
peer chaincode instantiate -o orderer.example.com:7050 -C zlktchannel -c '{"Args":["init","hallen","200","zhiliao","300"]}' -n mytxcc -v 1.0 -P "OR ('Org1MSP.member','Org2MSP.member')" --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/msp/tlscacerts/tlsca.example.com-cert.pem

peer chaincode query -C zlktchannel -n mytxcc -c '{"Args":["query","hallen"]}'
peer chaincode query -C zlktchannel -n mytxcc -c '{"Args":["query","hallen"]}'

peer chaincode invoke -C zlktchannel -n mytxcc -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/msp/tlscacerts/tlsca.example.com-cert.pem -c '{"Args":["invoke","hallen","zhiliao","20"]}'
  • 这里演示链码升级:
  • 把代码放到chaincode挂在的目录中,然后替换原来的旧代码
  • 安装新版本chaincode:
    docker exec -it cli bash
    peer chaincode install -n mytxcc -v 2.0 -p github.com/hyperledger/fabric/examples/chaincode/go/mytxcc
  • ​ -n 一定要之前的保持一致,只需要改版本号
  • 更新:
peer chaincode upgrade -o orderer.example.com:7050 -C zlktchannel -c '{"Args":["init","hallen","200","zhiliao","300"]}' -n mytxcc -v 2.0 -P "OR ('Org1MSP.member','Org2MSP.member')" --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/msp/tlscacerts/tlsca.example.com-cert.pem
  • 用户目录下:hyperledger/chaincode/go目录下 (由于是挂载,所以用户目录与容器目录要一致)
    mkdir myivcc
    cd myivcc
  • rz上传链码
  • 重新上传,重新安装,重新实例化
    peer chaincode install -n myivcc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/myivcc
peer chaincode instantiate -o orderer.example.com:7050 -C zlktchannel -c '{"Args":["init","hallen","200"]}' -n myivcc -v 1.0 -P "OR ('Org1MSP.member','Org2MSP.member')" --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/msp/tlscacerts/tlsca.example.com-cert.pem
peer chaincode query -o orderer.example.com:7050 -C zlktchannel -n myivcc -c '{"Args":["invoke","mytxcc","hallen","zlktchannel"]}'
16.链码打包
  • 在容器下执行/opt/gopath/src/github.com/hyperledger/fabric/peer#:
    peer chaincode package -n myivcc -p github.com/hyperledger/fabric/examples/chaincode/go/myivcc -v 1.0 myivcc.1.0.out

以上是关于Fabric网络搭建流程的主要内容,如果未能解决你的问题,请参考以下文章

手动搭建Fabric流程

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

基于区块链/Hyperledger Fabric与IPFS的电子病历数据传输系统搭建流程

基于Spring的Fabric区块链Gateway,简化区块链开发

搭建Fabric网络

区块链100例 编写脚本快速搭建超级账本 Fabric