hyperledger fabric 网络操作基本操作和概念
hyperledger fabric 网络操作基本操作和概念
在搭建好hyperledger fabric环境后,继续进行网络搭建操作 新建文件夹twonodes(与github.com同级)
1. cryptogen 操作
1.1 cryptogen 生成证书文件
1.1.1 生成证书文件模板
cryptogen showtemplate
cryptogen showtemplate > crypto-config.yaml
生成模板配置文件说明
1.1.1.1 排序节点OrdererOrgs配置
OrdererOrgs:
- Name: Orderer
Domain: example.com
EnableNodeOUs: false
Specs:
- Hostname: orderer
1.1.1.2 普通节点PeerOrgs配置
PeerOrgs:
- Name: Org1
Domain: org1.example.com
EnableNodeOUs: true
1.1.1.3 Template 配置
Template:
Count: 1
1.1.1.4 Users用户配置
(user和peer区别,user可以理解为终端,指人,而peer是节点,只是一台运行的机器或集群的一个节点)
Users:
Count: 1
1.1.1.5 crypto-config.yaml
Order组织节点定义
OrdererOrgs:
- Name: Orderer
Domain: trace.com
Specs:
- Hostname: orderer
PeerOrgs:
- Name: Org1
Domain: org1.trace.com
Template:
Count: 1
Users:
Count: 1
- Name: Org2
Domain: org2.trace.com
Template:
Count: 1
Users:
Count: 1
- Name: Org3
Domain: org3.trace.com
Template:
Count: 1
Users:
Count: 1
- Name: Org4
Domain: org4.trace.com
Template:
Count: 1
Users:
Count: 1
- Name: Org5
Domain: org5.trace.com
Template:
Count: 1
Users:
Count: 1
1.1.2 生成密钥材料
cryptogen generate --config=crypto-config.yaml
1.2 cryptogen.yaml 创建通道配置
拷贝/hyperledger/fabric-samples/test-network/configtx/configx.yaml 到目录下 修改msp配置文件(成员服务提供者 (MSP)及相关证书配置 因为Fabric是一个认证性的网络, 所以区块链参与者需要一种向网络中的其他参与者证实自己身份的机制从而在网络中进行交易 证书机构通过生成可以用来证实身份的由公钥和私钥形成的键值对来发放认证信息。因为一个私钥永远不会被公开,所以引入了一种可以证实身份的机制即MSP。例如,一个peer节点用它的私钥进行数字签名或背书交易。接着排序节点包含的该peer节点的公钥会被用来验证交易携带的签名是否合法。私钥被用作生成交易信息上的,只有与私钥相对应的且作为MSP一部分的公钥可以匹配的签名。因此,MSP是一个可让身份被信任和被网络中其他参与者公认的,而不需要暴露成员的私钥的机制。
MSPDir: ../organizations/ordererOrganizations/example.com/msp
MSPDir: crypto-config/ordererOrganizations/example.com/msp
1.2.1 configx.yaml
---
Organizations:
- &OrdererOrg
Name: OrdererOrg
ID: OrdererMSP
MSPDir: crypto-config/ordererOrganizations/trace.com/msp
- &Org1
Name: Org1MSP
ID: Org1MSP
MSPDir: crypto-config/peerOrganizations/org1.trace.com/msp
AnchorPeers:
- Host: peer0.org1.trace.com
Port: 7051
- &Org2
Name: Org2MSP
ID: Org2MSP
MSPDir: crypto-config/peerOrganizations/org2.trace.com/msp
AnchorPeers:
- Host: peer0.org2.trace.com
Port: 7051
- &Org3
Name: Org3MSP
ID: Org3MSP
MSPDir: crypto-config/peerOrganizations/org3.trace.com/msp
AnchorPeers:
- Host: peer0.org3.trace.com
Port: 7051
- &Org4
Name: Org4MSP
ID: Org4MSP
MSPDir: crypto-config/peerOrganizations/org4.trace.com/msp
AnchorPeers:
- Host: peer0.org4.trace.com
Port: 7051
- &Org5
Name: Org5MSP
ID: Org5MSP
MSPDir: crypto-config/peerOrganizations/org5.trace.com/msp
AnchorPeers:
- Host: peer0.org5.trace.com
Port: 7051
Capabilities:
Global: &ChannelCapabilities
V1_1: true
Orderer: &OrdererCapabilities
V1_1: true
Application: &ApplicationCapabilities
V1_2: true
Application: &ApplicationDefaults
Organizations:
Orderer: &OrdererDefaults
OrdererType: solo
Addresses:
- orderer.trace.com:7050
BatchTimeout: 2s
BatchSize:
MaxMessageCount: 10
AbsoluteMaxBytes: 99 MB
PreferredMaxBytes: 512 KB
Kafka:
Brokers:
- 127.0.0.1:9092
Organizations:
Profiles:
FiveOrgsOrdererGenesis:
Capabilities:
<<: *ChannelCapabilities
Orderer:
<<: *OrdererDefaults
Organizations:
- *OrdererOrg
Capabilities:
<<: *OrdererCapabilities
Consortiums:
SampleConsortium:
Organizations:
- *Org1
- *Org2
- *Org3
- *Org4
- *Org5
FiveOrgsChannel:
Consortium: SampleConsortium
Application:
<<: *ApplicationDefaults
Organizations:
- *Org1
- *Org2
- *Org3
- *Org4
- *Org5
Capabilities:
<<: *ApplicationCapabilities
1.3 网络初始化
configtxgen -profile SampleMultiNodeEtcdRaft -channelID systemchannel -outputBlock ./channel-artifacts/genesis.block
configtxgen -profile Thr eeOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID testchannel
configtxgen -profile ThreeOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID appchannel -asOrg Org1MSP
configtxgen -profile ThreeOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID appchannel -asOrg Org2MSP
configtxgen -profile ThreeOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org3MSPanchors.tx -channelID appchannel -asOrg Org3MSP
2. docker compose
区块链docker节点集群搭建(基于docker compose)
2.1 docker-compose.yaml
version: '2'
volumes:
orderer.trace.com:
peer0.org1.trace.com:
peer0.org2.trace.com:
peer0.org3.trace.com:
peer0.org4.trace.com:
peer0.org5.trace.com:
networks:
basic:
services:
ca.trace.com:
image: hyperledger/fabric-ca
environment:
- FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
- FABRIC_CA_SERVER_CA_NAME=ca.trace.com
- FABRIC_CA_SERVER_CA_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca.org1.trace.com-cert.pem
- FABRIC_CA_SERVER_CA_KEYFILE=/etc/hyperledger/fabric-ca-server-config/38495dada2d3a879981547003d601ed405cde661e55e568cce718a6618360d79_sk
ports:
- 7054:7054
command: sh -c 'fabric-ca-server start -b admin:adminpw -d'
volumes:
- ./crypto-config/peerOrganizations/org1.trace.com/ca/:/etc/hyperledger/fabric-ca-server-config
container_name: ca.trace.com
networks:
- basic
orderer.trace.com:
container_name: orderer.trace.com
image: hyperledger/fabric-orderer
environment:
- ORDERER_GENERAL_LOGLEVEL=INFO
- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
- ORDERER_GENERAL_GENESISMETHOD=file
- ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block
- ORDERER_GENERAL_LOCALMSPID=OrdererMSP
- ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
working_dir: /opt/gopath/src/github.com/hyperledger/fabric
command: orderer
volumes:
- ./channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block
- ./crypto-config/ordererOrganizations/trace.com/orderers/orderer.trace.com/msp:/var/hyperledger/orderer/msp
- ./crypto-config/ordererOrganizations/trace.com/orderers/orderer.trace.com/tls/:/var/hyperledger/orderer/tls
- orderer.trace.com:/var/hyperledger/production/orderer
ports:
- 7050:7050
networks:
- basic
peer0.org1.trace.com:
container_name: peer0.org1.trace.com
image: hyperledger/fabric-peer
environment:
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_PEER_ID=peer0.org1.trace.com
- CORE_LOGGING_PEER=info
- CORE_CHAINCODE_LOGGING_LEVEL=info
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/peer/
- CORE_PEER_ADDRESS=peer0.org1.trace.com:7051
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=basic-network_basic
- CORE_LEDGER_STATE_STATEDATABASE=CouchDB
- CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984
- CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
- CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
ports:
- 7051:7051
- 7053:7053
volumes:
- /var/run/:/host/var/run/
- ./crypto-config/peerOrganizations/org1.trace.com/peers/peer0.org1.trace.com/msp:/etc/hyperledger/msp/peer
- ./crypto-config/peerOrganizations/org1.trace.com/users:/etc/hyperledger/msp/users
- ./config:/etc/hyperledger/configtx
- peer0.org1.trace.com:/var/hyperledger/production
depends_on:
- orderer.trace.com
- couchdb
networks:
- basic
peer0.org2.trace.com:
container_name: peer0.org2.trace.com
image: hyperledger/fabric-peer
environment:
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_PEER_ID=peer0.org2.trace.com
- CORE_LOGGING_PEER=info
- CORE_CHAINCODE_LOGGING_LEVEL=info
- CORE_PEER_LOCALMSPID=Org2MSP
- CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/peer/
- CORE_PEER_ADDRESS=peer0.org2.trace.com:7051
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=basic-network_basic
- CORE_LEDGER_STATE_STATEDATABASE=CouchDB
- CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984
- CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
- CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
ports:
- 8051:7051
- 8053:7053
volumes:
- /var/run/:/host/var/run/
- ./crypto-config/peerOrganizations/org2.trace.com/peers/peer0.org2.trace.com/msp:/etc/hyperledger/msp/peer
- ./crypto-config/peerOrganizations/org2.trace.com/users:/etc/hyperledger/msp/users
- ./config:/etc/hyperledger/configtx
- peer0.org2.trace.com:/var/hyperledger/production
depends_on:
- orderer.trace.com
- couchdb
networks:
- basic
peer0.org3.trace.com:
container_name: peer0.org3.trace.com
image: hyperledger/fabric-peer
environment:
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_PEER_ID=peer0.org3.trace.com
- CORE_LOGGING_PEER=info
- CORE_CHAINCODE_LOGGING_LEVEL=info
- CORE_PEER_LOCALMSPID=Org3MSP
- CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/peer/
- CORE_PEER_ADDRESS=peer0.org3.trace.com:7051
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=basic-network_basic
- CORE_LEDGER_STATE_STATEDATABASE=CouchDB
- CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984
- CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
- CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
ports:
- 9051:7051
- 9053:7053
volumes:
- /var/run/:/host/var/run/
- ./crypto-config/peerOrganizations/org3.trace.com/peers/peer0.org3.trace.com/msp:/etc/hyperledger/msp/peer
- ./crypto-config/peerOrganizations/org3.trace.com/users:/etc/hyperledger/msp/users
- ./config:/etc/hyperledger/configtx
- peer0.org3.trace.com:/var/hyperledger/production
depends_on:
- orderer.trace.com
- couchdb
networks:
- basic
peer0.org4.trace.com:
container_name: peer0.org4.trace.com
image: hyperledger/fabric-peer
environment:
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_PEER_ID=peer0.org4.trace.com
- CORE_LOGGING_PEER=info
- CORE_CHAINCODE_LOGGING_LEVEL=info
- CORE_PEER_LOCALMSPID=Org4MSP
- CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/peer/
- CORE_PEER_ADDRESS=peer0.org4.trace.com:7051
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=basic-network_basic
- CORE_LEDGER_STATE_STATEDATABASE=CouchDB
- CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984
- CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
- CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
ports:
- 10051:7051
- 10053:7053
volumes:
- /var/run/:/host/var/run/
- ./crypto-config/peerOrganizations/org4.trace.com/peers/peer0.org4.trace.com/msp:/etc/hyperledger/msp/peer
- ./crypto-config/peerOrganizations/org4.trace.com/users:/etc/hyperledger/msp/users
- ./config:/etc/hyperledger/configtx
- peer0.org4.trace.com:/var/hyperledger/production
depends_on:
- orderer.trace.com
- couchdb
networks:
- basic
peer0.org5.trace.com:
container_name: peer0.org5.trace.com
image: hyperledger/fabric-peer
environment:
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_PEER_ID=peer0.org4.trace.com
- CORE_LOGGING_PEER=info
- CORE_CHAINCODE_LOGGING_LEVEL=info
- CORE_PEER_LOCALMSPID=Org5MSP
- CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/peer/
- CORE_PEER_ADDRESS=peer0.org5.trace.com:7051
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=basic-network_basic
- CORE_LEDGER_STATE_STATEDATABASE=CouchDB
- CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984
- CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
- CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
ports:
- 11051:7051
- 11053:7053
volumes:
- /var/run/:/host/var/run/
- ./crypto-config/peerOrganizations/org5.trace.com/peers/peer0.org5.trace.com/msp:/etc/hyperledger/msp/peer
- ./crypto-config/peerOrganizations/org5.trace.com/users:/etc/hyperledger/msp/users
- ./config:/etc/hyperledger/configtx
- peer0.org5.trace.com:/var/hyperledger/production
depends_on:
- orderer.trace.com
- couchdb
networks:
- basic
couchdb:
container_name: couchdb
image: hyperledger/fabric-couchdb
environment:
- COUCHDB_USER=
- COUCHDB_PASSWORD=
ports:
- 5984:5984
networks:
- basic
cli:
container_name: cli
image: hyperledger/fabric-tools
tty: true
stdin_open: true
environment:
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_LOGGING_LEVEL=info
- CORE_PEER_ID=cli
- CORE_PEER_ADDRESS=peer0.org1.trace.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.trace.com/users/Admin@org1.trace.com/msp
- CORE_CHAINCODE_KEEPALIVE=10
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: /bin/bash
volumes:
- /var/run/:/host/var/run/
- ./../chaincode/:/opt/gopath/src/github.com/chaincode
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
- ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
depends_on:
- orderer.trace.com
- peer0.org1.trace.com
- peer0.org2.trace.com
- peer0.org3.trace.com
- peer0.org4.trace.com
- peer0.org5.trace.com
networks:
- basic
3 问题解决记录
问题解决:
Error: error getting endorser client for channel: endorser client failed to connect to peer0.org1.trace.com:7051: failed to create new connection: connection error: desc = "transport: error while dialing: dial tcp 172.22.0.7:7051: connect: connection refused"
Error: error getting endorser client for channel: endorser client failed to connect to peer0.org1.trace.com:7051: failed to create new connection: context deadline exceeded
解决方法:
sudo vi /etc/resolv.conf
按:wq!退出
4 启动区块链docker节点
4.1 常用docker命令
删除所有容器
docker stop $(docker ps -q)
docker rm $(docker ps -aq)
删除所有镜像
docker rmi -f $(docker images -qa)
删除数据卷
# 删除数据卷:
docker volume rm $(docker volume ls -q)
# 删除 network:
docker network rm $(docker network ls -q)
一键执行
docker stop $(docker ps -q)
docker rm $(docker ps -aq)
docker volume rm $(docker volume ls -q)
./start.sh
拉取对应版本镜像(包含区块链fabric1.4.7和1.2.0)
docker pull hyperledger/fabric-peer:1.4.7
docker tag hyperledger/fabric-peer:1.4.7 hyperledger/fabric-peer:latest
docker pull hyperledger/fabric-orderer:1.4.7
docker tag hyperledger/fabric-orderer:1.4.7 hyperledger/fabric-orderer:latest
docker pull hyperledger/fabric-tools:1.4.7
docker tag hyperledger/fabric-tools:1.4.7 hyperledger/fabric-tools:latest
docker pull hyperledger/fabric-ccenv:1.4.7
docker tag hyperledger/fabric-ccenv:1.4.7 hyperledger/fabric-ccenv:latest
docker pull hyperledger/fabric-ca:1.4.7
docker tag hyperledger/fabric-ca:1.4.7 hyperledger/fabric-ca:latest
docker pull hyperledger/fabric-baseos:0.4.15
docker tag hyperledger/fabric-baseos:0.4.15 hyperledger/fabric-baseos:latest
docker pull hyperledger/fabric-couchdb:0.4.15
docker tag hyperledger/fabric-couchdb:0.4.15 hyperledger/fabric-couchdb:latest
docker pull hyperledger/fabric-kafka:0.4.15
docker tag hyperledger/fabric-kafka:0.4.15 hyperledger/fabric-kafka:latest
docker pull hyperledger/fabric-zookeeper:0.4.15
docker tag hyperledger/fabric-zookeeper:0.4.15 hyperledger/fabric-zookeeper:latest
1.2 版本
## 1) 基础镜像
docker pull hyperledger/fabric-peer:1.2.0
docker tag hyperledger/fabric-peer:1.2.0 hyperledger/fabric-peer:latest
docker pull hyperledger/fabric-orderer:1.2.0
docker tag hyperledger/fabric-orderer:1.2.0 hyperledger/fabric-orderer:latest
docker pull hyperledger/fabric-tools:1.2.0
docker tag hyperledger/fabric-tools:1.2.0 hyperledger/fabric-tools:latest
docker pull hyperledger/fabric-ccenv:1.2.0
docker tag hyperledger/fabric-ccenv:1.2.0 hyperledger/fabric-ccenv:latest
docker pull hyperledger/fabric-ca:1.2.0
docker tag hyperledger/fabric-ca:1.2.0 hyperledger/fabric-ca:latest
## 2) 数据库与消息队列镜像
docker pull hyperledger/fabric-baseos:0.4.10
docker tag hyperledger/fabric-baseos:0.4.10 hyperledger/fabric-baseos:latest
docker pull hyperledger/fabric-couchdb:0.4.10
docker tag hyperledger/fabric-couchdb:0.4.10 hyperledger/fabric-couchdb:latest
docker pull hyperledger/fabric-kafka:0.4.10
docker tag hyperledger/fabric-kafka:0.4.10 hyperledger/fabric-kafka:latest
docker pull hyperledger/fabric-zookeeper:0.4.10
docker tag hyperledger/fabric-zookeeper:0.4.10 hyperledger/fabric-zookeeper:latest
## 3) Java语言包镜像(可选)
# docker pull hyperledger/fabric-javaenv:1.2.0
# docker tag hyperledger/fabric-javaenv:1.2.0 hyperledger/fabric-javaenv:latest
5 fabric-node-sdk后台搭建
nodejs 后台搭建(在ubuntu 阿里云服务器) 安装nvm
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash
执行
source ~/.bashrc
下载node依赖
nvm install 12.13.1
rm -rf node_modules
rm -rf hfc-key-store
nvm use 12.13.1
npm install
nvm use 12.13.1
npm rebuild
npm install fabric-client
npm install fabric-ca-client
node enrollAdmin.js
node registerUser.js
npm install express
npm install body-parser
npm install influx
npm install forever -g
forever start app.js
访问http://xxx.xxx.xxx:5984/_utils/#查看区块链的couchdb node sdk后台查看 docker节点查看
|