原文鏈接:超級賬本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-