Fabric單機節點配置與部署
參考檔案:原文地址
原文寫的就非常好,我這里不過是復制粘貼然后理解了一點罷了,
看不懂的命令就去搜,
fabric命令幫助檔案
這是命令幫助檔案的地址,docker-compose.yaml的檔案一定要理解,
授人以魚不如授人以漁,希望對讀者有所幫助,我也是剛開始學習相關知識,
下面的組態檔,我所用筆記的限制,沒加太多注解,后續整理完值后,可以查看另一篇文章,
1. 單機節點概念
solo模式,該環境中只有一個排序(orderer)服務,從節點(peer)發送的訊息由一個orderer進行排序和產生區塊,適用于開發和測驗環境
2. 部署流程
(1)創建一個檔案夾SingleDemo
[root@iZ2zeb7b8tz2dskwx11e8wZ home]# mkdir SingleDemo
[root@iZ2zeb7b8tz2dskwx11e8wZ home]# cd SingleDemo/
(2)獲取二進制檔案
下載對應版本 地址:https://github.com/hyperledger/fabric/releases/tag/v1.4.6
hyperledger-fabric-linux-amd64-1.4.6.tar.gz
#下載的檔案解壓,可以通過wget或curl下載,慢的話可以windows下載,ftp上傳到服務器
[root@iZ2zeb7b8tz2dskwx11e8wZ fabric-samples]# cp -r bin/ /home/SingleDemo/
#查看bin目錄下檔案
[root@iZ2zeb7b8tz2dskwx11e8wZ SingleDemo]# cd bin/
[root@iZ2zeb7b8tz2dskwx11e8wZ bin]# ll
總用量 157464
-rwxr-xr-x 1 root root 20472336 12月 26 18:44 configtxgen
-rwxr-xr-x 1 root root 22615568 12月 26 18:44 configtxlator
-rwxr-xr-x 1 root root 13554768 12月 26 18:44 cryptogen
-rwxr-xr-x 1 root root 21542608 12月 26 18:44 discover
-rwxr-xr-x 1 root root 12188496 12月 26 18:44 idemixgen
-rwxr-xr-x 1 root root 32257920 12月 26 18:44 orderer
-rwxr-xr-x 1 root root 38595984 12月 26 18:44 peer
(3)準備生成證書和區塊的組態檔
- 配置crypto-config.yaml檔案(私鑰證書組態檔)
OrdererOrgs:
- Name: Orderer
Domain: example.com
Specs:
- Hostname: orderer
PeerOrgs:
- Name: Org1
Domain: org1.example.com
EnableNodeOUs: true
Template:
Count: 1
Users:
Count: 1
- 配置configtx.yaml檔案
Organizations:
- &OrdererOrg
Name: OrdererOrg
ID: OrdererMSP
MSPDir: crypto-config/ordererOrganizations/example.com/msp
Policies:
Readers:
Type: Signature
Rule: "OR('OrdererMSP.member')"
Writers:
Type: Signature
Rule: "OR('OrdererMSP.member')"
Admins:
Type: Signature
Rule: "OR('OrdererMSP.admin')"
- &Org1
Name: Org1MSP
ID: Org1MSP
MSPDir: crypto-config/peerOrganizations/org1.example.com/msp
Policies:
Readers:
Type: Signature
Rule: "OR('Org1MSP.admin', 'Org1MSP.peer', 'Org1MSP.client')"
Writers:
Type: Signature
Rule: "OR('Org1MSP.admin', 'Org1MSP.client')"
Admins:
Type: Signature
Rule: "OR('Org1MSP.admin')"
AnchorPeers:
- Host: peer0.org1.example.com
Port: 7051
Capabilities:
Global: &ChannelCapabilities
V1_1: true
Orderer: &OrdererCapabilities
V1_1: true
Application: &ApplicationCapabilities
V1_2: true
Application: &ApplicationDefaults
Organizations:
Policies:
Readers:
Type: ImplicitMeta
Rule: "ANY Readers"
Writers:
Type: ImplicitMeta
Rule: "ANY Writers"
Admins:
Type: ImplicitMeta
Rule: "MAJORITY Admins"
Capabilities:
<<: *ApplicationCapabilities
Orderer: &OrdererDefaults
OrdererType: solo
Addresses:
- orderer.example.com:7050
BatchTimeout: 2s
BatchSize:
MaxMessageCount: 10
AbsoluteMaxBytes: 98 MB
PreferredMaxBytes: 512 KB
Kafka:
Brokers:
- 127.0.0.1:9092
Organizations:
Policies:
Readers:
Type: ImplicitMeta
Rule: "ANY Readers"
Writers:
Type: ImplicitMeta
Rule: "ANY Writers"
Admins:
Type: ImplicitMeta
Rule: "MAJORITY Admins"
BlockValidation:
Type: ImplicitMeta
Rule: "ANY Writers"
Capabilities:
<<: *OrdererCapabilities
Channel: &ChannelDefaults
Policies:
Readers:
Type: ImplicitMeta
Rule: "ANY Readers"
Writers:
Type: ImplicitMeta
Rule: "ANY Writers"
Admins:
Type: ImplicitMeta
Rule: "MAJORITY Admins"
Capabilities:
<<: *ChannelCapabilities
Profiles:
OneOrgsOrdererGenesis:
<<: *ChannelDefaults
Orderer:
<<: *OrdererDefaults
Organizations:
- *OrdererOrg
Consortiums:
SampleConsortium:
Organizations:
- *Org1
OneOrgsChannel:
Consortium: SampleConsortium
Application:
<<: *ApplicationDefaults
Organizations:
- *Org1
(4)生成公私鑰和證書
[root@iZ2zeb7b8tz2dskwx11e8wZ SingleDemo]# ./bin/cryptogen generate --config=./crypto-config.yaml
org1.example.com
(5)生成創世區塊
[root@iZ2zeb7b8tz2dskwx11e8wZ SingleDemo]# mkdir channel-artifacts
[root@iZ2zeb7b8tz2dskwx11e8wZ SingleDemo]# ./bin/configtxgen -profile OneOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
2021-12-27 18:04:07.575 CST [common.tools.configtxgen] main -> WARN 001 Omitting the channel ID for configtxgen for output operations is deprecated. Explicitly passing the channel ID will be required in the future, defaulting to 'testchainid'.
2021-12-27 18:04:07.575 CST [common.tools.configtxgen] main -> INFO 002 Loading configuration
2021-12-27 18:04:07.585 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2021-12-27 18:04:07.585 CST [common.tools.configtxgen.localconfig] Load -> INFO 004 Loaded configuration: /home/SingleDemo/configtx.yaml
2021-12-27 18:04:07.594 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 005 orderer type: solo
2021-12-27 18:04:07.594 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 006 Loaded configuration: /home/SingleDemo/configtx.yaml
2021-12-27 18:04:07.596 CST [common.tools.configtxgen] doOutputBlock -> INFO 007 Generating genesis block
2021-12-27 18:04:07.596 CST [common.tools.configtxgen] doOutputBlock -> INFO 008 Writing genesis block
[root@iZ2zeb7b8tz2dskwx11e8wZ SingleDemo]# tree channel-artifacts/
channel-artifacts/
└── genesis.block
(6)生成通道配置區塊
[root@iZ2zeb7b8tz2dskwx11e8wZ SingleDemo]# ./bin/configtxgen -profile OneOrgsChannel -outputCreateChannelTx ./channel-artifacts/mychannel.tx -channelID mychannel
2021-12-27 18:25:12.049 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2021-12-27 18:25:12.063 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/SingleDemo/configtx.yaml
2021-12-27 18:25:12.074 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2021-12-27 18:25:12.075 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/SingleDemo/configtx.yaml
2021-12-27 18:25:12.075 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 005 Generating new channel configtx
2021-12-27 18:25:12.075 CST [common.tools.configtxgen.encoder] NewChannelGroup -> WARN 006 Default policy emission is deprecated, please include policy specifications for the channel group in configtx.yaml
2021-12-27 18:25:12.076 CST [common.tools.configtxgen.encoder] NewChannelGroup -> WARN 007 Default policy emission is deprecated, please include policy specifications for the channel group in configtx.yaml
2021-12-27 18:25:12.077 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 008 Writing new channel tx
(7)準備docker組態檔
配置docker-compose-cli.yaml檔案,拷貝到singlepeer目錄下,
- 代碼
version: '2'
services:
orderer.example.com:
container_name: orderer.example.com
image: hyperledger/fabric-orderer
environment:
- ORDERER_GENERAL_LOGLEVEL=debug
- 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
# enabled TLS
- ORDERER_GENERAL_TLS_ENABLED=false
- ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
- ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
- ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
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/example.com/orderers/orderer.example.com/msp:/var/hyperledger/orderer/msp
- ./crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/:/var/hyperledger/orderer/tls
ports:
- 7050:7050
peer0.org1.example.com:
container_name: peer0.org1.example.com
image: hyperledger/fabric-peer
environment:
- CORE_PEER_ID=peer0.org1.example.com
- CORE_PEER_ADDRESS=peer0.org1.example.com:7051
- CORE_PEER_CHAINCODEADDRESS=peer0.org1.example.com:7052
- CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.example.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
# the following setting starts chaincode containers on the same
# bridge network as the peers
# https://docs.docker.com/compose/networking/
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=singlepeer_default
#- CORE_LOGGING_LEVEL=ERROR
- CORE_LOGGING_LEVEL=DEBUG
- CORE_PEER_TLS_ENABLED=false
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
- CORE_PEER_PROFILE_ENABLED=true
- CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
volumes:
- /var/run/:/host/var/run/
- ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp:/etc/hyperledger/fabric/msp
- ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls:/etc/hyperledger/fabric/tls
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
ports:
- 7051:7051
- 7052:7052
- 7053:7053
cli:
container_name: cli
image: hyperledger/fabric-tools
tty: true
environment:
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_LOGGING_LEVEL=DEBUG
- CORE_PEER_ID=cli
- CORE_PEER_ADDRESS=peer0.org1.example.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_PEER_TLS_ENABLED=false
- CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
- CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
volumes:
- /var/run/:/host/var/run/
- ./chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/singlepeer/chaincode/go
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
depends_on:
- orderer.example.com
- peer0.org1.example.com
(8)準備部署智能合約
這是官方案例中的智能合約
[root@iZ2zeb7b8tz2dskwx11e8wZ fabricDemo]# cd fabric-samples/chaincode/chaincode_example02
[root@iZ2zeb7b8tz2dskwx11e8wZ chaincode_example02]# ll
total 12
drwxrwxr-x 2 root root 4096 Mar 29 2019 go
drwxrwxr-x 3 root root 4096 Mar 29 2019 java
drwxrwxr-x 2 root root 4096 Mar 29 2019 node
[root@iZ2zeb7b8tz2dskwx11e8wZ chaincode_example02]# tree go
go
└── chaincode_example02.go
將go下面的目錄拷貝到SingleDemo/chaincode/go中
[root@iZ2zeb7b8tz2dskwx11e8wZ go]# clear
[root@iZ2zeb7b8tz2dskwx11e8wZ go]# cp -r /home/fabricDemo/fabric-samples/chaincode/chaincode_example02/go/chaincode_example02.go .
[root@iZ2zeb7b8tz2dskwx11e8wZ go]# tree
.
└── chaincode_example02.go
(9)啟動fabric網路
- 啟動orderer和peer
[root@iZ2zeb7b8tz2dskwx11e8wZ SingleDemo]# docker-compose -f docker-compose-cli.yaml up -d
Creating network "singledemo_default" with the default driver
Creating orderer.example.com ... done
Creating peer0.org1.example.com ... done
Creating cli ... done
- 啟動cli容器
[root@iZ2zeb7b8tz2dskwx11e8wZ SingleDemo]# docker exec -it cli /bin/bash
root@7246881c69b6:/opt/gopath/src/github.com/hyperledger/fabric/peer#
- 創建channel
peer channel create -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/mychannel.tx
- Peer加入Channel
peer channel join -b mychannel.block
(10)安裝智能合約
- 安裝智能合約
peer chaincode install -n mycc -p github.com/hyperledger/fabric/singlepeer/chaincode/go -v 1.0
#這里安裝可能會出點問題,可能是鏡像掛載沒設定好,對應的go的智能合約
#在go目錄下,這個-p 直到 go就可以了,具體查看cli中的peer命令,希望讀者能自己動手解決
root@5d08bf1c1ba8://opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode install -n mycc -p github.com/hyperledger/fabric/singlepeer/chaincode/go -v 1.0
2021-12-28 04:28:21.339 UTC [main] InitCmd -> WARN 001 CORE_LOGGING_LEVEL is no longer supported, please use the FABRIC_LOGGING_SPEC environment variable
2021-12-28 04:28:21.345 UTC [main] SetOrdererEnv -> WARN 002 CORE_LOGGING_LEVEL is no longer supported, please use the FABRIC_LOGGING_SPEC environment variable
2021-12-28 04:28:21.353 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2021-12-28 04:28:21.353 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2021-12-28 04:28:21.789 UTC [chaincodeCmd] install -> INFO 005 Installed remotely response:<status:200 payload:"OK" >
- 實體化智能合約
peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "AND ('Org1MSP.peer')"
#就是創建a和b的賬戶
#這里也有點問題,會提示
Error: could not assemble transaction, err proposal response was not successful, error code 500, msg error starting container: error starting container: API error (404): network singlepeer_default not found
#這是因為docker-compose.yaml組態檔的問題
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=singlepeer_default
#后面那個指向自己配置的網關 我的是singledemo_default
[root@iZ2zeb7b8tz2dskwx11e8wZ SingleDemo]# docker network ls
NETWORK ID NAME DRIVER SCOPE
f263e076fcfe bridge bridge local
2353ee0c77b7 docker_minifab bridge local
5c954f4d6795 helloworld_default bridge local
281118b78fa3 host host local
d2e1820e1460 minifab bridge local
55f475a6cc84 none null local
424027d782ff react-admin-docker_back bridge local
1560114714fd react-admin-docker_front bridge local
5c83342e31c6 singledemo_default bridge local
- 查詢資料(在peer上)
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
#查詢A,可以查到100的資料
root@7278ab21a056:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
2021-12-28 05:28:14.093 UTC [main] InitCmd -> WARN 001 CORE_LOGGING_LEVEL is no longer supported, please use the FABRIC_LOGGING_SPEC environment variable
2021-12-28 05:28:14.098 UTC [main] SetOrdererEnv -> WARN 002 CORE_LOGGING_LEVEL is no longer supported, please use the FABRIC_LOGGING_SPEC environment variable
100
- peer上進行a向b轉10交易
peer chaincode invoke -C mychannel -n mycc -c '{"Args":["invoke","a","b","10"]}'
root@7278ab21a056:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode invoke -C mychannel -n mycc -c '{"Args":["invoke","a","b","10"]}'
2021-12-28 05:34:14.052 UTC [main] InitCmd -> WARN 001 CORE_LOGGING_LEVEL is no longer supported, please use the FABRIC_LOGGING_SPEC environment variable
2021-12-28 05:34:14.059 UTC [main] SetOrdererEnv -> WARN 002 CORE_LOGGING_LEVEL is no longer supported, please use the FABRIC_LOGGING_SPEC environment variable
2021-12-28 05:34:14.076 UTC [chaincodeCmd] InitCmdFactory -> INFO 003 Retrieved channel (mychannel) orderer endpoint: orderer.example.com:7050
2021-12-28 05:34:14.087 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 004 Chaincode invoke successful. result: status:200
root@7278ab21a056:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode query -C mychannel -n mycc -c '{"Args":["query","b"]}'
2021-12-28 05:34:18.238 UTC [main] InitCmd -> WARN 001 CORE_LOGGING_LEVEL is no longer supported, please use the FABRIC_LOGGING_SPEC environment variable
2021-12-28 05:34:18.243 UTC [main] SetOrdererEnv -> WARN 002 CORE_LOGGING_LEVEL is no longer supported, please use the FABRIC_LOGGING_SPEC environment variable
210
到這里整個專案部署就算完成了,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/396266.html
標籤:區塊鏈
上一篇:SEAL全同態加密開源庫(十七)同態加密-理論知識整理補充
下一篇:Genesis公鏈的運行之DAO
