区块链Hyperledger Fabric1.1环境搭建步骤详解
区块链技术很火,到处都在聊区块链,但是现行基于区块链的开源框架或多或少存在各种短板,资料少、安装难、使用难等问题虽是老生常谈,却也始终未能做到见招拆招。经过多轮的采坑验证,结合格类书籍和技术大牛的指点,本文总结出基于Hyperledger Fabric1.1版本的安装配置详细流程,欢迎交流指导。
- 安装基础环境:centos7或者ubuntu17+
[root@localhost fabric]# uname -r
3.10.0-862.el7.x86_64
- 安装运行所需软件
1),go安装
go version go1.10.2 linux/amd64
并设置GOROOT和GOPATH目录
vim /etc/profile
export GOROOT=/apps/go
export GOPATH=/opt/gopath
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
设置完毕别忘了 source /etc/profile
2),docker安装
Version: 1.13.1 OR docker-ce 17+
yum install docker
配置镜像仓库加速
vi /etc/docker/daemon.json
{
"registry-mirrors": ["https://aj2rgad5.mirror.aliyuncs.com"]
}
3),其他组件
sudo yum install autoconf automake libtool curl make g++ unzip git bzip2 -y
4),安装 goprotobuf
wget https://github.com/google/protobuf/releases/download/v2.6.1/protobuf-2.6.1.tar.gz
tar zxvf protobuf-2.6.1.tar.gz
cd protobuf-2.6.1
./configure
make
make install
protoc -h
5),下载protoc-gen-go(需FQ)
go get github.com/golang/protobuf/protoc-gen-go
cd github.com/golang/protobuf/protoc-gen-go
go build
go install
。。。
- 下载Fabric源码
mkdir -p $GOPATH/src/github.com/hyperledger
cd $GOPATH/src/github.com/hyperledger
git clone https://github.com/hyperledger/fabric
注:目录不要更改,否则后面操作会很麻烦
4,编译源码
cd $GOPATH/src/github.com/hyperledger/fabric
#这一步会生成一系列的命令文件,后续所有操作都会用到
make release
#这一步会下载很多docker镜像,使用移动网络的话需要注意,文件很大
make docker
#注:make docker这一步会有很多坑,要有耐心一步步解决,只要成功拉取到镜像即可,后续步骤要靠人品,有兴趣可以继续。。。
#镜像中,只有fabric-baseimage、fabric-baseos和fabric-ccenv必备,启动链码需要,其他都是fabric的docker封装,我们直接启动不用docker
[root@localhost fabric]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hyperledger/fabric-testenv amd64-1.2.1-snapshot-33b0e5e 13d9380f17de 36 seconds ago 1.5 GB
hyperledger/fabric-testenv amd64-latest 13d9380f17de 36 seconds ago 1.5 GB
hyperledger/fabric-testenv latest 13d9380f17de 36 seconds ago 1.5 GB
hyperledger/fabric-buildenv amd64-1.2.1-snapshot-33b0e5e 125881f73ee0 2 minutes ago 1.41 GB
hyperledger/fabric-buildenv amd64-latest 125881f73ee0 2 minutes ago 1.41 GB
hyperledger/fabric-buildenv latest 125881f73ee0 2 minutes ago 1.41 GB
hyperledger/fabric-ccenv amd64-1.2.1-snapshot-33b0e5e 993292d8f6eb 2 minutes ago 1.43 GB
hyperledger/fabric-ccenv amd64-latest 993292d8f6eb 2 minutes ago 1.43 GB
hyperledger/fabric-ccenv latest 993292d8f6eb 2 minutes ago 1.43 GB
hyperledger/fabric-orderer amd64-1.2.1-snapshot-33b0e5e a1ec66074acb 15 minutes ago 152 MB
hyperledger/fabric-orderer amd64-latest a1ec66074acb 15 minutes ago 152 MB
hyperledger/fabric-orderer latest a1ec66074acb 15 minutes ago 152 MB
hyperledger/fabric-peer amd64-1.2.1-snapshot-33b0e5e 31afe87e3502 15 minutes ago 159 MB
hyperledger/fabric-peer amd64-latest 31afe87e3502 15 minutes ago 159 MB
hyperledger/fabric-peer latest 31afe87e3502 15 minutes ago 159 MB
docker.io/hyperledger/fabric-baseimage amd64-0.4.10 62513965e238 8 weeks ago 1.39 GB
docker.io/hyperledger/fabric-baseos amd64-0.4.10 52190e831002 8 weeks ago 132 MB
注:上述步骤只要成功一次即可,如果需要多机部署,其他机器安装简化如下:
(1),安装GO和docker环境,GOPATH和GOROOT一致
(2),从编译成功的机器上复制Fabric的源码到新机器的同样目录(不要重新下载)
(3),在原机器上导出镜像fabric-baseimage、fabric-baseos、fabric-ccenv,在新机器上进行导入即可
后续操作和原机器一致。
5,命令文件赋值
#将fabric操作需要使用的命令文件copy到/usr/local/bin下,方便后续操作
cp $GOPATH/src/github.com/hyperledger/fabric/release/linux-amd64/bin/* /usr/local/bin
[root@localhost chaincodes]# cd /usr/local/bin/
[root@localhost bin]# ll
总用量 83148
-rwxrwxrwx 1 root root 15502032 8月 26 01:42 configtxgen
-rwxrwxrwx 1 root root 16356184 8月 26 01:42 configtxlator
-rwxrwxrwx 1 root root 8584816 8月 26 01:42 cryptogen
-rwxrwxrwx 1 root root 757 8月 26 01:42 get-docker-images.sh
-rwxrwxrwx 1 root root 20114464 8月 26 01:42 orderer
-rwxrwxrwx 1 root root 24445808 8月 26 01:42 peer
如果非root用户:
sudo chmod -R 775 /usr/local/bin/
|
模块名称 |
功能 |
|
peer |
主节点模块,负责存储区块链数据,运行维护链码 |
|
orderer |
交易打包、排序模块操作 |
|
cryptogen |
组织和证书生成模块 |
|
configtxgen |
区块和交易生成模块 |
|
configtxlator |
区块和交易解析模块 |
6,生成证书文件
1),创建存放文件的目录
mkdir -p /opt/hyperledger/fabricconfig
2),cryptogen提供了一个命令可以获取cryptogen模块所需要的配置文件的样式,该命令如下所示:
cryptogen showtemplate
3),按照模板生成配置文件
cd /opt/hyperledger/fabricconfig
vim crypto-config.yaml
#生成一个orderer组织,包含三个orderer节点
OrdererOrgs:
- Name: Orderer
Domain: neotest.com
Specs:
- Hostname: orderer0
- Hostname: orderer1
- Hostname: orderer2
#生成两个peer组织,代表联盟链中的两个加盟方,每个组织有3个节点和3-2个用户
PeerOrgs:
- Name: Org1
Domain: org1.neotest.com
Template:
Count: 3
Users:
Count: 3
- Name: Org2
Domain: org2.neotest.com
Template:
Count: 3
Users:
Count: 2
4),使用命令生成证书
[root@localhost fabricconfig]# cryptogen generate --config=crypto-config.yaml --output ./crypto-config
org1.neotest.com
org2.neotest.com
[root@localhost fabricconfig]# ll
总用量 4
drwxr-xr-x 4 root root 59 8月 24 14:57 crypto-config
-rw-r--r-- 1 root root 300 8月 24 14:56 crypto-config.yaml
#可以通过tree命令查看生成证书文件的路径,大致了解fabric的证书结构
[root@localhost fabricconfig]# cd crypto-config
[root@localhost crypto-config]# tree -L 3
.
├── ordererOrganizations
│ └── neotest.com
│ ├── ca
│ ├── msp
│ ├── orderers
│ ├── tlsca
│ └── users
└── peerOrganizations
├── org1.neotest.com
│ ├── ca
│ ├── msp
│ ├── peers
│ ├── tlsca
│ └── users
└── org2.neotest.com
├── ca
├── msp
├── peers
├── tlsca
└── users
5),配置域名映射(按照自己的实际地址配置)
#这一步是因为创世块中是需要配置orderer和peer的IP端口的,如果写死,意味着该节点无法在其他机器上启动
#kafkahost是kafka集群的IP地址,如果solo启动可以不依赖kafka
[root@localhost crypto-config]# vim /etc/hosts
192.168.56.123 orderer0.neotest.com
192.168.56.123 orderer1.neotest.com
192.168.56.123 orderer2.neotest.com
192.168.56.123 peer0.org1.neotest.com
192.168.56.123 peer1.org1.neotest.com
192.168.56.123 peer2.org1.neotest.com
192.168.56.123 peer0.org2.neotest.com
192.168.56.123 peer1.org2.neotest.com
192.168.56.123 peer2.org2.neotest.com
192.168.56.104 kafkahost
7,生成orderer和channel的创世块
1),创建目录,并配置configtx.yaml配置文件
[root@localhost crypto-config]# mkdir -p /opt/hyperledger/order/
[root@localhost crypto-config]# cd /opt/hyperledger/order
#这是一个官方模板,只做借鉴,真正用的时候全部copy一下配置
[root@localhost order]# cp -r $GOPATH/src/github.com/hyperledger/fabric/sampleconfig/configtx.yaml /opt/hyperledger/order
[root@localhost order]# vim configtx.yaml
#加重警告:此文件只列出了需要修改的部分内容,不能直接copy使用,请先按照上一步cp模板,在模板上面进行局部修改
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: /opt/hyperledger/fabricconfig/crypto-config/ordererOrganizations/neotest.com/msp
- &Org1
Name: Org1MSP
ID: Org1MSP
MSPDir: /opt/hyperledger/fabricconfig/crypto-config/peerOrganizations/org1.neotest.com/msp
# 设置组织锚节点配置--负责组织对外通讯(order或其他组织)
# 最好设置多个,防止单点故障,一旦生成创世块无法更改
AnchorPeers:
- Host: peer0.org1.neotest.com
Port: 7150
- Host: peer1.org1.neotest.com
Port: 7151
- &Org2
Name: Org2MSP
ID: Org2MSP
MSPDir: /opt/hyperledger/fabricconfig/crypto-config/peerOrganizations/org2.neotest.com/msp
AnchorPeers:
- Host: peer0.org2.neotest.com
Port: 7250
- Host: peer1.org2.neotest.com
Port: 7251
Orderer: &OrdererDefaults
# solo or kafka
OrdererType: kafka
Addresses:
- orderer0.neotest.com:7050
- orderer1.neotest.com:7051
- orderer2.neotest.com:7052
BatchTimeout: 2s
BatchSize:
# 单个区块最大信息数
MaxMessageCount: 10
# 设置最大的区块大小(不包括头部)。
AbsoluteMaxBytes: 98 MB
# 推荐区块大小
PreferredMaxBytes: 512 KB
Kafka:
Brokers:
- kafkahost:9092
Organizations:
Application: &ApplicationDefaults
Organizations:
2),生成order节点创世区块
[root@localhost order]# configtxgen -profile TestTwoOrgsOrdererGenesis -outputBlock ./orderer.genesis.block
2018-08-26 01:50:13.943 EDT [common/tools/configtxgen] main -> INFO 001 Loading configuration
2018-08-26 01:50:13.952 EDT [common/tools/configtxgen] doOutputBlock -> INFO 002 Generating genesis block
2018-08-26 01:50:13.952 EDT [common/tools/configtxgen] doOutputBlock -> INFO 003 Writing genesis block
[root@localhost order]# ll
总用量 16
-rw-r--r-- 1 root root 2101 8月 26 01:49 configtx.yaml
-rw-r--r-- 1 root root 9186 8月 26 01:50 orderer.genesis.block
3),生成单个channel创世区块(每创建一个channel都需要生成)
[root@localhost order]# configtxgen -profile TestTwoOrgsChannel -outputCreateChannelTx ./neotest1channel.tx -channelID neotest1channel
2018-08-26 01:58:51.743 EDT [common/tools/configtxgen] main -> INFO 001 Loading configuration
2018-08-26 01:58:51.752 EDT [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 002 Generating new channel configtx
2018-08-26 01:58:51.772 EDT [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 003 Writing new channel tx
4),生成channel对应的各个组织的锚文件(组织节点加入通道需要)
[root@localhost order]# configtxgen -profile TestTwoOrgsChannel -outputAnchorPeersUpdate ./Org1MSPanchors.tx -channelID neotest1channel -asOrg Org1MSP
2018-08-26 02:03:48.324 EDT [common/tools/configtxgen] main -> INFO 001 Loading configuration
2018-08-26 02:03:48.332 EDT [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update
2018-08-26 02:03:48.332 EDT [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update
[root@localhost order]# configtxgen -profile TestTwoOrgsChannel -outputAnchorPeersUpdate ./Org2MSPanchors.tx -channelID neotest1channel -asOrg Org2MSP
2018-08-26 02:07:44.576 EDT [common/tools/configtxgen] main -> INFO 001 Loading configuration
2018-08-26 02:07:44.586 EDT [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update
2018-08-26 02:07:44.587 EDT [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update
[root@localhost order]# ll
总用量 28
-rw-r--r-- 1 root root 2101 8月 26 01:49 configtx.yaml
-rw-r--r-- 1 root root 320 8月 26 01:58 neotest1channel.tx
-rw-r--r-- 1 root root 9186 8月 26 01:50 orderer.genesis.block
-rw-r--r-- 1 root root 327 8月 26 02:07 Org1MSPanchors.tx
-rw-r--r-- 1 root root 327 8月 26 02:07 Org2MSPanchors.tx
8,启动orderer节点
1),生成目录并copy模板
[root@localhost orderer0]# mkdir -p /opt/hyperledger/peers/orderpeers/orderer0
[root@localhost orderer1]# cd /opt/hyperledger/peers/orderpeers/orderer0
#copy原生模板
[root@localhost orderer0]# cp $GOPATH/src/github.com/hyperledger/fabric/sampleconfig/orderer.yaml /opt/hyperledger/peers/orderpeers/orderer0/
2),根据自身设置修改配置文件(节选更改部分,对照修改)
vim orderer.yaml
#加重警告:此文件只列出了需要修改的部分内容,不能直接copy使用,请先按照上一步cp模板,在模板上面进行局部修改
General:
LedgerType: file
ListenAddress: 0.0.0.0
ListenPort: 7050
LogLevel: debug
# Log Format: The format string to use when logging. Especially useful to disable color logging
LogFormat: '%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}'
GenesisMethod: file
GenesisProfile: TestTwoOrgsOrdererGenesis
GenesisFile: /opt/hyperledger/order/orderer.genesis.block
LocalMSPDir: /opt/hyperledger/fabricconfig/crypto-config/ordererOrganizations/neotest.com/orderers/orderer0.neotest.com/msp
LocalMSPID: OrdererMSP
Profile:
Enabled: false
Address: 0.0.0.0:6060
BCCSP:
Default: SW
SW:
Hash: SHA2
Security: 256
FileKeyStore:
KeyStore:
Authentication:
TimeWindow: 15m
FileLedger:
Location: /opt/hyperledger/peers/orderpeers/orderer0/production
Prefix: hyperledger-fabric-ordererledger
3),启动orderer节点
[root@localhost orderer0]# nohup orderer start &
*** orderer1和orderer2节点同样方式启动
9,启动org1 peer节点
[root@localhost peer0]# mkdir -p /opt/hyperledger/peers/org1peers/peer0
[root@localhost peer0]# cd /opt/hyperledger/peers/org1peers/peer0
#copy原生模板
[root@localhost peer0]# cp $GOPATH/src/github.com/hyperledger/fabric/sampleconfig/core.yaml /opt/hyperledger/peers/org1peers/peer0
2),根据自身设置修改配置文件(节选更改部分,对照修改)
vim core.yaml
#加重警告:此文件只列出了需要修改的部分内容,不能直接copy使用,请先按照上一步cp模板,在模板上面进行局部修改
peer:
#peer节点唯一id
id: peer0.org1.neotest.com
#逻辑组网
networkId: org1net
listenAddress: 0.0.0.0:7150
chaincodeListenAddress: 0.0.0.0:7140
address: 0.0.0.0:7150
addressAutoDetect: false
gomaxprocs: -1
gossip:
#账本数据同步节点,第一个节点直接填自身
bootstrap: peer0.org1.neotest.com:7150
externalEndpoint: peer0.org1.neotest.com:7150
# EventHub related configuration
events:
# The address that the Event service will be enabled on the peer
address: 0.0.0.0:7130
fileSystemPath: /opt/hyperledger/peers/org1peers/peer0/production
mspConfigPath: /opt/hyperledger/fabricconfig/crypto-config/peerOrganizations/org1.neotest.com/peers/peer0.org1.neotest.com/msp
localMspId: Org1MSP
3),启动org1 peer节点
[root@localhost peer0]# nohup peer node start &
*** org1 其余节点和org2节点以同样方式启动
10,peer节点订阅通道
1), peer节点创建通道---只需要创建一次
[root@localhost peer0]# export set CORE_PEER_LOCALMSPID=Org1MSP
[root@localhost peer0]# export set CORE_PEER_MSPCONFIGPATH=/opt/hyperledger/fabricconfig/crypto-config/peerOrganizations/org1.neotest.com/users/Admin@org1.neotest.com/msp
[root@localhost peer0]# peer channel create -t 50 -o orderer0.neotest.com:7050 -c neotest1channel -f /opt/hyperledger/order/neotest1channel.tx
2018-08-26 03:54:14.330 EDT [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2018-08-26 03:54:14.608 EDT [channelCmd] InitCmdFactory -> INFO 002 Endorser and orderer connections initialized
2018-08-26 03:54:14.810 EDT [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized
2018-08-26 03:54:15.013 EDT [channelCmd] InitCmdFactory -> INFO 004 Endorser and orderer connections initialized
2018-08-26 03:54:15.216 EDT [channelCmd] InitCmdFactory -> INFO 005 Endorser and orderer connections initialized
2018-08-26 03:54:15.422 EDT [main] main -> INFO 006 Exiting.....
#将生成的该组织该通道的block文件移动到组织目录下,因为是该组织所有节点都需要
[root@localhost peer0]# mv neotest1channel.block ../neotest1channel.block
[root@localhost peer0]# cd ..
[root@localhost org1peers]# pwd
/opt/hyperledger/peers/org1peers
[root@localhost org1peers]# ll
总用量 12
-rw-r--r-- 1 root root 12098 8月 26 03:54 neotest1channel.block
drwxr-xr-x 3 root root 58 8月 26 04:01 peer0
drwxr-xr-x 2 root root 6 8月 26 03:16 peer1
drwxr-xr-x 2 root root 6 8月 26 03:16 peer2
2),peer节点加入通道(export配置需要根据不同组织不同节点变动)
[root@localhost peer0]# export set CORE_PEER_LOCALMSPID=Org1MSP
[root@localhost peer0]# export set CORE_PEER_MSPCONFIGPATH=/opt/hyperledger/fabricconfig/crypto-config/peerOrganizations/org1.neotest.com/users/Admin@org1.neotest.com/msp
[root@localhost peer0]# export set CORE_PEER_ADDRESS=peer0.org1.neotest.com:7150
[root@localhost org1peers]# cd peer0/
[root@localhost peer0]# peer channel join -b ../neotest1channel.block
2018-08-26 04:04:56.299 EDT [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2018-08-26 04:04:56.331 EDT [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
2018-08-26 04:04:56.331 EDT [main] main -> INFO 003 Exiting.....
3),更新该通道锚节点配置
[root@localhost peer0]# peer channel update -o orderer0.neotest.com:7050 -c neotest1channel -f /opt/hyperledger/order/Org1MSPanchors.tx
2018-08-26 04:10:09.592 EDT [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2018-08-26 04:10:09.747 EDT [channelCmd] update -> INFO 002 Successfully submitted channel update
2018-08-26 04:10:09.747 EDT [main] main -> INFO 003 Exiting.....
#查看该节点已加入的通道
[root@localhost peer0]# peer channel list
2018-08-26 04:12:45.609 EDT [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
Channels peers has joined:
neotest1channel
2018-08-26 04:12:45.612 EDT [main] main -> INFO 002 Exiting.....
11,peer节点安装chaincode(链码,即智能合约)
1),部署chaincode(链码)
[root@localhost peer0]# export set CORE_PEER_LOCALMSPID=Org1MSP
[root@localhost peer0]# export set CORE_PEER_MSPCONFIGPATH=/opt/hyperledger/fabricconfig/crypto-config/peerOrganizations/org1.neotest.com/users/Admin@org1.neotest.com/msp
[root@localhost peer0]# export set CORE_PEER_ADDRESS=peer0.org1.neotest.com:7150
#这里使用的fabric代码中自带的样例链码,-p路径必须是$GOPATH下的相对路径
peer chaincode install -n neotest1cc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
#查看该节点已安装链码文件
[root@localhost peer0]# peer chaincode install -n neotest1cc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
2018-08-26 04:17:55.556 EDT [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2018-08-26 04:17:55.556 EDT [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2018-08-26 04:17:55.929 EDT [main] main -> INFO 003 Exiting.....
[root@localhost peer0]# cd production/chaincodes/
[root@localhost chaincodes]# ll
总用量 4
-rw-r--r-- 1 root root 2369 8月 26 04:17 neotest1cc.1.0
2),实例化chaincode,启动链码docker容器
##参数解释
-o 指定orderer地址,集群中任一orderer
-C 将链码安装在哪个通道
-c 代表初始化参数,fabric采用kv格式,再次表示a和b的账户初始值
-P 表示需要那些成员背书签名
[root@localhost peer0]# peer chaincode instantiate -o orderer0.neotest.com:7050 -C neotest1channel -n neotest1cc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR('Org1MSP.member','Org2MSP.member')"
2018-08-26 04:32:53.914 EDT [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2018-08-26 04:32:53.914 EDT [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2018-08-26 04:32:54.229 EDT [main] main -> INFO 003 Exiting.....
#可能会稍作等待,因为这一步要创建链码镜像并启动容器
[root@localhost peer0]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fadc11f4a152 org1net-peer0.org1.neotest.com-neotest1cc-1.0-fbc4f562cb10d5e1295f16eaa725925d9d6d4dfa74b6c74167ec143d8c8e99cf "chaincode -peer.a..." 12 seconds ago Up 10 seconds org1net-peer0.org1.neotest.com-neotest1cc-1.0
[root@localhost peer0]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
org1net-peer0.org1.neotest.com-neotest1cc-1.0-fbc4f562cb10d5e1295f16eaa725925d9d6d4dfa74b6c74167ec143d8c8e99cf latest 7070fb7c0838 About a minute ago 172 MB
docker.io/hyperledger/fabric-ccenv x86_64-1.1.0 c8b4909d8d46 5 months ago 1.39 GB
hyperledger/fabric-baseimage x86_64-0.4.6 dbe6787b5747 6 months ago 1.37 GB
hyperledger/fabric-baseos
3),测试调用链码操作
#调用链码,执行a给b转移1个账户积分的操作
[root@localhost peer0]# peer chaincode invoke -o orderer0.neotest.com:7050 -C neotest1channel -n neotest1cc -c '{"Args":["invoke","a","b","1"]}'
2018-08-26 04:42:18.080 EDT [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2018-08-26 04:42:18.080 EDT [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2018-08-26 04:42:18.100 EDT [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 003 Chaincode invoke successful. result: status:200
2018-08-26 04:42:18.101 EDT [main] main -> INFO 004 Exiting.....
4),查询a和b的账户,查看变化
#这里不再需要制定orderer地址,因为所有peer节点的账本都已经在本地同步,所以query其实是在本地进行查询操作
[root@localhost peer0]# peer chaincode query -C neotest1channel -n neotest1cc -c '{"Args":["query","a"]}'
2018-08-26 04:46:09.744 EDT [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2018-08-26 04:46:09.744 EDT [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
Query Result: 99
2018-08-26 04:46:09.749 EDT [main] main -> INFO 003 Exiting.....
[root@localhost peer0]# peer chaincode query -C neotest1channel -n neotest1cc -c '{"Args":["query","b"]}'
2018-08-26 04:46:15.060 EDT [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2018-08-26 04:46:15.060 EDT [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
Query Result: 201
2018-08-26 04:46:15.066 EDT [main] main -> INFO 003 Exiting.....
#查看本地的账本文件位置,根据目录可以得知,账本文件是按照不同通道生成的,每个节点可以按照自身需求部署不同的通道
#如果同一个通道部署了多个链码,不同链码的key值是通过chaincodeName做逻辑隔离的
[root@localhost neotest1channel]# pwd
/opt/hyperledger/peers/org1peers/peer0/production/ledgersData/chains/chains/neotest1channel
[root@localhost neotest1channel]# ll
总用量 36
-rw-r----- 1 root root 36314 8月 26 04:42 blockfile_000000
11,启动其他节点
1),启动org1-peer1
[root@localhost neotest1channel]# cd /opt/hyperledger/peers/org1peers/peer1
#将peer0的core.yaml复制过来,
[root@localhost peer1]# cp ../peer0/core.yaml .
#然后修改core文件,与peer0稍有不同,注意端口和文件路径
vim core.yaml
peer:
#peer节点唯一id
id: peer1.org1.neotest.com
#逻辑组网
networkId: org1net
listenAddress: 0.0.0.0:7151
chaincodeListenAddress: 0.0.0.0:7141
address: 0.0.0.0:7151
addressAutoDetect: false
gomaxprocs: -1
gossip:
#账本数据同步节点,后续节点启动寻找当前活跃节点
bootstrap: peer0.org1.neotest.com:7150
externalEndpoint: peer1.org1.neotest.com:7151
# EventHub related configuration
events:
# The address that the Event service will be enabled on the peer
address: 0.0.0.0:7131
fileSystemPath: /opt/hyperledger/peers/org1peers/peer1/production
mspConfigPath: /opt/hyperledger/fabricconfig/crypto-config/peerOrganizations/org1.neotest.com/peers/peer1.org1.neotest.com/msp
localMspId: Org1MSP
[root@localhost peer1]# nohup peer node start &
2),加入通道
[root@localhost peer0]# export set CORE_PEER_LOCALMSPID=Org1MSP
[root@localhost peer0]# export set CORE_PEER_MSPCONFIGPATH=/opt/hyperledger/fabricconfig/crypto-config/peerOrganizations/org1.neotest.com/users/Admin@org1.neotest.com/msp
[root@localhost peer0]# export set CORE_PEER_ADDRESS=peer1.org1.neotest.com:7151
#注意,同一组织已经创建的通道,不需要再次创建,直接加入即可
[root@localhost peer1]# peer channel join -b ../neotest1channel.block
2018-08-26 05:22:39.000 EDT [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2018-08-26 05:22:39.060 EDT [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
2018-08-26 05:22:39.060 EDT [main] main -> INFO 003 Exiting.....
#加入通道后,查看区块文件大小,peer1的区块文件已经与peer0自动同步
[root@localhost neotest1channel]# cd /opt/hyperledger/peers/org1peers/peer0/production/ledgersData/chains/chains/neotest1channel
[root@localhost neotest1channel]# ll
总用量 36
-rw-r----- 1 root root 36314 8月 26 04:42 blockfile_000000
[root@localhost neotest1channel]# cd /opt/hyperledger/peers/org1peers/peer1/production/ledgersData/chains/chains/neotest1channel
[root@localhost neotest1channel]# ll
总用量 36
-rw-r----- 1 root root 36314 8月 26 05:22 blockfile_000000
3),安装链码
#这一步只需要install,instantiate操作不需要,因为已经配置过了
[root@localhost peer1]# peer chaincode install -n neotest1cc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
2018-08-26 05:35:37.204 EDT [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2018-08-26 05:35:37.205 EDT [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2018-08-26 05:35:38.554 EDT [main] main -> INFO 003 Exiting.....
4),执行查询
#可以直接执行查询,可以看到a账户值为99,与peer0操作同步
#注:这一步有时候会报链码指纹不匹配的错误,如果发生,直接到peer0的production/chaincodes下,直接copy智能合约文件覆盖peer1的即可
[root@localhost peer1]# peer chaincode query -C neotest1channel -n neotest1cc -c '{"Args":["query","a"]}'
2018-08-26 05:37:21.898 EDT [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2018-08-26 05:37:21.898 EDT [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
Query Result: 99
2018-08-26 05:37:42.895 EDT [main] main -> INFO 003 Exiting.....
#这时候查看docker,已经生成了两个镜像和两个链码容器
[root@localhost peer1]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e6bf2e53bb8 org1net-peer1.org1.neotest.com-neotest1cc-1.0-123227a6ac2844fb73436a210fc358e60538abcdb8db04afc3c789d3397e8054 "chaincode -peer.a..." 11 seconds ago Up 10 seconds org1net-peer1.org1.neotest.com-neotest1cc-1.0
fadc11f4a152 org1net-peer0.org1.neotest.com-neotest1cc-1.0-fbc4f562cb10d5e1295f16eaa725925d9d6d4dfa74b6c74167ec143d8c8e99cf "chaincode -peer.a..." About an hour ago Up About an hour org1net-peer0.org1.neotest.com-neotest1cc-1.0
[root@localhost peer1]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
org1net-peer1.org1.neotest.com-neotest1cc-1.0-123227a6ac2844fb73436a210fc358e60538abcdb8db04afc3c789d3397e8054 latest 7a1ff6e970f2 31 seconds ago 172 MB
org1net-peer0.org1.neotest.com-neotest1cc-1.0-fbc4f562cb10d5e1295f16eaa725925d9d6d4dfa74b6c74167ec143d8c8e99cf latest 7070fb7c0838 About an hour ago 172 MB
docker.io/hyperledger/fabric-ccenv x86_64-1.1.0 c8b4909d8d46 5 months ago 1.39 GB
hyperledger/fabric-baseimage x86_64-0.4.6 dbe6787b5747 6 months ago 1.37 GB
hyperledger/fabric-baseos x86_64-0.4.6 220e5cf3fb7f 6 months ago 151 MB
-------------------------
#任一节点,只要安装同一个通道和同一链码,执行invoke和query操作都会得到一样的结果
#orderer节点为集群时,可以使用nginx1.14.* 以上版本做grpc协议的负载均衡(低版本不支持grpc负载)
#peer节点的负载目前没有好的办法,只有自己在调用sdk的时候自己实现
#目前fabric的sdk有go、nodejs、java等很多版本,根据自身需求使用,sdk不是本章重点,有时间单独开一章节介绍
#演示代码至此结束,org2的操作同org1,再次不在赘述。
本文仅作个人意见总结及交流

浙公网安备 33010602011771号