ubuntu16.04 的 hyperledger fabric1.4 搭建

写在前面,fabric搭建很是麻烦,博主也是花了很久四处看资料终于搭建成功,分享给各位,同时也用于记录,如有问题大家请指出

参考的网站:

https://hyperledgercn.github.io/hyperledgerDocs/build_network_zh/

https://hyperledger-fabric.readthedocs.io/en/latest/prereqs.html

https://blog.csdn.net/sinat_36742186/article/details/80809954

https://blog.csdn.net/diligent_lee/article/details/79098302

https://blog.csdn.net/so5418418/article/details/78355868

https://www.cnblogs.com/informatics/p/8276172.html

一、前期准备

  • 系统软件源改为 aliyun 
    • 方法:进入system setting   然后点击software and update,修改download from

ubuntu16.04 的 hyperledger fabric1.4 搭建_第1张图片

  • 安装vim          sudo apt-get install vim
  • 安装git            sudo apt-get install git
  • 安装curl          sudo apt-get install curl
  • 安装wget        sudo apt-get install wget

 

二、环境准备

  • 安装go1.11 以及以上版本
  • 安装docker 17.06.2-ce 以及以上版本
  • 安装docker-compose 1.14.0 以及以上版本
  • git 拉下 fabric-sample 项目
  • 下载docker image镜像

具体操作:

GO安装

  下载压缩包   无法 可以去这个网站        https://studygolang.com/dl

cd ~  
wget https://studygolang.com/dl/golang/go1.11.linux-amd64.tar.gz    //下载
tar -xzf go1.11.linux-amd64.tar.gz    //解压
sudo mv go /usr/local    //移动

 

  路径配置

export  PATH=$PATH:/usr/local/go/bin
export  GOROOT=/usr/local/go
export  GOPATH=$HOME/go
export  PATH=$PATH:$HOME/go/bin

 

  创建go目录   

cd ~   
mkdir  -p  go/src/github.com/hyperledger/fabric
sudo chmod -R 777 go

 

docker 安装

卸载旧版本的Docker,没安装的就跳过此步  

sudo apt-get remove docker docker-engine docker.io

开始安装

sudo apt-get update

下载安装工具

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    software-properties-common

添加官方密钥

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

加入 apt 仓库中

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 version

出现下面内容:

Client:
 Version:      18.03.1-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   9ee9f40
 Built:        Wed Jun 20 21:43:51 2018
 OS/Arch:      linux/amd64
 Experimental: false
 Orchestrator: swarm

Got permission denied..............

此时已经安装好Docker了,但是还需要一些小操作。因为普通用户没有权限 。

sudo groupadd docker    创建docker组

sudo usermod -aG docker XXX(XXX是当前用户名) 

下面添加阿里云的Docker镜像: 

sudo mkdir -p /etc/docker     

sudo vim /etc/docker/daemon.json 


将以下内容写入daemon.json

{
 "registry-mirrors": ["https://obou6wyb.mirror.aliyuncs.com"]
}

接下来输入:

sudo systemctl daemon-reload 

sudo systemctl restart docker 

docker version


如果还有权限不足提示,输入:

sudo chmod -R 777 /var/run/docker.sock

正确的结果:

Client:
 Version:      18.03.1-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   9ee9f40
 Built:        Wed Jun 20 21:43:51 2018
 OS/Arch:      linux/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.03.1-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.5
  Git commit:   9ee9f40
  Built:        Wed Jun 20 21:42:00 2018
  OS/Arch:      linux/amd64
  Experimental: false

到此docker安装完毕

 

docker-compose安装

输入

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

查看版本

docker-compose -version     //输入

docker-compose version 1.17.1, build unknow    //出现 即安装完成

 

fabric-sample下载

fabric-sample 用于构建测试联盟链

下载

cd ~
cd go/hyperledger/fabric
sudo git clone https://github.com/hyperledger/fabric-samples.git

然后要修改 fabric-sample的权限

sudo chmod -R 777 fabric-sample

docker 镜像安装

进入到fabric目录中,然后输入
curl -sSL https://bit.ly/2ysbOFE | bash -s -- 1.4.0

镜像很大,要下挺久,下完后输入

docker images

会有以下的镜像

ubuntu16.04 的 hyperledger fabric1.4 搭建_第2张图片

到此 docker 镜像下载完成

 

三、构建第一个fabric网络

进入 fabric-samples/first-network 中

cd go/hyperledger/fabric/fabric-samples/first-network

根据配置文件生成各种文件

sudo ./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 ...
/home/smallone/go/hyperledger/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

/home/smallone/go/hyperledger/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
2019-01-20 19:02:05.851 PST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-01-20 19:02:05.917 PST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 002 orderer type: solo
2019-01-20 19:02:05.919 PST [common.tools.configtxgen.localconfig] Load -> INFO 003 Loaded configuration: /home/smallone/go/hyperledger/fabric-samples/first-network/configtx.yaml
2019-01-20 19:02:05.985 PST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 004 orderer type: solo
2019-01-20 19:02:05.986 PST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 005 Loaded configuration: /home/smallone/go/hyperledger/fabric-samples/first-network/configtx.yaml
2019-01-20 19:02:05.989 PST [common.tools.configtxgen] doOutputBlock -> INFO 006 Generating genesis block
2019-01-20 19:02:05.989 PST [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
2019-01-20 19:02:06.049 PST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-01-20 19:02:06.103 PST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/smallone/go/hyperledger/fabric-samples/first-network/configtx.yaml
2019-01-20 19:02:06.168 PST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-01-20 19:02:06.169 PST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/smallone/go/hyperledger/fabric-samples/first-network/configtx.yaml
2019-01-20 19:02:06.170 PST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 005 Generating new channel configtx
2019-01-20 19:02:06.172 PST [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
2019-01-20 19:02:06.238 PST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-01-20 19:02:06.313 PST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/smallone/go/hyperledger/fabric-samples/first-network/configtx.yaml
2019-01-20 19:02:06.382 PST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-01-20 19:02:06.386 PST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/smallone/go/hyperledger/fabric-samples/first-network/configtx.yaml
2019-01-20 19:02:06.386 PST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2019-01-20 19:02:06.386 PST [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
2019-01-20 19:02:06.453 PST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-01-20 19:02:06.519 PST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/smallone/go/hyperledger/fabric-samples/first-network/configtx.yaml
2019-01-20 19:02:06.574 PST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-01-20 19:02:06.582 PST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/smallone/go/hyperledger/fabric-samples/first-network/configtx.yaml
2019-01-20 19:02:06.582 PST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2019-01-20 19:02:06.582 PST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 Writing anchor peer update
+ res=0
+ set +x

 

文件生成好,开始启动网络

sudo ./byfn.sh up

结果如下

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 peer0.org2.example.com ... 
Creating orderer.example.com ... 
Creating peer1.org2.example.com ... 
Creating peer1.org1.example.com ... 
Creating peer0.org1.example.com ... 
Creating peer0.org2.example.com
Creating orderer.example.com
Creating peer1.org2.example.com
Creating peer0.org1.example.com
Creating peer1.org1.example.com ... done
Creating cli ... 
Creating cli ... done

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

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

+ 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
Channel name : mychannel
Creating channel...
+ res=0
+ set +x
2019-01-21 03:03:50.236 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-01-21 03:03:50.336 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
2019-01-21 03:03:50.452 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-01-21 03:03:50.580 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
2019-01-21 03:03:53.689 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-01-21 03:03:53.824 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
2019-01-21 03:03:56.921 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-01-21 03:03:57.089 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
2019-01-21 03:04:00.193 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-01-21 03:04:00.324 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
2019-01-21 03:04:03.431 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-01-21 03:04:03.457 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
2019-01-21 03:04:06.622 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-01-21 03:04:06.639 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
===================== Anchor peers updated for org 'Org2MSP' on channel 'mychannel' ===================== 

Installing chaincode on peer0.org1...
+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
+ res=0
+ set +x
2019-01-21 03:04:09.759 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2019-01-21 03:04:09.759 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2019-01-21 03:04:10.162 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response: 
===================== 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
2019-01-21 03:04:10.259 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2019-01-21 03:04:10.259 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2019-01-21 03:04:10.457 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response: 
===================== 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
2019-01-21 03:04:10.573 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2019-01-21 03:04:10.573 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
2019-01-21 03:05:22.854 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
2019-01-21 03:05:23.185 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2019-01-21 03:05:23.185 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2019-01-21 03:05:23.381 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 =========== 


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

 

关闭网络

sudo ./byfn.sh down
Stopping for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] y
proceeding ...
Stopping cli                    ... done
Stopping peer1.org2.example.com ... done
Stopping peer0.org1.example.com ... done
Stopping peer1.org1.example.com ... done
Stopping peer0.org2.example.com ... done
Stopping orderer.example.com    ... done
Removing cli                    ... done
Removing peer1.org2.example.com ... done
Removing peer0.org1.example.com ... done
Removing peer1.org1.example.com ... done
Removing peer0.org2.example.com ... done
Removing orderer.example.com    ... done
Removing network net_byfn
Removing volume net_peer0.org3.example.com
WARNING: Volume net_peer0.org3.example.com not found.
Removing volume net_peer1.org3.example.com
WARNING: Volume net_peer1.org3.example.com not found.
Removing volume net_orderer.example.com
Removing volume net_peer0.org2.example.com
Removing volume net_peer0.org1.example.com
Removing volume net_peer1.org1.example.com
Removing volume net_peer1.org2.example.com
7099c8e18bb2
70420b81a1e0
5cac3d957453
Untagged: dev-peer1.org2.example.com-mycc-1.0-26c2ef32838554aac4f7ad6f100aca865e87959c9a126e86d764c8d01f8346ab:latest
Deleted: sha256:8acfbc36b83a661e8ba4e06af9a25be327800a1a954c51662042b103679e235f
Deleted: sha256:1f0f19f14b84bcc437ae4dabd4806211c299218f4e2fdf56d8972958a3c7024c
Deleted: sha256:258f101951678547643d18c2ddbef5ba57ef23ed48ef5049107c23c477d736e9
Deleted: sha256:6b31bdff9d04dbd1c704b9071d73ec982c6f8e4e70c2c3091928a5f5a417fbbb
Untagged: dev-peer0.org1.example.com-mycc-1.0-384f11f484b9302df90b453200cfb25174305fce8f53f4e94d45ee3b6cab0ce9:latest
Deleted: sha256:66a25bf8ac064d55ad42d4e2cc32cc2d7a440a30074a9cc09e35d4f30b202d22
Deleted: sha256:48c5d4811d5935a08df2b18b83b9571caf0b0b33921502463ed55fbbea1061f3
Deleted: sha256:d4ecbca9ab425280b33c7b3a3b41f109fab04cd00680aef9c610adcf7044a9b2
Deleted: sha256:43e0195311c5e556f5d5072f571d2d1e0b8365c9de89cb6f96e4a5c7d2cb9f33
Untagged: dev-peer0.org2.example.com-mycc-1.0-15b571b3ce849066b7ec74497da3b27e54e0df1345daff3951b94245ce09c42b:latest
Deleted: sha256:078677606efd7f694b46c1d5bab88106c79878cca0e3f898006ee28ce52a2f29
Deleted: sha256:233d51c5f9072e3252411b9b7b62dde5e75c414f530f10598c874af2e639432a
Deleted: sha256:69ff826bf5f97bebcda3fafe6cb8d4d468a786d0a2d1a83ba480f866967a285c
Deleted: sha256:c9b581623159c14517026f99e0acedb2d181995afd089bd898be73934807fb62

 

到此hyperledger fabric1.4 就搭建完成了

你可能感兴趣的:(区块链,-,hyperledger,fabric)