【链块技术56期】超级账本Fabric教程(四):使用docker编译fabric源码(上)
原文链接:超级账本Fabric教程(四):使用docker编译fabric源码(上)本文介绍如何在docker镜像中编译fabric源码,方便修改代码后快速测试。 一、前言 本文档的搭建流程基于如下环境:操作系统:unbuntu16.04 二、实验前准备 在搭建fabric网络之前先要安装一些必要的软件 2.1 安装gitsudo apt-ge...
原文链接:超级账本Fabric教程(四):使用docker编译fabric源码(上)
本文介绍如何在docker镜像中编译fabric源码,方便修改代码后快速测试。
一、前言
本文档的搭建流程基于如下环境:
操作系统:unbuntu16.04
二、实验前准备
在搭建fabric网络之前先要安装一些必要的软件
2.1 安装git
sudo apt-get install git
2.2 安装git
sudo apt-get install curl
2.3 安装go
下载go二进制包:https://studygolang.com/dl/golang/go1.9.2.linux-amd64.tar.gz
sudo tar -C /usr/local -zvxf go1.9.2.linux-amd64.tar.gz mkdir -p ~/go //设置GOPATH环境变量 echo "export GOPATH=$HOME/go" >> ~/.bashrc //将go的可执行文件所在目录加入到PATH环境变量中 echo "export PATH=$PATH:$HOME/go/bin:/usr/local/go/bin" >> ~/.bashrc //使得环境变量修改立即生效 source ~/.bashrc //验证go是否安装成功 go version
2.4 安装nodejs
必须要安装8.9.x或者更高版本 下载二进制包: https://nodejs.org/dist/latest-v10.x/node-v10.13.0-linux-x64.tar.gz
//解压二进制包 tar zvxf node-v10.13.0-linux-x64.tar.gz -C //将可执行文件目录加入环境变量 echo "export PATH=$PATH:$HOME/node-v10.13.0-linux-x64/bin" >> ~/.bashrc //验证是否安装成功 node -v npm -v
2.5 安装docker
必须安装17.06.2-ce或以上版本
切换到下载好的安装包路径:
sudo dpkg -i docker-ce_18.06.1~ce~3-0~ubuntu_amd64.deb
验证是否安装成功:
sudo docker run hello-world
2.6 安装docker-compose
必须安装1.14.0或以上版本
下载并安装
sudo curl -L https://github.com/docker/compose/releases/download/1.14.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
修改执行文件的执行权限
sudo chmod +x /usr/local/bin/docker-compose
验证是否安装成功
docker-compose -v
三、超级账本源代码下载
下载源代码
mkdir -p ~/go/src/github.com/hyperledger cd ~/go/src/github.com/hyperledger git clone https://github.com/hyperledger/fabric.git
将源码版本切换到v1.0.0
git checkout v1.0.0
修改peer模块的main函数,加入一行打印,后面启动peer的过程中可以测试我们自己编译的代码是否生效
func main() {
fmt.Println("----------Welcome to my-basic-network----------")
// For environment variables.
viper.SetEnvPrefix(cmdRoot)
viper.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
......
四、下载yeasy/hyperledger-fabric:1.0.0镜像
yeasy/hyperledger-fabric镜像是基于golang镜像构建,给我们安装好了编译Fabric的环境依赖、gotools等,我们可以直接在这个镜像里面编译fabric源码。
docker pull yeasy/hyperledger-fabric:1.0.0
五、编译fabric源码
创建工程目录,并切换到工程目录下
mkdir ~/my-basic-network cd ~/my-basic-network
在my-basic-network下创建一个名为docker-compose-compile.yaml的配置文件
version: '2' services: compile: container_name: compile image: yeasy/hyperledger-fabric:1.0.0 command: /bin/bash -c 'sleep 10000000' working_dir: /go/src/github.com/hyperledger/fabric volumes: - ~/go/bin:/go/bin - ~/go/src/github.com/hyperledger/fabric:/go/src/github.com/hyperledger/fabric
六、启动容器
//如果启动容器由于权限问题失败, 请参考本文章最后的“没有权限执行dock命令解决办法” docker-compose -f docker-compose-compile.yaml up -d
进入容器交互式命令行,编译peer和orderer模块
docker exec -it compile bash #进入peer模块子目录 root@99edaa3a60c5:/go/src/github.com/hyperledger/fabric# cd peer/ #编译peer模块 root@99edaa3a60c5:/go/src/github.com/hyperledger/fabric/peer# go build #安装编译后的可执行文件 root@99edaa3a60c5:/go/src/github.com/hyperledger/fabric/peer# go install #进入orderer模块子目录 root@99edaa3a60c5:/go/src/github.com/hyperledger/fabric/peer# cd ../orderer/ #编译orderer模块 root@99edaa3a60c5:/go/src/github.com/hyperledger/fabric/orderer# go build #安装orderer模块 root@99edaa3a60c5:/go/src/github.com/hyperledger/fabric/orderer# go install
编译完成后,在镜像的/go/bin目录下看到新生成的peer和orderer可执行程序,并且在本机~/go/bin目录下也可以看到编译生成的peer和orderer可执行文件。
我们在本地修改代码后,镜像当中可以立即看到被修改内容,只需要在镜像中重新编译就可以使修改后的代码生效。
七、搭建测试网络
我们搭建一个只有一个Orderer和一个Peer的简单网络。需要编译生成两个工具:
1. cryptogen生成加密证书
2. configtxgen生成交易配置
这两个工具在fabric源代码目录分别为:
-
~/go/src/github.com/hyperledger/fabric/common/tools/cryptogen
-
~/go/src/github.com/hyperledger/fabric/common/configtx/tool/configtxgen
在主机上分别进入其中执行go build,go install
如果在执行过程中遇到如下错误
# github.com/hyperledger/fabric/vendor/github.com/miekg/pkcs11 ../../../vendor/github.com/miekg/pkcs11/pkcs11.go:29:18: fatal error: ltdl.h: No such file or directory compilation terminated.:
需要安装libltdl-dev库解决
sudo apt-get install libltdl-dev
7.1 生成机构加密证书的配置文件
在主机的工程目录my-basic-network下新建crypto-config.yaml这是一个生成机构加密证书的配置文件,具体内容如下:
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
# ---------------------------------------------------------------------------
# "OrdererOrgs" - Definition of organizations managing orderer nodes
# ---------------------------------------------------------------------------
OrdererOrgs:
# ---------------------------------------------------------------------------
# Orderer
# ---------------------------------------------------------------------------
- Name: Orderer
Domain: example.com
# ---------------------------------------------------------------------------
# "Specs" - See PeerOrgs below for complete description
# ---------------------------------------------------------------------------
Specs:
- Hostname: orderer
# ---------------------------------------------------------------------------
# "PeerOrgs" - Definition of organizations managing peer nodes
# ---------------------------------------------------------------------------
PeerOrgs:
# ---------------------------------------------------------------------------
# Org1
# ---------------------------------------------------------------------------
- Name: Org1
Domain: org1.example.com
# ---------------------------------------------------------------------------
# "Specs"
# ---------------------------------------------------------------------------
# Uncomment this section to enable the explicit definition of hosts in your
# configuration. Most users will want to use Template, below
#
# Specs is an array of Spec entries. Each Spec entry consists of two fields:
# - Hostname: (Required) The desired hostname, sans the domain.
# - CommonName: (Optional) Specifies the template or explicit override for
# the CN. By default, this is the template:
#
# "{{.Hostname}}.{{.Domain}}"
#
# which obtains its values from the Spec.Hostname and
# Org.Domain, respectively.
# ---------------------------------------------------------------------------
# Specs:
# - Hostname: foo # implicitly "foo.org1.example.com"
# CommonName: foo27.org5.example.com # overrides Hostname-based FQDN set above
# - Hostname: bar
# - Hostname: baz
# ---------------------------------------------------------------------------
# "Template"
# ---------------------------------------------------------------------------
# Allows for the definition of 1 or more hosts that are created sequentially
# from a template. By default, this looks like "peer%d" from 0 to Count-1.
# You may override the number of nodes (Count), the starting index (Start)
# or the template used to construct the name (Hostname).
#
# Note: Template and Specs are not mutually exclusive. You may define both
# sections and the aggregate nodes will be created for you. Take care with
# name collisions
# ---------------------------------------------------------------------------
Template:
Count: 1
# Start: 5
# Hostname: {{.Prefix}}{{.Index}} # default
# ---------------------------------------------------------------------------
# "Users"
# ---------------------------------------------------------------------------
# Count: The number of user accounts _in addition_ to Admin
# ---------------------------------------------------------------------------
Users:
Count: 1
使用~/go/bin/cryptogen 工具来生成证书:
在主机上my-basic-network工程目录下执行如下命令,由于我们之前已经把~/go/bin加入到PATH环境变量,所有不需要指定绝对路径,直接使用cryptogen命令
cd ~/my-basic-network/ cryptogen generate --config=./crypto-config.yaml
执行命令之后,在当前目录下可以看到生成了一个crypto-config文件夹,里面包含两个文件夹:
ordererOrganizations peerOrganizations
-END-



魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐

所有评论(0)