安裝環境:
系統版本:CentOS 7.2
docker版本爲:19.03.2
docker-compose版本: 1.23.2
1、安裝軟件
一、 安裝依賴軟件包
yum -y install ca-certificates curl software-properties-common wget git yum-utils device-mapper-persistent-data lvm2 gcc gcc-c++
2.添加倉庫,使用docker官方的docker倉庫,版本會比較高:
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
列出安裝的版本:
yum list docker-ce --showduplicates | sort -r #要安裝特定版本的Docker CE,請列出repo中的可用版本
3.配置加速器,修改爲docker官方的地址
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://8ad7943c.m.daocloud.io
vi /etc/docker/daemon.json
{"registry-mirrors": ["https://registry.docker-cn.com"]}
4.安裝docker
yum -y install docker-ce
5.設置開機自啓動、啓動、中止
systemctl enable docker
systemctl start docker
systemctl stop docker
systemctl restart dockernode
安裝docker-compose,並賦予執行權限
curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
7.安裝go語言
mkdir -p /home/data/go/{src,pkg,bin}
wget https://storage.googleapis.com/golang/go1.10.1.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.10.1.linux-amd64.tar.gz
設置環境變量:
vim ~/.bashrc
export GOROOT=/usr/local/go
export GOPATH=/home/data/code/go //go語言安裝路徑,按實際路徑配置
export GOBIN=$GOROOT/bin
export PATH=$GOPATH/bin:$GOBIN:$PATH
使變量生效
source ~/.bashrc
檢查go語言版本linux
go version go1.10.1 linux/amd64
8.安裝nodejs,更新yum源:
curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -c++
yum -y install nodejs 驗證版本 #Node –v v8.16.1 #npm –v 6.4.1
2、下載fabric源碼
mkdir -p $GOPATH/src/github.com/hyperledger/
cd $GOPATH/src/github.com/hyperledger/
git clone https://github.com/hyperledger/fabric.git
cd fabric
git checkout release-1.1
1.進入目錄/fabric/scripts,執行bootstrap.sh,下載docker鏡像,下載完成後如:git
解析bootstrap.sh執行過程:
腳本內容:分別調用了dockerFabricPull 、dockerCaPull 、dockerThirdPartyImagesPull 三個函數去拉取相應的docker鏡像並打標籤,最後還經過docker images | grep hyperledger*檢查下載的鏡像。github
下載軟件包:經過腳本自動下載,也能夠使用瀏覽器單獨下載,但需注意版本要與當前系統一致。
1).curl https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/linux-amd64-1.1.0/hyperledger-fabric-linux-amd64-1.1.0.tar.gz | tar xz
2).curl https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric-ca/hyperledger-fabric-ca/linux-amd64-1.1.0/hyperledger-fabric-ca-linux-amd64-1.1.0.tar.gz | t ar xzgolang
2.鏡像下載完後,將該目錄下生成的bin目錄拷貝到../examples目錄下再進入到../examples/e2e_cli目錄運行e2e_cli實例,執行命令./network_setup.sh up
1).Bin目錄下可執行命令的做用:docker
2).cd /home/data/code/go/src/github.com/hyperledger/fabric/examples/e2e_clinpm
執行過程當中有報錯:Error: Error endorsing chaincode: rpc error: code = Unknown desc = error starting container: API error (404): {"message":"network e2ecli_default not found"}json
問題緣由:
e2e_cli目錄是固定的,啓動後會建立一個docker network以此爲名字,這裏是e2e_cli。若是修改該目錄,要修改/home/data/code/go/src/github.com/hyperledger/fabric/examples/e2e_cli/base目錄下的peer-base.yaml
vi /home/data/code/go/src/github.com/hyperledger/fabric/examples/e2e_cli/base/peer-base.yamlbootstrap
修改完成後在啓動:
中間圖省略
完成之後啓動的容器:
Fabric執行過程分析,即執行network_setup.sh腳本:
從這個腳本文件能夠看出,主要步驟爲:
1.執行generateArtifacts.sh
generateArtifacts.sh內容:
#!/bin/bash +x
CHANNEL_NAME=$1
: ${CHANNEL_NAME:="mychannel"}
echo $CHANNEL_NAME
export FABRIC_ROOT=$PWD/../..
export FABRIC_CFG_PATH=$PWD
echo
OS_ARCH=$(echo "$(uname -s|tr '[:upper:]' '[:lower:]'|sed 's/mingw64_nt.*/windows/')-$(uname -m | sed 's/x86_64/amd64/g')" | awk '{print tolower($0)}')
function replacePrivateKey () {
ARCH=uname -s | grep Darwin
if [ "$ARCH" == "Darwin" ]; then
OPTS="-it"
else
OPTS="-i"
fi
cp docker-compose-e2e-template.yaml docker-compose-e2e.yaml CURRENT_DIR=$PWD cd crypto-config/peerOrganizations/org1.example.com/ca/ PRIV_KEY=$(ls *_sk) cd $CURRENT_DIR sed $OPTS "s/CA1_PRIVATE_KEY/${PRIV_KEY}/g" docker-compose-e2e.yaml cd crypto-config/peerOrganizations/org2.example.com/ca/ PRIV_KEY=$(ls *_sk) cd $CURRENT_DIR sed $OPTS "s/CA2_PRIVATE_KEY/${PRIV_KEY}/g" docker-compose-e2e.yaml
}
function generateCerts (){
CRYPTOGEN=$FABRIC_ROOT/release/$OS_ARCH/bin/cryptogen
if [ -f "$CRYPTOGEN" ]; then echo "Using cryptogen -> $CRYPTOGEN" else echo "Building cryptogen" make -C $FABRIC_ROOT release fi echo echo "##########################################################" echo "##### Generate certificates using cryptogen tool #########" echo "##########################################################" $CRYPTOGEN generate --config=./crypto-config.yaml echo
}
function generateChannelArtifacts() {
CONFIGTXGEN=$FABRIC_ROOT/release/$OS_ARCH/bin/configtxgen if [ -f "$CONFIGTXGEN" ]; then echo "Using configtxgen -> $CONFIGTXGEN" else echo "Building configtxgen" make -C $FABRIC_ROOT release fi echo "##########################################################" echo "######### Generating Orderer Genesis block ##############" echo "##########################################################" # Note: For some unknown reason (at least for now) the block file can't be # named orderer.genesis.block or the orderer will fail to launch! $CONFIGTXGEN -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block echo echo "#################################################################" echo "### Generating channel configuration transaction 'channel.tx' ###" echo "#################################################################" $CONFIGTXGEN -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME echo echo "#################################################################" echo "####### Generating anchor peer update for Org1MSP ##########" echo "#################################################################" $CONFIGTXGEN -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP echo echo "#################################################################" echo "####### Generating anchor peer update for Org2MSP ##########" echo "#################################################################" $CONFIGTXGEN -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP echo
}
generateCerts
replacePrivateKey
generateChannelArtifacts
腳本結束
generateCerts:使用cryptogen工具生成祕鑰和證書
replacePrivateKey:
根據/home/data/code/go/src/github.com/hyperledger/fabric/examples/e2e_cli/crypto-config/peerOrganizations/org1.example.com/ca/*_sk修改docker-compose-e2e.yaml文件
generateChannelArtifacts:生成排序創世區塊、通道配置文件、生成 組織錨節點配置
docker-compose-e2e.yaml文件,經過service定義了一條規則
如需中止進程,執行
./network_setup.sh down
測試過程:
查詢a帳戶的餘額
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
把a帳戶的餘額再轉20元給b帳戶
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 -c '{"Args":["invoke","a","b","20"]}'
再次查詢peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'