環境採用三臺實體機器,也可以嘗試使用OpenStack 三個 Instance進行建置:
主機名稱 | IP | 角色 |
---|---|---|
orderer.example.com | 10.0.0.78 | orderer |
peer0.org1.example.com | 10.0.0.79 | peer |
peer0.org2.example.com | 10.0.0.201 | peer |
peer 表示一個區塊鏈網路中的節點,負責儲存交易帳本以及背書其他節點的交易,orderer 表示排序者,負責處理各個節點傳送過來的交易,依據傳送時間點進行排序,將其結果打包並生成一個Block。
wget https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.8.3.linux-amd64.tar.gz
編輯環境變量
vim ~/.profile
添加以下內容
export PATH=$PATH:/usr/local/go/bin
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$HOME/go/bin
載入修改的環境變量
source ~/.profile
因為將go的目錄GOPATH設置在$HOME/go,所以需要在$HOME目錄底下建置go目錄
mkdir ~/go
sudo apt-get install docker.io
sudo apt-get install python-pip
curl -L https://get.daocloud.io/docker/compose/releases/download/1.10.1/docker-compose-`uname -s`-`uname -m` > ~/docker-compose
sudo mv ~/docker-compose /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
go get github.com/hyperledger/fabric
下載成功後會在~/go/src/目錄下看到已經下載好的Fabric
cd ~/go/src/github.com/hyperledger/fabric/examples/e2e_cli/
source download-dockerimages.sh -c x86_64-1.0.0 -f x86_64-1.0.0
以上即完成整個 Hyperledger Fabric 所有環境的建置,接下來只需要透過修改配置文件來自訂所需的環境
./generateArtifacts.sh mychannel
系统會創建 channel-artifacts 以及 crypto-config 兩個資料夾,分別存放通道所需的相關文件以及各個節點的公私鑰與證書
ports:
- 7051:7051
- 7052:7052
- 7053:7053
該配置文件原先為提供同一台主機中設置多節點網路,因此會透過修改 port 號來區別這個節點,不過我們這邊為多機器部署,因此必須將port皆設置為原先模樣
cp docker-compose-cli.yaml docker-compose-orderer.yaml
vim docker-compose-orderer.yaml
接著將其修改為以下樣子
version: '2'
services:
orderer.example.com:
extends:
file: base/docker-compose-base.yaml
service: orderer.example.com
container_name: orderer.example.com
cp docker-compose-cli.yaml docker-compose-peer.yaml
vim docker-compose-peer.yaml
接著將其修改為以下樣子
version: '2'
services:
peer0.org1.example.com:
container_name: peer0.org1.example.com
extends:
file: base/docker-compose-base.yaml
service: peer0.org1.example.com
extra_hosts:
- "orderer.example.com:10.0.0.78"
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=true
- 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/examples/chaincode/go
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
- ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
depends_on:
- peer0.org1.example.com
extra_hosts:
- "orderer.example.com:10.0.0.78"
- "peer0.org1.example.com:10.0.0.79"
- "peer0.org2.example.com:10.0.0.201"
與 orderer 不同 peer 需要與 orderer 溝通所以需要配置 extra_hosts ,而 cli 需要與所有節點溝通,因此需要配置與所有節點溝通的 extra_hosts 配置
version: '2'
services:
peer0.org2.example.com:
container_name: peer0.org2.example.com
extends:
file: base/docker-compose-base.yaml
service: peer0.org2.example.com
extra_hosts:
- "orderer.example.com:10.0.0.78"
- "peer0.org1.example.com:10.0.0.79"
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.org2.example.com:7051
- CORE_PEER_LOCALMSPID=Org1MSP
- CORE_PEER_TLS_ENABLED=true
- 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/examples/chaincode/go
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
- ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
depends_on:
- peer0.org2.example.com
extra_hosts:
- "orderer.example.com:10.0.0.78"
- "peer0.org1.example.com:10.0.0.79"
- "peer0.org2.example.com:10.0.0.201"
與 peer0.org1 不同在於 extra_hosts 部分需要加入 "peer0.org1.example.com:10.0.0.79" 配置
那麼以上完成配置文件後,即可在各自的主機運行剛剛修改的配置文件
docker-compose -f docker-compose-orderer.yaml up –d
可以透過 docker ps 看到已有一個名為 orderer.example.com 的節點運行中
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9e6d7c58ab1f hyperledger/fabric-orderer "orderer" 11 days ago Up 11 days 0.0.0.0:7050->7050/tcp orderer.example.com
docker-compose -f docker-compose-orderer.yaml up –d
可以透過 docker ps 看到 peer 與 cli 已啟動
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
21d1df7f3a91 hyperledger/fabric-tools "/bin/bash" 11 days ago Up 11 days cli
4ac3288023f8 hyperledger/fabric-peer "peer node start" 11 days ago Up 11 days 0.0.0.0:7051-7053->7051-7053/tcp peer0.org1.example.com
peer0.org2也是使用相同啟動步驟
那個以上環境皆啟動完畢,接下來需要透過創建Channel與測試chaincode來確定環境設定為正確的
首先於 peer0.org1 的主機上進入 cli container
docker exec -ti cli bash
接著會在 /opt/gopath/src/github.com/hyperledger/fabric/peer/scripts 底下看到 script.sh 腳本,將其複製來進行修改
cp ./script/script.sh ./script/test.sh
於腳本 function 運行區塊部分,修改為以下樣子
## Create channel
echo "Creating channel..."
createChannel
## Join all the peers to the channel
echo "Having all peers join the channel..."
joinChannel
## Set the anchor peers for each org in the channel
echo "Updating anchor peers for org1..."
updateAnchorPeers 0
echo "Updating anchor peers for org2..."
updateAnchorPeers 2
## Install chaincode on Peer0/Org1 and Peer2/Org2
echo "Installing chaincode on org1/peer0..."
installChaincode 0
echo "Install chaincode on org2/peer2..."
installChaincode 2
#Instantiate chaincode on Peer2/Org2
echo "Instantiating chaincode on org2/peer2..."
instantiateChaincode 2
#Query on chaincode on Peer0/Org1
echo "Querying chaincode on org1/peer0..."
chaincodeQuery 0 100
#Invoke on chaincode on Peer0/Org1
echo "Sending invoke transaction on org1/peer0..."
chaincodeInvoke 0
以上修改方式為僅保留 Peer0/Org1 與 Peer2/Org2
./scripts/script.sh mychannel
運行完成後看到以下結果,表示整個 Hyperledger Fabric 多節點的部署已經成功建置
===================== All GOOD, End-2-End execution completed =====================