Hyperledger Fabric项目搭建区块链浏览器Hyperledger-blockchain-explorer

Hyperledger Fabric项目搭建区块链浏览器

1.下载配置文件

区块链浏览器官网:https://github.com/hyperledger/blockchain-explorer

# 根据官网来部署
# 在项目目录创建文件夹
# org1部署区块浏览器
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
# 如果虚拟机没有联网,导致下载不下来,可以在官网点击三个配置文件,自己创建相对应名称,复制并保存,
# config.json与docker-compose.yaml直接放在explorer文件夹下,
# 但注意test-network.json,需要先新建connection-profile文件夹,然后将test-network.json放入connection-profile文件夹内

Hyperledger Fabric项目搭建区块链浏览器Hyperledger-blockchain-explorer_第1张图片

2.拷贝证书目录

mkdir organizations
cp -r ../crypto-config/* organizations
cd explorer

此时目录结构如下图所示
Hyperledger Fabric项目搭建区块链浏览器Hyperledger-blockchain-explorer_第2张图片

3.以两个组织,每个组织一个peer结点为例修改配置文件

3.1修改test-network.json—— 网络配置文件,包含身份的指定

# 先修改test-network.json文件为org1-network.json
mv test-network.json org1-network.json
# 进入修改org1-network.json中对应参数
vim org1-network.json
# 修改证书连接文件
# 将用户的证书替换为连接配置文件 (test-network.json) 中的管理员证书和机密(私钥)
修改前
"adminPrivateKey": {
    "path": "/tmp/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore/priv_sk"
}
修改后,将[email protected]改为[email protected]
"adminPrivateKey": {
    "path": "/tmp/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore/priv_sk"
}
# 修改完毕保存退出

# 拷贝一份并命名为org2-network.json
cp org1-network.json org2-network.json
# 修改org2-network.json中对应参数
vim org2-network.json
#将所有的Org1修改文Org2,如下所示
Org1MSP -----> Org2MSP,org1 -----> org2
# 修改完毕保存退出
#或者使用sed
sed -i "s/org1/org2/g" org2-network.json
sed -i "s/Org1/Org2/g" org2-network.json

3.2.修改config.json——多网络配置文件

# config.json文件内只配置了一个组织的网络,所以需要添加第二个组织网络
vim config.json
# 修改为以下配置
{
        "network-configs": {
                "org1-network": {		// 需要和org1-network.json中的名称对应
                        "name": "org1-network",
                        "profile": "./connection-profile/org1-network.json"		// 对应配置文件
                },
                "org2-network": {		// 需要和org2-network.json中的名称对应
                        "name": "org2-network",
                        "profile": "./connection-profile/org2-network.json"		// 对应配置文件
                }
        },
        "license": "Apache-2.0"
}
# 修改完毕后退出

3.3修改docker-compose——部署配置文件

首先要找到fabric使用网络的名字,运行区块链项目,之后输入docker network ls找出区块链网络

docker network ls

Hyperledger Fabric项目搭建区块链浏览器Hyperledger-blockchain-explorer_第3张图片
之后输入

vim docker-compose.yaml

docker-compose.yaml修改后如下:

version: '2.1'

volumes:
  pgdata:
  walletstore:

networks:
  mynetwork.com:
    external:
      name: multinodes_default    # 修改为自己的fabric网络

services:

  explorerdb.mynetwork.com:
    image: hyperledger/explorer-db:latest
    container_name: explorerdb.mynetwork.com
    hostname: explorerdb.mynetwork.com
    ports:     # 暴露端口
      - 5432:5432
    restart: always    # 增加重启参数
    environment:
      - DATABASE_DATABASE=fabricexplorer  # db 库
      - DATABASE_USERNAME=exploreradmin   # db 账户
      - DATABASE_PASSWORD=exploreradminpw  # db 密码
    healthcheck:
      test: "pg_isready -h localhost -p 5432 -q -U postgres"
      interval: 30s
      timeout: 10s
      retries: 5
    volumes:
      - pgdata:/var/lib/postgresql/data
      - /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime    # 与本机时间保持一致
      - /etc/hosts:/etc/hosts    # 映射hosts文件,否则会连接不了其它节点,或者添加extra_hosts参数
    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  # 与上方db 库、账号、密码保持一致
      - DATABASE_USERNAME=exploreradmin
      - DATABASE_PASSWD=exploreradminpw
      - LOG_LEVEL_APP=info
      - LOG_LEVEL_DB=info
      - LOG_LEVEL_CONSOLE=debug
      - LOG_CONSOLE_STDOUT=true
   		 # 浏览器是否开启远程访问, true表示只有部署的机器可以访问
      - DISCOVERY_AS_LOCALHOST=false
    volumes:
      - ./config.json:/opt/explorer/app/platform/fabric/config.json
      - ./connection-profile:/opt/explorer/app/platform/fabric/connection-profile
      - ../crypto-config:/tmp/crypto  # 映射证书目录
      - /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime
      - /etc/hosts:/etc/hosts
      - walletstore:/opt/explorer/wallet
    ports:
      - 8080:8080
    restart: always
    depends_on:
      explorerdb.mynetwork.com:
        condition: service_healthy
    networks:
      - mynetwork.com

4.启动区块链浏览器

docker-compose -f docker-compose.yaml up -d
# 如果是第一次启动,他会自动拉取浏览器镜像
# 如果虚拟机没有网络的话需要提前在外网拉取好explorer-db与explorer,然后导入到相应虚机
docker pull hyperledger/explorer-db:latest
docker pull hyperledger/explorer:latest

docker ps
#结果应该如下所示
CONTAINER ID   IMAGE                              COMMAND                  CREATED          STATUS                    PORTS                                       NAMES
72d7227b1306   hyperledger/explorer:latest        "docker-entrypoint.s…"   39 seconds ago   Up 3 seconds              0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   explorer.mynetwork.com
5ac9b1f927cb   hyperledger/explorer-db:latest     "docker-entrypoint.s…"   39 seconds ago   Up 36 seconds (healthy)   5432/tcp                                    explorerdb.mynetwork.com
6735ebc7baf2   hyperledger/fabric-orderer:2.4.2   "orderer"                22 hours ago     Up 48 minutes             0.0.0.0:7050->7050/tcp, :::7050->7050/tcp   orderer.example.com

若要关闭区块链浏览器需要在explorer路径下输入

docker-compose down -v(此处-v不能去掉,因为删除持久化数据才能在下一次启动区块链浏览器时启动成功)

5.访问区块链浏览器

可以直接在虚拟机内浏览器输入localhost:8080访问区块链浏览器,也可在宿主机输入虚拟机(服务器)ip:8080(需要开放端口)访问

explorer账号:exploreradmin
explorer密码:exploreradminpw
Hyperledger Fabric项目搭建区块链浏览器Hyperledger-blockchain-explorer_第4张图片
登录之后可以查看区块链网络、区块和交易等信息。
Hyperledger Fabric项目搭建区块链浏览器Hyperledger-blockchain-explorer_第5张图片

6.组织内多个peer结点的配置文件

若项目是组织内出现两个peer结点的网络,如果想要配置这种网络的配置文件,只需要将org1-network.json内所有出现peer0的位置同步加上peer1即可,多个peer结点同理多加几个peer即可,如下所示

{
	"name": "org1-network",
	"version": "1.0.0",
	"client": {
		"tlsEnable": true,
		"adminCredential": {
			"id": "exploreradmin",
			"password": "exploreradminpw"
		},
		"enableAuthentication": true,
		"organization": "Org1MSP",
		"connection": {
			"timeout": {
				"peer": {
					"endorser": "300"
				},
				"orderer": "300"
			}
		}
	},
	"channels": {
		"mychannel": {
			"peers": {
				"peer0.org1.example.com": {},
				//加上peer1
				"peer1.org1.example.com": {}
			},
			"connection": {
				"timeout": {
					"peer": {
						"endorser": "6000",
						"eventHub": "6000",
						"eventReg": "6000"
					}
				}
			}
		}
	},
	"organizations": {
		"Org1MSP": {
			"mspid": "Org1MSP",
			"adminPrivateKey": {
				"path": "/tmp/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore/priv_sk"
			},
			//加上peer1
			"peers": ["peer0.org1.example.com","peer1.org1.example.com"],
			"signedCert": {
				"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"
		},
		//加上peer1
		"peer1.org1.example.com": {
			"tlsCACerts": {
				"path": "/tmp/crypto/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/ca.crt"
			},
			"url": "grpcs://peer1.org1.example.com:9051"
		}
	}
}

你可能感兴趣的:(环境配置,区块链,#,Linux项目,fabric,区块链,运维)