Ubuntu环境下搭建hyperledger fabric超级无敌详细教程

Posted Jude00000

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ubuntu环境下搭建hyperledger fabric超级无敌详细教程相关的知识,希望对你有一定的参考价值。

Ubuntu环境下搭建hyperledger fabric超级无敌详细教程

一、搭建前提条件

1.1 如果尚未安装最新版本的git,请安装它

$ sudo apt-get install git

1.2 如果尚未安装最新版本的curl,请安装它

$ sudo apt-get install curl

1.3 如果尚未安装最新版本的Docker,请安装它

1.3.1 如果安装了docker旧版本,卸载旧版本docker(可选)
 $ sudo apt-get remove docker docker-engine docker.io containerd runc
1.3.2 更新apt软件包索引并安装软件包以允许apt通过HTTPS使用存储库
$ sudo apt-get update
$ sudo apt-get install \\
       apt-transport-https \\
       ca-certificates \\
       curl \\
       gnupg \\
       lsb-release
1.3.3 添加Docker的官方GPG密钥
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
1.3.4 设置稳定的存储库
$ echo \\
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \\
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
1.3.5 更新apt软件包索引,并安装最新版本的Docker Engine和容器化的容器
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
1.3.6 通过运行hello-world 映像来验证是否正确安装了Docker Engine
$ sudo docker run hello-world
此命令下载测试图像并在容器中运行它。容器运行时,它会打印参考消息并退出。
1.3.7 如果允许非特权用户运行Docker命令执行以下步骤(可选)
  1. 创建docker组
$ sudo groupadd docker
如果存在docker组,显示已存在docker组,执行下一步
  1. 将自己的用户添加到该docker组
$ sudo usermod -aG docker $USER
  1. 激活对组的更改
$ newgrp docker
  1. 验证是否可以运行docker不带命令sudo
$ docker run hello-world
此命令下载测试图像并在容器中运行它。容器运行时,它会打印参考消息并退出。
1.3.8 配置Docker以在启动时启动
> $ sudo systemctl enable docker.service
> $ sudo systemctl enable containerd.service
> 若要禁用此行为,enable改成disable
> $ sudo systemctl disable docker.service
> $ sudo systemctl disable containerd.service
1.3.9 测试docker的安装
$ docker version
打印以下信息:
Client: Docker Engine - Community
 Version:           20.10.6
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        370c289
 Built:             Fri Apr  9 22:47:17 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.6
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       8728dd2
  Built:            Fri Apr  9 22:45:28 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.4
  GitCommit:        05f951a3781f4f2c1911b05e61c160e9c30eaa8e
 runc:
  Version:          1.0.0-rc93
  GitCommit:        12644e614e25b05da6fd08a38ffa0cfe1903fdec
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

1.4 安装Docker Compose

  1. 下载Docker Compose的当前稳定版本1.29.1
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.29.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
要安装其他版本的Compose,替换1.29.1为要使用的Compose版本
  1. 将可执行权限应用于二进制文件
$ sudo chmod +x /usr/local/bin/docker-compose
  1. 测试Docker Compose的安装
$ docker-compose --version
打印以下信息:
docker-compose version 1.29.1, build c34c88b2

1.5 安装go

  1. 下载go的压缩包(最好选用1.15.5版本)
$ wget https://dl.google.com/go/go1.15.5.linux-amd64.tar.gz
  1. 将下载的压缩文件解压到/ usr / local,在/ usr / local / 目录中创建一个Go文件夹
在解压之前删除/ usr / local / go上的先前安装(如果有)
$ sudo rm -rf / usr / local / go  
解压
$ tar -C / usr / local -xzf go1.16.4.linux-amd64.tar.gz
  1. 将/ usr / local / go / bin添加到PATH环境变量
$ export PATH=$PATH:/usr/local/go/bin
使环境变量生效
$ source $HOME/.profile
  1. 查看go是否配置成功
$ go version
go version go1.15.5 linux/amd64

二、安装Fabric and Fabric Samples

  1. 选定安装的目录(自己选择)
$ mkdir hyperledger
$ cd hyperledger
  1. 从github上克隆所需的fabric-samples
$ git clone git://github.com/hyperledger/fabric-samples.git
下载过程比较慢,如果出现网络中断下载失败可以选择在Windows上面下好传入linux
  1. 克隆完成后在hyperledger文件夹下面会产生一个fabric-samples文件夹
$ cd fabric-samples
  1. 下载最新版本的Fabric示例,docker映像和二进制文件
4.1 在Windows下打开网址https://bit.ly/2ysbOFE,复制全文
4.2 在linux的fabric-samples目录下使用以下命令
$ sudo vi bootstrap.sh
把刚刚复制的文本粘贴进去,然后保存退出
4.3 然后执行bootstrap.sh文件
$ sh bootstrap.sh
如果出现Syntax error: "(" unexpected (expecting "fi")的错误信息,执行以下命令
$ sudo dpkg-reconfigure dash
在弹出的界面选择no
4.4 执行完成后会在fabric-samples目录下生成一个bin文件夹和一个config文件夹
$ cd bin
$ ls
显示以下目录:
configtxgen  configtxlator  cryptogen  discover  fabric-ca-client  fabric-ca-server  idemixgen  orderer  osnadmin  peer
4.5 使用以下命令让这些二进制文件能全局使用
$ cp * /usr/local/bin
  1. 查看docker镜像
$ docker images
显示以下信息:
REPOSITORY                   TAG       IMAGE ID       CREATED        SIZE
busybox                      latest    c55b0f125dc6   6 days ago     1.24MB
hyperledger/fabric-tools     2.3       a206a1593b4c   2 weeks ago    448MB
hyperledger/fabric-tools     2.3.2     a206a1593b4c   2 weeks ago    448MB
hyperledger/fabric-tools     latest    a206a1593b4c   2 weeks ago    448MB
hyperledger/fabric-peer      2.3       85c825d4769f   2 weeks ago    54.2MB
hyperledger/fabric-peer      2.3.2     85c825d4769f   2 weeks ago    54.2MB
hyperledger/fabric-peer      latest    85c825d4769f   2 weeks ago    54.2MB
hyperledger/fabric-orderer   2.3       7cad713cbfea   2 weeks ago    37.8MB
hyperledger/fabric-orderer   2.3.2     7cad713cbfea   2 weeks ago    37.8MB
hyperledger/fabric-orderer   latest    7cad713cbfea   2 weeks ago    37.8MB
hyperledger/fabric-ccenv     2.3       627c556b15ca   2 weeks ago    514MB
hyperledger/fabric-ccenv     2.3.2     627c556b15ca   2 weeks ago    514MB
hyperledger/fabric-ccenv     latest    627c556b15ca   2 weeks ago    514MB
hyperledger/fabric-baseos    2.3       e50ea411d694   2 weeks ago    6.86MB
hyperledger/fabric-baseos    2.3.2     e50ea411d694   2 weeks ago    6.86MB
hyperledger/fabric-baseos    latest    e50ea411d694   2 weeks ago    6.86MB
hyperledger/fabric-ca        1.5       24a7c19a9fd8   2 months ago   70.8MB
hyperledger/fabric-ca        1.5.0     24a7c19a9fd8   2 months ago   70.8MB
hyperledger/fabric-ca        latest    24a7c19a9fd8   2 months ago   70.8MB
hello-world                  latest    d1165f221234   2 months ago   13.3kB

三、测试网络

3.1 建立测试网络

  1. 进入测试网络目录
$ cd fabric-samples/test-network
  1. 开启网络
$ ./network.sh up
打印以下信息:
Creating network "fabric_test" with the default driver
Creating volume "docker_orderer.example.com" with default driver
Creating volume "docker_peer0.org1.example.com" with default driver
Creating volume "docker_peer0.org2.example.com" with default driver
Creating orderer.example.com    ... done
Creating peer0.org2.example.com ... done
Creating peer0.org1.example.com ... done
Creating cli                    ... done
CONTAINER ID   IMAGE                               COMMAND             CREATED         STATUS                    PORTS                                                                                  NAMES
16b3a43f6016   hyperledger/fabric-tools:latest     "/bin/bash"         1 second ago    Up Less than a second                                                                                            cli
8df4f93b6fc9   hyperledger/fabric-peer:latest      "peer node start"   4 seconds ago   Up 1 second               0.0.0.0:7051->7051/tcp, :::7051->7051/tcp                                              peer0.org1.example.com
a9c1304be159   hyperledger/fabric-peer:latest      "peer node start"   4 seconds ago   Up 1 second               7051/tcp, 0.0.0.0:9051->9051/tcp, :::9051->9051/tcp                                    peer0.org2.example.com
89d3f565c91e   hyperledger/fabric-orderer:latest   "orderer"           4 seconds ago   Up 1 second               0.0.0.0:7050->7050/tcp, :::7050->7050/tcp, 0.0.0.0:7053->7053/tcp, :::7053->7053/tcp   orderer.example.com
  1. 运行以下命令以列出自己计算机上运行的所有Docker容器。应该会看到该network.sh脚本创建的三个节点
$ docker ps -a
打印以下信息:
8df4f93b6fc9   hyperledger/fabric-peer:latest      "peer node start"   2 minutes ago   Up 2 minutes              0.0.0.0:7051->7051/tcp, :::7051->7051/tcp                                              peer0.org1.example.com
a9c1304be159   hyperledger/fabric-peer:latest      "peer node start"   2 minutes ago   Up 2 minutes              7051/tcp, 0.0.0.0:9051->9051/tcp, :::9051->9051/tcp                                    peer0.org2.example.com
89d3f565c91e   hyperledger/fabric-orderer:latest   "orderer"           2 minutes ago   Up 2 minutes              0.0.0.0:7050->7050/tcp, :::7050->7050/tcp, 0.0.0.0:7053->7053/tcp, :::7053->7053/tcp   orderer.example.com

3.2 创建通道

  1. 运行以下命令会创建默认通道名称mychannel
$ ./network.sh createChannel
打印以下信息:
2021-05-10 14:01:47.255 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-05-10 14:01:47.293 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
Anchor peer set for org 'Org2MSP' on channel 'mychannel'
Channel 'mychannel' joined
  1. 创建指定名称的通道(可选)。也可以使用channel标志创建具有自定义名称的通道。例如,以下命令将创建一个名为channel1的通道
$ ./network.sh createChannel -c channel1

3.3 启动链码

  1. 在启动链码之前先安装go依赖,进入链码路径
$ cd fabric-samples/asset-transfer-basic/chaincode-go
$ ls
在没有安装go依赖之前没有vendor文件夹
assetTransfer.go  chaincode  go.mod  go.sum
安装go依赖/为go语言换一个中国境内源下载
$ go env -w GOPROXY=https://goproxy.cn,direct
$ go mod vendor
然后生成一个vendor文件夹
$ ls
显示以下目录:
assetTransfer.go  chaincode  go.mod  go.sum  vendor
  1. 启动链码
$ cd fabric-samples/test-network
$ ./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
会看到以下输出,组织1和组织2:
Committed chaincode definition for chaincode 'basic' on channel 'mychannel':
Version: 1.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
Query chaincode definition successful on peer0.org1 on channel 'mychannel'
Using organization 2
Querying chaincode definition on peer0.org2 on channel 'mychannel'...
Attempting to Query committed status on peer0.org2, Retry after 3 seconds.
+ peer lifecycle chaincode querycommitted --channelID mychannel --name basic
+ res=0
Committed chaincode definition for chaincode 'basic' on channel 'mychannel':
Version: 1.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
Query chaincode definition successful on peer0.org2 on channel 'mychannel'
Chaincode initialization is not required

3.4 与网络交互

3.4.1 交互之前添加两个环境变量
$ export PATH=${PWD}/../bin:$PATH
$ export FABRIC_CFG_PATH=$PWD/../config/
3.4.2 组织1交互
  1. 添加组织1的环境变量
$ export CORE_PEER_TLS_ENABLED=true
  export CORE_PEER_LOCALMSPID="Org1MSP"
  export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
  export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
  export CORE_PEER_ADDRESS=localhost:7051
  1. 与组织1进行交互,运行以下命令初始化账本资产
$ peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"InitLedger","Args":[]}'
如果成功,应该看到类似以下的输出:
INFO 001 Chaincode invoke successful. result: status:200 
  1. 调用以下命令查询账本资产
$ peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
如果成功,应该看到以下输出:
[{"ID":"asset1","color":"blue","size":5,"owner":"Tomoko","appraisedValue":300},
{"ID":"asset2","color":"red","size":5,"owner":"Brad","appraisedValue":400},
{"ID":"asset3","color":"green","size":10,"owner":"Jin Soo","appraisedValue":500},
{"ID":"asset4","color":"yellow","size":10,"owner":"Max","appraisedValue":600},
{"ID":"asset5","color":"black","size":15,"owner":"Adriana","appraisedValue":700},
{"ID":"asset6","color":"white","size":15,"owner":"Michel","appraisedValue":800}]
  1. 当网络成员要转移或更改账本的资产时,将调用链码。使用以下命令通过调用资产转移(basic)链码来更改账本资产的所有者。
$ peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"TransferAsset","Args":["asset6","Christopher"]}'
如果命令成功,应该看到以下信息:
chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
3.4.3 组织2交互
  1. 添加组织2的环境变量
$ export CORE_PEER_TLS_ENABLED=true
  export CORE_PEER_LOCALMSPID="Org2MSP"
  export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
  export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
  export CORE_PEER_ADDRESS=localhost:9051
  1. 现在,可以查询资产转移(basic)链码peer0.org2.example.com
$ peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'
结果将显示"asset6"已转移到Christopher:
{"ID":"asset6","color":"white","size":15,"owner":"Christopher","appraisedValue":800}

四、测试结束,中断网络

使用以下命令关闭测试网络:

$ ./network.sh down

以上是关于Ubuntu环境下搭建hyperledger fabric超级无敌详细教程的主要内容,如果未能解决你的问题,请参考以下文章

ubuntu16.04下Hyperledger之搭建Fabric环境简单操作(五步启动e2e_cli)

Hyperledger-fabric 环境搭建

Hyperledger Fabric环境搭建过程

Hyperledger Fabric 1.4环境搭建详尽

Hyperledger Fabric 1.4环境搭建详尽

Hyperledger Fabric 1.1安装部署-基础环境搭建