开发先决条件:
Git客户端
Go - 版本1.12.x
Docker - 17.06.2-ce或更高版本
Docker Compose - 1.14.0或更高版本
#(macOS) 必须安装Xcode
安装git
yum -y install gcc-c++ wget
yum -y install git
安装docker
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum makecache fast
yum install docker-ce -y
systemctl start docker
systemctl enable docker
安装docker-compose,负责实现对Docker容器集群的快速编排
yum -y install epel-release
yum -y install python-pip
pip --version
pip install --upgrade pip
pip install docker-compose
yum install bash-completion -y
docker-compose version
安装go语言
wget https://dl.google.com/go/go1.12.9.linux-amd64.tar.gz
tar -xf go1.12.9.linux-amd64.tar.gz
mv go /usr/local/
echo "export PATH=$PATH:/usr/local/go/bin" >>/etc/profile
source /etc/profile
安装Node.js Runtime and NPM
fabric目前不支持低于8.x系列的其他版本
wget https://npm.taobao.org/mirrors/node/v8.9.4/node-v8.9.4-linux-x64.tar.xz
tar xf node-v8.9.4-linux-x64.tar.xz
mv node-v8.9.4-linux-x64/ /usr/local/nodejs
chown -R root.root /usr/local/nodejs/
ln -s /usr/local/nodejs/bin/node /usr/bin/node
echo "export PATH=$PATH:/usr/local/nodejs/bin" >>/etc/profile
source /etc/profile
npm config set registry https://registry.npm.taobao.org
npm config get registry
npm install express -g
node -v
npm -v
安装fabric二进制文件和Docker镜像
cd /usr/local/go/src/
mkdir -p github.com/hyperledger
cd github.com/hyperledger
yum -y install libtool libltdl-dev
wget https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh
chmod +x ./bootstrap.sh
./bootstrap.sh
安装成功如下:
......
......
+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
+ res=0
+ set +x
2019-08-29 08:29:37.103 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2019-08-29 08:29:37.104 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2019-08-29 08:29:37.388 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 ===========
_____ _ _ ____
| ____| | \ | | | _ \
| _| | \| | | | | |
| |___ | |\ | | |_| |
|_____| |_| \_| |____/
建立第一个网络
yum install softhsm -y
cd /usr/local/go/src/github.com/hyperledger/fabric-samples/first-network
生成网络工件
./byfn.sh generate
启动网络(不知道为什么我每次都需要启动两次才能成功,第一次up之后要先down掉再up)
./byfn.sh up
./byfn.sh down
查看docker运行的容器:
[root@localhost first-network]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
003d092e2472 dev-peer1.org2.example.com-mycc-1.0-26c2ef32838554aac4f7ad6f100aca865e87959c9a126e86d764c8d01f8346ab "chaincode -peer.add…" 2 minutes ago Up 2 minutes dev-peer1.org2.example.com-mycc-1.0
22a21ccfe3a7 dev-peer0.org1.example.com-mycc-1.0-384f11f484b9302df90b453200cfb25174305fce8f53f4e94d45ee3b6cab0ce9 "chaincode -peer.add…" 3 minutes ago Up 3 minutes dev-peer0.org1.example.com-mycc-1.0
506eec0c9c06 dev-peer0.org2.example.com-mycc-1.0-15b571b3ce849066b7ec74497da3b27e54e0df1345daff3951b94245ce09c42b "chaincode -peer.add…" 3 minutes ago Up 3 minutes dev-peer0.org2.example.com-mycc-1.0
793d1d55689e hyperledger/fabric-tools:latest "/bin/bash" 4 minutes ago Up 4 minutes cli
bac1260268b7 hyperledger/fabric-peer:latest "peer node start" 4 minutes ago Up 4 minutes 0.0.0.0:7051->7051/tcp peer0.org1.example.com
8ab55bde2496 hyperledger/fabric-peer:latest "peer node start" 4 minutes ago Up 4 minutes 0.0.0.0:9051->9051/tcp peer0.org2.example.com
f96d21d797e6 hyperledger/fabric-peer:latest "peer node start" 4 minutes ago Up 4 minutes 0.0.0.0:8051->8051/tcp peer1.org1.example.com
adc6f6c38686 hyperledger/fabric-orderer:latest "orderer" 4 minutes ago Up 4 minutes 0.0.0.0:7050->7050/tcp orderer.example.com
f82e8152f834 hyperledger/fabric-peer:latest "peer node start" 4 minutes ago Up 4 minutes 0.0.0.0:10051->10051/tcp peer1.org2.example.com
测试转账:
docker exec -it cli bash
查询
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
export CHANNEL_NAME=mychannel
转账
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:9051 --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"]}'
invoke1.2之后(包含1.2)需要check --peer这个参数
1.2 之前的官方脚本:
peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}'
1.2之后官方脚本:
peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc $PEER_CONN_PARMS -c '{"Args":["invoke","a","b","10"]}'
注意:
上面是虚拟机测试环境,如果用阿里云的服务器启动会报错如下:
Error: error getting endorser client for channel: endorser client failed to connect to peer0.org1.example.com:7051: failed to create new connection: connection error: desc = "transport: error while dialing: dial tcp: lookup peer0.org1.example.com: no such host"
peer0.org1 failed to join the channel, Retry after 3 seconds
原因是阿里云服务bug不兼容fabric
解决如下:
vim /usr/local/go/src/github.com/hyperledger/fabric-samples/first-network/base/docker-compose-base.yaml
在每个environment下面都追加下面的参数,然后重新up就搞定。
- GODEBUG=netdns=go