欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HyperLedger Fabric(First-Network)

程序员文章站 2024-03-21 22:31:28
...

HyperLedger Fabric(First-Network)

基于官方脚本快速部署第一个fabric网络,本次实验vagrant创建的虚拟机中执行。

实验环境搭建

本文假设你已经安装好vagrant,当你出现如下图所示的信息之后,即可执行下一步。

HyperLedger Fabric(First-Network)

若还未安装vagrant 请前往https://www.vagrantup.com/ 进行安装后进行下一步操作

创建虚拟机

  1. 创建vagrant配置文件,这里指定操作系统为centos7

    vagrant init centos/7
    

​ 执行后,将在文件夹下创建Vagrantfile 配置文件

HyperLedger Fabric(First-Network)

  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目录下

    HyperLedger Fabric(First-Network)

    这里将使用 byfn.sh 脚本启动fabric网络,执行如下命令。

    ./byfn.sh up
    

    后面需询问你,输入y即可

    HyperLedger Fabric(First-Network)

  3. 当终端输出如下图所示,即完成了fabric网络的启动。

    HyperLedger Fabric(First-Network)

    注意:当你执行./byfn.sh up 后,中间过程有任何一步的报错,都需要执行 ./byfn.sh down 后才能再次执行./byfn.sh up。

  4. Byfn.sh 还有如下操作,这里我就不详细说明了。

    HyperLedger Fabric(First-Network)

总结

本次搭建fabric网络是基于官方一键部署脚本搭建的1.4版本的fabric网络,只适合开发时使用,不建议生产环境下使用,后面会带大家搭建生产级fabric网络。

相关标签: Fabric