Fabric多臺伺服器的部署(一)
1、前言
Hyperledger Fabric是一個提供分散式賬本解決方案的平臺。Hyperledger Fabric由模組化架構支撐,並具備極佳的保密性、可伸縮性、靈活性和可擴充套件性。Hyperledger Fabric被設計成支援不同的模組元件直接拔插啟用,並能適應在經濟生態系統中錯綜複雜的各種場景。
目前Fabric在區塊鏈溯源場景應用的挺多,農產品,奢侈品,藝術品,紅酒等方面都看過到案例,區塊鏈可以儲存產品各個環節的資料,並上鏈,使用者最後可能只需掃描一個二維碼,就可以查出某個產品整個的生產過程,與中心化資料不同的是,區塊鏈溯源專案資料一旦上鍊後不可更改,而且資料公開透明,任何一方都沒有權利來更改它,同時每個環節需要為你上鍊的資料負責,如果哪天消費查出某個環節的資料是假的,由於賬本資料是分散式的、不可更改的,大家只相信資料,商家也無法抵賴,最後商家只能認錯,名譽掃地,所以區塊鏈本質上是解決一個多方協作的一個信任問題,而對於一件事情需要多方協作,共同記賬來一起完成的,Fabric作為聯盟鏈,顯然很符合這種需求。
2、環境安裝及伺服器配置
2.1 環境安裝
在配置Fabric網路之前,需要安裝一下相關環境,包括GO語言安裝和Docker的安裝
2.1.1 GO語言安裝
1、從官網下載它的安裝包,地址:ofollow,noindex">https://golang.org/dl/ ,根據自己電腦或伺服器不同系統下載對應的安裝包。
2、下載後解壓包
tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz
3、配置GO的環境變數
export PATH=$PATH:/usr/local/go/bin export GOPATH=/opt/gopath
最後執行source命令,使其生效
source profile
最後列印一下GO版本
go version go version go1.10.1 darwin/amd64
不同系統安裝略有不同,具體建議檢視官方文件https://golang.org/doc/install
2.1.2 Docker、Docker-Compose的安裝
docker安裝不同系統也是有區別的,我是mac電腦,安裝也相對簡單,到docker官網 下載一個包,直接安裝就行,裝好後可以查一下docker的版本,值得注意的是,mac電腦只安裝docker就行,docker版本已經包括了compose和其它docker應用,所以無需再另行安裝compose
docker version #docker的版本資訊 Client: Version:18.06.1-ce API version:1.38 Go version:go1.10.3 Git commit:e68fc7a Built:Tue Aug 21 17:21:31 2018 OS/Arch:darwin/amd64 Experimental:false Server: Engine: Version:18.06.1-ce API version:1.38 (minimum version 1.12) Go version:go1.10.3 Git commit:e68fc7a Built:Tue Aug 21 17:29:02 2018 OS/Arch:linux/amd64 Experimental:true
如果是Linux系統,需要安裝docker和docker-compose,可以參考這裡https://yeasy.gitbooks.io/docker_practice/install/ ,針對不同的linux系統,都有安裝方法,寫的比較詳細,也可以參考官網資訊https://docs.docker.com/compose/install/
2.1.3 nodejs安裝
因為我們專案用的是nodejs SDK,所以實際專案中還需要安裝nodejs和npm來跑專案,因為Fabric目前只支援nodejs v8.4.0~v9.0.0的版本(2018-10),所以建議安裝的時候指定一個固定的版本,比如v8.9.0,我用的是這個版本,還是很穩定的,當然也可以先下載一個nvm來管理nodejs的版本,不同專案需要不同的node版本,使用nvm可以管理不同的node版本。
1、安裝nvm,執行下面命令下載安裝指令碼
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash #輸出 => Close and reopen your terminal to start using nvm or run the following to use it now: export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"# This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"# This loads nvm bash_completion
檢查下是否安裝成功
nvm --version 0.33.11
2、安裝nodejs,使用nvm ls-remote檢視遠端可安裝的node版本
nvm ls-remote v8.0.0 v8.1.0 v8.1.1 v8.1.2 v8.1.3 v8.1.4 v8.2.0 v8.2.1 v8.3.0 v8.4.0 v8.5.0 v8.6.0 v8.7.0 v8.8.0 v8.8.1 v8.9.0
找到自己想要安裝的版本來安裝nodejs
nvm install 8.9.0
使用node命令檢視是否安裝成功
node -v v8.9.1
也可以使用nvm ls來檢視當前環境有幾個node版本
nvm ls ->v8.9.1 system default -> 8.9.1 (-> v8.9.1) node -> stable (-> v8.9.1) (default) stable -> 8.9 (-> v8.9.1) (default) iojs -> N/A (default) lts/* -> lts/dubnium (-> N/A) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.15.0 (-> N/A) lts/carbon -> v8.14.0 (-> N/A) lts/dubnium -> v10.14.1 (-> N/A)
可以看到目前只安裝了8.9.0,而且系統預設使用的就是8.9.0,如果想改變系統的預設版本,可以使用如下命令
nvm use 8.9.0 Now using node v10.13.0 (npm v6.4.1)
2.1.4 Git工具安裝
Git安裝就簡單了吧,做過開發的人應該都安裝過,這裡貼個安裝教程,
https://git-scm.com/book/en/v2/Getting-Started-Installing-Git ,跟著上面安裝就行。
2.2 伺服器配置說明
說明一下伺服器的整個情況,我申請了4臺伺服器, 做4個peer,兩個orderer,具體的crypto-config.yaml配置如下:
OrdererOrgs: # --------------------------------------------------------------------------- # Orderer # --------------------------------------------------------------------------- - Name: Orderer Domain: xuyao.com # --------------------------------------------------------------------------- # "Specs" - See PeerOrgs below for complete description # --------------------------------------------------------------------------- Specs: - Hostname: orderer1 - Hostname: orderer2 # --------------------------------------------------------------------------- # "PeerOrgs" - Definition of organizations managing peer nodes # --------------------------------------------------------------------------- PeerOrgs: # --------------------------------------------------------------------------- # Org1 # --------------------------------------------------------------------------- - Name: Org1 Domain: org1.xuyao.com EnableNodeOUs: true # --------------------------------------------------------------------------- # "Specs" # --------------------------------------------------------------------------- # Uncomment this section to enable the explicit definition of hosts in your # configuration.Most users will want to use Template, below # # Specs is an array of Spec entries.Each Spec entry consists of two fields: #- Hostname:(Required) The desired hostname, sans the domain. #- CommonName: (Optional) Specifies the template or explicit override for #the CN.By default, this is the template: # #"{{.Hostname}}.{{.Domain}}" # #which obtains its values from the Spec.Hostname and #Org.Domain, respectively. # --------------------------------------------------------------------------- # Specs: #- Hostname: foo # implicitly "foo.org1.example.com" #CommonName: foo27.org5.example.com # overrides Hostname-based FQDN set above #- Hostname: bar #- Hostname: baz # --------------------------------------------------------------------------- # "Template" # --------------------------------------------------------------------------- # Allows for the definition of 1 or more hosts that are created sequentially # from a template. By default, this looks like "peer%d" from 0 to Count-1. # You may override the number of nodes (Count), the starting index (Start) # or the template used to construct the name (Hostname). # # Note: Template and Specs are not mutually exclusive.You may define both # sections and the aggregate nodes will be created for you.Take care with # name collisions # --------------------------------------------------------------------------- Template: Count: 2 # Start: 5 # Hostname: {{.Prefix}}{{.Index}} # default # --------------------------------------------------------------------------- # "Users" # --------------------------------------------------------------------------- # Count: The number of user accounts _in addition_ to Admin # --------------------------------------------------------------------------- Users: Count: 1 # --------------------------------------------------------------------------- # Org2: See "Org1" for full specification # --------------------------------------------------------------------------- - Name: Org2 Domain: org2.xuyao.com EnableNodeOUs: true Template: Count: 2 Users: Count: 1 # --------------------------------------------------------------------------- # Org3: See "Org1" for full specification # --------------------------------------------------------------------------- - Name: Org3 Domain: org3.xuyao.com EnableNodeOUs: true Template: Count: 2 Users: Count: 1 # --------------------------------------------------------------------------- # Org4: See "Org1" for full specification # --------------------------------------------------------------------------- - Name: Org4 Domain: org4.xuyao.com EnableNodeOUs: true Template: Count: 2 Users: Count: 1
每臺伺服器的執行大致如下
伺服器名稱 | 功能標識 |
---|---|
server1 | Org1, Peer1,Orderer1,Ca1,Kafka1, Zookeeper1 |
server1 | Org2, Peer2,Orderer2,Ca2,Kafka2, Zookeeper2 |
server3 | Org3, Peer3,Ca3,Kafka3, Zookeeper3 |
server4 | Org4, Peer4,Ca4,Kafka4 |
每臺伺服器一個Org組織,和一個節點, 其中兩臺伺服器有Orderer服務,四臺伺服器都有Ca認證。