今天是一个值得纪念的日子,经过了n+1天的踩坑,四处投医询问了各路牛人,划水了无数个晨会,终于在两台阿里云上成功的实现了fabric1.1.0的solo多机部署,一时间感动的涕泗横流,竟不知从何说起。
我在部署过程中参考了《HyperLedger Fabric开发实战-快速掌握区块链技术》一书以及《HyperLedger Fabric
solo 模式多服务器部署》一文,询问了数名区块链方面的大佬,并且百度了n种fabric部署过程中的错误提示。谨以此文向以上各位表示感谢!
书归正传。首先先简单聊一聊solo多机模式的工作原理:
我部署了两台服务器,其中一台服务器作为orderer节点运行,另一台服务器作为peer节点运行,如果部署多台,其他服务器也作为peer节点运行。
每个peer节点会启动一个peerx.orgx.example.com容器和一个cli容器。
peerx.orgx.example.com容器依赖于orderer容器。
cli容器依赖于peerx.orgx.example.com容器和orderer容器。
peerx.orgx.example.com容器通过宿主机IP地址及基于SSH协议的默认端口22与orderer容器通信。
cli容器通过宿主机IP地址及基于SSH协议的默认端口22与orderer容器通信,通过基于TCP协议的7050-7053端口与peerx.orgx.example.com容器通信。
= = = = = = = = = = = = = = = = = = = = = = = =以下为部署的详细步骤= = = = = = = = = = = = = = = = = = = = = = = =
我将fabric基础环境过程中所需要的文件及安装包全部打包放在了fabric1.1.0-solo多机部署-附件.zip下,有需要的朋友自取。
1.aliyun服务器我使用的是CentOS 7.4
服务器需要开启文中出现的端口(入方向和出方向都要开),包括但不限于7050-7053等
2. go语言版本 1.14.6
(2.1)下载附件,找到文件夹中的go包
(2.2)将下载的语言包go1.14.6.linux-amd64.tar.gz上传至文件夹/tmp/docker
(2.3)进入/tmp/docker:cd /tmp/docker
(2.4)解压至/usr/local目录下:tar -C /usr/local -zxvf go1.11.linux-amd64.tar.gz
(2.5)配置go语言环境变量,参照配置jdk环境变量:vim /etc/profile
在文件末尾加以下内容:
#go环境变量
export PATH=$PATH:/usr/local/go/bin
export GOPATH=/opt/gopath
最后source /etc/profile
使文件生效。
(2.6)通过go version
指令查看是否安装成功
3.安装docker ce
指令sudo yum install docker-ce
即可在线安装。缺失相关依赖的话根据提示下载即可。
通过docker --version
查看安装情况
启动docker:systemctl start docker
(有些版本的启动命令为service docker start
)
设置docker开机自启:chkconfig docker on
4.安装docker-compose
(4.1)在附件中找到docker-compose-Linux-x86_64上传至服务器的/tmp/docker/docker-compose
文件夹下,若无此文件夹,创建即可。
(4.2)进入文件所在目录:cd /tmp/docker/docker-compose
(4.3)将其移动至/usr/local/bin安装目录:mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
(4.4)赋予可执行权限:chmod +x /usr/local/bin/docker-compose
(4.5)查看版本信息:docker-compose --version
5.安装fabric源码
(5.1)创建文件夹:
mkdir -p $GOPATH/src/github.com/hyperledger
进入文件夹:
cd $GOPATH/src/github.com/hyperledger
(5.2)下载fabric源码
git clone https://github.com/hyperledger/fabric.git
由于github在国内网速较差,我将fabric源码clone到了我自己的gitee仓库,大家也可输入以下指令下载fabric源码(国内网速2m/s以上)
git clone https://gitee.com/qiaoao-spec/fabric
(5.3)进入fabric目录内,执行git branch -a
指令查看所有分支
使用命令git checkout -b 1.1 origin/release-1.1
切换到1.1版本,此命令的作用是checkout远程的release-1.1分支,在本地重命名为1.1分支,并切换到本地的1.1分支。
(5.4)下载相应镜像
进入fabric文件夹下的scripts文件夹,这个文件夹保存有大多数配置环境所需的脚本。
执行./bootstrap.sh
即可下载镜像,此处等待时间较长,视网速而定。执行之前最好cat bootstrap.sh
查看一下文件,看看里面的版本号是否为1.1.0。
如果报错:Permission Denied.
原因是没有可执行权限
执行chmod +x 脚本名
即可。
下载完成之后通过docker images
指令查看镜像
6.安装平台二进制文件
运行fabric需要下载特定的二进制文件。及将附件中文件夹1.1.0
内的bin
以及config
文件夹上传至fabric文件夹下。
修改/etc/profile
文件的末尾为
export PATH=$PATH:/opt/gopath/src/github.com/hyperledger/fabric/bin
export GOPATH=/opt/gopath
首先要将你所有的服务器都完成上一点中的内容,即基础环境配置全部完成。
1.生成基础文件,如权限、创始块相关文件
orderer 服务器中创建项目文件夹 mkdir solo_multiserver
,并把 附件中的 chaincode_example02
拷贝到 solo_multiserver/chaincode/go
目录下。不存在的文件夹新建即可。
新建文件vim crypto-config.yaml
如下:
OrdererOrgs:
- Name: Orderer
Domain: example.com
Specs:
- Hostname: orderer
PeerOrgs:
- Name: Org1
Domain: org1.example.com
Template:
Count: 2
Users:
Count: 1
- Name: Org2
Domain: org2.example.com
Template:
Count: 2
Users:
Count: 1
生成安全文件:
cryptogen generate --config=./crypto-config.yaml
# 返回信息
org1.example.com
org2.example.com
新建文件vim configtx.yaml
,如下
Profiles:
TwoOrgsOrdererGenesis:
Orderer:
<<: *OrdererDefaults
Organizations:
- *OrdererOrg
Consortiums:
SampleConsortium:
Organizations:
- *Org1
- *Org2
TwoOrgsChannel:
Consortium: SampleConsortium
Application:
<<: *ApplicationDefaults
Organizations:
- *Org1
- *Org2
Organizations:
- &OrdererOrg
Name: OrdererOrg
ID: OrdererMSP
MSPDir: crypto-config/ordererOrganizations/example.com/msp
- &Org1
Name: Org1MSP
ID: Org1MSP
MSPDir: crypto-config/peerOrganizations/org1.example.com/msp
AnchorPeers:
- Host: peer0.org1.example.com
Port: 7051
- &Org2
Name: Org2MSP
ID: Org2MSP
MSPDir: crypto-config/peerOrganizations/org2.example.com/msp
AnchorPeers:
- Host: peer0.org2.example.com
Port: 7051
Orderer: &OrdererDefaults
OrdererType: solo
Addresses:
- orderer.example.com:7050
BatchTimeout: 2s
BatchSize:
MaxMessageCount: 10
AbsoluteMaxBytes: 98 MB
PreferredMaxBytes: 512 KB
Kafka:
Brokers:
- 127.0.0.1:9092
Organizations:
Application: &ApplicationDefaults
Organizations:
依次执行命令,生成创始块和频道认证文件:
mkdir channel-artifacts
configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/mychannel.tx -channelID mychannel
2.部署orderer服务
在orderer 服务器的 solo_multiserver 下新建 docker-orderer.yaml,内容如下:
version: '2'
services:
orderer.example.com:
container_name: orderer.example.com
image: hyperledger/fabric-orderer
environment:
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=solo_multiserver_default
- ORDERER_GENERAL_LOGLEVEL=debug
- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
- ORDERER_GENERAL_LISTENPORT=7050
- ORDERER_GENERAL_GENESISMETHOD=file
- ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block
- ORDERER_GENERAL_LOCALMSPID=OrdererMSP
- ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
- ORDERER_GENERAL_TLS_ENABLED=false
- ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
- ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
- ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
working_dir: /opt/gopath/src/github.com/hyperledger/fabric
command: orderer
volumes:
- ./channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block
- ./crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp:/var/hyperledger/orderer/msp
- ./crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/:/var/hyperledger/orderer/tls
networks:
default:
aliases:
- solo_multiserver
ports:
- 7050:7050
启动 orderer:
docker-compose -f docker-orderer.yaml up -d
至此完成orderer节点的部署。
首先需要保证peer节点的服务器也完成了第一点的基本部署。然后通过scp指令
将orderer 服务器中的 solo_multiserver
文件夹拷贝到peer服务器。
1.配置peer节点
在拷贝过来的solo_multiserver
文件夹下,删除docker-orderer.yaml
文件,新建docker-peer01.yaml
,内容:
version: '2'
services:
couchdb:
container_name: couchdb
image: hyperledger/fabric-couchdb
ports:
- "5984:5984"
ca:
container_name: ca
image: hyperledger/fabric-ca
environment:
- FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
- FABRIC_CA_SERVER_CA_NAME=ca
- FABRIC_CA_SERVER_TLS_ENABLED=false
- FABRIC_CA_SERVER_TLS_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem
- FABRIC_CA_SERVER_TLS_KEYFILE=/etc/hyperledger/fabric-ca-server-config/e706525228346492ecce846ff0cf61330dbc1b28207762fc4bece45356f21546_sk
ports:
- "7054:7054"
command: sh -c 'fabric-ca-server start --ca.certfile /etc/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem --ca.keyfile /etc/hyperledger/fabric-ca-server-config/e706525228346492ecce846ff0cf61330dbc1b28207762fc4bece45356f21546_sk -b admin:adminpw -d'
volumes:
- ./crypto-config/peerOrganizations/org1.example.com/ca/:/etc/hyperledger/fabric-ca-server-config
peer0.org1.example.com:
container_name: peer0.org1.example.com
image: hyperledger/fabric-peer
environment:
- CORE_LEDGER_STATE_STATEDATABASE=CouchDB
- CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984
- CORE_PEER_ID=peer0.org1.example.com
- CORE_PEER_NETWORKID=solo_multiserver
- CORE_PEER_ADDRESS=peer0.org1.example.com:7051
- CORE_PEER_CHAINCODELISTENADDRESS=peer0.org1.example.com:7052
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.example.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_LOGGING_LEVEL=DEBUG
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=solo_multiserver_default
- CORE_PEER_GOSSIP_SKIPHANDSHAKE=true
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
- CORE_PEER_PROFILE_ENABLED=false
- CORE_PEER_TLS_ENABLED=false
- CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
volumes:
- /var/run/:/host/var/run/
- ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp:/etc/hyperledger/fabric/msp
- ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls:/etc/hyperledger/fabric/tls
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
ports:
- 7051:7051
- 7052:7052
- 7053:7053
depends_on:
- couchdb
networks:
default:
aliases:
- solo_multiserver
extra_hosts:
- "orderer.example.com:172.31.159.130"
cli:
container_name: cli
image: hyperledger/fabric-tools
tty: true
environment:
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_LOGGING_LEVEL=DEBUG
- CORE_PEER_ID=cli
- CORE_PEER_ADDRESS=peer0.org1.example.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_PEER_TLS_ENABLED=false
- CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
- CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
volumes:
- /var/run/:/host/var/run/
- ./chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/solo_multiserver/chaincode/go
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
depends_on:
- peer0.org1.example.com
extra_hosts:
- "orderer.example.com:192.168.31.241"
- "peer0.org1.example.com:192.168.31.7"
需要修改2个地方:
- ca下面的 _sk 文件名,到
crypto-config/peerOrganizations/org1.example.com/ca
下查找。一共两处需要修改
2.其中的IP修改为自己的对应IP
3.启动peer节点
docker-compose -f docker-peer01.yaml up -d
4.peer0.org1 上部署 chaincode
进入 cli 容器:
docker exec -it cli bash
创建channel
peer channel create -o orderer.example.com:7050 -c mychannel -t 50 -f ./channel-artifacts/mychannel.tx
peer channel join -b mychannel.block
安装智能合约:
peer chaincode install -n mychannel -p github.com/hyperledger/fabric/aberic/chaincode/go/chaincode_example02 -v 1.0
实例化chaincode:
peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n mychannel -c '{"Args":["init","A","10","B","10"]}' -P "OR ('Org1MSP.member')" -v 1.0
至此完成两台服务器的solo多机部署!如果你在过程中没有任何报错,那么恭喜你,你可能是天选之子!如果你有一些百度难以找到或无法解决的问题,欢迎在下方留言,和我共同研究!
//update 2020.7.30
//能够帮助到你是我的荣幸!欢迎留言点赞!