安装环境:
系统版本: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 docker
-
安装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语言版本go version
go version go1.10.1 linux/amd64
8.安装nodejs,更新yum源:
curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -yum -y install nodejs 验证版本 #Node –v v8.16.1 #npm –v 6.4.1
二、下载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镜像,下载完成后如:
解析bootstrap.sh执行过程:
脚本内容:分别调用了dockerFabricPull 、dockerCaPull 、dockerThirdPartyImagesPull 三个函数去拉取相应的docker镜像并打标签,最后还通过docker images | grep hyperledger*检查下载的镜像。
下载软件包:通过脚本自动下载,也可以使用浏览器单独下载,但需注意版本要与当前系统一致。
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 xz
2.镜像下载完后,将该目录下生成的bin目录拷贝到../examples目录下再进入到../examples/e2e_cli目录运行e2e_cli实例,执行命令./network_setup.sh up
1).Bin目录下可执行命令的作用:
2).cd /home/data/code/go/src/github.com/hyperledger/fabric/examples/e2e_cli
执行过程中有报错:Error: Error endorsing chaincode: rpc error: code = Unknown desc = error starting container: API error (404): {"message":"network e2ecli_default not found"}
问题原因:
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.yaml
修改完成后在启动:
中间图省略
完成以后启动的容器:
Fabric执行过程分析,即执行network_setup.sh脚本:
从这个脚本文件可以看出,主要步骤为:
1.执行generateArtifacts.sh
- Docker-composeup 目录下的docker-compose-cli.yaml文件
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)}')
Using docker-compose template replace private key file names with constants
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
}
Generates Org certs using cryptogen tool
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
}
Generate orderer genesis block , channel configuration transaction and anchor peer update transactions
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"]}'