一次性工程设置部署操作 (本地化开发 Fabric 应用-- kafka 共识模式)

目录

(一)、Fabric项目工程配置。

(二)、证书及TX配置文件 生成。    // 可用脚本一次性生成: cfgtx.sh 

(三)、创建通道,加入通道。       //  cfgch.sh              

附录一 : yaml配置

1、configtx.yaml

2、core.yaml(修改地方节选)

3、crypto-config.yaml(无修改点,略)

4、orderer.yaml(General段修改处节选)

附录二、测试脚本

1、cfgtx.sh

2、cfgch.sh

3、chaincodetest.sh


(一)、Fabric项目工程配置。

环境变量设置。新开一个终端,进入工程根目录

cd /devel/fabric-1.0.0/build/_worksp-Fabric1-0-0  

export PATH=$PWD/bin:$PATH        # 将Fabric工具命令插入系统执行路径

export GOPATH=`pwd`:$GOPATH

编译安装fabric自带工具。

go build -o bin/configtxgen  src/github.com/hyperledger/fabric/common/configtx/tool/configtxgen/main.go 

go build -o bin/cryptogen  src/github.com/hyperledger/fabric/common/tools/cryptogen/main.go

go build -o bin/configtxlator  src/github.com/hyperledger/fabric/common/tools/configtxlator/main.go

yaml工程配置文件拷贝、修改。新建一配置目录并进入 : cd  cfg/fabric 

拷贝工程自带的配置样例 :sampleconfig/*.*,

拷贝fabric-sample-with-kafka-master/first-network/crypto-config.yaml、configtx.yaml,其中,后者替换旧的configtx.yaml。

yaml参数具体修改("-chenlx"注释处): orderer.yaml (LocalMSPID: OrdererMSP)、 configtx.yaml (&Org1 / &Org2的 MSPDir路径错) 、    core.yaml  (localMspId: Org1MSP)  

IDE 的工程调试文件设置: 

VSCode-1.36   launch.json        

//注,宏"FABRIC_CFG_PATH"、"ORDERER_FILELEDGER_LOCATION"、"CORE_PEER_FILESYSTEMPATH" 指向自定义路径。

主机别名填写。vi  /etc/hosts               

#fabric network

127.0.0.1       peer0.org2.example.com

127.0.0.1       peer0.org1.example.com

127.0.0.1       localhost

(二)、证书及TX配置文件 生成。    // 可用脚本一次性生成: cfgtx.sh 

# 生成MSP证书

chenlx@chenlx-ubu:/devel/fabric-1.0.0/build/_worksp-Fabric1-0-0/cfg/fabric$ cryptogen generate         //crypto-config.yaml  

org1.example.com

org2.example.com

# 生成排序服务的创始区块                            

chenlx@chenlx-ubu:/devel/fabric-1.0.0/build/_worksp-Fabric1-0-0/cfg/fabric$                              //configtx.yaml 

mkdir channel-artifacts  &&  configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block      

2019-08-09 15:30:33.179 CST [common/configtx/tool] main -> INFO 001 Loading configuration

2019-08-09 15:30:33.284 CST [common/configtx/tool] doOutputBlock -> INFO 002 Generating genesis block

2019-08-09 15:30:33.295 CST [common/configtx/tool] doOutputBlock -> INFO 003 Writing genesis block

 # 生成通道配置文件 ka1.tx

chenlx@chenlx-ubu:/devel/fabric-1.0.0/build/_worksp-Fabric1-0-0/cfg/fabric$ configtxgen -channelID ka1 -outputCreateChannelTx ka1.tx -profile TwoOrgsChannel                

2019-08-09 15:31:17.630 CST [common/configtx/tool] main -> INFO 001 Loading configuration

2019-08-09 15:31:17.654 CST [common/configtx/tool] doOutputChannelCreateTx -> INFO 002 Generating new channel configtx

2019-08-09 15:31:17.654 CST [common/configtx/tool] doOutputChannelCreateTx -> INFO 003 Writing new channel tx

 # 定义组织锚节点 

chenlx@chenlx-ubu:/devel/fabric-1.0.0/build/_worksp-Fabric1-0-0/cfg/fabric$ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID ka1 -asOrg Org1MSP            

2019-08-09 15:31:29.173 CST [common/configtx/tool] main -> INFO 001 Loading configuration

2019-08-09 15:31:29.191 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update

2019-08-09 15:31:29.191 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update

chenlx@chenlx-ubu:/devel/fabric-1.0.0/build/_worksp-Fabric1-0-0/cfg/fabric$ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID ka1 -asOrg Org2MSP

2019-08-09 15:31:35.428 CST [common/configtx/tool] main -> INFO 001 Loading configuration

2019-08-09 15:31:35.444 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update

2019-08-09 15:31:35.445 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update

(三)、创建通道,加入通道。      //  cfgch.sh              

启动 orderer(orderer0.example.com + orderer1.example.com ) 。分别创建目录/var/hyperledger/production、prod1/orderer。

启动 peer(org1.example.com  || org2.example.com)。    // peer启动时,设 "--peer-chaincodedev=true",使链码能以独立的进程运行,不依赖容器。

 

创建通道'ka1'  

//若创建成功,则生成通道创始区块 ./ka1.block , 并生成: /var/hyperledger/production、prod1/orderer/chains/ka1/blockfile_000000

chenlx@chenlx-ubu:/devel/fabric-1.0.0/build/_worksp-Fabric1-0-0/cfg/fabric$ 

rm -R msp                          // 删掉原来旧的msp目录

ln  -s  crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp  msp 

  //链接路径用org1,原因是下一行充当客户端命令的'peer'进程出自 launch.json : "org1.example.com",需参其"CORE_PEER_MSPCONFIGPATH"

peer channel create -o 127.0.0.1:7050  -t 30  -c  ka1 -f  ka1.tx               //加入'-t'设为30秒超时 (default 5秒),避免超时报错 

ll -h                         //  显示 " -rwxrwxrwx 1 root root  12K 8月  09  16:27 ka1.block* " 

//注,若去掉参数'-t',超时报错如下。

2019-08-09 15:32:57.318 CST [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized

2019-08-09 15:32:57.438 CST [channelCmd] InitCmdFactory -> INFO 002 Endorser and orderer connections initialized

...  ...  

Error: timeout waiting for channel creation       

 //注,创建通道出错,重新执行 "peer channel create"命令前,要删掉channel-artifacts目录、crypto-config目录、ka1.tx ,及

             /var/hyperledger/production目录, 重复操作第(二)、(三)步;

//若,创建通道成功,只是想重新生成 所有的文件、数据库,则删掉/var/hyperledger/production、prod1目录,重复操作第(三)步即可。

 

#加入通道 :        

//加入通道成功,则生成节点数据库文件: /var/hyperledger/production/ledgersData/chains/chains/${通道名}/blockfile_000000

peer channel join -b ka1.block                         // 一次性操作。 创建账本,LedgerID 即通道名。

 

附录一 : yaml配置

1、configtx.yaml

# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

---
  ################################################################################
  #
  #   Profile
  #
  #   - Different configuration profiles may be encoded here to be specified
  #   as parameters to the configtxgen tool
  #
  ################################################################################
  Profiles:
  
      TwoOrgsOrdererGenesis:
          Orderer:
              <<: *OrdererDefaults
              Organizations:
                  - *OrdererOrg
          Consortiums:
              SampleConsortium:
                  Organizations:
                      - *Org1
                      - *Org2
      TwoOrgsChannel:
          Consortium: SampleConsortium
          Application:
              <<: *ApplicationDefaults
              Organizations:
                  - *Org1
                  - *Org2

      SampleInsecureSolo:
        Orderer:
            <<: *OrdererDefaults
        Consortiums:
            SampleConsortium:
                Organizations:

    # SampleNoConsortium is very similar to SampleInsecureSolo, except it
    # does not define Consortiums.
      SampleNoConsortium:
        Orderer:
            <<: *OrdererDefaults

    # SampleInsecureKafka defines a configuration that differs from the
    # SampleInsecureSolo one only in that is uses the Kafka-based orderer.
      SampleInsecureKafka:
        Orderer:
            <<: *OrdererDefaults
            OrdererType: kafka
        Consortiums:
            SampleConsortium:
                Organizations:


  ################################################################################
  #
  #   Section: Organizations
  #
  #   - This section defines the different organizational identities which will
  #   be referenced later in the configuration.
  #
  ################################################################################
  Organizations:
  
      # SampleOrg defines an MSP using the sampleconfig.  It should never be used
      # in production but may be used as a template for other definitions
      - &OrdererOrg
          # DefaultOrg defines the organization which is used in the sampleconfig
          # of the fabric.git development environment
          Name: OrdererOrg
  
          # ID to load the MSP definition as
          ID: OrdererMSP
  
          # MSPDir is the filesystem path which contains the MSP configuration
# MSPDir: crypto-config/ordererOrganizations/example.com/msp
          MSPDir: crypto-config/ordererOrganizations/example.com/users/[email protected]/msp
  
      - &Org1
          # DefaultOrg defines the organization which is used in the sampleconfig
          # of the fabric.git development environment
          Name: Org1MSP
  
          # ID to load the MSP definition as
          ID: Org1MSP
  
# MSPDir: crypto-config/peerOrganizations/org1.example.com/msp    #-chenlx
          MSPDir: crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
  
          AnchorPeers:
              # AnchorPeers defines the location of peers which can be used
              # for cross org gossip communication.  Note, this value is only
              # encoded in the genesis block in the Application section context
              - Host: peer0.org1.example.com      
                Port: 7051
  
      - &Org2
          # DefaultOrg defines the organization which is used in the sampleconfig
          # of the fabric.git development environment
          Name: Org2MSP
  
          # ID to load the MSP definition as
          ID: Org2MSP
  
# MSPDir: crypto-config/peerOrganizations/org2.example.com/msp
          MSPDir: crypto-config/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
  
          AnchorPeers:
              # AnchorPeers defines the location of peers which can be used
              # for cross org gossip communication.  Note, this value is only
              # encoded in the genesis block in the Application section context
              - Host: peer0.org2.example.com    
                # 7051     #-chenlx
                Port: 17051    
  
  ################################################################################
  #
  #   SECTION: Orderer
  #
  #   - This section defines the values to encode into a config transaction or
  #   genesis block for orderer related parameters
  #
  ################################################################################
  Orderer: &OrdererDefaults
  
      # Orderer Type: The orderer implementation to start
      # Available types are "solo" and "kafka"
      OrdererType: kafka
  
      Addresses:
          #- orderer0.example.com:7050
          #- orderer1.example.com:7050
          - 127.0.0.1:17050
          - 127.0.0.1:7050
  
      # Batch Timeout: The amount of time to wait before creating a batch
      BatchTimeout: 1s
  
      # Batch Size: Controls the number of messages batched into a block
      BatchSize:
  
          # Max Message Count: The maximum number of messages to permit in a batch
          MaxMessageCount: 10
  
          # Absolute Max Bytes: The absolute maximum number of bytes allowed for
          # the serialized messages in a batch.
          AbsoluteMaxBytes: 98 MB
  
          # Preferred Max Bytes: The preferred maximum number of bytes allowed for
          # the serialized messages in a batch. A message larger than the preferred
          # max bytes will result in a batch larger than preferred max bytes.
          PreferredMaxBytes: 512 KB
  
      Kafka:
          # Brokers: A list of Kafka brokers to which the orderer connects
          # NOTE: Use IP:port notation
          Brokers:
             # - kafka0.example.com:9092
             # - kafka1.example.com:9092
             # - kafka2.example.com:9092
             # - kafka3.example.com:9092
             - 127.0.0.1:9092
  
      # Organizations is the list of orgs which are defined as participants on
      # the orderer side of the network
      Organizations:
  
  ################################################################################
  #
  #   SECTION: Application
  #
  #   - This section defines the values to encode into a config transaction or
  #   genesis block for application related parameters
  #
  ################################################################################
  Application: &ApplicationDefaults
  
      # Organizations is the list of orgs which are defined as participants on
      # the application side of the network
      Organizations:

2、core.yaml(修改地方节选)

修改地方如下。

 # Path on the file system where peer will store data (eg ledger). This
    # location must be access control protected to prevent unintended
    # modification that might corrupt the peer operations.
    fileSystemPath: /var/hyperledger/production

    # BCCSP (Blockchain crypto provider): Select which crypto implementation or
    # library to use
    BCCSP:
        Default: SW
        SW:
            # TODO: The default Hash and Security level needs refactoring to be
            # fully configurable. Changing these defaults requires coordination
            # SHA2 is hardcoded in several places, not only BCCSP
            Hash: SHA2
            Security: 256
            # Location of Key Store
            FileKeyStore:
                # If "", defaults to 'mspConfigPath'/keystore
                # TODO: Ensure this is read with fabric/core/config.GetPath() once ready
                KeyStore:

    # Path on the file system where peer will find MSP local configurations
    mspConfigPath: msp

    # Identifier of the local MSP
    # ----!!!!IMPORTANT!!!-!!!IMPORTANT!!!-!!!IMPORTANT!!!!----
    # Deployers need to change the value of the localMspId string.
    # In particular, the name of the local MSP ID of a peer needs
    # to match the name of one of the MSPs in each of the channel
    # that this peer is a member of. Otherwise this peer's messages
    # will not be identified as valid by other nodes.
#localMspId: DEFAULT    # -chenlx
    localMspId: Org1MSP

    # Used with Go profiling tools only in none production environment. In
    # production, it should be disabled (eg enabled: false)
    profile:
        enabled:     false
        listenAddress: 0.0.0.0:6060

3、crypto-config.yaml(无修改点,略)

4、orderer.yaml(General段修改处节选)

 # Genesis file: The file containing the genesis block to use when
    # initializing the orderer system channel and GenesisMethod is set to
    # "file". Ignored if GenesisMethod is set to "provisional".
    GenesisFile: genesisblock

    # LocalMSPDir is where to find the private crypto material needed by the
    # orderer. It is set relative here as a default for dev environments but
    # should be changed to the real location in production.
    LocalMSPDir: msp

    # LocalMSPID is the identity to register the local MSP material with the MSP
    # manager. IMPORTANT: The local MSP ID of an orderer needs to match the MSP
    # ID of one of the organizations defined in the orderer system channel's
    # /Channel/Orderer configuration. The sample organization defined in the
    # sample configuration provided has an MSP ID of "DEFAULT".
# LocalMSPID: DEFAULT        #-chenlx
    LocalMSPID: OrdererMSP

    # Enable an HTTP service for Go "pprof" profiling as documented at:
    # https://golang.org/pkg/net/http/pprof
    Profile:
        Enabled: false
        Address: 0.0.0.0:6060

 

附录二、测试脚本

chenlx@chenlx-ubu:/devel/fabric-1.0.0/build/_worksp-Fabric1-0-0/cfg/fabric$    cd ../../

chenlx@chenlx-ubu:/devel/fabric-1.0.0/build/_worksp-Fabric1-0-0$ 

export PATH=$PWD/bin:$PATH

export GOPATH=`pwd`:$GOPATH

cd cfg/fabric/

chenlx@chenlx-ubu:/devel/fabric-1.0.0/build/_worksp-Fabric1-0-0/cfg/fabric$ 

./cfgtx.sh ka1                       // 创建通道ka1 

./cfgch.sh ka1      // 启动orderer 、peer服务先 

./cctest.sh ka1 i1 i2             //安装 -> 实例化 -->调用 --->查询链码。注,要在调试模式下启动目标链码程序先。

./cctest.sh ka1 0 i2    //链码安装上,但没有初始化成功:去掉"i1",保留"i2",第2个参数填了个0占位

./cctest.sh ka1      //链码在ka1通道内已实例化,则只调用、查询链码

1、cfgtx.sh

CHANNEL_NAME="$1"
#OLD_CHAN_NAME="$2"

rm -R ../../var/prodka  ../../var/prodkaf
rm -R crypto-config channel-artifacts
#rm $OLD_CHAN_NAME.block $OLD_CHAN_NAME.tx 

cryptogen generate
mkdir channel-artifacts  &&  configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
configtxgen -channelID $CHANNEL_NAME -outputCreateChannelTx tmp/$CHANNEL_NAME.tx -profile TwoOrgsChannel

configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP

2、cfgch.sh

CHANNEL_NAME="$1"
#OLD_CHAN_NAME="$2"

rm -R ../../var/prodka  ../../var/prodkaf
rm -R crypto-config channel-artifacts
#rm $OLD_CHAN_NAME.block $OLD_CHAN_NAME.tx 

cryptogen generate
mkdir channel-artifacts  &&  configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
configtxgen -channelID $CHANNEL_NAME -outputCreateChannelTx tmp/$CHANNEL_NAME.tx -profile TwoOrgsChannel

configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP

3、chaincodetest.sh

CHANNEL_NAME="$1"

if [ "$2" == "i1" ]; then
peer chaincode install -n mycc -v 0  -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02  -o 127.0.0.1:7050
fi

if [ "$3" == "i2" ]; then
peer chaincode instantiate -n mycc -v 0 -c '{"Args":["init","a","100","b","200"]}' -C $CHANNEL_NAME  -o 127.0.0.1:7050
sleep 2
fi

peer chaincode  invoke  -n mycc -c '{"Args":["invoke","a","b","3"]}' -C $CHANNEL_NAME -o 127.0.0.1:7050
sleep 1
peer chaincode query -n mycc -c '{"Args":["query","a"]}' -C $CHANNEL_NAME  -o 127.0.0.1:7050

你可能感兴趣的:(软件产品开发)