zoukankan      html  css  js  c++  java
  • HyperLedger Fabric 多机部署(一)

    本文参考:http://www.lijiaocn.com/%E9%A1%B9%E7%9B%AE/2018/04/26/hyperledger-fabric-deploy.html  学习。

    1、准备工作:

    准备了两台阿里云ecs  A,B两台机器。:centOs7.2系统。

    规划: 打算部署一个order节点,两个peer节点。peer分属两个不同的组织。

    A机IP orderer.example.com 7050
    A机IP peer0.org1.example.com
    B机IP peer0.org2.example.com

    1)首先将AB两台机器的hosts文件添加上面的内容。

    2)两台机器安装docker:

    yum install -y docker 
    systemctl start docker (启动docker)

    3) A机器安装go 

    4)启动peer 需要准备镜像,AB机器上分别执行 

    docker pull hyperledger/fabric-javaenv:x86_64-1.1.0
    docker pull hyperledger/fabric-ccenv:x86_64-1.1.0
    docker pull hyperledger/fabric-baseos:x86_64-0.4.6

    2:下载相关源码

    1.创建文件夹  fabric-deploy 存放源码。

    2.cd  fabric-deploy   

    wget https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/linux-amd64-1.1.0/hyperledger-fabric-linux-amd64-1.1.0.tar.gz
    解压 tar -xvf hyperledger-fabric-linux-amd64-1.1.0.tar.gz
    解压完有 bin 和config两个文件夹
    $ ls bin/
    configtxgen   configtxlator   cryptogen   get-byfn.sh   get-docker-images.sh orderer   peer
    
    $ ls config/
    configtx.yaml  core.yaml  orderer.yaml


    3.准备证书

       证书的作用是确保每个节点,成员都是受认可的。证书的生成方式有两种:一种用cryptogen命令生成,一种是通过fabric-ca服务生成。因为源码自带的cyptogen,且这种方法比较简单,这里介绍cyptogen方式生成证书。

       1.首先创建创建一个证书的配置文件  crypto-config.yaml,这里配置了两个组织,org1的Count是2,表示两个peer:

    OrdererOrgs:
      - Name: Orderer
        Domain: example.com
        Specs:
          - Hostname: orderer
    PeerOrgs:
      - Name: Org1
        Domain: org1.example.com     组织名称
        Template:
          Count: 1                  peer节点数量
        Users:
          Count: 1                  用户数量
      - Name: Org2
        Domain: org2.example.com
        Template:
          Count: 1
        Users:
          Count: 1

    执行cyptogen 生成对应数量的证书:

    ./bin/cryptogen generate --config=crypto-config.yaml --output ./certs
    cert目录项生成两个证书
    $ ls ./certs/
    ordererOrganizations  peerOrganizations
    

      证书文件说明:

    这里目录中的内容是用于orderer.example.com的,里面有两个子目录tlsmsp

    $ tree certs/ordererOrganizations/example.com/orderers/orderer.example.com/
    certs/ordererOrganizations/example.com/orderers/orderer.example.com/
    |-- msp
    |   |-- admincerts
    |   |   -- Admin@example.com-cert.pem
    |   |-- cacerts
    |   |   -- ca.example.com-cert.pem
    |   |-- keystore
    |   |   -- 16da15d400d4ca4b53d369b6d6e50a084d4354998c3b4d7a0934635d3907f90f_sk
    |   |-- signcerts
    |   |   -- orderer.example.com-cert.pem
    |   -- tlscacerts
    |       -- tlsca.example.com-cert.pem
    -- tls
        |-- ca.crt
        |-- server.crt
        -- server.key
    

    tls目录中的内容很好理解,它是order对外服务时使用的私钥(server.key)和证书(server.crt),ca.crt是签注这个证书的CA,需要提供给发起请求的一端。

    msp中有五个目录,对区块链进行操作时需要使用这些文件。

    msp/admincerts中存放的是用户证书,使用该证书的用户对orderer.example.com具有管理权限。

    msp/cacerts是签署msp/signcerts中用户证书的ca,可以用来校验用户的证书:

    $ cd ./certs/ordererOrganizations/example.com/orderers/orderer.example.com/msp/
    $ openssl verify -CAfile ./cacerts/ca.example.com-cert.pem  admincerts/Admin@example.com-cert.pem
    admincerts/Admin@example.com-cert.pem: OK
    

    msp/keystore是orderer.example.com操作区块,进行签署时使用的的私钥。

    msp/signcerts是orderer.example.com提供其它组件用来核实它的签署的公钥。

    msp/tlscacerts文件与tls/ca.crt相同。

    这里需要特别提到的是msp/admincerts,在使用fabric时,你可能会发现有些操作需要admin权限,例如在某个peer上安装合约。

    那么管理员是如何认定的?就是看当前用户的证书是不是在msp/admincerts目录中。

    这个目录中的内容目前是(版本1.1.x)启动时加载的,因此如果在里面添加或删除文件后,需要重启使用到它的组件。

    ordererOrganizations/example.com/还有其它几个目录:

    ca  msp  orderers  tlsca  users
    

    orderers中存放就签名分析的每个orderer组件的证书文件。

    users中存放的用户的证书文件,与orderer.example.com中内容基本相同,不过tls目录中文件名变成了client.X

    $ tree ordererOrganizations/example.com/users
    ordererOrganizations/example.com/users
    -- Admin@example.com
        |-- msp
        |   |-- admincerts
        |   |   -- Admin@example.com-cert.pem
        |   |-- cacerts
        |   |   -- ca.example.com-cert.pem
        |   |-- keystore
        |   |   -- 1ac3b40c9ddda7e7a0f724b18faa0ce6fdf3f9e9ff5eac59e1e3f9739499ac2d_sk
        |   |-- signcerts
        |   |   -- Admin@example.com-cert.pem
        |   -- tlscacerts
        |       -- tlsca.example.com-cert.pem
        -- tls
            |-- ca.crt
            |-- client.crt
            -- client.key
    

    certs/peerOrganizations中的内容与certs/ordererOrganizations中也基本相同,只不过它里面存放的是peer要使用的证书文件。

    certs目录中的文件留着备用。

    2.部署文件准备

    每一个peer节点都有对应的文件,为了方便,这里先在fabric-deploy下面创建一个order.example.com 的文件夹

    orderer.example.com


    mkdir orderer.example.com
    然后把所需要的文件放进去,后面在放到对应的服务器中启动节点。另外两个peer节点做同样的操作。
    1、先将order需要的证书放进去:
    cp bin/orderer orderer.example.com/
    2.在创建order的配置文件:orderer.example.com/orderer.yaml:
    General:
        LedgerType: file
        ListenAddress: 0.0.0.0
        ListenPort: 7050           order对应的端口号
        TLS:
            Enabled: true
            PrivateKey: ./tls/server.key
            Certificate: ./tls/server.crt
            RootCAs:
              - ./tls/ca.crt
    #        ClientAuthEnabled: false
    #        ClientRootCAs:
        LogLevel: debug
        LogFormat: '%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}'
    #    GenesisMethod: provisional
        GenesisMethod: file
        GenesisProfile: SampleInsecureSolo
        GenesisFile: ./genesisblock
        LocalMSPDir: ./msp
        LocalMSPID: OrdererMSP
        Profile:
            Enabled: false
            Address: 0.0.0.0:6060
        BCCSP:
            Default: SW
            SW:
                Hash: SHA2
                Security: 256
                FileKeyStore:
                    KeyStore:
    FileLedger:
        Location:  /opt/app/fabric/orderer/data            order数据文件安装的位置
        Prefix: hyperledger-fabric-ordererledger
    RAMLedger:
        HistorySize: 1000
    Kafka:
        Retry:
            ShortInterval: 5s
            ShortTotal: 10m
            LongInterval: 5m
            LongTotal: 12h
            NetworkTimeouts:
                DialTimeout: 10s
                ReadTimeout: 10s
                WriteTimeout: 10s
            Metadata:
                RetryBackoff: 250ms
                RetryMax: 3
            Producer:
                RetryBackoff: 100ms
                RetryMax: 3
            Consumer:
                RetryBackoff: 2s
        Verbose: false
        TLS:
          Enabled: false
          PrivateKey:
            #File: path/to/PrivateKey
          Certificate:
            #File: path/to/Certificate
          RootCAs:
            #File: path/to/RootCAs
        Version:

    注意,orderer将被部署在目标机器的/opt/app/fabric/orderer目录中,如果要部署在其它目录中,需要修改配置文件中路径。

    这里需要用到一个data目录,存放orderer的数据:

    mkdir orderer.example.com/data

    peer0.org1.example.com

    peer 配置跟order相似:
    mkdir peer0.org1.example.com
    1.复制证书文件:

    先将bin/peer以及证书复制到peer0.org1.example.com目录中。

    cp bin/peer peer0.org1.example.com/
    cp -rf certs/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/* peer0.org1.example.com/
    2.添加配置文件 core.yaml
    logging:
        peer:       debug
        cauthdsl:   warning
        gossip:     warning
        ledger:     info
        msp:        warning
        policies:   warning
        grpc:       error
        format: '%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}'
    peer:
        id: peer0.org1.example.com
        networkId: dev
        listenAddress: 0.0.0.0:7051
        address: 0.0.0.0:7051
        addressAutoDetect: false
        gomaxprocs: -1
        gossip:
            bootstrap: 127.0.0.1:7051
            bootstrap: peer0.org1.example.com:7051
            useLeaderElection: true
            orgLeader: false
            endpoint:
            maxBlockCountToStore: 100
            maxPropagationBurstLatency: 10ms
            maxPropagationBurstSize: 10
            propagateIterations: 1
            propagatePeerNum: 3
            pullInterval: 4s
            pullPeerNum: 3
            requestStateInfoInterval: 4s
            publishStateInfoInterval: 4s
            stateInfoRetentionInterval:
            publishCertPeriod: 10s
            skipBlockVerification: false
            dialTimeout: 3s
            connTimeout: 2s
            recvBuffSize: 20
            sendBuffSize: 200
            digestWaitTime: 1s
            requestWaitTime: 1s
            responseWaitTime: 2s
            aliveTimeInterval: 5s
            aliveExpirationTimeout: 25s
            reconnectInterval: 25s
            externalEndpoint: peer0.org1.example.com:7051
            election:
                startupGracePeriod: 15s
                membershipSampleInterval: 1s
                leaderAliveThreshold: 10s
                leaderElectionDuration: 5s
        events:
            address: 0.0.0.0:7053
            buffersize: 100
            timeout: 10ms
        tls:
            enabled: true
            cert:
                file: ./tls/server.crt
            key:
                file: ./tls/server.key
            rootcert:
                file: ./tls/ca.crt
            serverhostoverride:
        fileSystemPath: /opt/app/fabric/peer/data
        BCCSP:
            Default: SW
            SW:
                Hash: SHA2
                Security: 256
                FileKeyStore:
                    KeyStore:
        mspConfigPath: msp
        localMspId: Org1MSP
        profile:
            enabled:    true
            listenAddress: 0.0.0.0:6060
    vm:
        endpoint: unix:///var/run/docker.sock
        docker:
            tls:
                enabled: false
                ca:
                    file: docker/ca.crt
                cert:
                    file: docker/tls.crt
                key:
                    file: docker/tls.key
            attachStdout: false
            hostConfig:
                NetworkMode: host
                Dns:
                   # - 192.168.0.1
                LogConfig:
                    Type: json-file
                    Config:
                        max-size: "50m"
                        max-file: "5"
                Memory: 2147483648
    chaincode:
        peerAddress:
        id:
            path:
            name:
        builder: $(DOCKER_NS)/fabric-ccenv:$(ARCH)-$(PROJECT_VERSION)
        golang:
            runtime: $(BASE_DOCKER_NS)/fabric-baseos:$(ARCH)-$(BASE_VERSION)
        car:
            runtime: $(BASE_DOCKER_NS)/fabric-baseos:$(ARCH)-$(BASE_VERSION)
        java:
            Dockerfile:  |
                from $(DOCKER_NS)/fabric-javaenv:$(ARCH)-$(PROJECT_VERSION)
        startuptimeout: 300s
        executetimeout: 30s
        mode: net
        keepalive: 0
        system:
            cscc: enable
            lscc: enable
            escc: enable
            vscc: enable
            qscc: enable
        logging:
          level:  info
          shim:   warning
          format: '%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}'
    ledger:
      blockchain:
      state:
        stateDatabase: goleveldb
        couchDBConfig:
           couchDBAddress: 127.0.0.1:5984
           username:
           password:
           maxRetries: 3
           maxRetriesOnStartup: 10
           requestTimeout: 35s
           queryLimit: 10000
      history:
        enableHistoryDatabase: true

    注意,peer将被部署在目标机器的/opt/apt/fabric/peer目录中,如果要部署在其它目录中,需要修改配置文件中路径。

    这里需要用到一个data目录,存放peer的数据:

    mkdir peer0.org1.example.com/data

    peer0.org2.example.com

    跟peer0.org1.example.com 相同。、

    4.开始部署

    拷贝 orderer.example.com/到A机的/opt/app/fabric/orderer/

    拷贝 peer0.org1.example.com/到A机的/opt/app/fabric/peer/

    拷贝 peer0.org2.example.com/到B机的/opt/app/fabric/peer/

    到这里 启动需要的文件基本准备完毕。查看 orderer.yaml 可以看到: 

    GenesisMethod: file
    GenesisFile: ./genesisblock
    GenesisProfile: SampleInsecureSolo 这几行代码。前两行配置了创世区块的获取方式,第一个区块的获取方式有多种,这里采用最简单的一种做法,用configtxgen生成。
    创建一个 生成创世区块的需要的配置文件configtx.yaml:
    Profiles:
        TwoOrgsOrdererGenesis:
            Orderer:
                <<: *OrdererDefaults
                Organizations:
                    - *OrdererOrg
            Consortiums:
                SampleConsortium:
                    Organizations:
                        - *Org1
                        - *Org2
        TwoOrgsChannel:
            Consortium: SampleConsortium
            Application:
                <<: *ApplicationDefaults
                Organizations:
                    - *Org1
                    - *Org2
    Organizations:
        - &OrdererOrg
            Name: OrdererOrg
            ID: OrdererMSP
            MSPDir: ./certs/ordererOrganizations/example.com/msp
        - &Org1
            Name: Org1MSP
            ID: Org1MSP
            MSPDir: ./certs/peerOrganizations/org1.example.com/msp
            AnchorPeers:
                - Host: peer0.org1.example.com
                  Port: 7051
        - &Org2
            Name: Org2MSP
            ID: Org2MSP
            MSPDir: ./certs/peerOrganizations/org2.example.com/msp
            AnchorPeers:
                - Host: peer0.org2.example.com
                  Port: 7051
    Orderer: &OrdererDefaults
        OrdererType: solo
        Addresses:
            - orderer.example.com:7050
        BatchTimeout: 2s
        BatchSize:
            MaxMessageCount: 10
            AbsoluteMaxBytes: 99 MB
            PreferredMaxBytes: 512 KB
        Kafka:
            Brokers:
                - 127.0.0.1:9092
        Organizations:
    Application: &ApplicationDefaults
        Organizations:

    使用:

    ./bin/configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./genesisblock 生成创世区块。  将生成的genesisblock文件放到 A机器order节点下。




     
     
     
     
  • 相关阅读:
    codevs 3160 最长公共子串(SAM)
    noi2018d2t1
    后缀自动机
    [Scoi2014]方伯伯的玉米田
    【NOI2017】泳池
    【BZOJ2560】串珠子
    POJ 1737 统计有n个顶点的连通图有多少个 (带标号)
    【BZOJ3590】[Snoi2013]Quare 状压DP
    【BZOJ1095】【ZJOI2007】捉迷藏 [动态点分治]
    BZOJ1316: 树上的询问
  • 原文地址:https://www.cnblogs.com/gzhlt/p/9599616.html
Copyright © 2011-2022 走看看