fabric学习(1)——不使用byfn搭建fabric网络(1)

byfn一键启动脚本确实很方便,但是不利于理解fabric网络。下面演示不使用docker的情况下,在3台服务器上搭建一个fabric网络。

1,fabric编译和安装
mkdir -p $GOPATH/src/github.com/hyperledger
cd $GOPATH/src/github.com/hyperledger
git clone https://github.com/hyperledger/fabric

切换到1.4.6版本,编译

make release
make docker

把可执行文件拷贝到系统bin文件夹

cp $GOPATH/src/github.com/hyperledger/fabric/release/linux-amd64/bin/* /usr/local/bin

修改权限

sudo chmod -R 775  /usr/local/bin/configtxgen
sudo chmod -R 775  /usr/local/bin/configtxlator
sudo chmod -R 775  /usr/local/bin/cryptogen
sudo chmod -R 775  /usr/local/bin/peer
sudo chmod -R 775  /usr/local/bin/orderer

使用version命令检查是否成功

peer version

如果编译有困难,这些可执行文件也是可以直接下载的。

2,启动第一个org和peer
第一步,配置网络规则

首先新建一个目录用来存放配置文件

mkdir -p /myfabric/hyperledger/fabricconfig
cd /myfabric/hyperledger/fabricconfig

新增配置文件crypto-config.yaml,内容如下:

OrdererOrgs:
  - Name: Orderer
    Domain: caohuan.com
    Specs:
      - Hostname: orderer
PeerOrgs:
  - Name: Org1
    Domain: org1.caohuan.com
    Template:
      Count: 2
    Users:
      Count: 1
  - Name: Org2
    Domain: org2.caohuan.com
    Template:
      Count: 2
    Users:
      Count: 1

配置文件修改完成之后执行如下面命令生成配置文件。

cryptogen generate --config=crypto-config.yaml --output ./crypto-config

在当前目录下生成了crypto-config文件夹,其中内容即为各个节点的证书文件。

第二步,生成创世块

新建一个目录用来存放配置文件

mkdir -p /myfabric/hyperledger/order
cd /myfabric/hyperledger/order

把配置文件拷贝进来之后修改

cp -r $GOPATH/src/github.com/hyperledger/fabric/sampleconfig/configtx.yaml   /myfabric/hyperledger/order

修改后的内容:

Organizations:
    - &OrdererOrg
        Name: OrdererOrg
        ID: OrdererMSP
        MSPDir: /myfabric/hyperledger/fabricconfig/crypto-config/ordererOrganizations/caohuan.com/msp

    - &Org1

        Name: Org1MSP
        ID: Org1MSP
        MSPDir: /myfabric/hyperledger/fabricconfig/crypto-config/peerOrganizations/org1.caohuan.com/msp
        AnchorPeers:
            - Host: peer0.org1.caohuan.com
              Port: 7051

    - &Org2
        Name: Org2MSP
        ID: Org2MSP
        MSPDir: /myfabric/hyperledger/fabricconfig/crypto-config/peerOrganizations/org2.caohuan.com/msp
        AnchorPeers:
            - Host: peer0.org2.caohuan.com
              Port: 7051

Orderer: &OrdererDefaults

    OrdererType: solo
    Addresses:
        - orderer.caohuan.com:7050
    BatchTimeout: 2s

    BatchSize:
        MaxMessageCount: 10
        AbsoluteMaxBytes: 98 MB
        PreferredMaxBytes: 512 KB

    Kafka:
        Brokers:
            - 127.0.0.1:9092
    Organizations:

Application: &ApplicationDefaults

    Organizations:

Profiles:

    CaohuanTestTwoOrgsOrdererGenesis:
        Orderer:
            <<: *OrdererDefaults
            Organizations:
                - *OrdererOrg
        Consortiums:
            SampleConsortium:
                Organizations:
                    - *Org1
                    - *Org2

    CaohuanTestTwoOrgsChannel:
        Consortium: SampleConsortium
        Application:
            <<: *ApplicationDefaults
            Organizations:
                - *Org1
                - *Org2

配置文件修改完成之后执行如下面命令生成创世块文件

configtxgen -profile  CaohuanTestTwoOrgsOrdererGenesis  -outputBlock  caohuan.orderer.genesis.block

上述命令执行完成之后会在文件夹/myfabric/hyperledger/order中生成文件orderer.genesis.block

创建Channel:

configtxgen -profile  CaohuanTestTwoOrgsChannel  -outputCreateChannelTx  caohuanchannel.tx -channelID  caohuanchannel

生成锚节点文件:

configtxgen -profile  CaohuanTestTwoOrgsChannel  -outputAnchorPeersUpdate ./Org1MSPanchors.tx -channelID  caohuanchannel -asOrg Org1MSP

configtxgen -profile  CaohuanTestTwoOrgsChannel  -outputAnchorPeersUpdate ./Org2MSPanchors.tx -channelID  caohuanchannel -asOrg Org2MSP

生成文件Org1MSPanchors.tx和Org2MSPanchors.tx

第三步,启动order服务

Orderer节点负责交易的打包和区块的生成

cp $GOPATH/src/github.com/hyperledger/fabric/sampleconfig/orderer.yaml /myfabric/hyperledger/order

在模板的基础上修改,主要修改内容就是节点的名字及证书路径

启动order:

orderer start
第四步,启动peer节点

新建文件夹,并把配置文件拷贝进去

mkdir -p /myfabric/hyperledger/peer
cd /myfabric/hyperledger/peer
cp  $GOPATH/src/github.com/hyperledger/fabric/sampleconfig/core.yaml /myfabric/hyperledger/peer

修改配置文件,主要还是节点名称及证书路径

设置环境变量并启动第一个peer节点

export set FABRIC_CFG_PATH=/myfabric/hyperledger/peer
peer node start

也可以用nohup的方式启动,方便后面测试时观察日志

第五步,创建通道
export set FABRIC_CFG_PATH=/myfabric/hyperledger/peer
export set CORE_PEER_LOCALMSPID=Org1MSP
export set CORE_PEER_MSPCONFIGPATH=/myfabric/hyperledger/fabricconfig/crypto-config/peerOrganizations/org1.caohuan.com/users/Admin@org1.caohuan.com/msp

cd /myfabric/hyperledger/order

peer channel create -t 50s -o orderer.caohuan.com:7050 -c caohuanchannel -f /myfabric/hyperledger/order/caohuanchannel.tx

让已经运行的Peer模块加入通道

export set CORE_PEER_ADDRESS=peer0.org1.caohuan.com:7051
peer channel join -b /myfabric/hyperledger/order/caohuanchannel.block

更新锚节点

peer channel update -o orderer.caohuan.com:7050 -c caohuanchannel -f  /myfabric/hyperledger/order/Org1MSPanchors.tx
3,部署和调用chaincode

部署chaincode代码

peer chaincode install -n caohuanTestcc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/example02/cmd

实例化chaincode代码

peer chaincode instantiate -o  orderer.caohuan.com:7050 -C caohuanchannel -n caohuanTestcc -v 1.0 -c '{"Args":["init","a","100","b","500"]}' -P " OR    ('Org1MSP.member','Org2MSP.member')"

调用chaincode

peer chaincode invoke -o orderer.caohuan.com:7050 -C caohuanchannel -n caohuanTestcc -c '{"Args":["invoke","a","b","1"]}'

查询

peer chaincode query -C caohuanchannel -n caohuanTestcc -c '{"Args":["query","a"]}'

转载请注明来源

×

喜欢就点赞,疼爱就打赏