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

1.下载配置文件

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

# 根据官网来部署# 在项目目录创建文件夹# org1部署区块浏览器mkdir explorercd explorer# 下载配置文件wget https://raw.githubusercontent.com/hyperledger/blockchain-explorer/main/examples/net1/config.jsonwget https://raw.githubusercontent.com/hyperledger/blockchain-explorer/main/examples/net1/connection-profile/test-network.json -P connection-profilewget 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文件夹内


如果虚拟机没有联网,导致下载不下来,也可以前往Fabric-explorer附件下载进行下载,之后在本地根据需要修改,修改后上传到虚拟机

2.拷贝证书目录

mkdir organizationscp -r ../crypto-config/* organizationscd explorer

此时目录结构如下图所示

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

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

# 先修改test-network.json文件为org1-network.jsonmv 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/User1@org1.example.com/msp/keystore/priv_sk"}修改后,将User1@org1.example.com改为Admin@org1.example.com"adminPrivateKey": {"path": "/tmp/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/priv_sk"}# 修改完毕保存退出# 拷贝一份并命名为org2-network.jsoncp org1-network.json org2-network.json# 修改org2-network.json中对应参数vim org2-network.json#将所有的Org1修改文Org2,如下所示Org1MSP -----> Org2MSP,org1 -----> org2# 修改完毕保存退出#或者使用sedsed -i "s/org1/org2/g" org2-network.jsonsed -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


之后输入

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:latestcontainer_name: explorerdb.mynetwork.comhostname: explorerdb.mynetwork.comports: # 暴露端口- 5432:5432restart: 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: 30stimeout: 10sretries: 5volumes:- pgdata:/var/lib/postgresql/data- /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime# 与本机时间保持一致- /etc/hosts:/etc/hosts# 映射hosts文件,否则会连接不了其它节点,或者添加extra_hosts参数networks:- mynetwork.comexplorer.mynetwork.com:image: hyperledger/explorer:latestcontainer_name: explorer.mynetwork.comhostname: explorer.mynetwork.comenvironment:- 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=falsevolumes:- ./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/walletports:- 8080:8080restart: alwaysdepends_on:explorerdb.mynetwork.com:condition: service_healthynetworks:- mynetwork.com

4.启动区块链浏览器

docker-compose -f docker-compose.yaml up -d# 如果是第一次启动,他会自动拉取浏览器镜像# 如果虚拟机没有网络的话需要提前在外网拉取好explorer-db与explorer,然后导入到相应虚机docker pull hyperledger/explorer-db:latestdocker pull hyperledger/explorer:latest
docker ps#结果应该如下所示CONTAINER ID IMAGECOMMANDCREATEDSTATUSPORTS NAMES72d7227b1306 hyperledger/explorer:latest"docker-entrypoint.s…" 39 seconds ago Up 3 seconds0.0.0.0:8080->8080/tcp, :::8080->8080/tcp explorer.mynetwork.com5ac9b1f927cb hyperledger/explorer-db:latest "docker-entrypoint.s…" 39 seconds ago Up 36 seconds (healthy) 5432/tcpexplorerdb.mynetwork.com6735ebc7baf2 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

登录之后可以查看区块链网络、区块和交易等信息。

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/Admin@org1.example.com/msp/keystore/priv_sk"},//加上peer1"peers": ["peer0.org1.example.com","peer1.org1.example.com"],"signedCert": {"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"},//加上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"}}}

7 部署问题

如果部署后出现FabricGateway – Failed to create wallet,pleasse check the configureration, and valide file path:{},如下图所示:

如果检查不出来有什么问题,可能是你的json文件格式在复制粘贴时出现了问题,可以前往Fabric-explorer附件进行下载,之后根据自己的fabric网络有几个组织、几个peer结点来修改json文件