hyperledger fabric 测试(八) 1.18 服务器多机部署

环境:腾讯云服务器三台:一台提供Order服务,另外两台为两个组织单节点,资金有限,全部测试都在solo环境,而且测试一台服务器多节点是失败,是不支持吗?不太确定

服务器配置:

  • 操作系统   Ubuntu Server 16.04.1 LTS 64位

  • CPU  2 核

  • 内存  4 GB

  • 公网带宽  5 Mbps

环境安装如单机单节点

设置/etc/hosts文件:sudo vim /etc/hosts

编辑文件:

172.27.0.7 orderer.example.com

172.27.0.10 peer0.org1.example.com

172.27.0.17 peer0.org2.example.com

关闭当前网络:cd scripts/fabricSample/first-network

              ./byfn.sh –m down

进入e2ecli文件夹:cd ~/go/src/github.com/hyperledger/fabric/examples/e2e_cli/

运行启动一键脚本:./network_setup.sh up

测试成功后关闭一键脚本:./network_setup.sh down

一键脚本生成fabric公私钥、证书、创世区块文件:./generateArtifacts.sh mychannel

复制文件生成peer容器配置文件:cp docker-compose-cli.yaml docker-compose-peer.yaml

复制文件生成order容器配置文件:cp docker-compose-cli.yaml docker-compose-orderer.yaml

编辑文件docker-compose-peer.yaml:

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:172.27.0.7"

  

  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_LOCALMSPTYPE=bccsp

      - 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/[email protected]/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:172.27.0.7"

    - "peer0.org1.example.com:172.27.0.10"

- "peer0.org2.example.com:172.27.0.17"

编辑docker-compose-orderer.yaml:

    orderer.example.com:

    extends:

      file:   base/docker-compose-base.yaml

      service: orderer.example.com

    container_name: orderer.example.com

    depends_on:

      - zookeeper0

      - zookeeper1

      - zookeeper2

      - kafka0

      - kafka1

      - kafka2

      - kafka3

将服务器镜像备份,其他服务器安装设置好服务器的镜像。 //腾讯云测试过可以

移除e2e_cli文件夹:rm –rf e2e_cli

退出到examples目录:cd ..

通过Linux文件分发命令分发文件:scp -r e2e_cli [email protected]:/home/ubuntu/go/src/github.com/hyperledger/fabric/examples/  //e2e为要分发的文件Ubuntu为要访问的账户名,172为要访问的内网IP,路径为文件复制到的路径

分发到组织2:scp -r e2e_cli [email protected]:/home/ubuntu/go/src/github.com/hyperledger/fabric/examples/

Order主机:

进入e2e_cli文件夹:cd e2e_cli/

构建排序容器:docker-compose -f docker-compose-orderer.yaml up –d

组织1服务器:

构建节点0容器服务:docker-compose -f docker-compose-peer.yaml up –d

进入节点容器:docker exec -it cli bash

组织2服务器:

构建节点0容器服务:docker-compose -f docker-compose-peer.yaml up –d

进入节点容器:docker exec -it cli bash

以下两节点同步执行:

组织1:引入名字环境变量:export CHANNEL_NAME=mychannel

生成通道配置区块:peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

节点加入通道:peer channel join -b mychannel.block

将通道配置文件上传至组织2 //下载mychannel.block文件,使用winscp上传至/opt/gopath/src/github.com/hyperledger/fabric/peer

组织2执行:引入名字环境变量:export CHANNEL_NAME=mychannel

节点加入通道:peer channel join -b mychannel.block

组织1设置锚节点: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 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 peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org1MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

组织2设置锚节点: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 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 peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org2MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

组织1和组织2都使用winscp上传智能合约至新建test文件夹,/opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go/test

组织1和组织2安装链码:peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/test

实例化链码(好像是一个实例化就可以了):peer chaincode instantiate -o orderer.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "OR ('Org1MSP.peer','Org2MSP.peer')" //需要指定export CHANNEL_NAME=mychannel

测试:组织1和组织2分别查询当前a数量:peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'  //常理都是100

组织1执行转账操作:peer chaincode invoke -o orderer.example.com:7050  --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem  -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}'

组织1和组织2分别查询当前a数量:peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'  //常理都是90

注意:每次启动容器都要重新设置通道环境变量export CHANNEL_NAME=mychannel

你可能感兴趣的:(区块链(Blockchain),hyperledger)