Fabric初体验(踩坑笔记)
搭建fabric部署合约学习笔记
- 环境准备
- CURl
- 安装docker
- 参照官网文档实现(2025.05.19)
- 根据前言交代的文章去尝试(失败版)
- 安装fabric-samples
- 安装指定2.2.0版本Fabric二进制文件和配置文件
- 直接手动下载(不建议)
- 重新拉取fabric二进制文件并安装docker镜像
- 使用官方文档搭建
- 安装fabric镜像失败443错
- 尝试解决办法
- 理论知识补充
- channel
写在前面,本文很多一部分是参照这个教程去实现的(原作者文章如截图),同时结合fabric官方文档去实践。
环境准备
使用的centos7.9系统,选择该系统是因为听说生产环境多是这个环境,当然开发环境好像更多的用Ubuntu,无所谓啦最主要的是我的centos装了不少的环境了,不想重复了。。。已有的环境安装部署我就省略了。
CURl
安装curl
sudo yum install curl
安装docker
安装 Docker 所需的依赖包:
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
添加 Docker 仓库
将 Docker 官方仓库添加到系统中:
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
安装docker-ce即docker社区版
安装 Docker Engine
安装 Docker Engine、CLI 和相关组件:
sudo yum install -y docker-ce docker-ce-cli containerd.io
启动 Docker 服务
启动 Docker 服务并设置开机自启:
sudo systemctl start docker
sudo systemctl enable docker
不是说现在新版的docker集成了docker-compose的吗,现在查询版本又有问题?
因为新版的docker集成docker-compose到docker-cli后,命令变成docker version,docker compose version了。不加-了
[root@localhost test-network]# docker-compose --version
bash: docker-compose: 未找到命令...
[root@localhost test-network]#
[root@localhost test-network]# mkdir -p ~/.docker/cli-plugins
[root@localhost test-network]# curl -SL https://github.com/docker/compose/releases/download/v2.36.0/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0
100 70.3M 100 70.3M 0 0 8712k 0 0:00:08 0:00:08 --:--:-- 10.8M
[root@localhost test-network]# chmod +x ~/.docker/cli-plugins/docker-compose
[root@localhost test-network]# docker compose version
Docker Compose version v2.36.0
[root@localhost test-network]# sudo systemctl start docker #重启docker
[root@localhost test-network]# sudo systemctl enable docker #设置系统开机docker自启动
[root@localhost test-network]# sudo gpasswd -a $USER docker #将当前用户加入docker组,确保当前用户命令下可执行
正在将用户“root”加入到“docker”组中
[root@localhost test-network]# newgrp docker #更新用户组
[root@localhost test-network]# docker info #查看docker配置信息
验证安装
运行以下命令,确认 Docker 是否安装成功:
sudo docker run hello-world
添加阿里云镜像加速
1.登录阿里云控制台
访问阿里云容器镜像服务控制台,使用账号密码或扫码登录
2导航至镜像加速器页面
在控制台左侧菜单栏选择「镜像工具」→「镜像加速器」,进入加速器配置界面https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
3复制加速器地址
找到CentOS系统对应的加速器地址(格式如https://xxxxxx.mirror.aliyuncs.com),注意该地址是账号专属的
进入阿里云镜像加速如图。网上那些教程乱七八糟的说不明白,直接看官网的教程执行就可以了。
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{"registry-mirrors": ["https://自己的地址码.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
收集的一些镜像源地址
{"registry-mirrors": ["https://自己的阿里云加速地址.mirror.aliyuncs.com","https://registry.docker-cn.com","https://alzgoonw.mirror.aliyuncs.com","https://docker.m.daocloud.io","https://dockerhub.icu","https://docker.anyhub.us.kg","https://docker.1panel.live"]
}~
最新版docker无需安装docker-compose
现在的docker直接集成了docker-compose的功能,不需要再额外安装docker-compose
但是旧版的fabric写的搭建测试网络的脚本里面,命令用的是docker-compose,你需要去配置docker的命令和更改脚本源码。
[root@localhost ~]# docker --version
Docker version 26.1.4, build 5650f9b
[root@localhost ~]# docker compose version
Docker Compose version v2.27.1
参照官网文档实现(2025.05.19)
fabric官方文档地址:https://hyperledger-fabric.readthedocs.io/zh-cn/latest/getting_started.html
当然出问题比较多的就是2和3啦
前言
煮波的环境是26.1版docker,用的centos7.9,1.22.2版的go,装的是2.4版的fabric(官网现在默认是2.5.12)
建议:如果想自己安装需要的版本的话,最好还是去查一下,或者AI问一下适配哪个版本的fabric,不然很难受的。2.5以上的fabric是用docker compose,2.4及其以下的使用的docker-compose,新版的fabric会造成无法发现老板docker命令docker-compose的现象。
进入 no title
去下载fabric的安装脚本,这个脚本比较省事,拉下来直接执行就会自动给你装什么二进制文件、docker镜像环境什么的了
1.如官网所说,先建一个目录去搭建你的fabric项目
前面说的一堆都没用,都是告诉你要装什么二进制、镜像啥的,实际上都写进安装脚本了,不用管
例如煮波的目录是这样的:/opt/go/src/github.com/hyperledger/
2.拉安装脚本
cd到hyperledger目录下执行安装脚本拉取命令
curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/release-2.4/scripts/bootstrap.sh -o bootstrap.sh
3.执行安装脚本
这个命令表示安装2.4.4版本的fabric,二进制文件。binary镜像、samples都默认他给你一起装好
./bootstrap.sh 2.4.4
如果想自己装,就做减法
如:./bootstrap.sh 2.4.4 -d。不用docker镜像自己去找过来(新手不建议)
安装完以后,这个时候其实就可以直接执行脚本了,就在执行脚本fabric-samples>test-network>下面的network.sh脚本。./network.sh up
,同理down命令就是停止。
4.修改执行脚本
如前面所说,我的版本是2.4版本的fabric,无法找到docker-compose命令。
于是需要修改一下执行脚本fabric-samples>test-network>下面的network.sh脚本
不懂的话,建议把脚本用编译器打开复制一下丢给AI,告诉AI什么问题让他改。
先改一下docker
- 创建全局符号链接(推荐方案)
# 将docker compose映射为docker-compose命令
sudo ln -s /usr/bin/docker /usr/local/bin/docker-compose
# 验证映射关系
ls -l /usr/local/bin/docker-compose | grep '-> /usr/bin/docker'
- 环境变量别名配置
# 在用户环境配置文件(.bashrc/.zshrc)中添加
echo 'alias docker-compose="docker compose"' >> ~/.bashrc
source ~/.bashrc
验证:执行alias docker-compose应显示alias docker-compose=‘docker compose’
- network.sh脚本修改docker-compsoe相关
# 修改变量定义 主要是这个!!!
- : ${CONTAINER_CLI_COMPOSE:="${CONTAINER_CLI}-compose"} 修改前
+ : ${CONTAINER_CLI_COMPOSE:="${CONTAINER_CLI} compose"} 修改后# 添加项目名变量(可选)
+ : ${COMPOSE_PROJECT_NAME:="fabric-test-net"}# 清理容器逻辑调整
- ${CONTAINER_CLI} rm -f $(${CONTAINER_CLI} ps -aq --filter label=service=hyperledger-fabric) 2>/dev/null || true
+ ${CONTAINER_CLI} rm -f $(${CONTAINER_CLI} ps -aq --filter label=com.docker.compose.project=${COMPOSE_PROJECT_NAME}) 2>/dev/null || true
最终执行./network.sh up就会成功啦
根据前言交代的文章去尝试(失败版)
安装fabric-samples
测试fabric网络环境(参照官网的步骤https://hyperledger-fabric.readthedocs.io/zh-cn/latest/install.html)
找一下go的项目路径
echo $GOPATH #看go的项目默认路径env | grep -i go #查看go的全部环境变量
克隆 hyperledger/fabric-samples 仓库
mkdir -p $GOPATH/src/github.com/hyperledger #创建hyperledger文件夹(-p意为父目录不存在则自动创建)
#$GOPATH/src/github.com/ 这个目录是go开发的项目惯用路径
[root@localhost ~]# cd $GOPATH/src/github.com/hyperledger
[root@localhost hyperledger]# git clone https://github.com/hyperledger/fabric-samples.git
正克隆到 'fabric-samples'...
remote: Enumerating objects: 14783, done.
remote: Counting objects: 100% (259/259), done.
remote: Compressing objects: 100% (172/172), done.
remote: Total 14783 (delta 142), reused 87 (delta 87), pack-reused 14524 (from 3)
接收对象中: 100% (14783/14783), 23.74 MiB | 1.96 MiB/s, done.
处理 delta 中: 100% (8097/8097), done.
[root@localhost hyperledger]# cd fabric-samples
[root@localhost fabric-samples]# git checkout release-2.2
分支 release-2.2 设置为跟踪来自 origin 的远程分支 release-2.2。根据版本选择
切换到一个新分支 'release-2.2'
[root@localhost fabric-samples]# git branch #查看当前版本main
* release-2.2
安装指定2.2.0版本Fabric二进制文件和配置文件
根据官网要求是要安装到fabric-samples 下的 /bin和 /config 目录中,并下载指定版本的 Hyperledger Fabric docker 镜像。安装这个版本的话443是很常见,甚至我一直都没弄好,最后放弃了这个版本。
#如果你想要最新的生产发布版本,忽略所有的版本标识符。
# curl -sSL https://bit.ly/2ysbOFE | bash -s
# curl -sSL https://bit.ly/2ysbOFE | bash -s -- <fabric_version> <fabric-ca_version> <thirdparty_version>
# 若报错详见文末附加问题2
curl -sSL https://bit.ly/2ysbOFE | bash -s -- 2.2.0 1.4.7 0.4.18
# 若不行试试下面这个
curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh | bash -s 2.2.0 1.4.7 0.4.18
直接手动下载(不建议)
不建议这么做,反正我是失败了。可参考的文章也不多,而且你不知道他这个脚本会给你配置些什么东西,很容易整错。反复多下载几次,但是对于2.2.0这些个旧版的,确实不好下载,我是没有下载好过。
我未能如教程所示去下载安装相关的配置。于是直接去GitHub下载对应的fabric二进制文件传到了centos当中。GitHub地址如下:https://github.com/hyperledger/fabric/releases/tag/v2.2.0
下载这个:
hyperledger-fabric-linux-amd64-2.2.0.tar.gz
在Windows解压后用远程工具拖进了这个目录中:/opt/go/src/github.com/hyperledger/fabric-samples/
拖进去以后验证一下
peer version
orderer version
cd chaincode-go
sudo vi go.mod
# 进入文件发现是1.14 自己把改成1.13 ,要与你下载的go版本匹配
环境变量设置
环境变量的设置哪个版本什么的都一样,重要的是找到你的bin文件所在,添加保存进去就好了
vi ~/.bashrc
# 添加下面的变量
export PATH=$PATH:$GOPATH/src/github.com/hyperledger/fabric-samples/bin
# 使之生效
source ~/.bashrc
# 检验成功否
fabric-ca-client version
示例:
[root@localhost asset-transfer-basic]# vi ~/.bashrc
[root@localhost asset-transfer-basic]# source ~/.bashrc
[root@localhost asset-transfer-basic]# fabric-ca-client version
fabric-ca-client:Version: 1.4.7Go version: go1.13.9OS/Arch: linux/amd64
重新拉取fabric二进制文件并安装docker镜像
(莫名其妙,白天一直下不来,大半夜一次就下好了,纯运气)
记录一下下载的过程。
[root@localhost bin]# cd ..
[root@localhost fabric-samples]# curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh -o install-fabric.sh
[root@localhost fabric-samples]# chmod +x install-fabric.sh
[root@localhost fabric-samples]# ./install-fabric.sh dockerPull Hyperledger Fabric docker imagesFABRIC_IMAGES: peer orderer ccenv baseos
===> Pulling fabric Images
====> ghcr.io/hyperledger/fabric-peer:2.5.12
2.5.12: Pulling from hyperledger/fabric-peer
9cb31e2e37ea: Pull complete
db23127575f9: Pull complete
ab49923c0970: Pull complete
8b1c4d4ea1b4: Pull complete
d8dbf9e3c4ef: Pull complete
ee3e31e4b37b: Pull complete
Digest: sha256:f5f3fb3b061a067df0d13ed109fe1109cc9cbf02207936438435fcf431a11440
Status: Downloaded newer image for ghcr.io/hyperledger/fabric-peer:2.5.12
ghcr.io/hyperledger/fabric-peer:2.5.12
====> ghcr.io/hyperledger/fabric-orderer:2.5.12
2.5.12: Pulling from hyperledger/fabric-orderer
9cb31e2e37ea: Already exists
904f7cf7cc46: Pull complete
5a2ed0dfe063: Pull complete
eeedc9027925: Pull complete
92d83fce7ae2: Pull complete
ea198b0fa631: Pull complete
Digest: sha256:5a91a378ed13de92d1566781dede60baaca42265076f96b5921ad03b25184c6a
Status: Downloaded newer image for ghcr.io/hyperledger/fabric-orderer:2.5.12
ghcr.io/hyperledger/fabric-orderer:2.5.12
====> ghcr.io/hyperledger/fabric-ccenv:2.5.12
2.5.12: Pulling from hyperledger/fabric-ccenv
9cb31e2e37ea: Already exists
402cfb9ba580: Pull complete
6b3d92f86452: Pull complete
a650c2728f06: Pull complete
1f31c61b2127: Pull complete
fc2f70f80fa1: Pull complete
6f72b6cc2521: Pull complete
Digest: sha256:72bda013702ad5a4103c00e6b58617110da69c496b60993e35c9d9e09c1a338e
Status: Downloaded newer image for ghcr.io/hyperledger/fabric-ccenv:2.5.12
ghcr.io/hyperledger/fabric-ccenv:2.5.12
====> ghcr.io/hyperledger/fabric-baseos:2.5.12
2.5.12: Pulling from hyperledger/fabric-baseos
9cb31e2e37ea: Already exists
ed2e4dedd96b: Pull complete
dadd0deeae37: Pull complete
9e9ec9334ad1: Pull complete
Digest: sha256:f0dab8b4acd484e35c2303593c6ce850e3d000b77d86b25d2116cbcb67f8b6ff
Status: Downloaded newer image for ghcr.io/hyperledger/fabric-baseos:2.5.12
ghcr.io/hyperledger/fabric-baseos:2.5.12
===> Pulling fabric ca Image
====> ghcr.io/hyperledger/fabric-ca:1.5.15
1.5.15: Pulling from hyperledger/fabric-ca
9cb31e2e37ea: Already exists
4c80c17dd71c: Pull complete
dbbb11f20e2d: Pull complete
943126a1b0fb: Pull complete
Digest: sha256:4704861f82be659372510b72c1e8b141563743e1f5840d8c094ded1df9253c68
Status: Downloaded newer image for ghcr.io/hyperledger/fabric-ca:1.5.15
ghcr.io/hyperledger/fabric-ca:1.5.15
===> List out hyperledger images
ghcr.io/hyperledger/fabric-peer 2.5.12 63a9820373a4 2 months ago 151MB
hyperledger/fabric-peer 2.5 63a9820373a4 2 months ago 151MB
hyperledger/fabric-peer 2.5.12 63a9820373a4 2 months ago 151MB
hyperledger/fabric-peer latest 63a9820373a4 2 months ago 151MB
hyperledger/fabric-orderer 2.5 d69cac8bbe84 2 months ago 118MB
hyperledger/fabric-orderer 2.5.12 d69cac8bbe84 2 months ago 118MB
hyperledger/fabric-orderer latest d69cac8bbe84 2 months ago 118MB
ghcr.io/hyperledger/fabric-orderer 2.5.12 d69cac8bbe84 2 months ago 118MB
hyperledger/fabric-ccenv 2.5 a39804456867 2 months ago 676MB
hyperledger/fabric-ccenv 2.5.12 a39804456867 2 months ago 676MB
hyperledger/fabric-ccenv latest a39804456867 2 months ago 676MB
ghcr.io/hyperledger/fabric-ccenv 2.5.12 a39804456867 2 months ago 676MB
hyperledger/fabric-baseos 2.5 32d16a66e457 2 months ago 142MB
hyperledger/fabric-baseos 2.5.12 32d16a66e457 2 months ago 142MB
hyperledger/fabric-baseos latest 32d16a66e457 2 months ago 142MB
ghcr.io/hyperledger/fabric-baseos 2.5.12 32d16a66e457 2 months ago 142MB
hyperledger/fabric-ca 1.5 03492c5437b1 3 months ago 225MB
hyperledger/fabric-ca 1.5.15 03492c5437b1 3 months ago 225MB
hyperledger/fabric-ca latest 03492c5437b1 3 months ago 225MB
ghcr.io/hyperledger/fabric-ca 1.5.15 03492c5437b1 3 months ago 225MB
[root@localhost fabric-samples]#
配置docker镜像源
sudo vi /etc/docker/daemon.json
#把以下代码加进去
{
"registry-mirrors":["https://registry.docker-cn.com"]
}
查看运行的docker镜像有哪些
docker images | grep hyperledger
使用官方文档搭建
如果你不指定任何组件和版本,脚本将使用默认版本安装所有组件:./install-fabric.sh
安装fabric镜像失败443错
默认安装官方二进制文件和镜像
显然又报错下不下来:1.5.15 fabric-ca-client binary is not available to download
curl: (35) TCP connection reset by peergzip: stdin: unexpected end of file
tar: Child returned status 1
tar: Error is not recoverable: exiting now
==> There was an error downloading the binary file.------> 1.5.15 fabric-ca-client binary is not available to download (Available from 1.1.0-rc1) <----
Downloading version 2.5.12 platform specific fabric binaries
Failed connect to github.com:443; 拒绝连接
[root@localhost hyperledger]# ./install-fabric.sh docker samples binaryClone hyperledger/fabric-samples repo===> Changing directory to fabric-samples
fabric-samples v2.5.12 does not exist, defaulting to main. fabric-samples main branch is intended to work with recent versions of fabric.Pull Hyperledger Fabric binaries===> Downloading version 2.5.12 platform specific fabric binaries
===> Downloading: https://github.com/hyperledger/fabric/releases/download/v2.5.12/hyperledger-fabric-linux-amd64-2.5.12.tar.gz
===> Will unpack to: /opt/go/src/github.com/hyperledger/fabric-samples% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed0 0 0 0 0 0 0 0 --:--:-- 0:00:21 --:--:-- 0curl: (7) Failed connect to github.com:443; 拒绝连接gzip: stdin: unexpected end of file
tar: Child returned status 1
tar: Error is not recoverable: exiting now
==> There was an error downloading the binary file.------> 2.5.12 platform specific fabric binary is not available to download <----
尝试解决办法
加DNS路径
有时 DNS 解析失败也会导致类似错误。尝试直接使用 GitHub 的 IP 地址进行访问,或者更换 DNS 服务器(比如 Google 的公共 DNS:8.8.8.8 和 8.8.4.4):
sudo vi /etc/resolv.conf
# 添加如下行:nameserver 8.8.8.8nameserver 8.8.4.4
sudo systemctl restart network
重新再试了一遍,woc这是成了吗
@root hyperledger>./install-fabric.sh
还是老毛病啊
这里跑不通并不是真的没有下载安装二进制的文件和镜像。实际上都装好了,你也可以自己看一下自己的fabric目录。比如我的是/opt/go/src/github.com/hyperledger/fabric-samples/这个路径下有bin(存放peer oderer等文件)和config文件夹。此外这里这两份文件,也会在全局备一份比如我的在/usr/local/bin (其实似乎删了也没啥影响,至少我是这样的)。应该是最新版的有点问题不稳定吧,我也不懂,反正一直是这个问题,配置环境变量换DNS路径等等都做不好。
[root@localhost test-network]# ls
addOrg3 bft-config CHAINCODE_AS_A_SERVICE_TUTORIAL.md compose configtx monitordocker.sh network.config network.sh organizations prometheus-grafana README.md scripts setOrgEnv.sh system-genesis-block
[root@localhost test-network]# ./network.sh up
Using docker and docker compose
Starting nodes with CLI timeout of '5' tries and CLI delay of '3' seconds and using database 'leveldb' with crypto from 'cryptogen'
`·Peer binary and configuration files not found..`Follow the instructions in the Fabric docs to install the Fabric Binaries:
https://hyperledger-fabric.readthedocs.io/en/latest/install.html
理论知识补充
channel
在如fabric这般的联盟链当中,账本的组织方式和数据隔离机制是通过“通道”(Channel)来实现的。也就是说不存在“整个区块链系统的账本”,因为账本是按照通道来划分的。整个区块链系统更像是一个容器,包含了多个通道及其对应的账本。
在联盟链当中,区块链系统可以看成一个底层的容器,提供基础的设施和服务,比如共识机制等。
具体的账本和数据则是在channel当中实现
而一个联盟链当中存在着多个channel,他们之间互不干扰、互相隔离。比如A、B、C三个企业组成的联盟链当中,A、B之间的channelA和B、C之间的channelB是并存且互相隔离的。保证了组织之间的灵活的记账方式。