自2017年4月份fabric-1.0 alpha发布至今,fabric-1.0经历了alpha、alpha2、beta、rc1、1.0等版本。与fabric-0.6相比,fabric-1.0在架构和角色上发生很大变化,其节点类型分成peer、orderer、ca、client,因此,搭建fabric-1.0网络对于刚接触fabric的爱好者显得比较复杂。在对fabric-1.0各个版本进行搭建实验的基础上,总结了fabric-1.0.0的各种搭建方式。
sudo yum install git
sudo yum remove golang
sudo yum autoremove
下载链接:https://golang.org/dl/
下载:https://storage.googleapis.com/golang/go1.8.1.linux-amd64.tar.gz
由于GFW,因此备份了一份在百度云上:链接: http://pan.baidu.com/s/1c2lJrG0 密码: 7pye
解压:
sudo tar -xvf go1.8.1.linux-amd64.tar.gz -C /usr/local/
在/etc/profile文件末尾(所有用户有效)添加:
export GOROOT=/usr/local/go
export GOBIN=$GOROOT/bin
export PATH=$PATH:$GOBIN
export GOPATH=/home/hyperledger/gopath
环境变量生效:
source /etc/profile
执行
go version
参考:https://docs.docker.com/engine/installation/linux/docker-ce/centos/
sudo yum remove docker \
docker-common \
container-selinux \
docker-selinux \
docker-engine
安装yum-utils和wget
sudo yum install -y yum-utils
sudo yum install -y wget
配置系统镜像(如果没有配置)
cd /etc/yum.repos.d
sudo wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
sudo yum clean all
sudo yum makecache
sudo yum update -y
添加stable repository
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
安装docker-ce
sudo yum makecache fast
sudo yum install docker-ce
启动docker
sudo systemctl start docker
添加docker用户组(如果不存在)
sudo groupadd docker
将用户添加到docker用户组中
sudo gpasswd -a ubuntu docker
重启docker服务
sudo service docker restart
重启group或者重启x会话
newgrp - docker
或
pkill X
sudo curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f8c3f422.m.daocloud.io
重启docker服务
sudo systemctl restart docker
参考:https://docs.docker.com/compose/install/
sudo curl -L "https://github.com/docker/compose/releases/download/1.12.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
或从文件中获取(链接:http://pan.baidu.com/s/1dFu4Jgl 密码:329z)
添加执行权限
sudo chmod +x /usr/local/bin/docker-compose
安装gcc
sudo yum install -y gcc gcc-c++ kernel-devel
sudo yum install -y python-devel libxslt-devel libffi-devel openssl-devel
下载get-pip.py
wget https://bootstrap.pypa.io/get-pip.py
安装pip
sudo python get-pip.py
安装其他包
sudo pip install --upgrade pip
sudo pip install behave nose docker-compose
sudo pip install -I flask python-dateutil pytz pyyaml couchdb flask-cors request pyOpenSSL pysha3 grpcio
sudo pip install urllib3 ndg-httpsclient pyasn1 ecdsa python-slugify grpcio-tools jinja2 b3j0f.aop six
克隆源码
cd $GOPATH/src
mkdir -p github.com/hyperledger
cd github.com/hyperledger
git clone https://github.com/hyperledger/fabric.git
cd $GOPATH/src/github.com/hyperledger/fabric
make configtxgen
# if you see an error stating - 'ltdl.h' file not found
sudo apt install libtool libltdl-dev
# then run the make again
make configtxgen
因为再编译过程中下载gotools需要,所以如果没有配置VPN则可能下载失败,因此需要事先将gotools下载好
将gotools.tar拷贝到任意目录
创建/opt/gotools目录并解压到此目录
sudo mkdir –p /opt/gotools
tar –xvf gotools.tar –C /opt/gotools
docker pull hyperledger/fabric-baseimage:x86_64-0.3.0
如果不用Java chaincode或java工具无法下载,可以修改
在orderer节点编译orderer
make orderer
在peer节点编译peer
make peer
编译完成,则可以启动相应服务
编译orderer镜像
make orderer-docker
编译peer镜像
make peer-docker
make all //编译所有组件
make checks //运行所有的测试和检查
make cryptogen //创建一个本地的加密生成工具
make clean //清除所有编译项
make dist-clean //清除所有编译项及移除本地持久化状态
直接从docker镜像库拉取镜像
docker pull hyperledger/fabric-peer:x86_64-1.0.0
docker pull hyperledger/fabric-orderer:x86_64-1.0.0
docker pull hyperledger/fabric-ccenv:x86_64-1.0.0
docker pull hyperledger/fabric-ca:x86_64-1.0.0
docker pull hyperledger/fabric-baseos:x86_64-0.3.0
docker pull hyperledger/fabric-tools:x86_64-1.0.0
docker tag docker.io/hyperledger/fabric-ca:x86_64-1.0.0 hyperledger/fabric-ca:latest
docker tag docker.io/hyperledger/fabric-peer:x86_64-1.0.0 hyperledger/fabric-peer:latest
docker tag docker.io/hyperledger/fabric-orderer:x86_64-1.0.0 hyperledger/fabric-orderer:latest
docker tag docker.io/hyperledger/fabric-ccenv:x86_64-1.0.0 hyperledger/fabric-ccenv:latest
docker tag docker.io/hyperledger/fabric-tools:x86_64-1.0.0 hyperledger/fabric-tools:latest
cd $GOPATH/src/github.com/hyperledger/fabric
cd examples/e2e_cli
chmod +x download-dockerimages.sh
./download-dockerimages.sh
这个过程可能会比较慢,解决方法:从已经下载好的docker镜像导入
假设某台机器的镜像已经下载完成
导出镜像
docker save 7182c260a5ca > ./fabric-ccenv-x86_64-1.0.0.tar
拷贝到另一台机器,导入镜像
docker load < /home/hyperledger/Images/fabric-ccenv-x86_64-1.0.0.tar
再次执行./download-dockerimages.sh则不会从Docker Hub上下载镜像,很快则执行完成
下载fabric-sample
cd $GOPATH/src/github.com/hyperledger
git clone https://github.com/hyperledger/fabric-samples
下载Platform-specific Binaries
cd fabric-samples
curl -sSL https://goo.gl/iX9dek | bash
启动
cd fabric-samples/first-network
./byfn.sh -m generate
./byfn.sh -m up
清理container
docker rm -f $(sudo docker ps -aq)
或
docker-compose -f docker-compose-cli.yaml down
清理image
docker rmi <IMAGE ID> <IMAGE ID> <IMAGE ID>
如:
docker rmi 362 c76 c10
sudo yum install libtool-ltdl-devel
cd $GOPATH/src/github.com/hyperledger/
git clone https://github.com/hyperledger/fabric-ca
sudo go get -u github.com/hyperledger/fabric-ca/cmd/...
fabric-ca-server start -b admin:adminpw
enroll根用户
export FABRIC_CA_CLIENT_HOME=/home/hyperledger/gopath/src/github.com/hyperledger/fabric-ca/client/admin
fabric-ca-client enroll -u http://admin:adminpw@localhost:7054
Register peer0
fabric-ca-client register --id.name peer0 --id.type peer --id.affiliation org1 --id.secret peer0pw
Enroll peer0
export FABRIC_CA_CLIENT_HOME=/home/hyperledger/gopath/src/github.com/hyperledger/fabric-ca/client/peer0
fabric-ca-client enroll -u http://peer0:peer0pw@localhost:7054 -M $FABRIC_CA_CLIENT_HOME/localMspConfig
Register orderer0
export FABRIC_CA_CLIENT_HOME=/home/hyperledger/gopath/src/github.com/hyperledger/fabric-ca/client/admin
fabric-ca-client register --id.name orderer0 --id.type client --id.affiliation org2 --id.secret orderer0pw
Enroll orderer0
export FABRIC_CA_CLIENT_HOME=/home/hyperledger/gopath/src/github.com/hyperledger/fabric-ca/client/orderer0
fabric-ca-client enroll -u http://orderer0:orderer0pw@localhost:7054 -M $FABRIC_CA_CLIENT_HOME/localMspConfig
则生成了peer0的证书和orderer0的证书,将localMspConfig中的signcerts复制一份命名为admincerts,将证书打包传到相应节点即可
cd $GOPATH/github.com/hyperledger/fabric-ca
make docker
参照3.1节
配置docker-compose.yaml
version: '2'
services:
root-ca-server:
container_name: root-ca-server
image: hyperledger/fabric-ca
command: /usr/local/bin/fabric-ca-server start -b admin:adminpw
volumes:
- ./fabric-ca-server:/etc/hyperledger/fabric-ca-server
ports:
- 7054:7054
启动
docker-compose up -d
编辑generateCert.sh,修改镜像、组织名、端口
执行./generateCert.sh生成crypto-config目录
编辑configtx.yaml
配置orderer和peer的组织名,与证书注册时的组织名一致
配置Orderer组织的名字,MSP的ID,MSP的目录
配置org1的名字,MSP的名字,MSP的目录,以及Anchor的ip和端口(用于组织间的通信)
配置orderer的模式(可以先从solo模式开始),ip和端口
如果配置了kafka模式和kafka集群,则需将kafka集群配置上
更新generateArtifacts.sh中-asOrg参数
执行generateArtifacts.sh生成创世块,脚本生成的信息保存于channel-artifacts
启动orderer (见run_orderer.sh)
$ ORDERER_GENERAL_LISTENADDRESS=0.0.0.0 \
ORDERER_GENERAL_GENESISMETHOD=file \
ORDERER_GENERAL_GENESISFILE=./orderer0/genesis.block \
ORDERER_GENERAL_LOCALMSPID=OrdererMSP \
ORDERER_GENERAL_LOCALMSPDIR=localMspConfig \
../build/bin/orderer
启动peer(见run_peer0.sh)
$ CORE_PEER_TLS_ENABLED=false \
CORE_NEXT=true CORE_PEER_ENDORSER_ENABLED=true \
CORE_PEER_GOSSIP_USELEADERELECTION=true \
CORE_PEER_GOSSIP_ORGLEADER=false \
CORE_PEER_GOSSIP_SKIPHANDSHAKE=true \
CORE_PEER_ID=peer0 \
CORE_PEER_ADDRESS=:7051 \
CORE_PEER_GOSSIP_EXTERNALENDPOINT=:7051\
CORE_PEER_MSPCONFIGPATH=$GOPATH/src/github.com/hyperledger/fabric/peer/localMspConfig \
CORE_PEER_LOCALMSPID=org1 \
../build/bin/peer node start --peer-defaultchain=false
按照上面方式启动各个orderer和peer则fabric集群启动完成
启动orderer(见start_orderer0.sh)
$ docker run --name=orderer0 \
--restart=unless-stopped \
-p 7050:7050 \
-w /opt/gopath/src/github.com/hyperledger/fabric \
-v /home/hyperledger/gopath/src/github.com/hyperledger/fabric/examples/e2e_cli/crypto/orderer:/var/hyperledger/orderer \
-e ORDERER_GENERAL_LOGLEVEL=debug \
-e ORDERER_GENERAL_LISTENADDRESS=0.0.0.0 \
-e ORDERER_GENERAL_GENESISMETHOD=file \
-e ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.block \
-e ORDERER_GENERAL_LOCALMSPID=OrdererMSP \
-e ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/localMspConfig \
-e ORDERER_GENERAL_TLS_ENABLED=false \
-e ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/localMspConfig/keystore/ordererSigner.pem \
-e ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/localMspConfig/signcerts/orderer0Signer.pem \
-e ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/localMspConfig/cacerts/ordererOrg0.pem] \
hyperledger/fabric-orderer:latest orderer
将configtx.yaml拷贝到fabric/common/configtx/tool/目录下(注意备份一起的文件)
生成名为upchannel02的通道文件,用于创建通道
./build/bin/configtxgen -outputCreateChannelTx upchannel02.tx -channelID upchannel02 -profile TwoOrgs
启动peer (见start_peer0.sh)
$ docker run --name=peer0 \
--restart=unless-stopped \
-p 7051:7051 \
-w /opt/gopath/src/github.com/hyperledger/fabric/peer \
-v /var/run/:/host/var/run/ \
-v /home/hyperledger/gopath/src/github.com/hyperledger/fabric/examples/e2e_cli/examples/:/opt/gopath/src/github.com/hyperledger/fabric/examples/ \
-v /home/hyperledger/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go \
-v /home/hyperledger/gopath/src/github.com/hyperledger/fabric/examples/e2e_cli/crypto/peer/peer0/localMspConfig:/etc/hyperledger/fabric/msp/sampleconfig \
-v /home/hyperledger/gopath/src/github.com/hyperledger/fabric/examples/e2e_cli/crypto:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ \
-e CORE_PEER_ID=peer0 \
-e CORE_PEER_ADDRESS=101.231.114.238:7051 \
-e CORE_PEER_GOSSOP_ENDPOINT=peer0 \
-e CORE_PEER_GOSSIP_EXTERNALENDPOINT=101.231.114.238:7051 \
-e CORE_PEER_LOCALMSPID=org1 \
-e CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock \
-e CORE_LOGGING_LEVEL=DEBUG \
-e CORE_PEER_TLS_ENABLED=false \
-e CORE_NEXT=true \
-e CORE_PEER_ENDORSER_ENABLED=true \
-e CORE_PEER_GOSSIP_USELEADERELECTION=true \
-e CORE_PEER_GOSSIP_ORGLEADER=false \
-e CORE_PEER_GOSSIP_SKIPHANDSHAKE=true \
-e CORE_PEER_PROFILE_ENABLED=true \
-e GOPATH=/opt/gopath \
-e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peer/peer0/localMspConfig \
hyperledger/fabric-peer:latest peer node start --peer-defaultchain=false
peer启动后,再另一个窗口输入
$ docker exec –it peer0 bash
则进入docker的bash,则可以输入相关命令
编辑docker-compose.yaml文件,在不同角色的节点启动不同的服务
启动fabric集群
$ docker-compose up -d
见scripts.sh
# 创建channel
$ peer channel create -o 172.18.163.117:7050 -c mychannel -f channel-artifacts/channel.tx
# 获取通道文件
$ peer channel fetch oldest -o 172.18.163.117:7050 -c mychannel
# 更新组织
$ peer channel update -o 172.18.163.117:7050 -c mychannel -f channel-artifacts/Org1MSPanchors.tx
# 加入channel
$ peer channel join -b mychannel_oldest.block
# 安装chaincode
$ peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
# 实例化chaincode
$ peer chaincode instantiate -o <orderer_ip>:7050 -C upchannel02 -n mycc -v 1.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "OR ('org1.member','org2.member')"
#调用chaincode
$peer chaincode invoke -o <orderer_ip>:7050 -C upchannel02 -n mycc -c '{"Args":["invoke","a","b","10"]}'
#查询chaincode
$ peer chaincode query -C upchannel02 -n mycc -c '{"Args":["query","a"]}'
各个orderer和peer启动的fabric的版本和启动方式需要一致
否则会出现一些错误
Identity store rejected *:37166 : Identity doesn’t match the computed pkiID
在启动peer时需要保证7051和7053端口未被占用,否则peer启动后会自动退出
如果在执行通道或chaincode命令是遇见grpc错误,一般是端口未打开或防火墙问题,将此端口加入防火墙允许列表即可
$ sudo firewall-cmd --zone=public --add-port=7050/tcp --permanent
$ sudo firewall-cmd --reload
或使用iptables相关命令
在安装chaincode遇见fingerprint dismatch的错误。
解决方法:
可以采用在一个peer的bash上远程安装到其他peer上来避免这个问题
或者
现将chaincode源码进行打包,经打包后的代码包发送给其他peer,进行安装
打包cc
$ peer chaincode package -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 ccpack.out
安装cc
$ peer chaincode install ccpack.out
问题描述
Error: Error endorsing invoke: rpc error: code = 2 desc = failed to obtain cds for anchor01 - could not find chaincode with name 'anchor01'
这是因为deploy交易为提交,等待一段时间再次执行即可
如果其他peer执行成功,本peer依旧出现此问题,则重启peer即可(此时不要删数据)
如果重新加入某个channel则需要重启peer,这时候就要删除Data目录,即在fileSystemPath配置的目录
在测试过程中遇见了提交了交易,状态很久不能更新,重启了peer才查询到更新,这是因为peer在一段时间内没有交易,某个orderer会断开与peer的连接,因此peer收不到orderer的广播请求。
解决方法:在configtx.yaml配置orderer的地址为各个组织orderer的内网ip,或者修改对外防火墙配置,使连接保持
如有疑问,请联系:
[email protected]