ubuntu1604 golang環境

copy來的,這裏記錄一下java

1. 升級系統: sudo apt-get upgrade
2. 安裝docker
下載docker-ce: https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/amd64/docker-ce_18.03.1~ce-0~ubuntu_amd64.deb
sudo dpkg -i docker-ce_18.03.1~ce-0~ubuntu_amd64.deb
sudo groupadd docker
sudo usermod -aG docker <YouUser>node

3. 安裝docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose python


4. linux

5.1 安裝與設置go語言開發環境
sudo tar -zxf go1.10.1.linux-amd64.tar.gz -C /usr/local/
vim ~/.bash_aliases
export GOROOT=/usr/local/go
export GOPATH=/home/jya/go
export GOBIN=$GOPATH/bin/
export PATH=.:$PATH:$GOROOT/bin:$GOBIN
export PATH=${PATH}:/home/jya/src/fabric-samples/bingit


5.2下載源碼
https://github.com/hyperledger/fabric-sdk-java.git
https://github.com/hyperledger/fabric-sdk-go.git
https://github.com/hyperledger/fabric-samples.git
https://github.com/hyperledger/fabric.git


6. 安裝基本工具: sudo apt-get install -y build-essential git make curl unzip g++ libtool openssl go-dep
sudo apt-get install python-pip
sudo pip install --upgrade pip
go get golang.org/x/tools/cmd/goimportsgithub

cd $GOPATH/src/golang.org/x/
git clone https://github.com/golang/tools.git
go install golang.org/x/tools/go/packages
go install golang.org/x/tools/imports
go install golang.org/x/tools/cover
go install golang.org/x/tools/go/loadergolang

mkdir $GOPATH/src/golang.org/ && cd $GOPATH/src/github.com/
git clone https://github.com/golang/lint.git
go install golang.org/x/lint/golintdocker



mkdir $GOPATH/src/github.com/maxbrunsfeld/
cd $GOPATH/src/github.com/maxbrunsfeld/
git clone https://github.com/maxbrunsfeld/counterfeiter.git
go install github.com/maxbrunsfeld/counterfeiterubuntu

mkdir $GOPATH/src/github.com/axw && cd $GOPATH/src/github.com/axw
git clone https://github.com/axw/gocov.git
go install github.com/axw/gocov
go install github.com/axw/gocov/gocov (必須執行)

mkdir $GOPATH/src/github.com/vektra && cd $GOPATH/src/github.com/vektra
git clone https://github.com/vektra/mockery.git
go install github.com/vektra/mockery
go install github.com/vektra/mockery/cmd/mockery


mkdir $GOPATH/src/github.com/golang && cd $GOPATH/src/github.com/golang
git clone https://github.com/golang/protobuf.git
make all
cp $GOPATH/bin/protoc-gen-go $GOPATH/src/github.com/hyperledger/fabric/.build/docker/gotools/bin/

cd $GOPATH/src/github.com/golang
git clone https://github.com/golang/dep.git
go install github.com/golang/dep
go install github.com/golang/dep/cmd/dep

注意: dns-nameservers 10.41.132.9 有時域名解析失敗,建議採用 10.30.1.9和8.8.8.8vim

7. 設置docker守護進程的代理


8. make all ,
若是在執行check_deps時報錯,能夠將./scripts/check_deps.sh文件中 dep version dep check兩個命令註釋掉。



部署fabric多機板
1. sudo mkdir -p /etc/hyperledger/fabric/ && sudo chown -R jya /etc/hyperledger/

2. 根據網絡拓撲建立crypto-config.yaml配置文件,該文件能夠以fabric-samples/first-network/crypto-config.yaml文件爲藍本,或者直接使用該文件。
3.生成相關材料: cryptogen generate --config=./crypto-config.yaml --output ./crypto-config 。執行命令後,會在當前目錄下生成文件夾 crypto-config,包含節點用戶的祕鑰以及證書文件,
4. 經過scp命令將crypto-config文件夾拷貝到其它設備的/etc/hyperledger/fabric 目錄下

5 .建立/etc/hyperledger/fabric/configtx.ymal文件,該文件能夠以fabric-samples/first-network/configtx.ymal文件爲藍本,或者直接使用該文件。


6. 根據configtx.ymal生成創世區塊以及通道等配置信息(位於10.43.33.120機器上)
6.0 設置環境變量:export FABRIC_CFG_PATH=/etc/hyperledger/fabric
6.1 建立channel-artifacts目錄: mkdir -p /etc/hyperledger/fabric/channel-artifacts
6.2 進入該目錄: cd /etc/hyperledger/fabric/channel-artifacts
6.3 建立創世區塊: configtxgen -profile TwoOrgsOrdererGenesis -outputBlock genesis.block
6.4 .建立通道事物: export CHANNEL_NAME=mychannel && configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel.tx -channelID $CHANNEL_NAME
注意: 能夠將mychannel替換成須要的通道名。
6.5.定義org1的anchor peer: configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP
6.6.定義org2的anchor peer: configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP
將會生成如下文件:channel.tx genesis.block Org1MSPanchors.tx Org2MSPanchors.tx

7. 修改每臺機器上的/etc/hosts文件,添加如下內容(須要根據本身的IP和網絡拓撲決定)
10.43.33.120 orderer.example.com
10.43.33.122 peer0.org1.example.com
10.43.33.122 peer0.org2.example.com
10.43.33.125 peer1.org1.example.com
10.43.33.125 peer1.org2.example.com

8. 準備order節點啓動所需的材料
orderer 節點的執行環境目錄下必須有如下文件及文件夾:./crypto-config、./msp、./tls、orderer.yaml、genesis.block
orderer.yaml文件能夠參考fabric-samples/config/orderer.yaml 文件,注意:
須要將 ListenAddress: 127.0.0.1 改爲 ListenAddress: 0.0.0.0
須要將 TwoOrgsOrdererGenesis
cd /etc/hyperledger/fabric/
cp channel-artifacts/genesis.block channel-artifacts/
cp -r ./crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp/ ./
若是上述命令啓動orderer時報錯,能夠考慮執行下面的命令: cp -r ./crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp/ .

cp -r ./crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/ ./
建立日誌目錄: sudo mkdir -p /var/hyperledger/ && sudo chown -R jya /var/hyperledger/
啓動orderer節點: orderer start
注意: 若是報 [orderer/commmon/multichannel] newLedgerResources -> PANI 004 Error creating channelconfig bundle: initializing channelconfig failed: could not create channel Orderer sub-group config: 如下錯誤,則須要刪除 /data/hyperledger/目錄,具體見orderer.yaml文件中的FileLedger的配置項

注意: 須要將 orderer.yaml配置文件中的 LocalMSPID: SampleOrg改爲 LocalMSPID: OrdererMSP,不然有可能報



啓動order節點: orderer start


9. 啓動peer0.org1節點。
peer 節點的執行環境目錄下必須有如下文件及文件夾:./msp、./tls、core.yaml、channel.tx 、 core.yaml
9.1 在peer建立/etc/hyperledger/fabric/ 目錄:
sudo mkdir -p /etc/hyperledger/fabric/ && sudo chown -R jya /etc/hyperledger/ 。
9.2 建立core.yaml配置文件,該文件能夠以fabric-samples/config/core.yaml文件爲藍本,或者直接使用該文件。
9.3 拷貝證書及可執行文件(在10.43.33.120機器上執行如下命令):
scp -r ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ 10.43.33.122:/etc/hyperledger/fabric/
scp -r ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp/ 10.43.33.122:/etc/hyperledger/fabric/
拷貝peer可執行文件: scp -r ./peer 10.43.33.122:/etc/hyperledger/fabric/
9.4 建立日誌目錄: sudo mkdir -p /var/hyperledger/ && sudo chown -R jya /var/hyperledger/
9.5 啓動peer節點: peer node start

10. 啓動peer0.org2節點。
peer 節點的執行環境目錄下必須有如下文件及文件夾:./msp、./tls、core.yaml、channel.tx 、 core.yaml
9.1 在peer建立/etc/hyperledger/fabric/ 目錄:
sudo mkdir -p /etc/hyperledger/fabric/ && sudo chown -R jya /etc/hyperledger/ 。
9.2 建立core.yaml配置文件,該文件能夠以fabric-samples/config/core.yaml文件爲藍本,或者直接使用該文件。
scp bin/peer config/core.yaml 10.43.33.125:/etc/hyperledger/fabric/
9.3 拷貝證書信息(在10.43.33.120機器上執行如下命令):
scp -r ./crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ 10.43.33.125:/etc/hyperledger/fabric/
scp -r ./crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/msp/ 10.43.33.125:/etc/hyperledger/fabric/
scp bin/peer config/core.yaml 10.43.33.125:/etc/hyperledger/fabric/(在10.43.33.119機器上執行命令)
9.5 建立日誌目錄: sudo mkdir -p /var/hyperledger/ && sudo chown -R jya /var/hyperledger/
9.5 啓動peer節點: peer node start

11. 在peer0.org1節點(10.43.33.122)上建立並加入channel
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/fabric/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
peer channel create -o orderer.example.com:7050 -c mychannel -f ./channel.tx --tls true --cafile /etc/hyperledger/fabric/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem 注意:若是報錯,則須要卻掉 --tls true 選項。
加入channel: peer channel join -b mychannel.block

12 更新peer0.org1的anchor節點
export FABRIC_CFG_PATH=/etc/hyperledger/fabric
peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org1MSPanchors.tx --cafile /etc/hyperledger/fabric/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem


13. 在peer0.org1節點(10.43.33.122)上安裝並初始化chainchdoe(建議最好打包chaincode再安裝,而不是直接調用peer chaincode install命令,不然有可能致使其它節點一樣的chaincode沒法實例化)
chaincode通常放在GOPATH/src目錄當中,而且引用時須要使用與GOATH/src的相對路徑, -p參數儘可能放在最後。
  安裝chaincode: peer chaincode install -n mychannel -v 1.0 -p chaincode/chaincode_example02/go
初始化chaincode: peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n mychannel -c '{"Args":["init","A","10","B","10"]}' -P "OR ('Org1MSP.member')" -v 1.0

14. 調用chaincode: $ peer chaincode query -C mychannel -n mychannel -c '{"Args":["query","A"]}'



常見錯誤:
建立channel時報錯:
Error: failed to create deliver client: orderer client failed to connect to orderer.example.com:7050: failed to create new connection: context deadline exceeded
多是由於建立channel時指定了 --tls true 參數,去掉便可。


錯誤描述:
[channel: mychannel] Rejecting broadcast of config message from 10.43.33.122:45346 because of error: Failed to reach implicit threshold of 1 sub-policies, required 1 remaining: permission denied
解決辦法: 將orderer.yaml配置文件中的 LocalMSPID: SampleOrg改爲 LocalMSPID: OrdererMSP,

錯誤描述:
在org1組織的peer0節點上加入通道(peer channel join -b mychannel.block)時,報錯:
MSP error: expected MSP ID SampleOrg, received Org1MSP
姐姐辦法,將 core.yaml配置文件中的 LocalMSPID: SampleOrg改爲 LocalMSPID:Org1MSP,

錯誤描述:安裝chaincode時報錯: Error: error getting chaincode code mychannel: <go, [env]>: failed with error: "exec: not started"解決拌飯: 多是由於沒有安裝go語言開發環境。

相關文章
相關標籤/搜索