区块链技术开发教程

区块链技术指南,区块链技术的工作原理讲解,区块链技术入门教程

超级账本Fabric教程(四):使用docker编译fabric源码(下)

2018-11-20

区块链应用技术开发,超级账本,本文介绍如何在docker镜像中编译fabric源码,方便修改代码后快速测试。

点击区块链技术培训课程获取更多区块链技术学习资料。


前文链接:http://www.liankuai.tech/technology/185.html 

                  http://www.liankuai.tech/technology/187.html

           


7.3 启动orderer和peer节点

编辑一个docker-compose的配置文件

#
# Copyright IBM Corp All Rights Reserved
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2'
 
networks:
  basic:
 
services:
  orderer.example.com:
    container_name: orderer.example.com
    image: yeasy/hyperledger-fabric:1.0.0
    environment:
      - ORDERER_GENERAL_LOGLEVEL=debug
      - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
      - ORDERER_GENERAL_GENESISMETHOD=file
      - ORDERER_GENERAL_GENESISFILE=/etc/hyperledger/configtx/genesis.block
      - ORDERER_GENERAL_LOCALMSPID=OrdererMSP
      - ORDERER_GENERAL_LOCALMSPDIR=/etc/hyperledger/msp/orderer/msp
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/orderer
    command: orderer
    ports:
      - 7050:7050
    volumes:
        - ./config/:/etc/hyperledger/configtx
        - ./crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/:/etc/hyperledger/msp/orderer
        - ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/:/etc/hyperledger/msp/peerOrg1
    networks:
      - basic
 
  peer0.org1.example.com:
    container_name: peer0.org1.example.com
    image: yeasy/hyperledger-fabric:1.0.0
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_PEER_ID=peer0.org1.example.com
      - CORE_LOGGING_PEER=debug
      - CORE_CHAINCODE_LOGGING_LEVEL=DEBUG
      - CORE_PEER_LOCALMSPID=Org1MSP
      - CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/peer/
      - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
      # # the following setting starts chaincode containers on the same
      # # bridge network as the peers
      # # https://docs.docker.com/compose/networking/
      - CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=${COMPOSE_PROJECT_NAME}_basic
      #- CORE_LEDGER_STATE_STATEDATABASE=CouchDB
      #- CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984
      # The CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME and CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD
      # provide the credentials for ledger to connect to CouchDB.  The username and password must
      # match the username and password set for the associated CouchDB.
      - CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
      - CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric
    command: peer node start
    # command: peer node start --peer-chaincodedev=true
    ports:
      - 7051:7051
      - 7053:7053
    volumes:
        - /var/run/:/host/var/run/
        - ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp:/etc/hyperledger/msp/peer
        - ./crypto-config/peerOrganizations/org1.example.com/users:/etc/hyperledger/msp/users
        - ./config:/etc/hyperledger/configtx
        - ~/go/bin:/go/bin
    depends_on:
      - orderer.example.com
    networks:
      - basic


启动前关闭网络,防止之前启动过

docker-compose  -f docker-compose.yaml down


启动orderer和peer节点

docker-compose -f docker-compose.yaml up -d peer0.org1.example.com orderer.example.com


查看容器进程

docker ps
CONTAINER ID      IMAGE        COMMAND          CREATED     STATUS      PORTS     NAMES
6f9d4ad6af8b      yeasy/hyperledger-fabric:1.0.0   "peer node start"        56 seconds ago      Up 53 seconds      0.0.0.0:7051->7051/tcp, 7050/tcp, 7054/tcp, 0.0.0.0:7053->7053/tcp   peer0.org1.example.com
0965146445ca       yeasy/hyperledger-fabric:1.0.0   "orderer"          58 seconds ago      Up 56 seconds       7051/tcp, 0.0.0.0:7050->7050/tcp, 7054/tcp                           orderer.example.com
99edaa3a60c5        yeasy/hyperledger-fabric:1.0.0   "/bin/bash -c 'sleep…"   2 hours ago         Up 2 hours          7050-7051/tcp, 7054/tcp                                              compile


创建通道

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.example.com/msp" peer0.org1.example.com peer channel create -o orderer.example.com:7050 -c mychannel -f /etc/hyperledger/configtx/channel.tx
----------Welcome to my-basic-network----------
2018-11-14 08:13:05.098 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-11-14 08:13:05.098 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-11-14 08:13:05.107 UTC [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized
2018-11-14 08:13:05.114 UTC [msp] GetLocalMSP -> DEBU 004 Returning existing local MSP
2018-11-14 08:13:05.116 UTC [msp] GetDefaultSigningIdentity -> DEBU 005 Obtaining default signing identity
2018-11-14 08:13:05.116 UTC [msp] GetLocalMSP -> DEBU 006 Returning existing local MSP
2018-11-14 08:13:05.117 UTC [msp] GetDefaultSigningIdentity -> DEBU 007 Obtaining default signing identity
2018-11-14 08:13:05.117 UTC [msp/identity] Sign -> DEBU 008 Sign: plaintext: 0A8C060A074F7267314D53501280062D...53616D706C65436F6E736F727469756D 
2018-11-14 08:13:05.118 UTC [msp/identity] Sign -> DEBU 009 Sign: digest: 5D1863244822DCB9603797144AD9F1BAA86A12AC3A730C069F510039530F35EC 
2018-11-14 08:13:05.119 UTC [msp] GetLocalMSP -> DEBU 00a Returning existing local MSP
2018-11-14 08:13:05.119 UTC [msp] GetDefaultSigningIdentity -> DEBU 00b Obtaining default signing identity
2018-11-14 08:13:05.119 UTC [msp] GetLocalMSP -> DEBU 00c Returning existing local MSP
2018-11-14 08:13:05.120 UTC [msp] GetDefaultSigningIdentity -> DEBU 00d Obtaining default signing identity
2018-11-14 08:13:05.120 UTC [msp/identity] Sign -> DEBU 00e Sign: plaintext: 0AC3060A1508021A060891B2AFDF0522...B9F3BEFA93B397ABE731E5922EB2AD40 
2018-11-14 08:13:05.120 UTC [msp/identity] Sign -> DEBU 00f Sign: digest: D6292E9E7628D66F3F2F227906C57901F400E361F0BFF889D6AF281EB8464E05 
2018-11-14 08:13:05.167 UTC [msp] GetLocalMSP -> DEBU 010 Returning existing local MSP
2018-11-14 08:13:05.167 UTC [msp] GetDefaultSigningIdentity -> DEBU 011 Obtaining default signing identity
2018-11-14 08:13:05.167 UTC [msp] GetLocalMSP -> DEBU 012 Returning existing local MSP
2018-11-14 08:13:05.167 UTC [msp] GetDefaultSigningIdentity -> DEBU 013 Obtaining default signing identity
2018-11-14 08:13:05.167 UTC [msp/identity] Sign -> DEBU 014 Sign: plaintext: 0AC3060A1508021A060891B2AFDF0522...63451B96B86D12080A021A0012021A00 
2018-11-14 08:13:05.167 UTC [msp/identity] Sign -> DEBU 015 Sign: digest: 660D6E5A80DA1D1C46FB93E63B697A4362E0A3201D08484EDB7EAB15798596E1 
2018-11-14 08:13:05.168 UTC [channelCmd] readBlock -> DEBU 016 Got status:*orderer.DeliverResponse_Status 
2018-11-14 08:13:05.168 UTC [msp] GetLocalMSP -> DEBU 017 Returning existing local MSP
2018-11-14 08:13:05.168 UTC [msp] GetDefaultSigningIdentity -> DEBU 018 Obtaining default signing identity
2018-11-14 08:13:05.170 UTC [channelCmd] InitCmdFactory -> INFO 019 Endorser and orderer connections initialized
2018-11-14 08:13:05.371 UTC [msp] GetLocalMSP -> DEBU 01a Returning existing local MSP
2018-11-14 08:13:05.371 UTC [msp] GetDefaultSigningIdentity -> DEBU 01b Obtaining default signing identity
2018-11-14 08:13:05.372 UTC [msp] GetLocalMSP -> DEBU 01c Returning existing local MSP
2018-11-14 08:13:05.372 UTC [msp] GetDefaultSigningIdentity -> DEBU 01d Obtaining default signing identity
2018-11-14 08:13:05.372 UTC [msp/identity] Sign -> DEBU 01e Sign: plaintext: 0AC3060A1508021A060891B2AFDF0522...763BCEC0A59A12080A021A0012021A00 
2018-11-14 08:13:05.372 UTC [msp/identity] Sign -> DEBU 01f Sign: digest: FEB035372AC14E18601FE0D76D77663902FE971F5E4C69C1DD8EC9C7823F3A6D 
2018-11-14 08:13:05.390 UTC [channelCmd] readBlock -> DEBU 020 Received block:0 
2018-11-14 08:13:05.397 UTC [main] main -> INFO 021 Exiting.....

 

将peer将入通道

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.example.com/msp" peer0.org1.example.com peer channel join -b mychannel.block
----------Welcome to my-basic-network----------
2018-11-14 08:14:04.808 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-11-14 08:14:04.808 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-11-14 08:14:04.810 UTC [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized
2018-11-14 08:14:04.811 UTC [msp/identity] Sign -> DEBU 004 Sign: plaintext: 0A8A070A5C08011A0C08CCB2AFDF0510...D06A96FA3B5F1A080A000A000A000A00
2018-11-14 08:14:04.811 UTC [msp/identity] Sign -> DEBU 005 Sign: digest: 1D06F595E5B39045672FA2476B6ECA972FE674864ECC13A14F8201C416594444
2018-11-14 08:14:04.862 UTC [channelCmd] executeJoin -> INFO 006 Peer joined the channel!
2018-11-14 08:14:04.862 UTC [main] main -> INFO 007 Exiting.....

 

7.4 总结

我们可以看到上面创建通道和将peer加入通道的输出中, 我们在源码里面的改动生效了, 通过这样的方式可以改动fabric源码后不用频繁编译超级账本镜像文件了。

如果我们要改动源码,流程是首先在主机上改动代码,然后到compile容器中编译代码, 然后在其他容器中执行编译后的程序,由于这几个容器都能看到主机上~/go/bin目录中的内容,只要compile容器中代码编译完成后,其他容器中立马就能使用它

 

八、 操作过程中的问题解决

8.1 没有权限执行dock命令解决办法

原因:

ocker进程使用Unix Socket而不是TCP端口。而默认情况下,Unix socket属于root用户,需要root权限才能访问

解决办法:

#添加docker用户组
sudo groupadd docker  
#将登陆用户加入到docker用户组中
sudo gpasswd -a $USER docker   
 #更新用户组
newgrp docker 
#测试docker命令是否可以使用sudo正常使  
 docker ps



-END-


001周末班+0628二维码.jpg

区块链应用案例手箭头.png

点击查看更多区块链应用成功案例 ,区块链技术开发教程 。

003学习路径+公众号.jpg








注:本文转载自链块学院,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除。

7*24客服电话

150-1118-1611

扫码添加助教微信

二维码
菜单 在线咨询 回到顶部
关闭