由于官方文档跟新很快,本文部分步骤已经过时,最新部署方式请移步这里:
部署到单组织的区块链上
部署到多组织的区块链上
Operating Systems: Ubuntu Linux 14.04 / 16.04 LTS (both 64-bit), or Mac OS 10.12
Docker Engine: Version 17.03 or higher
Docker-Compose: Version 1.8 or higher
Node: 8.9 or higher (note version 9 is not supported)
npm: v5.x
git: 2.9.x or higher
Python: 2.7.x
A code editor of your choice, we recommend VSCode.推荐 VSCode.
just copy and paste:
npm install -g composer-cli
npm install -g generator-hyperledger-composer
npm install -g composer-rest-server
npm install -g yo
下载fabric 工具(默认地址: ~/fabric-tools, 可以自定义):
mkdir ~/fabric-tools && cd ~/fabric-tools
curl -O https://raw.githubusercontent.com/hyperledger/composer-tools/master/packages/fabric-dev-servers/fabric-dev-servers.zip
unzip fabric-dev-servers.zip
声明使用1.0版本的fabric(默认hlfv1):
export FABRIC_VERSION=hlfv1
生成composer连接配置文件:
cd ~/fabric-tools
./downloadFabric.sh
./startFabric.sh
此时fabric网络已经开启了,配置文件也生成了。网络由1个ca+1个order+1个peer+1个couchdb组成:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
51453364229e hyperledger/fabric-peer:x86_64-1.0.0 "peer node start -..." 2 hours ago Up 2 hours 0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp peer0.org1.example.com
c769742bb485 hyperledger/fabric-couchdb:x86_64-1.0.0 "tini -- /docker-e..." 2 hours ago Up 2 hours 4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp couchdb
85a3c96fffd3 hyperledger/fabric-orderer:x86_64-1.0.0 "orderer" 2 hours ago Up 2 hours 0.0.0.0:7050->7050/tcp orderer.example.com
c8b231dd31ee hyperledger/fabric-ca:x86_64-1.0.0 "sh -c 'fabric-ca-..." 2 hours ago Up 2 hours 0.0.0.0:7054->7054/tcp ca.org1.example.com
.bna
文件这里借用下官方示例。
在你的工作目录下,clone官方示例:
git clone https://github.com/hyperledger/composer-sample-networks.git
复制其中一个demo到工作目录下:
cp -r ./composer-sample-networks/packages/basic-sample-network/ ./
对于.cto
.js
.acl
等文件,你可以按照自己的业务逻辑来修改。这里作为演示,就没修改。
生成bna文件:
cd basic-sample-network
npm install
运行结果:
......
Creating Business Network Archive
Looking for package.json of Business Network Definition
Input directory: /work/project/hyper/ttt
Found:
Description: The Hello World of Hyperledger Composer samples
Name: basic-sample-network
Identifier: basic-sample-network@0.1.6
Written Business Network Definition Archive file to
//*** bna路径
Output file: ./dist/basic-sample-network.bna
Command succeeded
.bna
文件cd dist/
composer network deploy -a basic-sample-network.bna -p hlfv1 -i PeerAdmin -s randomString
(等待一段时间)运行结果:
composer network ping -n basic-sample-network -p hlfv1 -i admin -s adminpw
运行结果:
此时查看网络:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8727288aa912 dev-peer0.org1.example.com-basic-sample-network-0.11.2 "chaincode -peer.a..." About an hour ago Up About an hour dev-peer0.org1.example.com-basic-sample-network-0.11.2
51453364229e hyperledger/fabric-peer:x86_64-1.0.0 "peer node start -..." 2 hours ago Up 2 hours 0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp peer0.org1.example.com
c769742bb485 hyperledger/fabric-couchdb:x86_64-1.0.0 "tini -- /docker-e..." 2 hours ago Up 2 hours 4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp couchdb
85a3c96fffd3 hyperledger/fabric-orderer:x86_64-1.0.0 "orderer" 2 hours ago Up 2 hours 0.0.0.0:7050->7050/tcp orderer.example.com
c8b231dd31ee hyperledger/fabric-ca:x86_64-1.0.0 "sh -c 'fabric-ca-..." 2 hours ago Up 2 hours 0.0.0.0:7054->7054/tcp ca.org1.example.com
注意:此时多了一个chaincode容器dev-peer0.org1.example.com-basic-sample-network-0.11.2
,可以推测composer在底层会把lib文件夹下的.js
文件编译成chaincode来部署。
进入到basic-sample-network
目录下:
composer-rest-server
运行效果(部分参数是由我们来指定的):
进入http://localhost:3000/explorer,查看效果:
接下来你可以愉悦对进行下面操作:
Assent: 增删改查
Participant:增删改查
Transaction: 提交交易
System: 身份管理以及历史交易记录查询
提示: 在移动端,web端 使用RESR API是比较好的选择,同时官方还提供了Composer Node SDK适用于Node.js应用。
.bna
文件业务需求会经常变更,因此我们需要修改现有的逻辑以及版本号。比如我们修改以下两个地方:
models/sample.cto
:
// 新增一个参与者
participant Teacher extends SampleParticipant{
}
package.json
:
// 修改版本号
"version": "1.0.0",
.bna
文件定位到项目根目录,执行npm run prepublish
:
cd dist/
composer network update -a basic-sample-network.bna -p hlfv1 -i PeerAdmin -s randomString
运行结果:
可以看到 多了一个 Teacher
相关的api,那么我们的业务网络更新成功了。