用yum安装docker
yum install –y docker git
把服务设备成开机启动并启动服务
systemctl enable docker.service
systemctl start docker
验证 docker正确安装
docker run hello-world
运行的时候发现错误: net/http: TLS handshake timeout
查了下是说不用官方的dockhub,所以转为用国内的仓库daocloud
$ curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://6b8ef0e5.m.daocloud.io
$ sudo service docker restart
Compose 定位是 “定义和运行多个 Docker 容器的应用”
安装
curl -L https://get.daocloud.io/docker/compose/releases/download/1.11.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
赋权
chmod +x /usr/local/bin/docker-compose
查看docker-compose版本
docker-compose version
下载go1.8.linux-amd64.tar.gz
上传到/usr/local/src目录下
解压
cd /usr/local/src
tar zxvf go1.8.linux-amd64.tar.gz -C /usr/local/
添加golang到环境变量
export GOPATH=/usr/local/fabricGo
export GOROOT=/usr/local/go
export PATH=$GOROOT/bin:$PATH
拉取镜像
mkdir /opt/fabric
cd /opt/fabric/
docker pull yeasy/hyperledger-fabric:0.6-dp #拉取镜像
docker tag yeasy/hyperledger-fabric:0.6-dp hyperledger/fabric-peer:latest #给镜像加标记
docker tag yeasy/hyperledger-fabric:0.6-dp hyperledger/fabric-baseimage:latest
docker tag yeasy/hyperledger-fabric:0.6-dp hyperledger/fabric-membersrvc:latest
在当前目录建立docker-compose.yml,内容如下
membersrvc:
image: hyperledger/fabric-membersrvc
ports:
- "7054:7054"
command: membersrvc
vp0:
image: hyperledger/fabric-peer
ports:
- "7050:7050"
- "7051:7051"
- "7053:7053"
environment:
- CORE_PEER_ADDRESSAUTODETECT=true
- CORE_VM_ENDPOINT=unix:///var/run/docker.sock
- CORE_LOGGING_LEVEL=DEBUG
- CORE_PEER_ID=vp0
- CORE_PEER_PKI_ECA_PADDR=membersrvc:7054
- CORE_PEER_PKI_TCA_PADDR=membersrvc:7054
- CORE_PEER_PKI_TLSCA_PADDR=membersrvc:7054
- CORE_PEER_VALIDATOR_CONSENSUS_PLUGIN=noops
- CORE_SECURITY_ENABLED=true
- CORE_SECURITY_ENROLLID=test_vp0
- CORE_SECURITY_ENROLLSECRET=MwYpmSRjupbT
links:
- membersrvc
command: sh -c "sleep 5; peer node start --peer-chaincodedev"
启动Hyperledger Fabric
docker-compose up
查看已经运行的容器
docker ps
查看vp0的日志
docker-compose logs -f vp0
下载fabric源码,将源码放到$GOPATH/src/github.com/hyperledger
下
在fabric的源码下有测试例子,操作如下
cd $GOPATH/src/github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
go build ./chaincode_example02.go
export CORE_CHAINCODE_ID_NAME=mycc
export CORE_PEER_ADDRESS=0.0.0.0:7051
./chaincode_example02 &
直至出现“Received REGISTERED”字样,代表成功。
运行成功后用RESTClient测试结果
登录:
post请求 http://192.168.76.128:7050/registrar
body:
{
"enrollId": "jim",
"enrollSecret": "6avZQLwcUe9b"
}
部署:
post请求 http://192.168.76.128:7050/chaincode
body:
{
"jsonrpc": "2.0",
"method": "deploy",
"params": {
"type": 1,
"chaincodeID":{
"name": "mycc",
"path":"github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02"
},
"ctorMsg": {
"function":"init",
"args":["a", "1000", "b", "2000"]
},
"secureContext": "jim"
},
"id": 1
}
调用(a给b 100):
post请求 http://192.168.76.128:7050/chaincode
body:
{
"jsonrpc": "2.0",
"method": "invoke",
"params": {
"type": 1,
"chaincodeID":{
"name":"mycc"
},
"ctorMsg": {
"function":"invoke",
"args":["a", "b", "100"]
},
"secureContext": "jim"
},
"id": 3
}
查询:
post请求 http://192.168.76.128:7050/chaincode
body:
{
"jsonrpc": "2.0",
"method": "query",
"params": {
"type": 1,
"chaincodeID":{
"name":"mycc"
}, "ctorMsg": {
"function":"query",
"args":["a"]
},
"secureContext": "jim"
},
"id": 5
}
到这里搭建环境的测试结束