ubuntu 18.04 搭建hyperledge-fabric 2.x网络和fabric-explorer

Posted null-un

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ubuntu 18.04 搭建hyperledge-fabric 2.x网络和fabric-explorer相关的知识,希望对你有一定的参考价值。

文章目录


本文详细说明在 ubuntu 18.04环境下搭建 fabric 2.x环境和 fabrix-explorer的过程。

1、前期工具准备

1.1 Git安装

$ apt-get update
$ apt-get install git

1.2 cURL安装

$ apt-get install curl

1.3 docker 安装

先卸载老版本的docker

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

安装辅助包:

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

添加dockerGPR密钥:

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

设置存储库:

$ 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

安装docker 引擎:

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

列出存储库可用的版本:

$ apt-cache madison docker-ce


下载指定版本的docker ,<VERSION_STRING>5:20.10.16~3-0~ubuntu-jammy 替代:

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

验证docker 是否安装正确:

$ sudo docker run hello-world

查看docker 版本:

$ docker -v

配置修改阿里云镜像仓库:

进入阿里云官网-控制台-容器镜像服务-镜像工具-镜像加速器 获取加速地址

$ sudo mkdir -p /etc/docker
$ sudo tee /etc/docker/daemon.json <<-'EOF'

  "registry-mirrors": ["加速器地址"]

EOF
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

安装docker-compose

尽量安装高版本的,不然后面创建channel时会报错。

下载docker-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 安装Go

下载go语言安装包:

$ 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

配置:

## 创建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 下载 fabric 和 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 拉取 fabric 二进制文件:

# 下载所需的二进制文件
## 要注意版本的兼容性,这边下载的是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

拉取过程可能很慢,可以自己本地下载后上使用 scp上传到 ubuntu

2.3 拉取fabric所需的镜像

官方bootstrap.sh

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

注释后面两个if


运行bootstrap.sh脚本:

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

3 测试用例

3.1 启用fabric自带的网络测试来测试环境是否装好:

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

启动成功会创建两个组织节点和一个排序节点:

注:使用 docker ps -a 查看容器是否启动,若是容器启动后秒退,说明fabric镜像没有拉取全。

3.2 创建channel

$ ./network.sh createChannel

出现这个则说明创建成功。

3.3 关闭测试网络

$ ./network.sh down

4 设置环境变量

fabric-samples/bin 加入环境变量 方便使用。

#添加环境变量
$ 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安装

使用 docker 镜像部署,前提是已经装好fabric 网络。

创建目录:

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

$ mkdir explorer
$ cd explorer

拉取配置文件到目录下:

$ 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

从结构网络复制整个加密工件目录(组织/)(例如/fabric-samples/test-network):

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

此时的目录结构:

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

查看fabric网络的名称:

$ docker network ls

网络名称为:fabric_test

编辑配置文件 docker-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

修改test-network.json 配置文件


        "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/User1@org1.example.com/msp/keystore/priv_sk"
                                "path": "/tmp/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/priv_sk"
                        ,
                        "peers": ["peer0.org1.example.com"],
                        "signedCert": 
                                # 原为 "path": "/tmp/crypto/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/signcerts/User1@org1.example.com-cert.pem"
                                "path": "/tmp/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/signcerts/Admin@org1.example.com-cert.pem"
                        
                
        ,
        "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"
                
        

在启动之前还需要在fabric的测试网络中创建通道,通道名称就用默认的 channel

启动容器服务:

$ docker-compose up -d

停止服务:

  1. 若要在不删除持久性数据的情况下停止服务,请运行以下命令:
$ docker-compose down
  1. docker-compose.yaml 中,为持久性数据(Postgres 数据和用户钱包)分配了两个命名卷。如果要清除这些命名卷,请运行以下命令:
$ docker-compose down -v

在浏览器输入ip:8080 就可访问fabric-explorer

部分错误排除

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

出现钱包创建失败则重点查看test-network.json文件"adminPrivateKey""signedCert"path是否修改正确。
出现channel 连接失败则先创建channel 在启动explorer

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

Hyperledger Fabric 1.4环境搭建详尽

操作系统:Ubuntu 16.04/18.04

1. 安装依赖工具

1.1 go语言

test@ubuntu :~/Soft$ wget https://dl.google.com/go/go1.14.2.linux-amd64.tar.gz

test@ubuntu :~/Soft$ sudo tar -zxvf go1.14.2.linux-amd64.tar.gz -C /opt

test@ubuntu :~/Soft$ echo "export GOROOT=/opt/go" >> ~/.bashrc
test@ubuntu :~/Soft$ echo "export GOPATH=/opt/gopath" >> ~/.bashrc
test@ubuntu :~/Soft$ echo "export PATH=\\$GOROOT/bin:\\$GOPATH/bin:\\$PATH" >>  ~/.bashrc

test@ubuntu :~/Soft$ . ~/.bashrc 

test@ubuntu :~/Soft$ go version
go version go1.14.2 linux/amd64

go语言的配置可以自由选择

1.2 docker

test@ubuntu-bionic:~/Soft$ curl -fsSL https://get.docker.com -o get-docker.sh
test@ubuntu-bionic:~/Soft$ sudo sh get-docker.sh 
test@ubuntu-bionic:~/Soft$ sudo usermod -aG docker $USER
test@ubuntu-bionic:~/Soft$ sudo systemctl restart docker
test@ubuntu-bionic:~$ docker version
Client: Docker Engine - Community
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        afacb8b7f0
 Built:             Wed Mar 11 01:25:46 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       afacb8b7f0
  Built:            Wed Mar 11 01:24:19 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

1.3 docker-compose

vagrant@ubuntu-bionic:~$ sudo curl -L "https://github.com/docker/compose/releases/download/1.25.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

vagrant@ubuntu-bionic:~$ sudo chmod 755 /usr/local/bin/docker-compose

vagrant@ubuntu-bionic:~$ docker-compose --version
docker-compose version 1.25.4, build 8d51620a

2 源码及镜像下载

2.1 源码下载

vagrant@ubuntu-bionic:~$ mkdir -p $GOPATH/src/github.com/hyperledger
vagrant@ubuntu-bionic:~$ cd $GOPATH/src/github.com/hyperledger
vagrant@ubuntu-bionic:/opt/gopath/src/github.com/hyperledger$ git clone https://github.com/hyperledger/fabric-samples.git

2.2 切换分支

[vagrant@RUAN:/opt/gopath/src/github.com/hyperledger/fabric-samples] (master)$ git checkout -b v1.4.6 v1.4.6
Switched to a new branch 'v1.4.6'

2.3 下载引导脚本

[vagrant@RUAN:/opt/gopath/src/github.com/hyperledger/fabric-samples] (v1.4.6)$ curl -sS https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh -o ./scripts/bootstrap.sh

[vagrant@RUAN:/opt/gopath/src/github.com/hyperledger/fabric-samples] (v1.4.6)$ chmod +x scripts/bootstrap.sh

2.4 下载必备二进制文件和docker镜像

# ./scripts/bootstrap.sh [version] [ca version] [thirdparty_version]
[vagrant@RUAN:/opt/gopath/src/github.com/hyperledger/fabric-samples] (v1.4.6)$ ./scripts/bootstrap.sh 1.4.6 1.4.6 0.4.18 -s

2.4.1 下载Hyperledger Fabric 二进制包

===> Downloading version 1.4.6 platform specific fabric binaries
===> Downloading:  https://github.com/hyperledger/fabric/releases/download/v1.4.6/hyperledger-fabric-linux-amd64-1.4.6.tar.gz

===> Downloading version 1.4.6 platform specific fabric-ca-client binary
===> Downloading:  https://github.com/hyperledger/fabric-ca/releases/download/v1.4.6/hyperledger-fabric-ca-linux-amd64-1.4.6.tar.gz

2.4.2 拉取docker镜像

Pull Hyperledger Fabric docker images
FABRIC_IMAGES: peer orderer ccenv tools javaenv
===> Pulling fabric Images

====> hyperledger/fabric-peer:1.4.6
1.4.6: Pulling from hyperledger/fabric-peer

====> hyperledger/fabric-orderer:1.4.6
1.4.6: Pulling from hyperledger/fabric-orderer

====> hyperledger/fabric-ccenv:1.4.6
1.4.6: Pulling from hyperledger/fabric-ccenv

====> hyperledger/fabric-tools:1.4.6
1.4.6: Pulling from hyperledger/fabric-tools

====> hyperledger/fabric-javaenv:1.4.6
1.4.6: Pulling from hyperledger/fabric-javaenv

====> hyperledger/fabric-ca:1.4.6
1.4.6: Pulling from hyperledger/fabric-ca

===> Pulling thirdparty docker images
====> hyperledger/fabric-zookeeper:0.4.18
0.4.18: Pulling from hyperledger/fabric-zookeeper

====> hyperledger/fabric-kafka:0.4.18
0.4.18: Pulling from hyperledger/fabric-kafka

====> hyperledger/fabric-couchdb:0.4.18
0.4.18: Pulling from hyperledger/fabric-couchdb

===> List out hyperledger docker images
hyperledger/fabric-javaenv     1.4                 68914607b3a5        6 weeks ago         1.68GB
hyperledger/fabric-javaenv     1.4.6               68914607b3a5        6 weeks ago         1.68GB
hyperledger/fabric-javaenv     latest              68914607b3a5        6 weeks ago         1.68GB
hyperledger/fabric-ca          1.4                 3b96a893c1e4        7 weeks ago         150MB
hyperledger/fabric-ca          1.4.6               3b96a893c1e4        7 weeks ago         150MB
hyperledger/fabric-ca          latest              3b96a893c1e4        7 weeks ago         150MB
hyperledger/fabric-tools       1.4                 0f9743ac0662        7 weeks ago         1.49GB
hyperledger/fabric-tools       1.4.6               0f9743ac0662        7 weeks ago         1.49GB
hyperledger/fabric-tools       latest              0f9743ac0662        7 weeks ago         1.49GB
hyperledger/fabric-ccenv       1.4                 191911f4454f        7 weeks ago         1.36GB
hyperledger/fabric-ccenv       1.4.6               191911f4454f        7 weeks ago         1.36GB
hyperledger/fabric-ccenv       latest              191911f4454f        7 weeks ago         1.36GB
hyperledger/fabric-orderer     1.4                 84eaba5388e7        7 weeks ago         120MB
hyperledger/fabric-orderer     1.4.6               84eaba5388e7        7 weeks ago         120MB
hyperledger/fabric-orderer     latest              84eaba5388e7        7 weeks ago         120MB
hyperledger/fabric-peer        1.4                 5a52faa5d8c2        7 weeks ago         128MB
hyperledger/fabric-peer        1.4.6               5a52faa5d8c2        7 weeks ago         128MB
hyperledger/fabric-peer        latest              5a52faa5d8c2        7 weeks ago         128MB
hyperledger/fabric-zookeeper   0.4                 ede9389347db        5 months ago        276MB
hyperledger/fabric-zookeeper   0.4.18              ede9389347db        5 months ago        276MB
hyperledger/fabric-zookeeper   latest              ede9389347db        5 months ago        276MB
hyperledger/fabric-kafka       0.4                 caaae0474ef2        5 months ago        270MB
hyperledger/fabric-kafka       0.4.18              caaae0474ef2        5 months ago        270MB
hyperledger/fabric-kafka       latest              caaae0474ef2        5 months ago        270MB
hyperledger/fabric-couchdb     0.4                 d369d4eaa0fd        5 months ago        261MB
hyperledger/fabric-couchdb     0.4.18              d369d4eaa0fd        5 months ago        261MB
hyperledger/fabric-couchdb     latest              d369d4eaa0fd        5 months ago        261MB

请耐心等待,很需要时间

2.4.3 启动验证

进入到first-network目录下并启动网络
[test@RUAN:~]$ cd /opt/gopath/src/github.com/hyperledger/fabric-samples/first-network
[test@RUAN:/opt/gopath/src/github.com/hyperledger/fabric-samples/first-network] (v1.4.6)$ ./byfn.sh up 

能顺利跑完就说明没有问题

以上是关于ubuntu 18.04 搭建hyperledge-fabric 2.x网络和fabric-explorer的主要内容,如果未能解决你的问题,请参考以下文章

Hyperledger Fabric环境搭建过程

HyperLedger Fabric实战:基础环境构建

Ubuntu环境下搭建hyperledger fabric超级无敌详细教程

Hyperledger Fabric 1.4环境搭建详尽

Hyperledger Fabric 1.4环境搭建详尽

Hyperledger Fabric 1.1安装部署-基础环境搭建