最近在学习fabric,中间遇到了好多坑。而由于fabric更新迭代版本比较快,网上的很多教程也都不安全适用了。我反正也在学习过程中,就尽力的把自己所学习到的东西都记录下来吧。希望能够帮助到需要的人。

本篇主要讲的是fabric-sdk-java的基础测试功能。 目前官方提供了三中比较稳定的sdk,分别是go,nodejs以及java。 博主尝试了几次go的sdk,由于才疏学浅,而且gopath太过奇葩,总是出现或多或少的问题,只好作罢。而b博主实在不想再多接触一门新的编程语言了,就想着看一看java的。 1.fabric的部署 首先是基础的fabric的安装与部署,就不在这篇文章里面讲解了。有时间我再写一篇文章详细介绍。 我在这里使用的是fabric v1.0.1版本,主要原因是官网说的java的sdk目前是1.0.1的。 部署好之后进入e2e_cli下载的是beta的镜像,会启动集成测试环境的时候出错。大体错误是

1
ERROR: for configtxlator  Cannot start service configtxlator: oci runtime error: container_linux.go:247: starting container process caused "exec: \"/usr/local/bin/configtxlator\": stat /usr/local/bin/configtxlator: no such file or directory"

[]官方网站给的解决办法是执行

1
export ORG_HYPERLEDGER_FABRIC_SDKTEST_VERSION="1.0.0"

我直接在命令行执行会出错,应该是写到docker-compose.yaml文件里面,我懒得测试了,反正说已经解决了,就不如下载新的镜像了。 在e2e_cli文件夹下面执行

1
bash download-dockerimages.sh -c x86_64-1.0.1 -f x86_64-1.0.1

下载1.0.1版的镜像,由于某种原因,下载比较慢,没办法。 主要就是这个容易出错的地方。 2.sdk的编译 由于fabric的jar已经添加到mvn仓库,正经开发的时候可以直接从maven下载了,不用看网上的编译使用的办法了。 直接在pom.xml里面添加个依赖就好了

1
2
3
4
5
<dependency>
    <groupId>org.hyperledger.fabric-sdk-java</groupId>
    <artifactId>fabric-sdk-java</artifactId>
    <version>1.0.1</version>
</dependency>

由于我们是为了熟悉其开发流程,所以自己下载生成工程项目。

1
git clone https://github.com/hyperledger/fabric-sdk-java.git

下载好了进入目录执行

1
mvn idea:idea

生成idea项目,就可以作为idea项目使用。

3.运行程序 首先进入到sdkintegration文件夹,执行

1
docker-compose up -d --force-recreate

运行出现

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Creating network "sdkintegration_default" with the default driver
Creating ca_peerOrg1
Creating ca_peerOrg2
Creating sdkintegration_ccenv_1
Creating configtxlator
Creating orderer.example.com
Creating peer0.org1.example.com
Creating peer0.org2.example.com
Creating peer1.org2.example.com
Creating peer1.org1.example.com

然后就代表执行成功了,在idea执行单元测试即可. rt,在idea中执行一个测试即可 fabric-java-test 比如,我是执行的End2endIT.java的setup函数,出现以下,运行成功

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
RUNNING: End2endIT.

2017-10-10 11:40:27,649 main WARN  SampleStore:90 - Could not find the file "/tmp/HFCSampletest.properties"
2017-10-10 11:40:27,655 main WARN  SampleStore:90 - Could not find the file "/tmp/HFCSampletest.properties"
Constructing channel foo
Created channel foo
2017-10-10 11:40:31,818 main INFO  Channel:569 - Peer peer0.org1.example.com joined into channel foo
Peer peer0.org1.example.com joined channel foo
2017-10-10 11:40:32,049 main INFO  Channel:569 - Peer peer1.org1.example.com joined into channel foo
Peer peer1.org1.example.com joined channel foo
Finished initialization channel foo
Running channel foo
Creating install proposal
Sending install proposal
............
current block number 0 has 1 envelope count:
  Transaction number 1 has transaction id: 
  Transaction number 1 has channel id: bar
  Transaction number 1 has epoch: 0
  Transaction number 1 has transaction timestamp: 十月 10,  2017  19:40:41 下午
  Transaction number 1 has type id: ENVELOPE
That's all folks!

就运行好了,可以进行后续的开发