Ubuntu 18.04 builds hyperledge-fabric 2.x network and fabric-explorer


This article ubuntu 18.04 details the process of fabric 2.x building the environment and the environment . fabrix-explorer

1. Preliminary tool preparation

1.1 Git installation

$ apt-get update
$ apt-get install git

1.2 cURL installation

$ apt-get install curl

1.3 docker installation

Uninstall the old version first docker:

$ sudo apt-get remove docker docker-engine docker.io containerd runc

Install auxiliary packages:

$ sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

Added keys dockerfor GPR:

$ sudo mkdir -p /etc/apt/keyrings
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Set up the repository:

$ echo \"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Install dockerthe engine:

$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

List the versions available in the repository:

$ apt-cache madison docker-ce

Optional version in docker repository
To download the specified version docker, <VERSION_STRING>use 5:20.10.16~3-0~ubuntu-jammyinstead :

$ sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io docker-compose-plugin

Verify dockerthat is installed correctly:

$ sudo docker run hello-world

View dockerversion:

$ docker -v

Configure and modify 阿里云the mirror warehouse:

Enter Alibaba Cloud official website - console - container mirroring service - mirroring tool - mirroring accelerator to obtain the acceleration address

$ sudo mkdir -p /etc/docker
$ sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["加速器地址"]
}
EOF
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

install docker-compose:

Try to install a higher version, otherwise an error will be reported when creating a channel later.

downloaddocker-compose1.25.4

$ curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.4/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
#将可执行权限应用于二进制文件
$ chmod +x /usr/local/bin/docker-compose
#创建软链
$ ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
#查询版本号
$ docker-compose -v
$ docker-compose version 1.25.4, build 8d51620a

1.4 Install Go

Download the go language installation package:

$ wget https://studygolang.com/dl/golang/go1.15.5.linux-amd64.tar.gz
#解压
$ tar -C /usr/local -xzf go1.15.5.linux-amd64.tar.gz

Configuration:

## 创建go目录
$ mkdir $HOME/go
## 设置环境变量
$ vi ~/.bashrc
## 在文末加上,其中GOPATH 是存放 Go 项目的目录;GOROOT 是 Go 的安装包所在目录。
$ export GOROOT=/usr/local/go
$ export GOPATH=$HOME/go
$ export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
## 使配置的环境变量生效
$ source ~/.bashrc

# 查看go的版本
$ go version
$ go version go1.15.5 linux/amd64

# 构建go项目目录
## src:存放源代码的目录。
## bin:存放生成后的可执行文件和 Go 相关的工具。
## pkg:存放编译后的包文件。

2 Download fabric and fabric-ca

2.1 clone fabric-sample:

# 创建存放源代码的目录,并进入
$ mkdir -p $GOPATH/src/github.com/hyperledger
$ cd $GOPATH/src/github.com/hyperledger
# 从gitee上克隆代码
$ git clone https://gitee.com/hyperledger/fabric-samples.git
# 进入目录,切换分支
$ cd fabric-samples
# 查看所有分支
$ git branch -a
# 切换到主分支
$ git checkout master

2.2 Pull the fabric binary file:

# 下载所需的二进制文件
## 要注意版本的兼容性,这边下载的是fabric 2.4.0和fabric-ca 1.5.0
$ wget https://github.com/hyperledger/fabric/releases/download/v2.4.0-alpha/hyperledger-fabric-linux-amd64-2.4.0-alpha.tar.gz
$ wget https://github.com/hyperledger/fabric-ca/releases/download/v1.5.0/hyperledger-fabric-ca-darwin-amd64-1.5.0.tar.gz

# 解压二进制文件,并放入fabric-sample目录下
$ tar -C $GOPATH/src/github.com/hyperledger/fabric-samples -xzf hyperledger-fabric-linux-amd64-2.4.0-alpha.tar.gz
$ tar -C $GOPATH/src/github.com/hyperledger/fabric-samples -xzf hyperledger-fabric-ca-darwin-amd64-1.5.0.tar.gz

The pulling process may be very slow, you can download it locally and scpupload itubuntu

2.3 Pull the image required by the fabric

Official bootstrap.sh :

# 在hyperledger目录下新建bootstrap.sh
$ cd $GOPATH/src/github.com/hyperledger
$ touch bootstrap.sh
# 将官方脚本中拉取镜像全部复制进来
$ vim bootstrap.sh

Note the latter two if:

bootstrap.sh
Run bootstrap.shthe script:

$ chmod +x bootstrap.sh
$ ./bootstrap.sh

3 test cases

3.1 Enable the network test that comes with the fabric to test whether the environment is installed:

#进入测试用例路径
$ cd facric-samples/test-network
#启动测试
$ ./network.sh up

If the startup is successful, two organization nodes and an ordering node will be created:
up

Note: Use docker ps -a to check whether the container is started. If the container exits within seconds after starting, it means that the fabric image has not been fully pulled.

3.2 Create a channel

$ ./network.sh createChannel

channel

If this appears, the creation is successful.

3.3 Close the test network

$ ./network.sh down

4 Set environment variables

Will fabric-samples/binbe added to the environment variable for convenience.

#添加环境变量
$ vim ~/.bashrc
#在最后一行加入
$ export PATH=$PATH:$GOROOT/bin:$GOPATH/bin:$HOME/go/src/github.com/hyperledger/fabric-samples/bin
#使配置生效
$ source ~/.bashrc
#验证是否生效
$ fabric-ca-client version
#看是否有输出 若是没有则重启虚拟机试试

5 fabric explorer installation

Use the docker image deployment, provided that the fabric network has been installed.

Create a directory:

#进入指定目录
$ cd  go/src/github.com/hyperledger

$ mkdir explorer
$ cd explorer

Pull the configuration file to the directory:

$ wget https://raw.githubusercontent.com/hyperledger/blockchain-explorer/main/examples/net1/config.json
$ wget https://raw.githubusercontent.com/hyperledger/blockchain-explorer/main/examples/net1/connection-profile/test-network.json -P connection-profile
$ wget https://raw.githubusercontent.com/hyperledger/blockchain-explorer/main/docker-compose.yaml

Copy the entire encrypted artifacts directory (org/) from fabric network (for example /fabric-samples/test-network):

$ cp -r ../fabric-samples/test-network/organizations/ .

The directory structure at this time:

docker-compose.yaml
config.json
connection-profile/test-network.json
organizations/ordererOrganizations/
organizations/peerOrganizations/

To see fabricthe name of the network:

$ docker network ls

docker  network ls

The network name is:fabric_test

edit configuration filedocker-compose.yaml

$ vim docker-commpose.yaml
# SPDX-License-Identifier: Apache-2.0
version: '2.1'

volumes:
  pgdata:
  walletstore:

networks:
  mynetwork.com:
      external:             # 自己添加
        name: fabric_test   #此处为fabric 名称 要对应

services:

  explorerdb.mynetwork.com:    # 不修改
    image: hyperledger/explorer-db:latest
    container_name: explorerdb.mynetwork.com
    hostname: explorerdb.mynetwork.com
    environment:
      - DATABASE_DATABASE=fabricexplorer
      - DATABASE_USERNAME=hppoc
      - DATABASE_PASSWORD=password
    healthcheck:
      test: "pg_isready -h localhost -p 5432 -q -U postgres"
      interval: 30s
      timeout: 10s
      retries: 5
    volumes:
      - pgdata:/var/lib/postgresql/data
    networks:
      - mynetwork.com

  explorer.mynetwork.com:
    image: hyperledger/explorer:latest
    container_name: explorer.mynetwork.com
    hostname: explorer.mynetwork.com
    environment:
      - DATABASE_HOST=explorerdb.mynetwork.com
      - DATABASE_DATABASE=fabricexplorer
      - DATABASE_USERNAME=hppoc
      - DATABASE_PASSWD=password
      - LOG_LEVEL_APP=info
      - LOG_LEVEL_DB=info
      - LOG_LEVEL_CONSOLE=debug
      - LOG_CONSOLE_STDOUT=true
      - DISCOVERY_AS_LOCALHOST=false    #通过网桥网络将 Explorer 连接到结构网络时,需要设置为 禁用到 localhost 的主机名映射。
      - PORT=${PORT:-8080}  #浏览器端口
    volumes:          # 一下内容 照样修改
          - ./config.json:/opt/explorer/app/platform/fabric/config.json
          - ./connection-profile:/opt/explorer/app/platform/fabric/connection-profile
          - ./organizations:/tmp/crypto
          - walletstore:/opt/explorer/wallet
    ports:
      - ${PORT:-8080}:${PORT:-8080}
    depends_on:
      explorerdb.mynetwork.com:
        condition: service_healthy
    networks:
      - mynetwork.com

Modify test-network.jsonthe configuration file

{
    
    
        "name": "test-network",   
        "version": "1.0.0",
        "client": {
    
    
                "tlsEnable": true,
                "adminCredential": {
    
        #浏览器登录时的账号和密码 可以修改为自己想要的
                        "id": "exploreradmin",
                        "password": "null"
                },
                "enableAuthentication": true,
                "organization": "Org1MSP",
                "connection": {
    
    
                        "timeout": {
    
    
                                "peer": {
    
    
                                        "endorser": "300"
                                },
                                "orderer": "300"
                        }
                }
        },
        "channels": {
    
    
                "mychannel": {
    
    
                        "peers": {
    
    
                                "peer0.org1.example.com": {
    
    }
                        }
                }
        },
        "organizations": {
    
    
                "Org1MSP": {
    
    
                        "mspid": "Org1MSP",
                        # 这部分一定要修改 否则会启动失败
                        "adminPrivateKey": {
    
    
                                # 原为 "path": "/tmp/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore/priv_sk"
                                "path": "/tmp/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore/priv_sk"
                        },
                        "peers": ["peer0.org1.example.com"],
                        "signedCert": {
    
    
                                # 原为 "path": "/tmp/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp/signcerts/[email protected]"
                                "path": "/tmp/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp/signcerts/[email protected]"
                        }
                }
        },
        "peers": {
    
    
                "peer0.org1.example.com": {
    
    
                        "tlsCACerts": {
    
    
                                "path": "/tmp/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt"
                        },
                        "url": "grpcs://peer0.org1.example.com:7051"
                }
        }
}

Before starting, you need to fabriccreate a channel in the test network, and the channel name will use the default channel.

Start the container service:

$ docker-compose up -d

Out of service:

  1. To stop the service without removing persistent data, run the following command:
$ docker-compose down
  1. docker-compose.yamlIn , Postgrestwo named volumes are allocated for persistent data (data and user wallets). If you want to clear these named volumes, run the following command:
$ docker-compose down -v

Enter in the browser ip:8080to access fabric-explorer:
ip:8080
some errors are excluded

# 查看container 是否在运行
$ docker ps -a  
# 查看容器输出日志
$ docker logs -f [container-name\ID]

If wallet creation fails, focus on checking whether test-network.jsonthe files and"adminPrivateKey" are modified correctly. If the connection fails, it will be created first before starting ."signedCert"path
channelchannelexplorer

ps:如有不正之处欢迎指出!!!

Guess you like

Origin blog.csdn.net/Unknow_if/article/details/126696793