Fabric 1.2版本的第一个测试网络【入门篇】

环境
fabric : 1.2
docker :20
ubuntu: 20
docker-compose : 2.19
go 1.10

下载fabric-sample

我们选择怎么简单怎么来,直接选择拿官网文档的脚本来将fabric 的镜像源,fabric-sample 的github文件,fabric的二进制文件下载下来【可能在镜像源部分会下载很慢,需要改一下docker 的镜像地址】。

curl -sSL http://bit.ly/2ysbOFE | bash -s 1.2.0 1.2.0 0.4.10

下载完成,可以查看一下当前目录文件

root@192-168-19-133:/yijiu/fabricfile/1.2# ll
总用量 12
drwxr-xr-x  3 root root 4096 94 14:05 ./
drwxr-xr-x  7 root root 4096 94 13:39 ../
drwxr-xr-x 14 root root 4096 94 14:05 fabric-samples/
root@192-168-19-133:/yijiu/fabricfile/1.2#

查看一下镜像 docker images -a

root@192-168-19-133:/yijiu/fabricfile/1.2# docker images -a
hyperledger/fabric-ca                   1.2            66cc132bd09c   5 years ago     252MB
hyperledger/fabric-ca                   1.2.0          66cc132bd09c   5 years ago     252MB
hyperledger/fabric-ca                   latest         66cc132bd09c   5 years ago     252MB
hyperledger/fabric-tools                1.2            379602873003   5 years ago     1.51GB
hyperledger/fabric-tools                1.2.0          379602873003   5 years ago     1.51GB
hyperledger/fabric-tools                latest         379602873003   5 years ago     1.51GB
hyperledger/fabric-ccenv                1.2            6acf31e2d9a4   5 years ago     1.43GB
hyperledger/fabric-ccenv                1.2.0          6acf31e2d9a4   5 years ago     1.43GB
hyperledger/fabric-ccenv                latest         6acf31e2d9a4   5 years ago     1.43GB
hyperledger/fabric-orderer              1.2            4baf7789a8ec   5 years ago     152MB
hyperledger/fabric-orderer              1.2.0          4baf7789a8ec   5 years ago     152MB
hyperledger/fabric-orderer              latest         4baf7789a8ec   5 years ago     152MB
hyperledger/fabric-peer                 1.2            82c262e65984   5 years ago     159MB
hyperledger/fabric-peer                 1.2.0          82c262e65984   5 years ago     159MB
hyperledger/fabric-peer                 latest         82c262e65984   5 years ago     159MB
hyperledger/fabric-baseos               amd64-0.4.10   52190e831002   5 years ago     132MB

这个脚本文件还会将当前的fabric版本镜像暂时设为latest

可以验证一下fabric 版本,去到bin目录

root@192-168-19-133:/yijiu/fabricfile/1.2/fabric-samples/bin# ./configtxgen  -version
2023-09-05 14:57:17.979 CST [common/tools/configtxgen] main -> WARN 001 Omitting the channel ID for configtxgen is deprecated.  Explicitly passing the channel ID will be required in the future, defaulting to 'testchainid'.
configtxgen:
 Version: 1.2.0
 Commit SHA: cae2ad4
 Go version: go1.10
 OS/Arch: linux/amd64

若想全局应用,可以
export PATH= /bin:$PATH
表示fabric-samples文件目录所在路径,
例如:

export PATH=/yijiu/fabricfile/1.2/fabric-samples/bin:$PATH
添加环境变量的意义为在系统中任何路径下使用Fabric相关的命令都可以让系统找到该命令并且顺利执行。后期我们会进入Fabric目录中执行相应的命令,所以也可以不添加该环境变量。

创建测试网路

我们可以执行里面的案例测试,在first-network,里面有一个byfn.sh 脚本文件
我们可以查看一下

root@192-168-19-133:/yijiu/fabricfile/1.2/fabric-samples# cd first-network/
root@192-168-19-133:/yijiu/fabricfile/1.2/fabric-samples/first-network# ./byfn.sh  -help
Usage:
  byfn.sh <mode> [-c <channel name>] [-t <timeout>] [-d <delay>] [-f <docker-compose-file>] [-s <dbtype>] [-l <language>] [-i <imagetag>] [-v]
    <mode> - one of 'up', 'down', 'restart', 'generate' or 'upgrade'
      - 'up' - bring up the network with docker-compose up
      - 'down' - clear the network with docker-compose down
      - 'restart' - restart the network
      - 'generate' - generate required certificates and genesis block
      - 'upgrade'  - upgrade the network from version 1.1.x to 1.2.x
    -c <channel name> - channel name to use (defaults to "mychannel")
    -t <timeout> - CLI timeout duration in seconds (defaults to 10)
    -d <delay> - delay duration in seconds (defaults to 3)
    -f <docker-compose-file> - specify which docker-compose file use (defaults to docker-compose-cli.yaml)
    -s <dbtype> - the database backend to use: goleveldb (default) or couchdb
    -l <language> - the chaincode language: golang (default) or node
    -i <imagetag> - the tag to be used to launch the network (defaults to "latest")
    -v - verbose mode
  byfn.sh -h (print this message)

Typically, one would first generate the required certificates and
genesis block, then bring up the network. e.g.:

        byfn.sh generate -c mychannel
        byfn.sh up -c mychannel -s couchdb
        byfn.sh up -c mychannel -s couchdb -i 1.2.x
        byfn.sh up -l node
        byfn.sh down -c mychannel
        byfn.sh upgrade -c mychannel

Taking all defaults:
        byfn.sh generate
        byfn.sh up
        byfn.sh down

up:启动; down:清除网络; restart:重新启动; generate:生成证书及创世区块

  • 先生成证书和密钥
    ./byfn.sh -m generate
root@192-168-19-133:/yijiu/fabricfile/1.2/fabric-samples/first-network# ./byfn.sh  -m generate
Generating certs and genesis block for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] y
proceeding ...
/yijiu/fabricfile/1.2/fabric-samples/first-network/../bin/cryptogen

##########################################################
##### Generate certificates using cryptogen tool #########
##########################################################
+ cryptogen generate --config=./crypto-config.yaml
org1.example.com
org2.example.com
+ res=0
+ set +x

/yijiu/fabricfile/1.2/fabric-samples/first-network/../bin/configtxgen
##########################################################
#########  Generating Orderer Genesis block ##############
##########################################################
+ configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
2023-09-05 15:05:01.925 CST [common/tools/configtxgen] main -> WARN 001 Omitting the channel ID for configtxgen is deprecated.  Explicitly passing the channel ID will be required in the future, defaulting to 'testchainid'.
2023-09-05 15:05:01.925 CST [common/tools/configtxgen] main -> INFO 002 Loading configuration
2023-09-05 15:05:01.932 CST [common/tools/configtxgen/encoder] NewChannelGroup -> WARN 003 Default policy emission is deprecated, please include policy specificiations for the channel group in configtx.yaml
2023-09-05 15:05:01.932 CST [common/tools/configtxgen/encoder] NewOrdererGroup -> WARN 004 Default policy emission is deprecated, please include policy specificiations for the orderer group in configtx.yaml
2023-09-05 15:05:01.932 CST [common/tools/configtxgen/encoder] NewOrdererOrgGroup -> WARN 005 Default policy emission is deprecated, please include policy specificiations for the orderer org group OrdererOrg in configtx.yaml
2023-09-05 15:05:01.932 CST [msp] getMspConfig -> INFO 006 Loading NodeOUs
2023-09-05 15:05:01.932 CST [common/tools/configtxgen/encoder] NewOrdererOrgGroup -> WARN 007 Default policy emission is deprecated, please include policy specificiations for the orderer org group Org1MSP in configtx.yaml
2023-09-05 15:05:01.932 CST [msp] getMspConfig -> INFO 008 Loading NodeOUs
2023-09-05 15:05:01.933 CST [common/tools/configtxgen/encoder] NewOrdererOrgGroup -> WARN 009 Default policy emission is deprecated, please include policy specificiations for the orderer org group Org2MSP in configtx.yaml
2023-09-05 15:05:01.933 CST [common/tools/configtxgen] doOutputBlock -> INFO 00a Generating genesis block
2023-09-05 15:05:01.934 CST [common/tools/configtxgen] doOutputBlock -> INFO 00b Writing genesis block
+ res=0
+ set +x

#################################################################
### Generating channel configuration transaction 'channel.tx' ###
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
2023-09-05 15:05:01.960 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration
2023-09-05 15:05:01.964 CST [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 002 Generating new channel configtx
2023-09-05 15:05:01.964 CST [common/tools/configtxgen/encoder] NewApplicationGroup -> WARN 003 Default policy emission is deprecated, please include policy specificiations for the application group in configtx.yaml
2023-09-05 15:05:01.964 CST [msp] getMspConfig -> INFO 004 Loading NodeOUs
2023-09-05 15:05:01.964 CST [common/tools/configtxgen/encoder] NewApplicationOrgGroup -> WARN 005 Default policy emission is deprecated, please include policy specificiations for the application org group Org1MSP in configtx.yaml
2023-09-05 15:05:01.965 CST [msp] getMspConfig -> INFO 006 Loading NodeOUs
2023-09-05 15:05:01.965 CST [common/tools/configtxgen/encoder] NewApplicationOrgGroup -> WARN 007 Default policy emission is deprecated, please include policy specificiations for the application org group Org2MSP in configtx.yaml
2023-09-05 15:05:01.965 CST [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 008 Writing new channel tx
+ res=0
+ set +x

#################################################################
#######    Generating anchor peer update for Org1MSP   ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP
2023-09-05 15:05:01.992 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration
2023-09-05 15:05:01.995 CST [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update
2023-09-05 15:05:01.996 CST [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update
+ res=0
+ set +x

#################################################################
#######    Generating anchor peer update for Org2MSP   ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
2023-09-05 15:05:02.021 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration
2023-09-05 15:05:02.025 CST [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update
2023-09-05 15:05:02.025 CST [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update
+ res=0
+ set +x

命令成功执行后会生成1个Orderer+4个Peer+1个CLI的网络结构,4个Peer包含在2个Org中

  • 然后启动网络
    root@192-168-19-133:/yijiu/fabricfile/1.2/fabric-samples/first-network# ./byfn.sh -m up
Error: error getting endorser client for channel: endorser client failed to connect to peer1.org1.example.com:7051: failed to create new connection: context deadline exceeded
peer1.org1 failed to join the channel, Retry after 3 seconds

但是报错了,这时候我们需要修改一下配置文件 /etc/resolv.conf

root@192-168-19-133:/yijiu/fabricfile/1.2/fabric-samples/first-network# vim /etc/resolv.conf
root@192-168-19-133:/yijiu/fabricfile/1.2/fabric-samples/first-network# cat /etc/resolv.conf
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "resolvectl status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 127.0.0.53
#options edns0 trust-ad  
search localdomain

将其 options edns0 trust-ad 这个给注释掉

然后再执行网络启动
启动成功的命令输出

===================== Query successful on peer1.org2 on channel 'mychannel' =====================

========= All GOOD, BYFN execution completed ===========


 _____   _   _   ____
| ____| | \ | | |  _ \
|  _|   |  \| | | | | |
| |___  | |\  | | |_| |
|_____| |_| \_| |____/

可以拿 docker ps 命令进行查看

root@192-168-19-133:/yijiu/fabricfile/1.2/fabric-samples/first-network# docker ps
CONTAINER ID   IMAGE                                                                                                  COMMAND                  CREATED              STATUS              PORTS                                                                                      NAMES
741bc17ae7ea   dev-peer1.org2.example.com-mycc-1.0-26c2ef32838554aac4f7ad6f100aca865e87959c9a126e86d764c8d01f8346ab   "chaincode -peer.add…"   57 seconds ago       Up 56 seconds                                                                                                  dev-peer1.org2.example.com-mycc-1.0
e637f8665f22   dev-peer0.org1.example.com-mycc-1.0-384f11f484b9302df90b453200cfb25174305fce8f53f4e94d45ee3b6cab0ce9   "chaincode -peer.add…"   About a minute ago   Up About a minute                                                                                              dev-peer0.org1.example.com-mycc-1.0
5a259ae479cc   dev-peer0.org2.example.com-mycc-1.0-15b571b3ce849066b7ec74497da3b27e54e0df1345daff3951b94245ce09c42b   "chaincode -peer.add…"   About a minute ago   Up About a minute                                                                                              dev-peer0.org2.example.com-mycc-1.0
1a6420b47277   hyperledger/fabric-tools:latest                                                                        "/bin/bash"              About a minute ago   Up About a minute                                                                                              cli
7cbb9dd07bf1   hyperledger/fabric-peer:latest                                                                         "peer node start"        About a minute ago   Up About a minute   0.0.0.0:10051->7051/tcp, :::10051->7051/tcp, 0.0.0.0:10053->7053/tcp, :::10053->7053/tcp   peer1.org2.example.com
c14aa1a3cfec   hyperledger/fabric-orderer:latest                                                                      "orderer"                About a minute ago   Up About a minute   0.0.0.0:7050->7050/tcp, :::7050->7050/tcp                                                  orderer.example.com
5d0db4d519a9   hyperledger/fabric-peer:latest                                                                         "peer node start"        About a minute ago   Up About a minute   0.0.0.0:7051->7051/tcp, :::7051->7051/tcp, 0.0.0.0:7053->7053/tcp, :::7053->7053/tcp       peer0.org1.example.com
27f1fd624d84   hyperledger/fabric-peer:latest                                                                         "peer node start"        About a minute ago   Up About a minute   0.0.0.0:8051->7051/tcp, :::8051->7051/tcp, 0.0.0.0:8053->7053/tcp, :::8053->7053/tcp       peer1.org1.example.com
b30dc22e06f1   hyperledger/fabric-peer:latest                                                                         "peer node start"        About a minute ago   Up About a minute   0.0.0.0:9051->7051/tcp, :::9051->7051/tcp, 0.0.0.0:9053->7053/tcp, :::9053->7053/tcp       peer0.org2.example.com
root@192-168-19-133:/yijiu/fabricfile/1.2/fabric-samples/first-network#

可以看到,启动了四个peer节点,一个orderer节点, 并且还有挂载了三个数据容器

关闭并删除网络

./byfn.sh -m down


root@192-168-19-133:/yijiu/fabricfile/1.2/fabric-samples/first-network# ./byfn.sh  -m down
Stopping for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] y
proceeding ...
[+] Running 14/14
 ✔ Container cli                      Removed                                                                                                                                     0.1s
 ✔ Container peer1.org2.example.com   Removed                                                                                                                                     0.8s
 ✔ Container peer0.org1.example.com   Removed                                                                                                                                     0.8s
 ✔ Container peer1.org1.example.com   Removed                                                                                                                                     0.5s
 ✔ Container orderer.example.com      Removed                                                                                                                                     0.7s
 ✔ Container peer0.org2.example.com   Removed                                                                                                                                     0.6s
 ✔ Volume net_peer0.org3.example.com  Removed                                                                                                                                     0.0s
 ✔ Volume net_peer1.org3.example.com  Removed                                                                                                                                     0.0s
 ✔ Volume net_orderer.example.com     Removed                                                                                                                                     0.0s
 ✔ Volume net_peer1.org2.example.com  Removed                                                                                                                                     0.0s
 ✔ Volume net_peer1.org1.example.com  Removed                                                                                                                                     0.0s
 ✔ Volume net_peer0.org2.example.com  Removed                                                                                                                                     0.0s
 ✔ Volume net_peer0.org1.example.com  Removed                                                                                                                                     0.0s
 ✔ Network net_byfn                   Removed                                                                                                                                     0.1s
741bc17ae7ea
e637f8665f22
5a259ae479cc
Untagged: dev-peer1.org2.example.com-mycc-1.0-26c2ef32838554aac4f7ad6f100aca865e87959c9a126e86d764c8d01f8346ab:latest
Deleted: sha256:93a61e607fcef26b4c55f05da79018724f4979c056e7522a2a8400ea42a6438f
Deleted: sha256:53ce5032dd2cd4305459528f543b9c7461fea984567492b1a1bb01432eac2a69
Deleted: sha256:d5fb65d11f8c3c9cd785089509738d18402d6d86e887038b5f3eed0e14daa373
Deleted: sha256:93d7c9c04e92cd237905208168bb0878675e594a7a545ead2620af619e4ba178
Untagged: dev-peer0.org1.example.com-mycc-1.0-384f11f484b9302df90b453200cfb25174305fce8f53f4e94d45ee3b6cab0ce9:latest
Deleted: sha256:a9fed219154ec65de57962b14208e364c434a9675242f42198e0036df55827a7
Deleted: sha256:d2273bf57b91a19b50d9e9f7b327abdbc94fc9c01653a95d3f5e00b58e6c5d51
Deleted: sha256:0a3c57ac99dde072733dcde526f8bd7ee176b0afa4a37f63c99e57f9494c92d0
Deleted: sha256:7221b8464a468558bab7e433b1ba8ee80426b0bbd5709fbfeba5c8bdeb0e0069
Untagged: dev-peer0.org2.example.com-mycc-1.0-15b571b3ce849066b7ec74497da3b27e54e0df1345daff3951b94245ce09c42b:latest
Deleted: sha256:c61666c5a9b2d3ae28de9b4c30a2df17fa2a92aa769603b7376c0a328efafda1
Deleted: sha256:6cbac39d0222a449c37f8869d43c2093fab4974dae8fcc36ab6e5322fe63bd11
Deleted: sha256:6face03a870ec4b969ada1187100ba981054eaecdd3923cda2f26c3d3eac87c8
Deleted: sha256:749f320176c49ff003c36160bbb53da890ba982febfe320b7a472a7fd6fbad67

用byfn.sh脚本关闭网络之后,将关闭容器,且删除加密文件,并从Docker Registry中删除链码图像

结语

这篇文章,只是检测测试一下fabric 1.2 版本是否能正常使用,并且使用官方的例子脚本文件测试网络。接下来将会详细讲解如何定制化配置fabric和运行fabric。

var code = “16a9ca1f-9566-440f-bdaa-7ceaf445ca89”

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

已久依依

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值