目标是跑通一个基本的 fabric-sdk-java
示例,需要先启动 fabric 的网络(使用 fabric-sample中的 fabcar示例),然后运行 java sdk 示例程序。
步骤
1. 启动示例网络
下载 fabric-sample
https://github.com/hyperledger/fabric-samples
我当前使用的是 release-1.1 分支。
如果还没有下载fabric相关docker镜像,需要运行:
fabric-samples/scripts/fabric-preload.sh
它会下载镜像,并设置好 tag。
然后运行:
fabric-samples/fabcar/startFabric.sh
会启动fabcar这个示例的fabric网络。
为了保证没有干扰,最好先关掉正在运行的容器和现有网络:
docker stop $(docker ps -qa)
docker rm $(docker ps -qa)
docker network prune
示例网络启动完成后,检查一下:
docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}"
看相关容器是否正常运行,我的结果如下:
CONTAINER ID NAMES PORTS
5201ea5a47b3 dev-peer0.org1.example.com-fabcar-1.0
01292cd01be3 cli
251b81ebbe9d peer0.org1.example.com 0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp
5b7db4b90f3c ca.example.com 0.0.0.0:7054->7054/tcp
a572618e2dc5 orderer.example.com 0.0.0.0:7050->7050/tcp
74461cc8ae42 couchdb 4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp
2. 运行示例程序
下载示例程序
git clone https://github.com/lkolisko/scratch.git tutorial
进入下载目录,运行java代码:
lkolisko.hyperledger.example.HFJavaSDKBasicExample
会打印一系列的结果信息,最后几行的信息:
----------------INBOUND--------------------
[id: 0xc7e45edf, L:/127.0.0.1:52197 - R:localhost/127.0.0.1:7051] HEADERS: streamId=3, headers=GrpcHttp2ResponseHeaders[grpc-status: 0, grpc-message: ], padding=0, endStream=true
------------------------------------
DEBUG Channel - Channel mychannel got back from peer peer0.org1.example.com status: 200, message: OK
INFO HFJavaSDKBasicExample - [{"Key":"CAR0", "Record":{"colour":"blue","make":"Toyota","model":"Prius","owner":"Tomoko"}},{"Key":"CAR1", "Record":{"colour":"red","make":"Ford","model":"Mustang","owner":"Brad"}},{"Key":"CAR2", "Record":{"colour":"green","make":"Hyundai","model":"Tucson","owner":"Jin Soo"}},{"Key":"CAR3", "Record":{"colour":"yellow","make":"Volkswagen","model":"Passat","owner":"Max"}},{"Key":"CAR4", "Record":{"colour":"black","make":"Tesla","model":"S","owner":"Adriana"}},{"Key":"CAR5", "Record":{"colour":"purple","make":"Peugeot","model":"205","owner":"Michel"}},{"Key":"CAR6", "Record":{"colour":"white","make":"Chery","model":"S22L","owner":"Aarav"}},{"Key":"CAR7", "Record":{"colour":"violet","make":"Fiat","model":"Punto","owner":"Pari"}},{"Key":"CAR8", "Record":{"colour":"indigo","make":"Tata","model":"Nano","owner":"Valeria"}},{"Key":"CAR9", "Record":{"colour":"brown","make":"Holden","model":"Barina","owner":"Shotaro"}}]