在Ubuntu 18.04上搭建HyperLedger Fabric 1.2.0 環境

來源:互聯網
上載者:User

00 導言

      由 Linux 基金會發起建立的開源區塊鏈分散式總帳 —— Hyperledger Fabric,已經迎來了 1.2.0 版本。該項目著重於效能和可靠性,以及推動區塊鏈和分散式總帳技術的跨行業協作。Hyperledger Fabric 可用於全球供應鏈管理、金融交易、資產賬和去中心化的社交網路等情境,但無意以此來構建一種加密貨幣。

      下面我們將帶著大家一步一步來搭建自己的HyperLedger Fabric開發環境。老司機已經發車,趕緊跟上。。。

01 作業系統準備

      我們選擇了ubuntu18.04這個版本。具體怎麼安裝請參考我們之前的文章 《在VirtualBox裡安裝用於區塊鏈開發的Ubuntu作業系統》

02 安裝curl

      後面的步驟我們需要用到curl命令來下載安裝指令碼和其他檔案,所以需要先安裝curl,如果已經安裝可略過。
      第1步: 安裝curl,如果已經安裝可略過

$ sudo apt install curl

03 安裝docker-ce

      docker分兩個怎麼,分為docker-ce(社區版)和docker-ee(企業版)。作為窮人階級代表的我,只能無奈選擇使用社區版。在ubuntu作業系統上安裝docker-ce,實際上docker官方提供了三種方式。

      1. 通過docker倉庫安裝

      2. 下載.deb包安裝

      3. 通過安裝指令碼安裝

      在這裡我們選擇第3種方式進行安裝。

      第1步: 使用curl下載安裝指令碼

$ curl -fsSL get.docker.com -o get-docker.sh$ ls get*get-docker.sh

      第2步: 執行指令碼進行安裝,這一步需要等待一陣時間。

$ sudo sh get-docker.sh

      第3步: 設定成非root使用者也能執行docker,需要將普通使用者加入docker組,我們當前的使用者叫blockchain。

$ sudo usermod -aG docker blockchain

      第4步: 執行hello-world鏡像,驗證docker是否安裝成功。如果看到 Hello from Docker! This message shows that your installation appears to be working 這句話表明安裝成功了。

$ sudo docker run hello-world...... 一堆提示資訊Hello from Docker!This message shows that your installation appears to be working correctly......一堆提示資訊

04 安裝docker-compose

      Compose 是 Docker 容器進行編排的工具,定義和運行多容器的應用,可以一條命令啟動多個容器。HyperLedger Fabric需要用到docker-compose,所以我們提前安裝。

      第1步: 下載某個版本的docker-compose,下載到/usr/local/bin/docker-compose目錄。更多版本請訪問:https://github.com/docker/compose/releases

$ sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose

      這裡解釋一下: uname -s 得到Linux,uname -m 得到x86_64。可在複製到命令列執行查看。

      第2步:允許其他使用者執行compose相關命令

$ sudo chmod +x /usr/local/bin/docker-compose

      第3步:驗證安裝是否成功

$ docker-compose --versiondocker-compose version 1.21.2, build a133471

05 安裝go

      第1步:下載golang的tar包

$ wget https://storage.googleapis.com/golang/go1.10.1.linux-amd64.tar.gz

      由於眾所周知的原因,google相應的網站被牆訪問不了(兄弟,接受現實吧),我們得想其他辦法。推薦從第三方鏡像源下載golang的tar包,推薦鏡像源 https://studygolang.com/dl,用瀏覽器開啟該網址,下載linux的tar包。

      第2步:解壓tar包到/usr/local

$ cd 下載$ sudo tar -C /usr/local -xzf go1.10.3.linux-amd64.tar.gz

      第3步:建立go目錄

$ mkdir $HOME/go

      第4步:用vi開啟~./bashrc,配置環境變數

$ vi ~/.bashrc

      增加下面的環境變數,儲存退出

export GOROOT=/usr/local/goexport GOBIN=$GOROOT/binexport PATH=$PATH:$GOBINexport GOPATH=$HOME/go

      使環境變數立即生效

$ source ~/.bashrc

      第5步:檢測go是否安裝好

$ go versiongo version go1.10.3 linux/amd64

06 安裝並檢測git

$ sudo apt install git$ git version

06 下載fabric的引導指令碼

      先建立放置的目錄,然後進入該目錄,用curl下載指令碼

$ cd ~$ mkdir hyperledger-fabric$ cd hyperledger-fabric$ curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/release-1.2/scripts/bootstrap.sh -o bootstrap.sh$ lsbootstrap.sh

07 執行引導指令碼

      這一步通過運行指令碼去安裝fabric,並且會下載fabric-sample,而且還會摘取docker鏡像。下載過程需要耗費比較長的時間。

$ cd ~/hyperledger-fabric/sudo bash ./bootstrap.sh 1.2.0 1.2.0 0.4.10

      下載完,可以執行docker images查看已經下載的docker相關鏡像

$ sudo docker images | grep hyperledger*hyperledger/fabric-ca          1.2.0               66cc132bd09c        2 weeks ago         252MBhyperledger/fabric-ca          latest              66cc132bd09c        2 weeks ago         252MBhyperledger/fabric-tools       1.2.0               379602873003        2 weeks ago         1.51GBhyperledger/fabric-tools       latest              379602873003        2 weeks ago         1.51GBhyperledger/fabric-ccenv       1.2.0               6acf31e2d9a4        2 weeks ago         1.43GBhyperledger/fabric-ccenv       latest              6acf31e2d9a4        2 weeks ago         1.43GBhyperledger/fabric-orderer     1.2.0               4baf7789a8ec        2 weeks ago         152MBhyperledger/fabric-orderer     latest              4baf7789a8ec        2 weeks ago         152MBhyperledger/fabric-peer        1.2.0               82c262e65984        2 weeks ago         159MBhyperledger/fabric-peer        latest              82c262e65984        2 weeks ago         159MBhyperledger/fabric-zookeeper   0.4.10              2b51158f3898        3 weeks ago         1.44GBhyperledger/fabric-zookeeper   latest              2b51158f3898        3 weeks ago         1.44GBhyperledger/fabric-kafka       0.4.10              936aef6db0e6        3 weeks ago         1.45GBhyperledger/fabric-kafka       latest              936aef6db0e6        3 weeks ago         1.45GBhyperledger/fabric-couchdb     0.4.10              3092eca241fc        3 weeks ago         1.61GBhyperledger/fabric-couchdb     latest              3092eca241fc        3 weeks ago         1.61GBhyperledger/fabric-ca          x86_64-1.0.4        8e691b3509bf        8 months ago        238MBhyperledger/fabric-tools       x86_64-1.0.4        6051774928a6        8 months ago        1.33GBhyperledger/fabric-orderer     x86_64-1.0.4        b17741e7b036        8 months ago        151MBhyperledger/fabric-peer        x86_64-1.0.4        1ce935adc397        8 months ago        154MBhyperledger/fabric-javaenv     latest              a517b70135c7        8 months ago        1.41GBhyperledger/fabric-javaenv     x86_64-1.0.4        a517b70135c7        8 months ago        1.41GBhyperledger/fabric-ccenv       x86_64-1.0.4        856061b1fed7        8 months ago        1.28GB

08 添加環境變數

      用vi開啟~./bashrc

$ vi ~/.bashrc

      增加下面的環境變數,儲存退出

export PATH=/home/blockchain/hyperledger-fabric/fabric-samples/bin:$PATH

      使環境變數立即生效

$ source ~/.bashrc

      檢驗環境變數是否成功

$ fabric-ca-client versionfabric-ca-client: Version: 1.2.0 Go version: go1.10 OS/Arch: linux/amd64

09 建立和管理網路

      第1步:執行 ./byfn.sh generate 命令產生相應的檔案

$ cd ~/hyperledger-fabric/fabric-samples/first-network/$ ./byfn.sh generateGenerating certs and genesis block for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' secondsContinue? [Y/n] Yproceeding ..........

      其中byfn.sh為啟動這個網路的啟動指令碼,啟動指令碼中除建立一個包含4個節點和1個Order service的網路外,還會啟動一個容器用來執行指令碼在channel中加入節點,部署和初始化chaincode,以及在部署的chaincode上執行交易。預設channel名稱為mychannel,指令碼程式會給網路執行個體產生數位憑證和密鑰;產生genesis block用來啟動ordering service;一些用來配置channel的配置交易

      第2步:執行 ./byfn.sh up 啟動網路

$ ./byfn.sh upStarting for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' secondsContinue? [Y/n] Yproceeding ...LOCAL_VERSION=1.2.0DOCKER_IMAGE_VERSION=1.2.0Creating network "net_byfn" with the default driverCreating volume "net_orderer.example.com" with default driverCreating volume "net_peer0.org1.example.com" with default driverCreating volume "net_peer1.org1.example.com" with default driverCreating volume "net_peer0.org2.example.com" with default driverCreating volume "net_peer1.org2.example.com" with default driverCreating peer0.org2.example.com ... doneCreating orderer.example.com    ... doneCreating peer0.org1.example.com ... doneCreating peer1.org1.example.com ... doneCreating peer1.org2.example.com ... doneCreating cli                    ... done ____    _____      _      ____    _____ / ___|  |_   _|    / \    |  _ \  |_   _|\___ \    | |     / _ \   | |_) |   | |   ___) |   | |    / ___ \  |  _ <    | |  |____/    |_|   /_/   \_\ |_| \_\   |_|  Build your first network (BYFN) end-to-end testChannel name : mychannelCreating channel..........

      當我們看到START的時候,表示啟動成功。
      執行結束,顯示END

========= All GOOD, BYFN execution completed ===========  _____   _   _   ____   | ____| | \ | | |  _ \  |  _|   |  \| | | | | | | |___  | |\  | | |_| | |_____| |_| \_| |____/  

      通過docker ps命令可以查看到節點的啟動情況。

$ docker psCONTAINER ID        IMAGE                                                                                                  COMMAND                  CREATED             STATUS              PORTS                                              NAMES7c129154a34a        dev-peer1.org2.example.com-mycc-1.0-26c2ef32838554aac4f7ad6f100aca865e87959c9a126e86d764c8d01f8346ab   "chaincode -peer.add…"   5 minutes ago       Up 5 minutes                                                           dev-peer1.org2.example.com-mycc-1.03746b3295e48        dev-peer0.org1.example.com-mycc-1.0-384f11f484b9302df90b453200cfb25174305fce8f53f4e94d45ee3b6cab0ce9   "chaincode -peer.add…"   6 minutes ago       Up 6 minutes                                                           dev-peer0.org1.example.com-mycc-1.0d2cdb7e80c20        dev-peer0.org2.example.com-mycc-1.0-15b571b3ce849066b7ec74497da3b27e54e0df1345daff3951b94245ce09c42b   "chaincode -peer.add…"   6 minutes ago       Up 6 minutes                                                           dev-peer0.org2.example.com-mycc-1.091034211fb91        hyperledger/fabric-tools:latest                                                                        "/bin/bash"              7 minutes ago       Up 7 minutes                                                           clib38fa60f4e76        hyperledger/fabric-peer:latest                                                                         "peer node start"        7 minutes ago       Up 7 minutes        0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp     peer0.org1.example.com69fd373480cf        hyperledger/fabric-peer:latest                                                                         "peer node start"        7 minutes ago       Up 7 minutes        0.0.0.0:10051->7051/tcp, 0.0.0.0:10053->7053/tcp   peer1.org2.example.comc5f1469bf120        hyperledger/fabric-orderer:latest                                                                      "orderer"                7 minutes ago       Up 7 minutes        0.0.0.0:7050->7050/tcp                             orderer.example.com1faf026ed9c9        hyperledger/fabric-peer:latest                                                                         "peer node start"        7 minutes ago       Up 7 minutes        0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp     peer1.org1.example.com4e0cb9a0a0aa        hyperledger/fabric-peer:latest                                                                         "peer node start"        7 minutes ago       Up 7 minutes        0.0.0.0:9051->7051/tcp, 0.0.0.0:9053->7053/tcp     peer0.org2.example.com

      第3步:如果我們要停止網路,可以執行./byfn.sh down 命令來停止

$ ./byfn.sh downStopping for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' secondsContinue? [Y/n] Yproceeding ...Stopping cli                    ... doneStopping peer0.org1.example.com ... doneStopping peer1.org2.example.com ... doneStopping orderer.example.com    ... doneStopping peer1.org1.example.com ... doneStopping peer0.org2.example.com ... doneRemoving cli                    ... doneRemoving peer0.org1.example.com ... doneRemoving peer1.org2.example.com ... doneRemoving orderer.example.com    ... doneRemoving peer1.org1.example.com ... doneRemoving peer0.org2.example.com ... doneRemoving network net_byfnRemoving volume net_orderer.example.comRemoving volume net_peer0.org1.example.comRemoving volume net_peer1.org1.example.comRemoving volume net_peer0.org2.example.comRemoving volume net_peer1.org2.example.comRemoving volume net_peer0.org3.example.comWARNING: Volume net_peer0.org3.example.com not found.Removing volume net_peer1.org3.example.comWARNING: Volume net_peer1.org3.example.com not found.7c129154a34a3746b3295e48d2cdb7e80c20Untagged: dev-peer1.org2.example.com-mycc-1.0-26c2ef32838554aac4f7ad6f100aca865e87959c9a126e86d764c8d01f8346ab:latestDeleted: sha256:af423f72155affe6f12819384004ffdbf3506274458e85ace79b85afa9f2edafDeleted: sha256:b3fbcd50f54c60d9454612a3ed2a01b0c4d060b27113fc1a9d1289647d43d0feDeleted: sha256:71212a99a5c510e8d4e06d998c9238f8aeab0d39b43585c0f592bcb783212115Deleted: sha256:6a155ef7756a3361c3c549a4bb9bb8b9138ef53ebfada38d65dc00d874ebc5b6Untagged: dev-peer0.org1.example.com-mycc-1.0-384f11f484b9302df90b453200cfb25174305fce8f53f4e94d45ee3b6cab0ce9:latestDeleted: sha256:c4c8845cab7101ee6a4456ee544c4a6599c42bbca545b11045d6b47c1223f205Deleted: sha256:f2f179f4758793728780f909833f710c71f40e77303bd64eb910d572a641c66bDeleted: sha256:03f9cd5e163dc2fae818d7864f8f068330eb7e0b0709a34d40dfcf0e40bd5d43Deleted: sha256:8747af4513550fd8f0f6cfd22c61fd531b073a1a733d02ff6e3fb97fb6def027Untagged: dev-peer0.org2.example.com-mycc-1.0-15b571b3ce849066b7ec74497da3b27e54e0df1345daff3951b94245ce09c42b:latestDeleted: sha256:2bffc21709916a8d9bd1c20a65134ac3ca31ef5cc6e1282cea3b6be28d295acbDeleted: sha256:7cf57b3452456b8da30c08d92d0c054fe32b2c91cd047dc8075706af2c14dc0dDeleted: sha256:6e5b8c88c9d03c7d776953ee8c1b638d02a60438130c9d174b99455bc43d07b8Deleted: sha256:b9eee4e0944c3b35fec12aa4bb5624d02825ab12e71d8a6a73e3de9702831382

10 總結

      通過官方提供的引導指令碼,我們可以快速的搭建好HyperLedger Fabric 1.2.0 環境,前提是我們需要安裝好docker-ce和docker-compose。另外官方給的引導指令碼路徑訪問不了,因此我們可以直接去到HyperLedger Fabric的github上找到相應的指令碼下載下來,然後再運行。

11 參考資料

1.https://docs.docker.com/install/linux/docker-ce/ubuntu/
2.https://docs.docker.com/compose/install/#install-compose
3.https://blog.csdn.net/qq_30383511/article/details/80354943
4.https://blog.csdn.net/sinat_36742186/article/details/80809954
5.https://www.jianshu.com/p/8beb3a355f99

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.