本文章所有操作基于的操作系统版本是:ubuntu16.04 64位
上一节中我们成功安装Fabric之后,就可以启动相关模块开始工作了,但是Fabric的配置和启动过程比较繁琐,在后续文章中会介绍详细的模块使用方法,在这之前我们先搭建一个简单的Fabric网络,这个网络虽然简单,但是包含了fabric的核心组成部分。
docker容器可以很方便的部署fabric节点,但是却不能很容易的调式fabric的源代码,如果要深入学习fabric源码,可能要经常进行fabric 源码调式,所以我们在容器外来搭建这个简单的fabric网络。
首先创建一个目录,并切换到该目录下,这个简单的网路所有配置文件放到这个目录下:
mkdir -p ~/fabric-ws/simple-demo/
cd ~/fabric-ws/simple-demo
创建一个名为crypto-config.yaml的配置文件,内容如下:
OrdererOrgs:
- Name: Orderer
Domain: simple-network.com
Specs:
- Hostname: orderer
PeerOrgs:
- Name: Org1
Domain: org1.simple-network.com
Template:
Count: 2
Users:
Count: 2
- Name: Org2
Domain: org2.simple-network.com
Template:
Count: 2
Users:
Count: 2
生成证书
cryptogen generate --config=crypto-config.yaml --output ./crypto-config
修改端口映射文件:
sudo vi /etc/host
追加如下内容:
127.0.0.1 orderer.simple-network.com
127.0.0.1 peer0.org1.simple-network.com
127.0.0.1 peer1.org1.simple-network.com
127.0.0.1 peer0.org2.simple-network.com
127.0.0.1 peer1.org2.simple-network.com
注意 :hosts文件中的127.0.0.1是本机IP,如果是多台机器部署请根据各个机器的IP指定,不要照抄。
测试是否能ping通
ping orderer.simple-network.com
首先创建一个order目录,来存放order的相关配置
mkdir -p ~/fabric-ws/simple-demo/order
cd ~/fabric-ws/simple-demo/order
在order目录中创建一个名为configtx.yaml的配置文件,内容如下:
Profiles:
TestTwoOrgsOrdererGenesis:
Orderer:
<<: *OrdererDefaults
Organizations:
- *OrdererOrg
Consortiums:
SampleConsortium:
Organizations:
- *Org1
- *Org2
TestTwoOrgsChannel:
Consortium: SampleConsortium
Application:
<<: *ApplicationDefaults
Organizations:
- *Org1
- *Org2
Organizations:
- &OrdererOrg
Name: OrdererOrg
ID: OrdererMSP
MSPDir: /home/zym/fabric-ws/simple-demo/crypto-config/ordererOrganizations/simple-network.com/msp
- &Org1
Name: Org1MSP
ID: Org1MSP
MSPDir: /home/zym/fabric-ws/simple-demo/crypto-config/peerOrganizations/org1.simple-network.com/msp
AnchorPeers:
- Host: peer0.org1.simple-network.com
- Port: 7051
- &Org2
Name: Org2MSP
ID: Org2MSP
MSPDir: /home/zym/fabric-ws/simple-demo/crypto-config/peerOrganizations/org2.simple-network.com/msp
AnchorPeers:
- Host: peer0.org2.simple-network.com
- Port: 7051
Orderer: &OrdererDefaults
OrdererType: solo
Addresses:
- orderer.simple-network.com:7050
BatchTimeout: 2s
BatchSize:
MaxMessageCount: 10
AbsoluteMaxBytes: 98 MB
PreferredMaxBytes: 512 KB
Kafka:
Brokers:
- 127.0.0.1:9092
Organizations:
Application: &ApplicationDefaults
Organizations:
根据configtx.yaml的配置生成创世区块
configtxgen -profile TestTwoOrgsOrdererGenesis -outputBlock ./orderer.genesis.block
根据configtx.yaml的配置生成名为testChannel的channel文件
configtxgen -profile TestTwoOrgsChannel -outputCreateChannelTx ./testchannel.tx -channelID testchannel
根据configtx.yaml的配置生成锚点文件
//org1的锚点文件
configtxgen -profile TestTwoOrgsChannel -outputAnchorPeersUpdate ./Org1MSPanchors.tx -channelID testchannel -asOrg Org1MSP
//org2的锚点文件
configtxgen -profile TestTwoOrgsChannel -outputAnchorPeersUpdate ./Org2MSPanchors.tx -channelID testchannel -asOrg Org2MSP
编辑Order节点配置信息文件,可以使用Fabric中源码提供的模板进行修改。
拷贝模板文件到order目录中:
cd /home/zym/fabric-ws/simple-demo/order
cp ~/go/src/github.com/hyperledger/fabric/sampleconfig/orderer.yaml .
修改orderer.yaml文件, 下面是修改以后的内容:
General:
LedgerType: file
ListenAddress: 0.0.0.0
ListenPort: 7050
TLS:
Enabled: false
PrivateKey: /home/zym/fabric-ws/simple-demo/crypto-config/ordererOrganizations/simple-network.com/orderers/orderer.simple-network.com/tls/server.key
Certificate: /home/zym/fabric-ws/simple-demo/crypto-config/ordererOrganizations/simple-network.com/orderers/orderer.simple-network.com/tls/server.crt
RootCAs:
- /home/zym/fabric-ws/simple-demo/crypto-config/ordererOrganizations/simple-network.com/orderers/orderer.simple-network.com/tls/ca.crt
ClientAuthEnabled: false
ClientRootCAs:
LogLevel: debug
#LogFormat: '%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x} %{color:reset} %{message}'
GenesisMethod: file
GenesisProfile: TestOrgsOrdererGenesis
GenesisFile: /home/zym/fabric-ws/simple-demo/order/orderer.genesis.block
LocalMSPDir: /home/zym/fabric-ws/simple-demo/crypto-config/ordererOrganizations/simple-network.com/orderers/orderer.simple-network.com/msp
LocalMSPID: OrdererMSP
Profile:
Enabled: false
Address: 0.0.0.0:6060
BCCSP:
Default: SW
SW:
Hash: SHA2
Security: 256
FileKeyStore:
KeyStore:
FileLedger:
Location: /home/zym/fabric-ws/simple-demo/order/production/orderer
Prefix: hyperledger-fabric-ordererledger
RAMLedger:
HistorySize: 1000
Kafka:
Retry:
ShortInterval: 5s
ShortTotal: 10m
LongInterval: 5m
LongTotal: 12h
NetworkTimeouts:
DialTimeout: 10s
ReadTimeout: 10s
WriteTimeout: 10s
Metadata:
RetryBackoff: 250ms
RetryMax: 3
Producer:
RetryBackoff: 100ms
RetryMax: 3
Consumer:
RetryBackoff: 2s
Verbose: false
TLS:
Enabled: false
PrivateKey:
Certificate:
#File: path/to/Certificate
RootCAs:
#File: path/to/RootCAs
Version:
在orderer.yaml所在目录运行如下命令启动orderer:
orderer start
创建一个peer目录,将Fabirc源码中的示例配置文件拷贝中peer0-org1目录中:
mkdir -p /home/zym/fabric-ws/simple-demo/peer0-org1
cd /home/zym/fabric-ws/simple-demo/peer0-org1
cp ~/go/src/github.com/hyperledger/fabric/sampleconfig/core.yaml .
修改配置文件,修改后的内容如下:
logging:
peer: debug
cauthdsl: warning
gossip: warning
ledger: info
msp: warning
policies: warning
grpc: error
format: '%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}'
peer:
id: peer0.org1.simple-network.com
networkId: dev
listenAddress: 0.0.0.0:7051
address: peer0.org1.simple-network.com:7051
chaincodeListenAddress: 0.0.0.0:7052
addressAutoDetect: false
gomaxprocs: -1
gossip:
bootstrap: 127.0.0.1:7051
useLeaderElection: true
orgLeader: false
endpoint:
maxBlockCountToStore: 100
maxPropagationBurstLatency: 10ms
maxPropagationBurstSize: 10
propagateIterations: 1
propagatePeerNum: 3
pullInterval: 4s
pullPeerNum: 3
requestStateInfoInterval: 4s
publishStateInfoInterval: 4s
stateInfoRetentionInterval:
publishCertPeriod: 10s
skipBlockVerification: false
dialTimeout: 3s
connTimeout: 2s
recvBuffSize: 20
sendBuffSize: 200
digestWaitTime: 1s
requestWaitTime: 1s
responseWaitTime: 2s
aliveTimeInterval: 5s
aliveExpirationTimeout: 25s
reconnectInterval: 2
externalEndpoint: peer0.org1.simpe-network.com:7051
election:
startupGracePeriod: 15s
membershipSampleInterval: 1s
leaderAliveThreshold: 10s
leaderElectionDuration: 5s
pvtData:
maxPeers: 3
minAck: 3
events:
address: 0.0.0.0:7053
buffersize: 100
timeout: 10ms
tls:
enabled: false
cert:
file: /home/zym/fabric-ws/simple-demo/crypto-config/peerOrganizations/org1.simple-network.com/peers/peer0.org1.simple-network.com/tls/server.crt
key:
file: /home/zym/fabric-ws/simple-demo/crypto-config/peerOrganizations/org1.simple-network.com/peers/peer0.org1.simple-network.com/tls/server.key
rootcert:
file: /home/zym/fabric-ws/simple-demo/crypto-config/peerOrganizations/org1.simple-network.com/peers/peer0.org1.simple-network.com/tls/ca.crt
serverhostoverride:
fileSystemPath: /home/zym/fabric-ws/simple-demo/peer0-org1/production
BCCSP:
Default: SW
SW:
Hash: SHA2
Security: 256
FileKeyStore:
KeyStore:
mspConfigPath: /home/zym/fabric-ws/simple-demo/crypto-config/peerOrganizations/org1.simple-network.com/peers/peer0.org1.simple-network.com/msp
localMspId: Org1MSP
profile:
enabled: false
listenAddress: 0.0.0.0:6060
handlers:
authFilter: "DefaultAuth"
decorator: "DefaultDecorator"
vm:
endpoint: unix:///var/run/docker.sock
docker:
tls:
enabled: false
ca:
file: docker/ca.crt
cert:
file: docker/tls.crt
key:
file: docker/tls.key
attachStdout: false
hostConfig:
NetworkMode: host
Dns:
LogConfig:
Type: json-file
Config:
max-size: "50m"
max-file: "5"
Memory: 2147483648
chaincode:
peerAddress:
id:
path:
name:
builder: $(DOCKER_NS)/fabric-ccenv:$(ARCH)-$(PROJECT_VERSION)
golang:
runtime: $(BASE_DOCKER_NS)/fabric-baseos:$(ARCH)-$(BASE_VERSION)
car:
runtime: $(BASE_DOCKER_NS)/fabric-baseos:$(ARCH)-$(BASE_VERSION)
java:
Dockerfile: | from $(DOCKER_NS)/fabric-javaenv:$(ARCH)-$(PROJECT_VERSION)
startuptimeout: 300s
executetimeout: 30s
mode: net
keepalive: 0
system:
cscc: enable
lscc: enable
escc: enable
vscc: enable
qscc: enable
logging:
level: info
shim: warning
format: '%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}'
ledger:
blockchain:
state:
stateDatabase: goleveldb
couchDBConfig:
couchDBAddress: 127.0.0.1:5984
username:
password:
maxRetries: 3
maxRetriesOnStartup: 10
requestTimeout: 35s
queryLimit: 10000
history:
enableHistoryDatabase: true
在core.yaml所在文件夹内执行一下命令启动order节点:
export set FABRIC_FCG_PATH=/home/zym/fabric-ws/simple-demo/peer0-org1/
peer node start >> log_peer.log 2>&1 &
首先切换到simple-demo目录, 然后在simple-demo下执行后续操作:
cd ~/fabric-ws/simple-demo
第一步:创建通道
export set CORE_PEER_LOCALMSPID=Org1MSP
export set CORE_PEER_MSPCONFIGPATH=/home/zym/fabric-ws/simple-demo/crypto-config/peerOrganizations/org1.simple-network.com/users/[email protected]/msp
peer channel create -t 50 -o orderer.simple-network.com:7050 -c testchannel -f /home/zym/fabric-ws/simple-demo/order/testchannel.tx
第二步:让已经运行的peer加入到通道中
export set CORE_PEER_LOCALMSPID=Org1MSP
export set CORE_PEER_MSPCONFIGPATH=/home/zym/fabric-ws/simple-demo/crypto-config/peerOrganizations/org1.simple-network.com/users/[email protected]/msp
export set CORE_PEER_ADDRESS=peer0.org1.simple-network.com:7051
peer channel join -b /home/zym/fabric-ws/simple-demo/testchannel.block
第三步:更新锚节点
export set CORE_PEER_LOCALMSPID=Org1MSP
export set CORE_PEER_MSPCONFIGPATH=/home/zym/fabric-ws/simple-demo/crypto-config/peerOrganizations/org1.simple-network.com/users/[email protected]/msp
export set CORE_PEER_ADDRESS=peer0.org1.simple-network.com:7051
peer channel update -o orderer.simple-network.com:7050 -c testchannel -f /home/zym/fabric-ws/simple-demo/order/Org1MSPanchors.tx
我们这里测试部署Fabric源码自带的Chaincode:
$GOPATH/src/github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
第一步:部署chaincode代码
export set CORE_PEER_LOCALMSPID=Org1MSP
export set CORE_PEER_MSPCONFIGPATH=/home/zym/fabric-ws/simple-demo/crypto-config/peerOrganizations/org1.simple-network.com/users/[email protected]/msp
export set CORE_PEER_ADDRESS=peer0.org1.simple-network.com:7051
peer chaincode install -n r_test_cc6 -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
第二步:部署实例化chaincode代码
export set CORE_PEER_LOCALMSPID=Org1MSP
export set CORE_PEER_MSPCONFIGPATH=/home/zym/fabric-ws/simple-demo/crypto-config/peerOrganizations/org1.simple-network.com/users/[email protected]/msp
export set CORE_PEER_ADDRESS=peer0.org1.simple-network.com:7051
peer chaincode instantiate -o orderer.simple-network.com:7050 -C testchannel -n r_test_cc6 -v 1.0 -c '{"Args":["init", "a", "100", "b", "200"]}' -P "OR ('Org1MSP.member', 'Org2MSP.member')"
第三步:调用chaincode写入
export set CORE_PEER_LOCALMSPID=Org1MSP
export set CORE_PEER_MSPCONFIGPATH=/home/zym/fabric-ws/simple-demo/crypto-config/peerOrganizations/org1.simple-network.com/users/[email protected]/msp
export set CORE_PEER_ADDRESS=peer0.org1.simple-network.com:7051
peer chaincode invoke -o orderer.simple-network.com:7050 -C testchannel -n r_test_cc6 -v 1.0 -c '{"Args":["invoke", "a", "b", "1"]}'
第四步:调用chaincode查询
export set CORE_PEER_LOCALMSPID=Org1MSP
export set CORE_PEER_MSPCONFIGPATH=/home/zym/fabric-ws/simple-demo/crypto-config/peerOrganizations/org1.simple-network.com/users/[email protected]/msp
export set CORE_PEER_ADDRESS=peer0.org1.simple-network.com:7051
peer chaincode query -o orderer.simple-network.com:7050 -C testchannel -n r_test_cc6 -v 1.0 -c '{"Args":["query", "a"]}'
本篇文章介绍了运行一个Fabric系统所需要的步骤,包括Fabric核心模块的编译,运行和配置。同时还介绍了Fabric核心概念Channel的创建和使用,最后介绍了Fabric的智能合约安装、实例化、以及调用的方法。