HyperLedger Fabric(First-Network)

HyperLedger Fabric(First-Network)

基於官方腳本快速部署第一個fabric網絡,本次實驗vagrant創建的虛擬機中執行。

實驗環境搭建

本文假設你已經安裝好vagrant,當你出現如下圖所示的信息之後,即可執行下一步。

image-20200124111356750

若還未安裝vagrant 請前往https://www.vagrantup.com/ 進行安裝後進行下一步操作

創建虛擬機

  1. 創建vagrant配置文件,這裏指定操作系統爲centos7

    vagrant init centos/7
    

​ 執行後,將在文件夾下創建Vagrantfile 配置文件

image-20200124112752291

  1. 編寫啓動腳本 bootstrap.sh

    #!/usr/bin/bash
    sudo su
    echo "nameserver 8.8.8.8" >> /etc/resolv.conf
    sleep 3s
    yum install -y epel-release
    yum install -y vim
    yum install -y golang
    
    yum remove docker docker-common docker-selinux docker-engine
    yum install -y yum-utils device-mapper-persistent-data lvm2
    yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    yum makecache fast
    yum -y install docker-ce
    yum -y install docker-compose
    service docker start
    
    mkdir -p /etc/docker
    echo {\"registry-mirrors\": [\"https://8w1wqmsz.mirror.aliyuncs.com\"]} > /etc/docker/daemon.json
    service docker restart
    
    echo "export GOPROXY=https://goproxy.io,direct" >> /etc/profile
    source /etc/profile
    
  2. 修改Vagrantfile

    # -*- mode: ruby -*-
    # vi: set ft=ruby :
    
    # All Vagrant configuration is done below. The "2" in Vagrant.configure
    # configures the configuration version (we support older styles for
    # backwards compatibility). Please don't change it unless you know what
    # you're doing.
    Vagrant.configure("2") do |config|
      # The most common configuration options are documented and commented below.
      # For a complete reference, please see the online documentation at
      # https://docs.vagrantup.com.
    
      # Every Vagrant development environment requires a box. You can search for
      # boxes at https://vagrantcloud.com/search.
      config.vm.box = "centos/7"
    
      # Disable automatic box update checking. If you disable this, then
      # boxes will only be checked for updates when the user runs
      # `vagrant box outdated`. This is not recommended.
      # config.vm.box_check_update = false
    
      # Create a forwarded port mapping which allows access to a specific port
      # within the machine from a port on the host machine. In the example below,
      # accessing "localhost:8080" will access port 80 on the guest machine.
      # NOTE: This will enable public access to the opened port
      # config.vm.network "forwarded_port", guest: 80, host: 8080
    
      # Create a forwarded port mapping which allows access to a specific port
      # within the machine from a port on the host machine and only allow access
      # via 127.0.0.1 to disable public access
      # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
    
      # Create a private network, which allows host-only access to the machine
      # using a specific IP.
      # config.vm.network "private_network", ip: "192.168.33.10"
    
      # Create a public network, which generally matched to bridged network.
      # Bridged networks make the machine appear as another physical device on
      # your network.
      # config.vm.network "public_network"
    
      # Share an additional folder to the guest VM. The first argument is
      # the path on the host to the actual folder. The second argument is
      # the path on the guest to mount the folder. And the optional third
      # argument is a set of non-required options.
      # config.vm.synced_folder "../data", "/vagrant_data"
    
      # Provider-specific configuration so you can fine-tune various
      # backing providers for Vagrant. These expose provider-specific options.
      # Example for VirtualBox:
      #
      # config.vm.provider "virtualbox" do |vb|
      #   # Display the VirtualBox GUI when booting the machine
      #   vb.gui = true
      #
      #   # Customize the amount of memory on the VM:
      #   vb.memory = "1024"
      # end
      #
      # View the documentation for the provider you are using for more
      # information on available options.
    
      # Enable provisioning with a shell script. Additional provisioners such as
      # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
      # documentation for more information about their specific syntax and use.
       config.vm.provision "shell", path: "bootstrap.sh"
    end
    
  3. 啓動虛擬機

    vagrant up
    

搭建fabric網絡

在完成虛擬機創建之後,使用vagrant ssh即可進入到虛擬機

# 進入到虛擬機
vagrant ssh
# 切換root 用戶
sudo su

獲取源碼

go的環境已經通過bootstrap.sh配置好了,接下來獲取所需的項目源碼

  1. Fabric 源碼

    go get github.com/hyperledger/fabric
    
  2. Fabric-samples 源碼

    go get github.com/hyperledger/fabric-samples
    

編譯工具cryptogen、configtxgen

進入到fabric源碼中切換到 release-1.4 分支

git checkout release-1.4
  1. 編譯安裝cryptogen

    進入到 fabric/common/tools/cryptogen 執行 go install 生成 cryptogen二進制工具

    go install
    
  2. 編譯安裝configtxgen工具

    進入到 fabric/common/tools/configtxgen 執行 go install 生成 configtxgen二進制工具

    go install
    

修改環境變量

修改/etc/profile文件,添加環境變量

export PATH=$PATH:/root/go/bin
#source生效
source /etc/profile

啓動fabric網絡

在上述操作都完成後,fabric所需環境已經配置好了,接下啓動fabric網絡。

  1. 進入到fabric-samples下,切換到 release-1.4

    git checkout release-1.4
    
  2. 進入到fabric-samples/first-network目錄下

    image-20200124135923070

    這裏將使用 byfn.sh 腳本啓動fabric網絡,執行如下命令。

    ./byfn.sh up
    

    後面需詢問你,輸入y即可

    image-20200124140606163

  3. 當終端輸出如下圖所示,即完成了fabric網絡的啓動。

    image-20200124142821103

    注意:當你執行./byfn.sh up 後,中間過程有任何一步的報錯,都需要執行 ./byfn.sh down 後才能再次執行./byfn.sh up。

  4. Byfn.sh 還有如下操作,這裏我就不詳細說明了。

    image-20200124143906505

總結

本次搭建fabric網絡是基於官方一鍵部署腳本搭建的1.4版本的fabric網絡,只適合開發時使用,不建議生產環境下使用,後面會帶大家搭建生產級fabric網絡。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章