BlockChain联盟链中的Hyperledger项目中的Fabrica项目是商业联盟应用区块链的基石之作,现在来搭建一个运行环境,进行技术验证。
安装组件如下:
1.Docker-compose:Docker 容器管理;
2.Go lang SDK:Go 语言开发、编译环境;
3.Git:git 镜像克隆与提交;
4.Rest Client: rest API 测试;
容器主要运行有:MemberSrv 和Peer;
一、HyperLedger Fabric环境资源准备
1、docker的安装
a)检查版本
$ uname –r
3.10.0-229.el7.x86_64
b)更新Centos
$ sudo yum update
c)安装需要软件
yum install –y docker git
d)把服务设备成开机启动并启动服务
systemctl enable docker.service
systemctl start docker
e)验证 docker
正确安装(通过运行一个测试镜像,当容器运行时,它打印一个信息性消息并退出。)
docker run hello-world
2、Docker Compose安装
Compose 定位是 “定义和运行多个 Docker 容器的应用”,可以很方便的从官方拉下来一些镜像,在此基础上再进行改造。它允许用户通过一个单独的 docker-compose.yml 模板文件(YAML 格式)来定义一组相关联的应用容器为一个项目(project),即:你可以通过docker-compose.yml 模板文件来定义你的多个容器之间的网络连接、端口,容器服务器名称、服务器中运行的镜像、服务器的环境配置、启动命令等,使用的镜像,可以手动拉取镜像,也可以自动以docker-compose.yml 构建环境后自动拉取。
a)docker-compose.yml文件说明:
------------------------------------------------------------------------------------------------------------------------------
version: '2' #使用yml文件版本2
membersrvc: #定义第一台虚拟机名称为membersrvc
image: hyperledger/fabric-membersrvc #指定镜像来源
ports: #暴露端口信息, 使用 宿主:容器 格式,
- "7054:7054"
command: membersrvc #覆盖容器启动后默认执行的命令
vp0: #定义第二台虚拟机名称为vp0
image: hyperledger/fabric-peer
ports:
- "7050:7050" #REST 服务端口
- "7051:7051" #peer gRPC 服务监听端口
- "7053:7053" #:peer 事件服务端口
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 # 定义了一致性机制使用noops
- CORE_SECURITY_ENABLED=true #启用安全模式
- CORE_SECURITY_ENROLLID=test_vp0
- CORE_SECURITY_ENROLLSECRET=MwYpmSRjupbT
volumes: #数据卷所挂载路径设置。可以设置宿主机路径( HOST:CONTAINER ),该指令中路径支持相对路径
- /var/run/:/var/run/
links: #链接到其它服务中的容器
- membersrvc
command: #覆盖容器启动后默认执行的命令
sh -c "sleep 5; peer node start --peer-chaincodedev"
------------------------------------------------------------------------------------------------------------------------------
b).安装容器Compose(使用官方提供的方法,安装1.10.1版本):
curl -L https://github.com/docker/compose/releases/download/1.10.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose version
二、部署fabric noops模式单结点开发环境
1、手动部署
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 run --name=vp0 \
--restart=unless-stopped \
-it \
-p 7050:7050 \
-p 7051:7051 \
-v /var/run/docker.sock:/var/run/docker.sock \
-e CORE_PEER_ID=vp0 \
-e CORE_PEER_ADDRESSAUTODETECT=true \
-e CORE_NOOPS_BLOCK_WAIT=10 \
hyperledger/fabric-peer:latest peer node start
ok!一个简单的单结点peer部署完成(CTRL+Z退出,程序仍然在运行)。
2、可以做些常用操作:
docker ps #查看已经运行的容器
docker-compose logs -f vp0 #查看vp0的日志
docker-compose exec vp0 bash #进入到vp0执行它的bash,exit命令可以退出。
三、运行与发布Chaincode
进入vp0的bash,运行如下命令
docker-compose exec vp0 bash
peer chaincode deploy -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 -c '{"Function":"init", "Args": ["a","100", "b", "200"]}'
(参数注释 -p:指定chaincode源码路径;-c:要执行的JSON格式shaincode代码)
如下图红框中返回了chaincode的名称hash值,就表示区域链已经建立成功了!下面可以通过CLI或POST方式进行测试与验证。
----------------------------------------------------------手动编译代码参考 --------------------------------------------------------------------
a)编译代码
cd $GOPATH/src/github.com/hyperledger/fabric/examples/chaincode/go
cp chaincode_example02/chaincode_example02.go $GOPATH/src/ #复制目录里面的例2到$GOPATH/src/ 进行编译
cd $GOPATH/src/
go build ./chaincode_example02.go #编译成功会在GOPATH/src目录生成chaincode_example02文件
(chaincode_example02.go 源码链接https://raw.githubusercontent.com/hyperledger/fabric/master/examples/chaincode/go/chaincode_example02/chaincode_example02.go)
b)发布chaincode在GOPATH/src 下运行
CORE_CHAINCODE_ID_NAME=mycc CORE_PEER_ADDRESS=0.0.0.0:7051 ./chaincode_example02
直至出现“Received REGISTERED”字样,代表成功。
使用exit命令或Ctrl+z退出终端。
----------------------------------------------------------手动编译代码--------------------------------------------------------------------
四、Fabric开发环境测试及验证
4、firefox中安装POST接口测试工具:
1)、在firefox的附加组件中下载Restclient插件安装
重启firefox后,打开RESTClient,进行POST测试
2)、系统环境测试:deploy(部署),invoke(调用),query(查询) chaincode(智能合约)(vp0中peer的用户名和密码在 membersrvc.yaml 文件里为jim: 6avZQLwcUe9b),在非安全模式下“CORE_SECURITY_ENABLED=true CORE_SECURITY_PRIVACY=true ”在CLI模式下不需要加
已经建成的环境架构,目前Farbric Peer只有一个vp0
a)进入vp0容器
docker-compose exec vp0 bash
b)login
CLI方式:
peer network login jim
输入密码:6avZQLwcUe9b
Rest方式(在Body中放入如下代码):
{
"enrollId": "jim",
"enrollSecret": "6avZQLwcUe9b"
}
返回用户已登录。
c)deploy(部署)
CLI方式:
CORE_SECURITY_ENABLED=true CORE_SECURITY_PRIVACY=true peer chaincode deploy -u jim -n mycc -c '{"Args": ["init", "a","100", "b", "200"]}'
Rest方式(POST HOST:7050/chaincode,在Body中放入如下代码):
{
"jsonrpc": "2.0",
"method": "deploy",
"params": {
"type": 1,
"chaincodeID":{
"path":"github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02"
},
"ctorMsg": {
"function":"init",
"args":["a", "1000", "b", "2000"]
},
"secureContext": "jim"
},
"id": 1
}
d)Invoke(调用)
CLI方式:
CORE_SECURITY_ENABLED=true CORE_SECURITY_PRIVACY=true peer chaincode invoke -u jim -l golang -n mycc -c '{"Args": ["invoke", "a", "b", "10"]}'
Rest方式(POST HOST:7050/chaincode,在Body中放入如下代码):
{
"jsonrpc": "2.0",
"method": "invoke",
"params": {
"type": 1,
"chaincodeID":{
"name":"980d4bb7f69578592e5775a6da86d81a221887817d7164d3e9d4d4df1c981440abf9a61417eaf8ad6f7fc79893da36de2cf4709131e9af39bca6ebc2e5a1cd9d" }, #980d4bb7f69…cd9来自chaincode的名称hash值
"ctorMsg": {
"function":"invoke",
"args":["a", "b", "100"]
},
"secureContext": "jim"
},
"id": 3
}
e)query(查询)
CLI方式:
CORE_SECURITY_ENABLED=true CORE_SECURITY_PRIVACY=true peer chaincode query -u jim -l golang -n mycc -c '{"Function": "query", "Args": ["b"]}'
Rest方式(POST HOST:7050/chaincode,在Body中放入如下代码):
{
"jsonrpc": "2.0",
"method": "query",
"params": {
"type": 1,
"chaincodeID":{
"name":"980d4bb7f69578592e5775a6da86d81a221887817d7164d3e9d4d4df1c981440abf9a61417eaf8ad6f7fc79893da36de2cf4709131e9af39bca6ebc2e5a1cd9d" }, "ctorMsg": {
"function":"query",
"args":["a"]
},
"secureContext": "jim"
},
"id": 5
}
f)区域信息查询
Rest方式(GET HOST:7050/chain/blocks/1):
HOST:7050/chain/blocks/0 是创世区块
HOST:7050/chain/blocks/1 第一个区块,是depoy部署时建立的
HOST:7050/chain/blocks/2 第二个区块,是invoke调用交易时建立的
query查询命令不会创建新的区块。
五、部署PBFT 模式Fabric开发环境
在FabricV0.6版本架构中(如下图中),共识算法包括 noops、pbft 两类,使用Noops模式,因其默认没有采用 consensus 机制,1 个节点即可,可以用来进行快速测试或开发。而PBFT 是经典的分布式一致性算法,也是 hyperledger 目前最推荐的算法,该算法至少需要 4个节点。
a)停止并删除原来的容器及镜像
docker stop
docker rm -f $(docker ps -aq) #强制删除全部容器
docker rmi -f $(docker images -q) #强制删除全部镜像
b)下载 镜像 文件
cd /opt/fabric/
docker pull yeasy/blockchain-explorer:latest
docker pull yeasy/hyperledger:latest
docker tag yeasy/hyperledger:latest hyperledger/fabric-baseimage:latest
docker pull yeasy/hyperledger-peer:latest
docker pull yeasy/hyperledger-membersrvc:latest
b)下载 compose 文件
git clone https://github.com/yeasy/docker-compose-files
cd /opt/fabric/docker-compose-files/hyperledger/0.6/pbft/
docker-compose -f 4-peers-with-membersrvc-explorer.yml up
因为4-peers-with-membersrvc-explorer.yml 中启用了权限管理,所以进入Chaincode需要登录,例如以内置账户 jim 账户登录。
c)登陆用户,
docker exec -it pbft_vp0_1 bash
peer network login jim
登陆密码:6avZQLwcUe9b
登录之后,chaincode 的部署、调用等操作与之前类似,只是需要通过 -u 选项来指定用户名。
例如chaincode 部署(在 vp0 上执行命令):
peer chaincode deploy -u jim -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 -c '{"Function":"init", "Args": ["a","100", "b", "200"]}'
附:4-peers-with-membersrvc-explorer.yml、explorer.yml、membersrvc.yml、peer.yml内容
4-peers-with-membersrvc-explorer.yml内容如下:
------------------------------------------------------------------------------------------------------------------------------
# This compose file will start 4 hyperledger peer nodes + 1 CA node.
# We add a delay to start the VP nodes after CA node:
# command: sh -c "sleep 5; peer node start"
#
# vp0: validating node as root
# vp1: validating node as peer
# vp2: validating node as peer
# vp3: validating node as peer
# https://github.com/yeasy/docker-compose-files
version: '2'
services:
# blockchain-explorer 这个浏览器可以图形化的验证Fabric的工作环境
explorer:
extends:
file: explorer.yml #基于其它模板文件进行扩展
service: explorer
hostname: explorer
environment:
- HYP_REST_ENDPOINT=http://vp0:7050
ports:
- "9090:9090"
membersrvc0:
extends:
file: membersrvc.yml #基于其它模板文件进行扩展
service: membersrvc
hostname: membersrvc
environment:
- CORE_LOGGING_SERVER=debug
ports:
- "7054:7054"
command: membersrvc # this repeats the default cmd to make sure
# vp0 will be used for client interactive operations
# If you want to run fabric command on the host, then map 7051:7051 to host
# port, or use like `CORE_PEER_ADDRESS=172.17.0.2:7051` to specify peer addr.
vp0: # validating node as the root
extends:
file: peer.yml #基于其它模板文件进行扩展
service: vp_sec
hostname: vp0
environment:
- CORE_PEER_ID=vp0
- CORE_SECURITY_ENROLLID=test_vp0
- CORE_SECURITY_ENROLLSECRET=MwYpmSRjupbT
ports:
- "7050:7050"
- "7051:7051"
links:
- membersrvc0
command: sh -c "sleep 10; peer node start"
vp1:
extends:
file: peer.yml #基于其它模板文件进行扩展
service: vp_sec
hostname: vp1
environment:
- CORE_PEER_ID=vp1
- CORE_PEER_DISCOVERY_ROOTNODE=vp0:7051
- CORE_SECURITY_ENROLLID=test_vp1
- CORE_SECURITY_ENROLLSECRET=5wgHK9qqYaPy
links:
- vp0
- membersrvc0
command: sh -c "sleep 10; peer node start"
vp2:
extends:
file: peer.yml #基于其它模板文件进行扩展
service: vp_sec
hostname: vp2
environment:
- CORE_PEER_ID=vp2
- CORE_PEER_DISCOVERY_ROOTNODE=vp0:7051
- CORE_SECURITY_ENROLLID=test_vp2
- CORE_SECURITY_ENROLLSECRET=vQelbRvja7cJ
links:
- vp0
- membersrvc0
command: sh -c "sleep 10; peer node start"
vp3:
extends:
file: peer.yml #基于其它模板文件进行扩展
service: vp_sec
hostname: vp3
environment:
- CORE_PEER_ID=vp3
- CORE_PEER_DISCOVERY_ROOTNODE=vp0:7051
- CORE_SECURITY_ENROLLID=test_vp3
- CORE_SECURITY_ENROLLSECRET=9LKqKH5peurL
links:
- vp0
- membersrvc0
command: sh -c "sleep 10; peer node start"
#networks:
# default:
# external:
# name: fabric_pbft
------------------------------------------------------------------------------------------------------------------------------
explorer.yml
------------------------------------------------------------------------------------------------------------------------------
# This is the default base file for blockchain-explorer
# https://github.com/yeasy/docker-compose-files
# Depends on the yeasy/blockchain-explorer image.
version: '2'
services:
explorer:
image: yeasy/blockchain-explorer:latest
restart: unless-stopped
expose:
- "9090"
------------------------------------------------------------------------------------------------------------------------------
membersrvc.yml
------------------------------------------------------------------------------------------------------------------------------
# This is the default base file to for fabric membersrvc
# https://github.com/yeasy/docker-compose-files/
# Depends on the hyperledger/fabric-membersrvc image.
version: '2'
services:
membersrvc:
image: hyperledger/fabric-membersrvc:latest
#image: yeasy/hyperledger-fabric-membersrvc:0.6-dp
#image: hyperledger/fabric-membersrvc:x86_64-0.6.1-preview
#image: ibmblockchain/fabric-membersrvc:x86_64-0.6.1-preview
restart: unless-stopped
expose:
- "7054"
volumes: # docker.sock is mapped as the default CORE_VM_ENDPOINT
- /var/run/docker.sock:/var/run/docker.sock
command: membersrvc # this repeats the default cmd to make sure
------------------------------------------------------------------------------------------------------------------------------
peer.yml
------------------------------------------------------------------------------------------------------------------------------
# This is the default base file to config env and command
# Notice that chaincode is executed inside docker in default net mode
# https://github.com/yeasy/docker-compose-files
# Depends on the hyperledger/fabric-peer image.
# This utilized the pbft as consensus
# See https://github.com/hyperledger/fabric/blob/master/docs/dev-setup/devnet-setup.md#using-consensus-plugin for more details.
version: '2'
services:
vp: # vp node no needing membersrvc
image: hyperledger/fabric-peer:latest
#image: yeasy/hyperledger-fabric-peer:0.6-dp
#image: hyperledger/fabric-peer:x86_64-0.6.1-preview
#image: ibmblockchain/fabric-peer:x86_64-0.6.1-preview
restart: unless-stopped
environment:
- CORE_PEER_ADDRESSAUTODETECT=true
- CORE_PEER_NETWORKID=dev
- CORE_LOGGING_LEVEL=info #critical, error, warning, notice, info, debug
#- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=fabric_pbft # uncomment this to use specific network
# The following section enables pbft consensus
- CORE_PEER_VALIDATOR_CONSENSUS_PLUGIN=pbft # noops, pbft
- CORE_PBFT_GENERAL_MODE=batch # batch. classic and sieve will be not supported again.
- CORE_PBFT_GENERAL_N=4
- CORE_PBFT_GENERAL_BATCHSIZE=2 # only useful when in batch mode
- CORE_PBFT_GENERAL_TIMEOUT_REQUEST=2s
expose:
- "7050" # Rest
- "7051" # Grpc
- "7052" # Peer CLI
- "7053" # Peer Event
- "7054" # eCAP
- "7055" # eCAA
- "7056" # tCAP
- "7057" # eCAA
- "7058" # tlsCAP
- "7059" # tlsCAA
volumes: # docker.sock is mapped as the default CORE_VM_ENDPOINT
- /var/run/docker.sock:/var/run/docker.sock
command: peer node start # this repeats the default cmd to make sure
# command: sh -c "sleep 5; peer node start" # peer node should have booting delay with CA
vp_sec: # vp node needing membersrvc suppot
extends:
service: vp
environment:
- CORE_SECURITY_ENABLED=true
- CORE_PEER_PKI_ECA_PADDR=membersrvc0:7054
- CORE_PEER_PKI_TCA_PADDR=membersrvc0:7054
- CORE_PEER_PKI_TLSCA_PADDR=membersrvc0:7054
------------------------------------------------------------------------------------------------------------------------------