参考文档:原文地址
原文写的就非常好,我这里不过是复制粘贴然后理解了一点罢了。
看不懂的命令就去搜,
fabric命令帮助文档
这是命令帮助文档的地址,docker-compose.yaml的文件一定要理解。
授人以鱼不如授人以渔,希望对读者有所帮助,我也是刚开始学习相关知识。
下面的配置文件,我所用笔记的限制,没加太多注解,后续整理完值后,可以查看另一篇文章。
大家可以稍微看看,看不懂的地方可以在线打扰。企鹅号:2663189778
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:
实例化智能合约
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
到这里整个项目部署就算完成了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)