经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 其他 » 区块链 » 查看文章
基于raft共识搭建的Fabric1.4.4多机网络环境
来源:cnblogs  作者:养许乐多  时间:2020/11/9 15:19:59  对本文有异议

1准备工作介绍

1各个主机ip以及节点分配情况

各个主机的节点分配情况 ip地址
orderer0.example.com,peer0.org1.example.com 172.17.3.60
orderer1.example.com,peer1.org1.example.com 172.17.3.61
orderer2.example.com,peer2.org1.example.com 172.17.3.62

2.基于Raft共识的多机的fabric网络搭建

? 这一节是对每台阿里云主机的各个节点的网络配置,其中主要涉及的是配置文件,每一步的后面会标注目标主机。

1. 创建raft目录(172.17.3.60)

  1. mkdir /root/go/src/github.com/hyperledger/fabric/raft
  2. cd /root/go/src/github.com/hyperledger/fabric/raft

2. 获取生成工具(172.17.3.60)

上传hyperledger-fabric-linux-amd64-1.4.4.tar.gz二进制文件包并解压,然后赋予权限。

  1. tar -zxvf hyperledger-fabric-linux-amd64-1.4.4.tar.gz
  2. chmod -R 777 ./bin

3. 准备crypto-config.yaml和configtx.yaml文件(172.17.3.60)

  • crypto-config.yaml
  1. OrdererOrgs:
  2. - Name: Orderer
  3. Domain: example.com
  4. Specs:
  5. - Hostname: orderer0
  6. - Hostname: orderer1
  7. - Hostname: orderer2
  8. PeerOrgs:
  9. - Name: Org1
  10. Domain: org1.example.com
  11. EnableNodeOUs: true
  12. Template:
  13. Count: 3
  14. Users:
  15. Count: 3
  • configtx.yaml
  1. ---
  2. Organizations:
  3. - &OrdererOrg
  4. Name: OrdererOrg
  5. ID: OrdererMSP
  6. MSPDir: crypto-config/ordererOrganizations/example.com/msp
  7. Policies:
  8. Readers:
  9. Type: Signature
  10. Rule: "OR('OrdererMSP.member')"
  11. Writers:
  12. Type: Signature
  13. Rule: "OR('OrdererMSP.member')"
  14. Admins:
  15. Type: Signature
  16. Rule: "OR('OrdererMSP.admin')"
  17. - &Org1
  18. Name: Org1MSP
  19. ID: Org1MSP
  20. MSPDir: crypto-config/peerOrganizations/org1.example.com/msp
  21. AnchorPeers:
  22. - Host: peer0.org1.example.com
  23. Port: 7051
  24. Policies:
  25. Readers:
  26. Type: Signature
  27. Rule: "OR('Org1MSP.admin','Org1MSP.peer','Org1MSP.client')"
  28. Writers:
  29. Type: Signature
  30. Rule: "OR('Org1MSP.admin','Org1MSP.client')"
  31. Admins:
  32. Type: Signature
  33. Rule: "OR('Org1MSP.admin')"
  34. Capabilities:
  35. Channel: &ChannelCapabilities
  36. V1_4_3: true
  37. V1_3: false
  38. V1_1: false
  39. Orderer: &OrdererCapabilities
  40. V1_4_2: true
  41. V1_1: false
  42. Application: &ApplicationCapabilities
  43. V1_4_2: true
  44. V1_3: false
  45. V1_2: false
  46. V1_1: false
  47. Application: &ApplicationDefaults
  48. Organizations:
  49. Policies:
  50. Readers:
  51. Type: ImplicitMeta
  52. Rule: "ANY Readers"
  53. Writers:
  54. Type: ImplicitMeta
  55. Rule: "ANY Writers"
  56. Admins:
  57. Type: ImplicitMeta
  58. Rule: "MAJORITY Admins"
  59. Capabilities:
  60. <<: *ApplicationCapabilities
  61. Orderer: &OrdererDefaults
  62. OrdererType: solo
  63. Addresses:
  64. - orderer0.example.com:7050
  65. BatchTimeout: 2s
  66. BatchSize:
  67. MaxMessageCount: 200
  68. AbsoluteMaxBytes: 2 MB
  69. PreferredMaxBytes: 512 KB
  70. Kafka:
  71. Brokers:
  72. - 127.0.0.1:9092
  73. Organizations:
  74. Policies:
  75. Readers:
  76. Type: ImplicitMeta
  77. Rule: "ANY Readers"
  78. Writers:
  79. Type: ImplicitMeta
  80. Rule: "ANY Writers"
  81. Admins:
  82. Type: ImplicitMeta
  83. Rule: "MAJORITY Admins"
  84. BlockValidation:
  85. Type: ImplicitMeta
  86. Rule: "ANY Writers"
  87. Channel: &ChannelDefaults
  88. Policies:
  89. Readers:
  90. Type: ImplicitMeta
  91. Rule: "ANY Readers"
  92. Writers:
  93. Type: ImplicitMeta
  94. Rule: "ANY Writers"
  95. Admins:
  96. Type: ImplicitMeta
  97. Rule: "MAJORITY Admins"
  98. Capabilities:
  99. <<: *ChannelCapabilities
  100. Profiles:
  101. TwoOrgsOrdererGenesis:
  102. <<: *ChannelDefaults
  103. Capabilities:
  104. <<: *ChannelCapabilities
  105. Orderer:
  106. <<: *OrdererDefaults
  107. OrdererType: etcdraft
  108. EtcdRaft:
  109. Consenters:
  110. - Host: orderer0.example.com
  111. Port: 7050
  112. ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/tls/server.crt
  113. ServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/tls/server.crt
  114. - Host: orderer1.example.com
  115. Port: 7050
  116. ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer1.example.com/tls/server.crt
  117. ServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer1.example.com/tls/server.crt
  118. - Host: orderer2.example.com
  119. Port: 7050
  120. ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer2.example.com/tls/server.crt
  121. ServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer2.example.com/tls/server.crt
  122. Addresses:
  123. - orderer0.example.com:7050
  124. - orderer1.example.com:7050
  125. - orderer2.example.com:7050
  126. Organizations:
  127. - *OrdererOrg
  128. Capabilities:
  129. <<: *OrdererCapabilities
  130. Application:
  131. <<: *ApplicationDefaults
  132. Organizations:
  133. - <<: *OrdererOrg
  134. Consortiums:
  135. SampleConsortium:
  136. Organizations:
  137. - *Org1
  138. TwoOrgsChannel:
  139. Consortium: SampleConsortium
  140. <<: *ChannelDefaults
  141. Application:
  142. <<: *ApplicationDefaults
  143. Organizations:
  144. - *Org1
  145. Capabilities:
  146. <<: *ApplicationCapabilities

  

4. 生成公私钥和证书(172.17.3.60)

  1. ./bin/cryptogen generate --config=./crypto-config.yaml

5.生成创世区块(172.17.3.60)

  1. mkdir channel-artifacts
  1. ./bin/configtxgen -profile TwoOrgsOrdererGenesis -channelID systemchannel -outputBlock ./channel-artifacts/genesis.block

6. 生成通道配置区块(172.17.3.60)

  1. ./bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID testchannel

7.上传智能合约(172.17.3.60)

  1. mkdir chaincode //将自己写好的链码放置此处即可

8.拷贝生成文件到其它电脑(172.17.3.60)

  1. cd ..
  2. scp -r raft root@172.17.3.61:/root/go/src/github.com/hyperledger/fabric
  3. scp -r raft root@172.17.3.62:/root/go/src/github.com/hyperledger/fabric

9.准备orderer配置文件(172.17.3.60)

docker-compose-orderer.yaml

  1. version: '2'
  2. volumes:
  3. orderer0.example.com:
  4. services:
  5. orderer0.example.com:
  6. container_name: orderer0.example.com
  7. image: hyperledger/fabric-orderer:latest
  8. environment:
  9. - FABRIC_LOGGING_SPEC=INFO
  10. - GODEBUG=netdns=go
  11. - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
  12. - ORDERER_GENERAL_GENESISMETHOD=file
  13. - ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block
  14. - ORDERER_GENERAL_LOCALMSPID=OrdererMSP
  15. - ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
  16. # enabled TLS
  17. - ORDERER_GENERAL_TLS_ENABLED=true
  18. - ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
  19. - ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
  20. - ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
  21. - ORDERER_KAFKA_TOPIC_REPLICATIONFACTOR=1
  22. - ORDERER_KAFKA_VERBOSE=true
  23. - ORDERER_GENERAL_CLUSTER_CLIENTCERTIFICATE=/var/hyperledger/orderer/tls/server.crt
  24. - ORDERER_GENERAL_CLUSTER_CLIENTPRIVATEKEY=/var/hyperledger/orderer/tls/server.key
  25. - ORDERER_GENERAL_CLUSTER_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
  26. working_dir: /opt/gopath/src/github.com/hyperledger/fabric
  27. command: orderer
  28. volumes:
  29. - ./channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block
  30. - ./crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/msp:/var/hyperledger/orderer/msp
  31. - ./crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/tls/:/var/hyperledger/orderer/tls
  32. - orderer0.example.com:/var/hyperledger/production/orderer
  33. ports:
  34. - 7050:7050
  35. extra_hosts:
  36. - "orderer0.example.com:172.17.3.60"
  37. - "orderer1.example.com:172.17.3.61"
  38. - "orderer2.example.com:172.17.3.62"

10.准备peer配置文件(172.17.3.60)

docker-compose-peer.yaml

  1. version: '2'
  2. volumes:
  3. peer0.org1.example.com:
  4. services:
  5. peer0.org1.example.com:
  6. container_name: peer0.org1.example.com
  7. hostname: peer0.org1.example.com
  8. image: hyperledger/fabric-peer:latest
  9. environment:
  10. - CORE_PEER_ID=peer0.org1.example.com
  11. - GODEBUG=netdns=go
  12. - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
  13. - CORE_PEER_CHAINCODELISTENADDRESS=peer0.org1.example.com:7052
  14. - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.example.com:7051
  15. - CORE_PEER_LOCALMSPID=Org1MSP
  16. - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
  17. - FABRIC_LOGGING_SPEC=INFO
  18. - CORE_PEER_GOSSIP_USELEADERELECTION=true
  19. - CORE_PEER_GOSSIP_ORGLEADER=false
  20. - CORE_PEER_PROFILE_ENABLED=true
  21. - CORE_PEER_TLS_ENABLED=true
  22. - CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
  23. - CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
  24. - CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
  25. working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
  26. command: peer node start
  27. volumes:
  28. - /var/run/:/host/var/run/
  29. - ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp:/etc/hyperledger/fabric/msp
  30. - ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls:/etc/hyperledger/fabric/tls
  31. - peer0.org1.example.com:/var/hyperledger/production
  32. ports:
  33. - 7051:7051
  34. - 7052:7052
  35. - 7053:7053
  36. extra_hosts:
  37. - "orderer0.example.com:172.17.3.60"
  38. - "orderer1.example.com:172.17.3.61"
  39. - "orderer2.example.com:172.17.3.62"

11.准备cli配置文件(172.17.3.60)

docker-compose-cli.yaml

  1. version: '2'
  2. services:
  3. cli:
  4. container_name: cli
  5. image: hyperledger/fabric-tools:latest
  6. tty: true
  7. environment:
  8. - GOPATH=/opt/gopath
  9. - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
  10. - FABRIC_LOGGING_SPEC=INFO
  11. - GODEBUG=netdns=go
  12. - CORE_PEER_ID=cli
  13. - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
  14. - CORE_PEER_LOCALMSPID=Org1MSP
  15. - CORE_PEER_TLS_ENABLED=true
  16. - 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
  17. - 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
  18. - 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
  19. - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
  20. working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
  21. volumes:
  22. - /var/run/:/host/var/run/
  23. - ./chaincode:/opt/gopath/src/github.com/chaincode
  24. - ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
  25. - ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
  26. extra_hosts:
  27. - "orderer0.example.com:172.17.3.60"
  28. - "orderer1.example.com:172.17.3.61"
  29. - "orderer2.example.com:172.17.3.62"
  30. - "peer0.org1.example.com:172.17.3.60"
  31. - "peer1.org1.example.com:172.17.3.61"
  32. - "peer2.org1.example.com:172.17.3.62"

12.准备ca配置文件(172.17.3.60)

docker-compose-ca.yaml

  1. version: '2'
  2.  
  3. services:
  4. ca0:
  5. image: hyperledger/fabric-ca:latest
  6. environment:
  7. - FABRIC_CA_HOME=/var/hyperledger/fabric-ca-server
  8. - FABRIC_CA_SERVER_CA_NAME=ca-org1
  9. - FABRIC_CA_SERVER_CA_CERTFILE=/var/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem
  10. - FABRIC_CA_SERVER_CA_KEYFILE=/var/hyperledger/fabric-ca-server-config/01695cb495cf30e01ec3f405f9ef370a35713ece8cbbf7d1334da245fbfa029c_sk
  11. - FABRIC_CA_SERVER_TLS_ENABLED=true
  12. - FABRIC_CA_SERVER_TLS_CERTFILE=/var/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem
  13. - FABRIC_CA_SERVER_TLS_KEYFILE=/var/hyperledger/fabric-ca-server-config/01695cb495cf30e01ec3f405f9ef370a35713ece8cbbf7d1334da245fbfa029c_sk
  14. - FABRIC_CA_SERVER_PORT=7054
  15. ports:
  16. - "7054:7054"
  17. command: sh -c 'fabric-ca-server start -b admin:adminpw -d'
  18. volumes:
  19. - ./crypto-config/peerOrganizations/org1.example.com/ca/:/var/hyperledger/fabric-ca-server-config
  20. container_name: ca_peerOrg1

13.创建启动容器脚本(172.17.3.60)

up.sh

  1. #!/bin/bash
  2. docker-compose -f ./docker-compose-orderer.yaml up -d
  3. sleep 10
  4. docker-compose -f ./docker-compose-peer.yaml up -d
  5. docker-compose -f ./docker-compose-cli.yaml up -d
  6. docker-compose -f ./docker-compose-ca.yaml up -d

 

  1. //给脚本添加权限
  2. chmod +x up.sh

 

 

 

准备第二台主机的配置,进入raft文件夹

1.准备orderer配置文件(172.17.3.61)

docker-compose-orderer.yaml

  1. version: '2'
  2. volumes:
  3. orderer1.example.com:
  4. services:
  5. orderer1.example.com:
  6. container_name: orderer1.example.com
  7. image: hyperledger/fabric-orderer:latest
  8. environment:
  9. - FABRIC_LOGGING_SPEC=INFO
  10. - GODEBUG=netdns=go
  11. - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
  12. - ORDERER_GENERAL_GENESISMETHOD=file
  13. - ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block
  14. - ORDERER_GENERAL_LOCALMSPID=OrdererMSP
  15. - ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
  16. # enabled TLS
  17. - ORDERER_GENERAL_TLS_ENABLED=true
  18. - ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
  19. - ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
  20. - ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
  21. - ORDERER_KAFKA_TOPIC_REPLICATIONFACTOR=1
  22. - ORDERER_KAFKA_VERBOSE=true
  23. - ORDERER_GENERAL_CLUSTER_CLIENTCERTIFICATE=/var/hyperledger/orderer/tls/server.crt
  24. - ORDERER_GENERAL_CLUSTER_CLIENTPRIVATEKEY=/var/hyperledger/orderer/tls/server.key
  25. - ORDERER_GENERAL_CLUSTER_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
  26. working_dir: /opt/gopath/src/github.com/hyperledger/fabric
  27. command: orderer
  28. volumes:
  29. - ./channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block
  30. - ./crypto-config/ordererOrganizations/example.com/orderers/orderer1.example.com/msp:/var/hyperledger/orderer/msp
  31. - ./crypto-config/ordererOrganizations/example.com/orderers/orderer1.example.com/tls/:/var/hyperledger/orderer/tls
  32. - orderer1.example.com:/var/hyperledger/production/orderer
  33. ports:
  34. - 7050:7050
  35. extra_hosts:
  36. - "orderer0.example.com:172.17.3.60"
  37. - "orderer1.example.com:172.17.3.61"
  38. - "orderer2.example.com:172.17.3.62"

2.准备peer配置文件(172.17.3.61)

docker-compose-peer.yaml

  1. version: '2'
  2. volumes:
  3. peer1.org1.example.com:
  4. services:
  5. peer1.org1.example.com:
  6. container_name: peer1.org1.example.com
  7. hostname: peer1.org1.example.com
  8. image: hyperledger/fabric-peer:latest
  9. environment:
  10. - CORE_PEER_ID=peer1.org1.example.com
  11. - GODEBUG=netdns=go
  12. - CORE_PEER_ADDRESS=peer1.org1.example.com:7051
  13. - CORE_PEER_CHAINCODELISTENADDRESS=peer1.org1.example.com:7052
  14. - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org1.example.com:7051
  15. - CORE_PEER_LOCALMSPID=Org1MSP
  16. - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
  17. - FABRIC_LOGGING_SPEC=INFO
  18. - CORE_PEER_GOSSIP_USELEADERELECTION=true
  19. - CORE_PEER_GOSSIP_ORGLEADER=false
  20. - CORE_PEER_PROFILE_ENABLED=true
  21. - CORE_PEER_TLS_ENABLED=true
  22. - CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
  23. - CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
  24. - CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
  25. working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
  26. command: peer node start
  27. volumes:
  28. - /var/run/:/host/var/run/
  29. - ./crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/msp:/etc/hyperledger/fabric/msp
  30. - ./crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls:/etc/hyperledger/fabric/tls
  31. - peer1.org1.example.com:/var/hyperledger/production
  32. ports:
  33. - 7051:7051
  34. - 7052:7052
  35. - 7053:7053
  36. extra_hosts:
  37. - "orderer0.example.com:172.17.3.60"
  38. - "orderer1.example.com:172.17.3.61"
  39. - "orderer2.example.com:172.17.3.62"

3.准备cli配置文件(172.17.3.61)

docker-compose-cli.yaml

  1. version: '2'
  2. services:
  3. cli:
  4. container_name: cli
  5. image: hyperledger/fabric-tools:latest
  6. tty: true
  7. environment:
  8. - GOPATH=/opt/gopath
  9. - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
  10. - FABRIC_LOGGING_SPEC=INFO
  11. - GODEBUG=netdns=go
  12. - CORE_PEER_ID=cli
  13. - CORE_PEER_ADDRESS=peer1.org1.example.com:7051
  14. - CORE_PEER_LOCALMSPID=Org1MSP
  15. - CORE_PEER_TLS_ENABLED=true
  16. - CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/server.crt
  17. - CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/server.key
  18. - CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/ca.crt
  19. - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
  20. working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
  21. volumes:
  22. - /var/run/:/host/var/run/
  23. - ./chaincode:/opt/gopath/src/github.com/chaincode
  24. - ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
  25. - ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
  26. extra_hosts:
  27. - "orderer0.example.com:172.17.3.60"
  28. - "orderer1.example.com:172.17.3.61"
  29. - "orderer2.example.com:172.17.3.62"
  30. - "peer0.org1.example.com:172.17.3.60"
  31. - "peer1.org1.example.com:172.17.3.61"
  32. - "peer2.org1.example.com:172.17.3.62"

4.创建启动容器脚本(172.17.3.61)

up.sh

  1. #!/bin/bash
  2. docker-compose -f ./docker-compose-orderer.yaml up -d
  3. sleep 10
  4. docker-compose -f ./docker-compose-peer.yaml up -d
  5. docker-compose -f ./docker-compose-cli.yaml up -d

 

  1. //给脚本添加权限
  2. chmod +x up.sh

  

 

 

准备第三台主机的配置,进入raft文件夹

1.准备orderer配置文件(172.17.3.62)

docker-compose-orderer.yaml

  1. version: '2'
  2. volumes:
  3. orderer2.example.com:
  4. services:
  5. orderer2.example.com:
  6. container_name: orderer2.example.com
  7. image: hyperledger/fabric-orderer:latest
  8. environment:
  9. - FABRIC_LOGGING_SPEC=INFO
  10. - GODEBUG=netdns=go
  11. - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
  12. - ORDERER_GENERAL_GENESISMETHOD=file
  13. - ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block
  14. - ORDERER_GENERAL_LOCALMSPID=OrdererMSP
  15. - ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
  16. # enabled TLS
  17. - ORDERER_GENERAL_TLS_ENABLED=true
  18. - ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
  19. - ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
  20. - ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
  21. - ORDERER_KAFKA_TOPIC_REPLICATIONFACTOR=1
  22. - ORDERER_KAFKA_VERBOSE=true
  23. - ORDERER_GENERAL_CLUSTER_CLIENTCERTIFICATE=/var/hyperledger/orderer/tls/server.crt
  24. - ORDERER_GENERAL_CLUSTER_CLIENTPRIVATEKEY=/var/hyperledger/orderer/tls/server.key
  25. - ORDERER_GENERAL_CLUSTER_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
  26. working_dir: /opt/gopath/src/github.com/hyperledger/fabric
  27. command: orderer
  28. volumes:
  29. - ./channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block
  30. - ./crypto-config/ordererOrganizations/example.com/orderers/orderer2.example.com/msp:/var/hyperledger/orderer/msp
  31. - ./crypto-config/ordererOrganizations/example.com/orderers/orderer2.example.com/tls/:/var/hyperledger/orderer/tls
  32. - orderer2.example.com:/var/hyperledger/production/orderer
  33. ports:
  34. - 7050:7050
  35. extra_hosts:
  36. - "orderer0.example.com:172.17.3.60"
  37. - "orderer1.example.com:172.17.3.61"
  38. - "orderer2.example.com:172.17.3.62"

2.准备peer配置文件(172.17.3.62)

docker-compose-peer.yaml

  1. version: '2'
  2. volumes:
  3. peer2.org1.example.com:
  4. services:
  5. peer2.org1.example.com:
  6. container_name: peer2.org1.example.com
  7. hostname: peer2.org1.example.com
  8. image: hyperledger/fabric-peer:latest
  9. environment:
  10. - CORE_PEER_ID=peer2.org1.example.com
  11. - GODEBUG=netdns=go
  12. - CORE_PEER_ADDRESS=peer2.org1.example.com:7051
  13. - CORE_PEER_CHAINCODELISTENADDRESS=peer2.org1.example.com:7052
  14. - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer2.org1.example.com:7051
  15. - CORE_PEER_LOCALMSPID=Org1MSP
  16. - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
  17. - FABRIC_LOGGING_SPEC=INFO
  18. - CORE_PEER_GOSSIP_USELEADERELECTION=true
  19. - CORE_PEER_GOSSIP_ORGLEADER=false
  20. - CORE_PEER_PROFILE_ENABLED=true
  21. - CORE_PEER_TLS_ENABLED=true
  22. - CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
  23. - CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
  24. - CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
  25. working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
  26. command: peer node start
  27. volumes:
  28. - /var/run/:/host/var/run/
  29. - ./crypto-config/peerOrganizations/org1.example.com/peers/peer2.org1.example.com/msp:/etc/hyperledger/fabric/msp
  30. - ./crypto-config/peerOrganizations/org1.example.com/peers/peer2.org1.example.com/tls:/etc/hyperledger/fabric/tls
  31. - peer2.org1.example.com:/var/hyperledger/production
  32. ports:
  33. - 7051:7051
  34. - 7052:7052
  35. - 7053:7053
  36. extra_hosts:
  37. - "orderer0.example.com:172.17.3.60"
  38. - "orderer1.example.com:172.17.3.61"
  39. - "orderer2.example.com:172.17.3.62"

3.准备cli配置文件(172.17.3.62)

docker-compose-cli.yaml

  1. version: '2'
  2. services:
  3. cli:
  4. container_name: cli
  5. image: hyperledger/fabric-tools:latest
  6. tty: true
  7. environment:
  8. - GOPATH=/opt/gopath
  9. - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
  10. - FABRIC_LOGGING_SPEC=INFO
  11. - GODEBUG=netdns=go
  12. - CORE_PEER_ID=cli
  13. - CORE_PEER_ADDRESS=peer2.org1.example.com:7051
  14. - CORE_PEER_LOCALMSPID=Org1MSP
  15. - CORE_PEER_TLS_ENABLED=true
  16. - CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer2.org1.example.com/tls/server.crt
  17. - CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer2.org1.example.com/tls/server.key
  18. - CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer2.org1.example.com/tls/ca.crt
  19. - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
  20. working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
  21. volumes:
  22. - /var/run/:/host/var/run/
  23. - ./chaincode:/opt/gopath/src/github.com/chaincode
  24. - ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
  25. - ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
  26. extra_hosts:
  27. - "orderer0.example.com:172.17.3.60"
  28. - "orderer1.example.com:172.17.3.61"
  29. - "orderer2.example.com:172.17.3.62"
  30. - "peer0.org1.example.com:172.17.3.60"
  31. - "peer1.org1.example.com:172.17.3.61"
  32. - "peer2.org1.example.com:172.17.3.62"

4.创建启动容器脚本(172.17.3.62)

up.sh

  1. #!/bin/bash
  2. docker-compose -f ./docker-compose-orderer.yaml up -d
  3. sleep 10
  4. docker-compose -f ./docker-compose-peer.yaml up -d
  5. docker-compose -f ./docker-compose-cli.yaml up -d

 

  1. //给脚本添加权限
  2. chmod +x up.sh

 

 

 

  • 各个主机进行交互时,主机名的解析会影响交互速度,所以我们在/etc/hosts中将ip与主机名绑定起来,执行vi /etc/hosts,并将下面代码块中的内容填写到文件中(每台主机都改)
  1. 172.17.3.60 peer0.org1.example.com
  2. 172.17.3.61 peer1.org1.example.com
  3. 172.17.3.62 peer2.org1.example.com
  4. 172.17.3.60 orderer0.example.com
  5. 172.17.3.61 orderer1.example.com
  6. 172.17.3.62 orderer2.example.com

  然后就可以启动每一台主机的启动容器脚本up.sh了

  1. ./up.sh

  

 

3.利用cli命令行工具和fabric底层网络进行交互

172.17.3.60:

  1. docker exec -it cli bash //进入到cli容器中
  2. peer channel create -o orderer0.example.com:7050 -c testchannel -f ./channel-artifacts/channel.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem //创建应用通道
  3. peer channel join -b testchannel.block //当前peer加入到通道中
  4. peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode //在当前peer上安装链码
  5. peer chaincode package -p github.com/chaincode -n mycc -v 1.0 mycc.1.0.out //将链码打包
  6. exit //退出容器
  7. docker cp cli:/opt/gopath/src/github.com/hyperledger/fabric/peer/testchannel.block ./channel-artifacts //将生成的创世块文件从docker容器中拷贝出来
  8. docker cp cli:/opt/gopath/src/github.com/hyperledger/fabric/peer/mycc.1.0.out ./channel-artifacts //将链码文件从docker容器中拷贝出来
  9. //然后将创世块和链码拷贝至 其他两台主机
  10. cd channel-artifacts
  11. scp -r testchannel.block root@172.17.3.61:/root/go/src/github.com/hyperledger/fabric/raft/channel-artifacts
  12. scp -r mycc.1.0.out root@172.17.3.62:/root/go/src/github.com/hyperledger/fabric/raft/channel-artifacts

 

172.17.3.61:

  1. docker exec -it cli bash //进入到cli容器中
  2. mv ./channel-artifacts/testchannel.block /opt/gopath/src/github.com/hyperledger/fabric/peer //将创世块文件拷贝到docker容器内
  3. mv ./channel-artifacts/mycc.1.0.out /opt/gopath/src/github.com/hyperledger/fabric/peer //将链码文件拷贝到docker容器内
  4. peer channel join -b testchannel.block //当前peer加入到通道中
  5. peer chaincode install mycc.1.0.out //当前peer上安装链码

  

172.17.3.62:

  1. docker exec -it cli bash //进入到cli容器中
  2. mv ./channel-artifacts/testchannel.block /opt/gopath/src/github.com/hyperledger/fabric/peer //将创世块文件拷贝到docker容器内
  3. mv ./channel-artifacts/mycc.1.0.out /opt/gopath/src/github.com/hyperledger/fabric/peer //将链码文件拷贝到docker容器内
  4. peer channel join -b testchannel.block //当前peer加入到通道中
  5. peer chaincode install mycc.1.0.out //当前peer上安装链码

  

172.17.3.60:

  1. docker exec -it cli bash //进入到cli容器中
  2.  
  3. peer chaincode instantiate -o orderer0.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C testchannel -n mycc -l golang -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "AND('Org1MSP.peer')" //实例化链码
  4.  
  5. peer chaincode query -C testchannel -n mycc -c '{"Args":["query","a"]}' //查询a的数据,看是否初始化成功
  6.  
  7. peer chaincode invoke -o orderer0.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C testchannel -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}' //将a的10块钱转账给b

  

172.17.3.61:

  1. docker exec -it cli bash //进入到cli容器中
  2. peer chaincode query -C testchannel -n mycc -c '{"Args":["query","a"]}' //查询交互后的a的值

 

172.17.3.62:

  1. docker exec -it cli bash //进入到cli容器中
  2. peer chaincode query -C testchannel -n mycc -c '{"Args":["query","b"]}' //查询交互后的b的值

  

 

原文链接:http://www.cnblogs.com/lin-zhifeng/p/13737857.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号