1. 环境安装
本教程不会详细解释 Hyperledger Fabric ,可访问其官方文档,
本文参照CHAINHERO,在Mac OS上实践,Hyperledger Fabric 框架与 Mac OS X、Windows 和其他 Linux 发行版兼容。
环境安装可参考fabric2.x环境搭建
请确保安装好了docker,go,git等环境
2.打造咱们的第一个区块链网络
为了docker构建区块链网络,我们将使用构建将处理不同角色的虚拟计算机。Hyperledger Fabric 需要大量证书来确保整个端到端流程(TSL、身份验证、签名块……)的加密。这些文件的创建需要一点时间,为了直接解决问题的核心,本文参考的教程已经fixtures在此存储库的文件夹中为准备好了所有这些。
按照我们的存储库命名,src在您的文件夹中创建一个新目录GOPATH:
mkdir -p $GOPATH/src/github.com/chainHero/heroes-service && \
cd $GOPATH/src/github.com/chainHero/heroes-service
要获取fixtures文件夹,您可以遵循此命令行,它将安装并使用 subversion 从该存储库中获取文件夹。或者从 Github下载zip 文件并仅提取fixtures文件夹。
brew install subversion
cd $GOPATH/src/github.com/chainHero/heroes-service
svn checkout https://github.com/chainHero/heroes-service/branches/v1.1.0/fixtures
rm -rf fixtures/.svn
注:如果是Linux
apt -y subversion && \
cd $GOPATH/src/github.com/chainHero/heroes-service && \
svn checkout https://github.com/chainHero/heroes-service/branches/v1.1.0/fixtures &&
rm -rf fixtures/.svn
为了检查网络是否工作,我们将使用docker-compose同时启动或停止所有容器。进入fixtures文件夹,然后运行:
cd $GOPATH/src/github.com/chainHero/heroes-service/fixtures && \
docker-compose up -d
此时能看到fabric网络已经启动
打开一个新终端运行:
docker ps
可以看到:两个对等节点,一个排序节点和一个 CA 容器。您已经成功地创建了一个新网络,可以与 SDK 一起使用。要停止网络返回上一个终端,按下Ctrl+C并等待所有容器停止。
-
提示:当网络停止时,所有使用的容器仍然可以访问。例如,这对于检查日志非常有用。您可以使用docker ps -a. 为了清理这些容器,您需要删除它们,docker rm $(docker ps -aq)或者如果您使用了一个docker-compose文件,请转到该文件所在的位置并运行docker-compose down.
-
提示:您可以docker-compose在后台运行该命令以保持提示。要做到这一点,使用参数-d,像这样:docker-compose up -d。要停止容器,请在所在的同一文件夹中运行docker-compose.yaml命令:(docker-compose stop或docker-compose down在所有容器停止后进行清理)。
3.使用Fabric SDK Go
3.1 配置文件
我们的应用程序需要很多参数,尤其是 Fabric 组件的地址来进行通信。我们将把所有东西都放在一个新的配置文件中,Fabric SDK Go 配置和我们的自定义参数。目前,我们只会尝试使 Fabric SDK Go 使用默认链码:
cd $GOPATH/src/github.com/chainHero/heroes-service && \
vi config.yaml
name: "heroes-service-network"
#
# Schema version of the content. Used by the SDK to apply the corresponding parsing rules.
#
version: 1.0.0
#
# The client section used by GO SDK.
#
client:
# Which organization does this application instance belong to? The value must be the name of an org
# defined under "organizations"
organization: org1
logging:
level: info
# Global configuration for peer, event service and orderer timeouts
# if this this section is omitted, then default values will be used (same values as below)
# peer:
# timeout:
# connection: 10s
# response: 180s
# discovery:
# # Expiry period for discovery service greylist filter
# # The channel client will greylist peers that are found to be offline
# # to prevent re-selecting them in subsequent retries.
# # This interval will define how long a peer is greylisted
# greylistExpiry: 10s
# eventService:
# # Event service type (optional). If not specified then the type is automatically
# # determined from channel capabilities.
# type: (deliver|eventhub)
# the below timeouts are commented out to use the default values that are found in
# "pkg/fab/endpointconfig.go"
# the client is free to override the defau