Learning of Fabric Framework-1-Basic knowledge storage and installation (supporting video learning resources)

One, Shell script basics

1.1 Concept

A collection of a series of shell commands, you can also add some logical operations (if else for) to put these commands into a script file

1.2 Basic format

Naming format: xxx.sh (generally, the suffix is ​​sh, or not written)

Writing format:

# test.sh  #是shell脚本的注释
#!/bin/bash  # 指定解析shell脚本使用的命令解释器  /bin/sh也可以这是不写这一行的默认解释器
# 一系列的shell命令
ls 
pwd
echo "hello world"

1.3 Execution of shell scripts

# shell脚本的编写完之后,必须添加执行权限
chmod u+x xxx.sh
# 执行脚本
./xxx.sh
sh xxx./sh

Shell scripts are executed sequentially from top to bottom, and interrupted if there is an error in the middle

1.4 Variables in shell scripts

  • Definition of variables

    1. Ordinary variables (local variables)

      # 定义变量,定义变量必须要赋值, =前后不允许有空格,有空格是比较(类似于==)
      temp=999
      # 普通变量只能在当前进程中使用
      
    2. Environment variables (generally uppercase)

      # 可以理解为全局变量,在当前操作系统中可全局访问
      # 分类
      	- 系统自带
      		例如: -PWD  -PATH
      	- 用户自定义
      		- 将普通变量提升为系统变量  前面加个set或export
      		GOPATH=/home/zoro/go/src ->普通环境变量
      		set GOPATH=/home/zoro/go/src ->系统环境变量
      		export GOPATH=/home/zore/go/src ->系统环境变量
      
  • Location variable

    When executing the script, you can pass parameters to the script, and use these parameters inside the script to use these positional variables

    # 执行脚本
    ./test.sh aa bb cc dd ...
    
    • $0: File name to be executed
    • $1: the first parameter, aa
    • $2: The second parameter, bb

    Note that the extra parameters are also passed in, but they are not used

  • Special variable

    • $#: Get the number of passed parameters

    • $@: all parameters passed to the script

    • $?: The state after the script is executed, success==0 or failure! =0

    • $$: The corresponding process ID after the script is executed

      echo "Hello World"
      echo "第一个参数: $0"
      echo "第二个参数: $1"
      echo "第三个参数: $2"
      echo "第四个参数: $3"
      echo "第五个参数: $4"
      echo "传递的所有参数的个数: $#"
      echo "传递的所有参数: $@"
      echo "脚本执行完的状态: $?"
      echo "脚本执行的进程编号: $$"
      

    Enter directly to echo $?view the status of the previous process

  • Ordinary variable value

    # 变量定义
    value=123  #默认以字符串处理
    echo $value #打印变量前面要加$
    echo ${value} #这样也可以,但是相对麻烦
    

  • Two ways to get the command execution result

    Save the results of the command execution with variables:

    var=$(shell命令)
    var=`shell命令`
    

  • Use of quotation marks

    # 双引号
    echo "xxxx $var"  #会将其中变量的值打印输出
    # 单引号
    echo 'xxxx $var'  #会原样输出,不会取变量值
    

1.5 Conditional judgment and loop

  • Judgement of if condition of shell script

    # if语句
    if [ 条件判断 ];then
    	逻辑处理 -> shell命令
    	xxxx
    fi
    # =====================或
    if [ 条件判断 ]
    then
    	逻辑处理 -> shell命令
    	xxxx
    fi
    # if ... elif ... fi
    if [ 条件判断 ];then
    	逻辑处理 -> shell命令
    	xxxx
    elif [ 条件判断 ];then
    	shell命令
    	xxx
    elif [ 条件判断 ];then
        shell命令
        xxx
    else
    	shell命令
    fi
    

    Note: There is a space between the if and the brackets, and there are spaces on both sides of the conditional judgment in the brackets and the brackets.

    #!/bin/bash
    #对传递到脚本内部的文件名做判断
    if [ -d $1 ];then
            echo "$1 是一个目录"
    elif [ -s $1 ];then
            echo "$1 是一个文件,且文件不为空"
    else
            echo "$1 不是一个文件也不是一个目录"
    fi
    

    Commonly used judgment conditions:

    • About file attributes

    • About the judgment of the character string

    • Common value judgment

    • Logical operators

  • shell script for loop

    # shell中有for、where
    # 语法: for 变量 in 集合; do; done
    #!/bin/bash
    # 对当前目录下的文件进行遍历
    list=$(ls)
    for var in $list;do
            echo "当前文件: $var"
    done
    ~     
    

1.6 Functions in shell scripts

# 没有函数的修饰,也没有参数也没有返回值
# 格式
funcName(){
    
    
	# 得到第一个参数
	arg1=$1
	# 得到第二个参数
	arg2=$2
	# 得到第三个参数
	arg3=$3
	函数体 ->一系列的shell命令  逻辑循环和判断
}
# 没有参数列表,但是可以传参
# 函数调用
funcName aa bb cc dd
# 函数调用之后的状态
0:成功
非0:失败

#!/bin/bash
# 判断传递进来的文件名是不是目录,如果不是就创建
# 定义函数
funcIsDir(){
    
    
        if [ -d $1 ];then
                echo "$1是一个目录"
        else
                # 创建这个目录
                mkdir $1
                if [ 0 -ne $? ];then
                        echo "目录创建失败"
                        exit
                fi
                echo "目录创建成功"
                ls -a
        fi
}

# 函数的调用
funcIsDir $1

Two, the basic concept of Fabric

The ecological picture of hyperledger:

Fabric

  • Ready to work

  • effect

    docker => Similar to a virtual machine, for multi-node simulation in the blockchain

    docker-compose => docker management tool

    go => Develop blockchain

    nodejs => write client

    python => some parts need to be used

  • All installation configurations can be seen below, or see https://blog.csdn.net/qq_40632760/article/details/94594177

2.1 Install docker

The environment is Ubuntu18, the specific installation documents can be found in the article https://blog.csdn.net/qq_40632760/article/details/94594177

The permission denied problem of installing docker:

No execution permission

solution:

2.2 Install docker-compose

method one:

Method Two:

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

# 添加可执行权限ls
sudo chmod +x /usr/local/bin/docker-compose

# 测试安装
sudo docker-compose --version

2.3 Installation of go environment

GOROOT is the installation path of go

GOPATH is used as the storage destination of the compiled binary and the search path when importing the package (in fact, it is also your working directory, you can create your own go source file under src, and then start working).

  1. There are mainly three directories under GOPATH: bin, pkg, src
  2. The bin directory mainly stores executable files; the pkg directory stores compiled library files, mainly *.a files; the src directory mainly stores go source files

2.4 nodejs installation

Environment variable configuration method two:

wget https://nodejs.org/dist/v8.11.4/node-v8.11.4-linux-x64.tar.xz
sudo tar xvf node-v8.11.4-linux-x64.tar.xz -C /opt
cd /opt
sudo mv node-v8.11.4-linux-x64/ node-v8.11.4  # 更名
vim ~/.bashrc  				# .bashrc文件的配置仅当前用户生效
sudo vim /etc/profile		#/etc/profile文件的配置对所有用户生效
# 在文件下面加两行
export NODEJS_HOME=/opt/node-v8.11.4
export PATH=$PATH:$NODEJS_HOME/bin
# 重新加载
source ~/.bashrc	
. /etc/profile 
# 验证
node -v

The configuration of the .bashrc file only takes effect for the current user

The configuration of the /etc/profile file is effective for all users

2.5 Deploy Hyperledger Fabric

# 1. 创建放置的目录,进入该目录,下载脚本
cd ~
mkdir hyperledger-fabric  	
cd hyperledger-fabric
# 下载并执行脚本
# 后接参数第一个 1.2.0=> Fabric的版本 第二个1.2.0=>Fabric的认证机构Fabric的版本  第三个是第三方库的版本   前两个参数一般保持一致
# 老版本 (视频中的老版本)
curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh | bash -s 1.2.0 1.2.0 0.4.10
# 新版本
curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh | bash -s -- 2.0.0 1.4.4 0.4.18 

Note: This step is very likely to prompt to deny access

Solution for access denied

Solution: https://blog.csdn.net/laoxuan2011/article/details/

step-1

At https://site.ip138.com/raw.Githubusercontent.com/

Enter raw.githubusercontent.com to query the IP address

step-2

Modify hosts Ubuntu, CentOS and macOS directly enter the terminal

sudo vi /etc/hosts

Add the following content and save it (modify the IP address after querying it, you can choose the best IP address for the delay of pinging different IPs)

151.101.108.133 raw.githubusercontent.com
151.101.76.133 raw.githubusercontent.com
151.101.228.133 raw.githubusercontent.com

Enter the command again and it should be OK

Solutions to download too slow

Add a mirror to docker

Add Alibaba Cloud image:

# 有就修改,没有就创建
vi /etc/docker/daemon.json
# 文件内编辑配置  注意:这里yourID需要自行注册替换
{
    
    
          "registry-mirrors": ["https://{yourId}.mirror.aliyuncs.com"]
}
# 写完保存,重启docker
sudo systemctl daemon-reload
sudo systemctl restart docker
# 注意阿里的镜像需要你先注册阿里云账户,详细地址https://www.aliyun.com/product/acr?spm=5176.10695662.1362911.1.3ae2262cdaoPFB
# 检查是否配置好
docker info
# 在底部看到你的加速地址则说明ok

Download content

The content downloaded by this command is divided into three parts:

The first two downloads may be slow and need to be patient

List of downloaded dependent images:

Related explanation:

  • peer: block storage
  • orderer: There are no miners in Fabric, the order here is responsible for data packaging
  • ccenv: A runtime environment for GO language
  • fabric-tools: many tools you will use
  • fabric-ca: Generate a certificate. Identity authentication is required to join the alliance chain and private chain, and only those who have passed can join. =>Generate account
  • fabric-couchdb: database
  • fabric-kafka: sorting, zookeeper is required
  • fabric-zookeeper: complementary to kafka

After the final completion, there will be this output display:

===> List out hyperledger docker images
hyperledger/fabric-ca        1.2                 66cc132bd09c        2 years ago         252MB
hyperledger/fabric-ca        1.2.0               66cc132bd09c        2 years ago         252MB
hyperledger/fabric-ca        latest              66cc132bd09c        2 years ago         252MB
hyperledger/fabric-tools     1.2                 379602873003        2 years ago         1.51GB
hyperledger/fabric-tools     1.2.0               379602873003        2 years ago         1.51GB
hyperledger/fabric-tools     latest              379602873003        2 years ago         1.51GB
hyperledger/fabric-ccenv     1.2                 6acf31e2d9a4        2 years ago         1.43GB
hyperledger/fabric-ccenv     1.2.0               6acf31e2d9a4        2 years ago         1.43GB
hyperledger/fabric-ccenv     latest              6acf31e2d9a4        2 years ago         1.43GB
hyperledger/fabric-orderer   1.2                 4baf7789a8ec        2 years ago         152MB
hyperledger/fabric-orderer   1.2.0               4baf7789a8ec        2 years ago         152MB
hyperledger/fabric-orderer   latest              4baf7789a8ec        2 years ago         152MB
hyperledger/fabric-peer      1.2                 82c262e65984        2 years ago         159MB
hyperledger/fabric-peer      1.2.0               82c262e65984        2 years ago         159MB
hyperledger/fabric-peer      latest              82c262e65984        2 years ago         159MB

Note: These images are not placed in the fabric-simples directory, they are all placed in the /var/lib/docker/imagedirectory

If you need to delete the environment, remember to delete the mirror here

2.6 Configure environment variables

hyperledger-fabric/fabric-samples/bin

Some executable binary files are placed in it

These commands are copied to /usr/local/binthe

cd hyperledger-fabric/fabric-samples/bin
sudo cp * /usr/local/bin
# 如果/usr/local/bin已经在PATH中了就已经完成了,没有的话加入到PATH中
# 查看PATH
echo $PATH

Tips

1. Log in again

After logging in again, you may not have permission. At this time, add yourself to the docker user group => newgrp - docker

2. Errors occurred when installing Fabric

If this bug has not been resolved, the best way is to reinstall the system and reinstall the environment, just a few more times (smile.jpg).

(Ps: If you talk too much, it's tears)

Guess you like

Origin blog.csdn.net/weixin_43988498/article/details/108942281