在Ubuntu18.04 Linux系统下搭建Fabric1.4.0

环境配置

Ubuntu18.04 Go最新版本

安装依赖包

编译Fabric相关代码,需要一些依赖包,可以通过如下命令安装:`

 $ sudo apt-get update \
    && apt-get install -y libsnappy-dev zlib1g-dev libbz2-dev libltdl-dev libtool

注意:是zlib1g-dev而不是zliblg-dev

安装Docker

Fabric网络目前依赖Docker服务作为链码容器的支持,因此即使是本地环境运行Fabric网络,也需要在Peer节点上安装Docker环境。

$ curl -fsSL https://get.docker.com/ | sh

建立仓库

$ sudo add-apt-repository \
    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

安装最新版本的Docker-ce

$ sudo apt-get update
$ sudo apt-get install docker-ce

添加docker镜像(这里我选的是阿里云,包括Ubuntu的软件源选择的也是阿里云)

$ sudo mkdir -p /etc/docker
$sudo gedit /etc/docker/daemon.json

修改一下daemon文件,保存

{
     
 "registry-mirrors": ["https://obou6wyb.mirror.aliyuncs.com"]
}
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
$ sudo usermod -aG docker lcy	(lcy是我的虚拟机的用户名)
$ sudo chmod -R 777 /var/run/docker.sock

这个时候一般来说已经安装好Docker,可以通过下面步骤来测试

$ docker version

出现截图所示的内容,说明已经docker安装完毕

安装Docker-compose

$ sudo apt-get install python-pip
$ sudo pip install docker-compose

查看是否安装完毕

$ docker-compose version

出现下面内容说明安装完毕

获取代码

克隆源码

首先,是基本的创建目录结构,并切换到该目录

$ mkdir -p $GOPATH/src/github.com/hyperledger
$ cd $GOPATH/src/github.com/hyperledger

然后是克隆源码,目前,Fabric代码的官方仓库实时同步到Github仓库上,所以我们可以直接从Github上导入代码。但是由于墙的原因,因此在直接git clone源码的时候速度会非常慢,最后会因为项目过大或导入时间太长的原因导致源码导入失败。
解决办法:可以先将github上的源码仓库导入到码云(Gitee)上,然后再导入

$ git clone 你的码云仓库地址
$ cd fabric
$ git checkout v1.4.0

下载镜像和fabric-samples

$ cd scripts/
$ ./bootstrap.sh

在执行 ./bootstrap.sh 脚本自动下载fabric-samples和fabric镜像时,由于国内网络的原因,常常会下载 hyperledger-fabric-linux-amd64-1.4.0.tar.gz 失败的错误导致无法继续进行

$ cd fabric-samples
#到相应的网址下载二进制文件到fabric-samples文件夹
$ sudo wget https://github.com/hyperledger/fabric/releases/download/v1.4.0/hyperledger-fabric-linux-amd64-1.4.0.tar.gz
$ tar -zxvf hyperledger-fabric-linux-amd64-1.4.0.tar.gz

然后要注释或者删去该脚本文件中的binariesInstall部分语句,然后再运行脚本文件不会报错

$ sudo ./bootstrap.sh

最后执行

docker images

当出现以下内容时说明镜像部署成功:

REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
hyperledger/fabric-javaenv     1.4.0               3d91b3bf7118        2 months ago        1.75GB
hyperledger/fabric-javaenv     latest              3d91b3bf7118        2 months ago        1.75GB
hyperledger/fabric-tools       1.4.0               0a44f4261a55        3 months ago        1.56GB
hyperledger/fabric-tools       latest              0a44f4261a55        3 months ago        1.56GB
hyperledger/fabric-ccenv       1.4.0               5b31d55f5f3a        3 months ago        1.43GB
hyperledger/fabric-ccenv       latest              5b31d55f5f3a        3 months ago        1.43GB
hyperledger/fabric-orderer     1.4.0               54f372205580        3 months ago        150MB
hyperledger/fabric-orderer     latest              54f372205580        3 months ago        150MB
hyperledger/fabric-peer        1.4.0               304fac59b501        3 months ago        157MB
hyperledger/fabric-peer        latest              304fac59b501        3 months ago        157MB
hyperledger/fabric-ca          1.4.0               1a804ab74f58        3 months ago        244MB
hyperledger/fabric-ca          latest              1a804ab74f58        3 months ago        244MB
hyperledger/fabric-zookeeper   0.4.14              d36da0db87a4        5 months ago        1.43GB
hyperledger/fabric-zookeeper   latest              d36da0db87a4        5 months ago        1.43GB
hyperledger/fabric-kafka       0.4.14              a3b095201c66        5 months ago        1.44GB
hyperledger/fabric-kafka       latest              a3b095201c66        5 months ago        1.44GB
hyperledger/fabric-couchdb     0.4.14              f14f97292b4c        5 months ago        1.5GB
hyperledger/fabric-couchdb     latest              f14f97292b4c        5 months ago        1.5GB

至此,Fabric1.4.0基础环境部署完成

启动网络

构建第一个Fabric网络,先进入到对应的目录中

cd $GOPATH/go/src/github.com/hyperledger/fabric/fabric-samples/first-network

根据配置文件生成各种需要的文件

sudo ./byfn.sh -m generate

成功会出现下图结果

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

/opt/go/src/github.com/hyperledger/fabric/fabric-samples/first-network/../bin/configtxgen
##########################################################
#########  Generating Orderer Genesis block ##############
##########################################################
CONSENSUS_TYPE=solo
+ '[' solo == solo ']'
+ configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block
2020-07-16 22:33:26.358 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2020-07-16 22:33:26.377 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 002 orderer type: solo
2020-07-16 22:33:26.377 CST [common.tools.configtxgen.localconfig] Load -> INFO 003 Loaded configuration: /opt/go/src/github.com/hyperledger/fabric/fabric-samples/first-network/configtx.yaml
2020-07-16 22:33:26.402 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 004 orderer type: solo
2020-07-16 22:33:26.403 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 005 Loaded configuration: /opt/go/src/github.com/hyperledger/fabric/fabric-samples/first-network/configtx.yaml
2020-07-16 22:33:26.404 CST [common.tools.configtxgen] doOutputBlock -> INFO 006 Generating genesis block
2020-07-16 22:33:26.404 CST [common.tools.configtxgen] doOutputBlock -> INFO 007 Writing genesis block
+ res=0
+ set +x

#################################################################
### Generating channel configuration transaction 'channel.tx' ###
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
2020-07-16 22:33:26.462 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2020-07-16 22:33:26.482 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /opt/go/src/github.com/hyperledger/fabric/fabric-samples/first-network/configtx.yaml
2020-07-16 22:33:26.502 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2020-07-16 22:33:26.502 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /opt/go/src/github.com/hyperledger/fabric/fabric-samples/first-network/configtx.yaml
2020-07-16 22:33:26.502 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 005 Generating new channel configtx
2020-07-16 22:33:26.504 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 006 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
2020-07-16 22:33:26.549 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2020-07-16 22:33:26.569 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /opt/go/src/github.com/hyperledger/fabric/fabric-samples/first-network/configtx.yaml
2020-07-16 22:33:26.590 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2020-07-16 22:33:26.590 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /opt/go/src/github.com/hyperledger/fabric/fabric-samples/first-network/configtx.yaml
2020-07-16 22:33:26.590 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2020-07-16 22:33:26.590 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 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
2020-07-16 22:33:26.692 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2020-07-16 22:33:26.712 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /opt/go/src/github.com/hyperledger/fabric/fabric-samples/first-network/configtx.yaml
2020-07-16 22:33:26.733 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2020-07-16 22:33:26.733 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /opt/go/src/github.com/hyperledger/fabric/fabric-samples/first-network/configtx.yaml
2020-07-16 22:33:26.733 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2020-07-16 22:33:26.733 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 Writing anchor peer update
+ res=0
+ set +x

接下来启动网络,输入

sudo ./byfn.sh up

若启动失败,报错为

cryptogen tool not found. exiting

则需要手动把fabric-samples目录下bin文件夹中的两个文件:configtxgen 和cryptogen复制到fabric-samples目录下的config文件夹中,具体操作可以通过linux系统的cp命令完成。

出现下图结果表示启动成功(输入选择y)

Starting for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] y
proceeding ...
LOCAL_VERSION=1.4.0
DOCKER_IMAGE_VERSION=1.4.0
Creating network "net_byfn" with the default driver
Creating volume "net_peer0.org2.example.com" with default driver
Creating volume "net_peer1.org2.example.com" with default driver
Creating volume "net_peer1.org1.example.com" with default driver
Creating volume "net_peer0.org1.example.com" with default driver
Creating volume "net_orderer.example.com" with default driver
Creating peer1.org1.example.com ... done
Creating peer0.org2.example.com ... done
Creating peer1.org2.example.com ... done
Creating orderer.example.com    ... done
Creating peer0.org1.example.com ... done
Creating cli                    ... done

 ____    _____      _      ____    _____ 
/ ___|  |_   _|    / \    |  _ \  |_   _|
\___ \    | |     / _ \   | |_) |   | |  
 ___) |   | |    / ___ \  |  _ <    | |  
|____/    |_|   /_/   \_\ |_| \_\   |_|  

Build your first network (BYFN) end-to-end test

Channel name : mychannel
Creating channel...
+ peer channel create -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/channel.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
+ res=0
+ set +x
2020-07-16 14:35:56.397 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2020-07-16 14:35:56.458 UTC [cli.common] readBlock -> INFO 002 Received block: 0
===================== Channel 'mychannel' created ===================== 

Having all peers join the channel...
+ peer channel join -b mychannel.block
+ res=0
+ set +x
2020-07-16 14:35:56.552 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2020-07-16 14:35:56.593 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
===================== peer0.org1 joined channel 'mychannel' ===================== 

+ peer channel join -b mychannel.block
+ res=0
+ set +x
2020-07-16 14:35:59.814 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2020-07-16 14:35:59.836 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
===================== peer1.org1 joined channel 'mychannel' ===================== 

+ peer channel join -b mychannel.block
+ res=0
+ set +x
2020-07-16 14:36:02.895 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2020-07-16 14:36:02.982 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
===================== peer0.org2 joined channel 'mychannel' ===================== 

+ peer channel join -b mychannel.block
+ res=0
+ set +x
2020-07-16 14:36:06.042 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2020-07-16 14:36:06.064 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
===================== peer1.org2 joined channel 'mychannel' ===================== 

Updating anchor peers for org1...
+ peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
+ res=0
+ set +x
2020-07-16 14:36:09.129 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2020-07-16 14:36:09.145 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
===================== Anchor peers updated for org 'Org1MSP' on channel 'mychannel' ===================== 

Updating anchor peers for org2...
+ peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
+ res=0
+ set +x
2020-07-16 14:36:12.225 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2020-07-16 14:36:12.233 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
===================== Anchor peers updated for org 'Org2MSP' on channel 'mychannel' ===================== 
+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/

Installing chaincode on peer0.org1...
+ res=0
+ set +x
2020-07-16 14:36:15.315 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2020-07-16 14:36:15.315 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2020-07-16 14:36:18.457 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" > 
===================== Chaincode is installed on peer0.org1 ===================== 

Install chaincode on peer0.org2...
+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
+ res=0
+ set +x
2020-07-16 14:36:18.522 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2020-07-16 14:36:18.523 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2020-07-16 14:36:18.682 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" > 
===================== Chaincode is installed on peer0.org2 ===================== 

Instantiating chaincode on peer0.org2...
+ peer chaincode instantiate -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc -l golang -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P 'AND ('\''Org1MSP.peer'\'','\''Org2MSP.peer'\'')'
+ res=0
+ set +x
2020-07-16 14:36:18.751 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2020-07-16 14:36:18.751 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
===================== Chaincode is instantiated on peer0.org2 on channel 'mychannel' ===================== 

Querying chaincode on peer0.org1...
===================== Querying on peer0.org1 on channel 'mychannel'... ===================== 
Attempting to Query peer0.org1 ...3 secs
+ peer chaincode query -C mychannel -n mycc -c '{
     "Args":["query","a"]}'
+ res=0
+ set +x

100
===================== Query successful on peer0.org1 on channel 'mychannel' ===================== 
Sending invoke transaction on peer0.org1 peer0.org2...
+ peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{
     "Args":["invoke","a","b","10"]}'
+ res=0
+ set +x
2020-07-16 14:38:11.780 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200 
===================== Invoke transaction successful on peer0.org1 peer0.org2 on channel 'mychannel' ===================== 

Installing chaincode on peer1.org2...
+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
+ res=0
+ set +x
2020-07-16 14:38:11.847 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2020-07-16 14:38:11.847 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2020-07-16 14:38:12.088 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response: 
===================== Chaincode is installed on peer1.org2 ===================== 

Querying chaincode on peer1.org2...
===================== Querying on peer1.org2 on channel 'mychannel'... ===================== 
Attempting to Query peer1.org2 ...3 secs
+ peer chaincode query -C mychannel -n mycc -c '{
     "Args":["query","a"]}'
+ res=0
+ set +x

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

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


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

查看启动网络后的容器

docker ps

可以看到下图所示

CONTAINER ID        IMAGE                                                                                                  COMMAND                  CREATED              STATUS              PORTS                                              NAMES
112d57fdb8df        dev-peer1.org2.example.com-mycc-1.0-26c2ef32838554aac4f7ad6f100aca865e87959c9a126e86d764c8d01f8346ab   "chaincode -peer.add…"   32 seconds ago       Up 30 seconds                                                          dev-peer1.org2.example.com-mycc-1.0
3be6e90b2697        dev-peer0.org1.example.com-mycc-1.0-384f11f484b9302df90b453200cfb25174305fce8f53f4e94d45ee3b6cab0ce9   "chaincode -peer.add…"   About a minute ago   Up 57 seconds                                                          dev-peer0.org1.example.com-mycc-1.0
f96b74ef20d3        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
aaf6320390b7        hyperledger/fabric-tools:latest                                                                        "/bin/bash"              2 minutes ago        Up 2 minutes                                                           cli
ece402eb6d07        hyperledger/fabric-peer:latest                                                                         "peer node start"        2 minutes ago        Up 2 minutes        0.0.0.0:9051->7051/tcp, 0.0.0.0:9053->7053/tcp     peer0.org2.example.com
ed453fd893f6        hyperledger/fabric-peer:latest                                                                         "peer node start"        2 minutes ago        Up 2 minutes        0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp     peer0.org1.example.com
7c087f3393f9        hyperledger/fabric-peer:latest                                                                         "peer node start"        2 minutes ago        Up 2 minutes        0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp     peer1.org1.example.com
0dd17bf1bf05        hyperledger/fabric-orderer:latest                                                                      "orderer"                2 minutes ago        Up 2 minutes        0.0.0.0:7050->7050/tcp                             orderer.example.com
b38ea513f85d        hyperledger/fabric-peer:latest                                                                         "peer node start"        2 minutes ago        Up 2 minutes        0.0.0.0:10051->7051/tcp, 0.0.0.0:10053->7053/tcp   peer1.org2.example.com

其中dev开头的即为智能合约的一些容器,下面的是fabric网络的一些容器。

若要关闭网络,则输入

sudo ./byfn.sh down

你可能感兴趣的:(Fabric,区块链)