ps:之前自己部署時記錄的,上傳作個筆記。
Fabric1.0.5
教程系統centos7
下載
鏡像下載腳本:
#!/bin/bash
DOCKER_NS=hyperledger
ARCH=x86_64
VERSION=1.0.5
FABRIC_IMAGES=(fabric-peer fabric-orderer fabric-ccenv fabric-javaenv fabric-kafuka fabric-zookeeper fabric-couchdb fabric-ca fabric-tools)
for image in ${FABRIC_IMAGES [@]}; do
echo “Pulling ${DOCKER_NS}/$image:${ARCH}-${VERSION}”
docker pull ${DOCKER_NS}/$image:${ARCH}-${VERSION}
done
下載輔助工具
(注意複製進去可能會顯示成中文字符)
# cd ~/workspace #目錄自己定
# VERSION=1.0.5
# ARCH=$(echo "$(uname -s|tr '[:upper:]' '[:lower:]' | sed 's/mingw64_nt.*/windows/')-$(uname -m | sed 's/x86_64/amd64/g')" | awk '{print tolower($0)}')
#curl https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/${ARCH}-${VERSION}/hyperledger-fabric-${ARCH}-${VERSION}.tar.gz | tar xz
插:curl不支持https解決方法
1、安裝openssl
yum install openssl openssl-devel
2、openssl version -a 查看證書目錄
3、進入curl安裝目錄 執行 ./configure –with-ssl=上一步的證書目錄,查看結果是否如下
4、重新編譯安裝curl
make make install
正式開始
準備文件夾
[root@localhost /]# cd ~/workspace
[root@localhost workspace]# mkdir -p first_network/crypto-config
[root@localhost workspace]# mkdir -p first_network/channel-artifacts
創建crypto-config.yaml配置文件
注意: YAML⽂件對縮進格式有嚴格要求。 YAML⽂件的常⽤語法:
- '#': 註釋
- ':': 對象定義
- '-': 數組成員
- '&': 聲明錨點對象
- '*': 引⽤錨點內容
- '<<': 合併內容到當前位置
參考: http://www.yaml.org/spec/1.2/spec.html
[root@localhost first_network]# vi crypto-config.yaml
OrdererOrgs:
- Name: Orderer
Domain: example.com
Specs:
- Hostname: orderer
PeerOrgs:
- Name: Org1
Domain: org1.example.com
Template:
Count: 1
Users:
Count: 1
- Name: Org2
Domain: org2.example.com
Template:
Count: 1
Users:
Count: 1
使用之前下載的fabric工具生成各節點證書
[root@localhost first_network]# ../bin/cryptogen generate --config=crypto-config.yaml
創建 'configtx.yaml'配置⽂件
'configtx.yaml'⽂件⽤於定義區塊鏈⽹絡中的排序節點信息和Channel信息,包括排序節點的共識⽅式、域名和IP,以及Channel的內部組織節點構成等。
#
# configtx.yaml
#
---
Profiles:
TwoOrgsOrdererGenesis:
Orderer:
<<: *OrdererDefaults
Organizations:
- *OrdererOrg
Consortiums:
SampleConsortium:
Organizations:
- *Org1
- *Org2
TwoOrgsChannel:
Consortium: SampleConsortium
Application:
<<: *ApplicationDefaults
Organizations:
- *Org1
- *Org2
Organizations:
- &OrdererOrg
Name: OrdererOrg
ID: OrdererMSP
MSPDir: crypto-config/ordererOrganizations/example.com/msp
- &Org1
Name: Org1MSP
ID: Org1MSP
MSPDir: crypto-config/peerOrganizations/org1.example.com/msp
AnchorPeers:
- Host: peer0.org1.example.com
Port: 7051
- &Org2
Name: Org2MSP
ID: Org2MSP
MSPDir: crypto-config/peerOrganizations/org2.example.com/msp
AnchorPeers:
- Host: peer0.org2.example.com
Port: 7051
Orderer: &OrdererDefaults
OrdererType: solo
Addresses:
- orderer.example.com:7050
BatchTimeout: 2s
BatchSize:
MaxMessageCount: 10
AbsoluteMaxBytes: 99 MB
PreferredMaxBytes: 512 KB
Kafka:
Brokers:
- 127.0.0.1:9092
Organizations:
Application: &ApplicationDefaults
Organizations:
生成初始區塊
$ cd ~/workspace/first-network
$ export FABRIC_CFG_PATH=$PWD
$../bin/configtxgen -profile TwoOrgsOrdererGenesis -outputBlock
./channel-artifacts/genesis.block
繼續在該PATH下生成channel文件
$../bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx
./channel-artifacts/channel01.tx -channelID channel01
⽣成配置文件中定義的org1和org2的錨點⽂件,錨點的作⽤就是代表所屬組織參與共識。
$../bin/configtxgen -profile TwoOrgsChannel -
outputAnchorPeersUpdate ./channe-lartifacts/channel01_Org1MSPanchors.tx -channelID channel01 -asOrg Org1MSP
$ ../bin/configtxgen -profile TwoOrgsChannel -
outputAnchorPeersUpdate ./channel-artifacts/channel01_Org2MSPanchors.tx -channelID channel01 -asOrg Org2MSP
genesis.block --- 排序節點的起始區塊⽂件,channel01.tx --- Channel定義⽂件
Org1MSPanchors.tx --- Org1的錨點定義⽂件,Org2MSPanchors.tx --- Org2的錨點定義⽂件
組建網絡
⾸先需要爲Docker⽹絡設置⼀個環境變量 COMPOSE_PROJECT_NAME ,保存在當前⽂件
夾下的⽂件'.env'中:
$ cd ~/workspace/first_network
$ echo "COMPOSE_PROJECT_NAME=net" > .env
創建 docker-compose腳本⽂件peer-base.yaml
該配置文件定義各組織節點的概要信息
#
# peer-base.yaml
#
version: '2'
services:
peer-base:
image: hyperledger/fabric-peer:x86_64-1.0.5
environment:
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=${COMPOSE_PROJECT_NAME}_bc-dev
- CORE_LOGGING_LEVEL=DEBUG
- CORE_PEER_TLS_ENABLED=false
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
- CORE_PEER_PROFILE_ENABLED=true
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
參考鏈接:
docker-compose腳本⽂件 base/docker-composebase.yaml
定義各組織節點的詳細信息。(可能不全,注意文件路徑是否一致)
#
# docker-compose-base.yaml
#
version: '2'
services:
orderer.example.com:
container_name: orderer.example.com
image: hyperledger/fabric-orderer:x86_64-1.0.5
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
- ORDERER_GENERAL_TLS_ENABLED=false
working_dir: /opt/gopath/src/github.com/hyperledger/fabric
command: orderer
volumes:
- ../channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.bl
ock
- ../crypto-config/ordererOrganizations/example.com/orderers/orderer.example.co
m/msp:/var/hyperledger/orderer/msp
- ../crypto-config/ordererOrganizations/example.com/orderers/orderer.example.co
m/tls/:/var/hyperledger/orderer/tls
ports:
- 7050:7050
peer0.org1.example.com:
container_name: peer0.org1.example.com
extends:
file: peer-base.yaml
service: peer-base
environment:
- CORE_PEER_ID=peer0.org1.example.com
- CORE_PEER_ADDRESS=peer0.org1.example.com:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.example.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
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
ports:
- 7051:7051
- 7053:7053
peer0.org2.example.com:
container_name: peer0.org2.example.com
extends:
file: peer-base.yaml
service: peer-base
environment:
- CORE_PEER_ID=peer0.org2.example.com
- CORE_PEER_ADDRESS=peer0.org2.example.com:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.example.com:7051
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org2.example.com:7051
- CORE_PEER_LOCALMSPID=Org2MSP
volumes:
- /var/run/:/host/var/run/
- ../crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/msp:/etc/hyperledger/fabric/msp
- ../crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls:/etc/hyperledger/fabric/tls
ports:
- 9051:7051
- 9053:7053
參考鏈接:
創建 docker-compose 腳本⽂件 docker-compose.yaml
將前⾯定義好的各個節點進⾏組⽹,⽤於⽹絡啓動。在first_network目錄下(可能不全,注意文件路徑是否一致)
#
# docker-compose.yaml
#
version: '2'
networks:
bc-dev:
services:
orderer.example.com:
extends:
file: base/docker-compose-base.yaml
service: orderer.example.com
container_name: orderer.example.com
networks:
- bc-dev
peer0.org1.example.com:
container_name: peer0.org1.example.com
extends:
file: base/docker-compose-base.yaml
service: peer0.org1.example.com
networks:
- bc-dev
peer0.org2.example.com:
container_name: peer0.org2.example.com
extends:
file: base/docker-compose-base.yaml
service: peer0.org2.example.com
networks:
- bc-dev
cli:
container_name: cli
image: hyperledger/fabric-tools:x86_64-1.0.5
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_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: /bin/bash
volumes:
- /var/run/:/host/var/run/
- ./../chaincodes/:/opt/gopath/src/chaincodes
- ./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
- peer0.org2.example.com
networks:
- bc-dev
啓動⽹絡
在first_network目錄下啓動:
$ docker-compose up -d或docker-compose -f xx.yaml
設置channel
打開終端窗⼝,執⾏下述命令(登錄到 cli 容器的系統) 注意docker容器重啓後,要重新執行本步驟:
$ cd ~/workspace/first_network
$ docker exec -it cli bash
在 cli 的終端中執⾏如下命令(設置環境變量,表明當前操作代表的節點
是 peer0.org1.exmple.com ):
CORE_PEER_LOCALMSPID="Org1MSP"
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/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
注意: 通過定義修改這些環境變量,我們可以在同⼀個容器中模擬不同的節點
身份。
在 cli 的終端中執⾏如下命令(創建Channel channel01 ):
$ peer channel create -o orderer.example.com:7050 -c channel01 -f ./channel-artifacts/channel01.tx
該操作會產⽣⼀個⽂件( channel01.block ),將作爲加⼊這個Channel的唯⼀憑
據。
在 cli 的終端中執⾏如下命令(將節點 peer0.org1.example.com 加⼊到該Channel
channel01 中):
$ peer channel join -b channel01.block
在 cli 的終端中執⾏如下命令(將節點 peer0.org1.example.com 設置爲Channel
channel01 中的錨點,代表 Org1 參與共識):
peer channel update -o orderer.example.com:7050 -c channel01 -f
./channel-artifacts/channel01_Org1MSPanchors.tx
在 cli 的終端中執⾏如下命令(驗證節點 peer0.org1.example.com ⼀共加⼊了哪些
Channel):
$ peer channel list
以peer0.org2.exmple.com 的身份
在 cli 的終端中執⾏如下命令(設置環境變量,表明當前操作代表的節點
是 peer0.org2.exmple.com ):
CORE_PEER_LOCALMSPID="Org2MSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org2.example.com:7051
在 cli 的終端中執⾏如下命令(將節點 peer0.org2.example.com 加⼊到該Channel
channel01 中):
$ peer channel join -b channel01.block
在 cli 的終端中執⾏如下命令(將節點 peer0.org2.example.com 設置爲Channel
channel01 中的錨點,代表 Org2 參與共識):
peer channel update -o orderer.example.com:7050 -c channel01 -f./channel-artifacts/channel01_Org2MSPanchors.tx
在 cli 的終端中執⾏如下命令(驗證節點 peer0.org2.example.com ⼀共加⼊了哪些
Channel):
$ peer channel list
2018-01-25 03:29:08.934 UTC [channelCmd] list -> INFO 006 Channels
peers has joined to:
2018-01-25 03:29:08.937 UTC [channelCmd] list -> INFO 007 channel01
2018-01-25 03:29:08.939 UTC [main] main -> INFO 008 Exiting.....
安裝cc
先從源碼中隨便拿一個帶單元測試的CC放在workspace內,我選的 /hyperledger/fabric/examples/chaincode/go/chaincode_example02
放在 ~/workspace/ chaincodes/chaincode_example
以 peer0.org1.exmple.com 的身份安裝指定Chaincode
$ peer chaincode install -n example -v 1.0 -p chaincodes/chaincode_example
(節點身份設置⽅法,就是上一步設置channel中更改變量的步驟,若兩個節點部署在不同服務器就不用這麼麻煩)
以 peer0.org2.exmple.com 的身份,安裝指定Chaincode
$ peer chaincode install -n example -v 1.0 -p chaincodes/chaincode_example
'-p'參數⽤來指定chaincode的存放路徑, 這是當前登錄的系統中 ('cli')$GOPATH/src下⾯的相對路徑。
在Channel channel01 中將cc實例化, 同時指定共識策略):
$ peer chaincode instantiate -o orderer.example.com:7050 -C channel01 -n example -v 1.0 -c '{"Args": ["init","a","100","b","100"]}' -P "OR('Org1MSP.member','Org2MSP.member')"
注:Chaincode調⽤的參數是個字符串數組, 第⼀個參數代表調⽤的功能名稱(對於
部署操作時, 該值只能爲'init'),從第⼆個參數開始的字符串數組將作爲實際被
調⽤的功能參數。'-P'參數後⾯是共識策略的表達⽅式, 語法: OR(x, y) 或 AND(x, y), 甚⾄可以是AND(x, OR(y,z))。
此操作結束後docker將生成專門用於運行CC的鏡像和容器
參考:
http://hyperledger-fabric.readthedocs.io/en/latest/endorsementpolicies.html#endorsement-policy-syntax-in-the-cli
常用操作
#刪除所有容器
docker rm -f `docker ps -qa`
#刪除所有⾃動⽣成的鏡像
docker rmi -f `docker images | grep "dev\|none\|test-vp\|peer[0-9]-
" | awk '{print $3}'`
#查看現有docker⽹絡
docker network list
#刪除所有docker⽹絡
docker network prune