一、手动搭建私有网络
网络结构如下:
组织 | 组织标识 | 组织ID |
---|---|---|
GO学科 | OrgGo | OrgGoMSP |
JAVA学科 | OrgJava | OrgJavaMSP |
MSP(Membership service provider)是一个提供虚拟成员操作的管理框架的组件。每一个组织、节点、用户都有一个MSP账号。
下面是Fabric私有网络的搭建过程:
1.1 生成证书
第一步:在当前用户主目录下创建一个fabric_demo1目录。
mkdir ~/fabric-demo1
cd ~/fabric-demo1
第二步:新建crypto-config.yaml文件,通过该文件可以生成证书。
OrdererOrgs: # orderer节点的组织信息
- Name: Orderer # 组织名称
Domain: example.com # 访问节点的域名
Specs:
- Hostname: orderer # 访问这台orderer对应的域名为: orderer.example.com
PeerOrgs: # peer节点的组织信息
- Name: OrgGo # 组织名称
Domain: orggo.example.com # 访问组织节点的域名,
EnableNodeOUs: true # 是否支持node.js
Template:
Count: 2 # 生成2个peer节点:节点地址为peer0.orggo.example.com、peer1.orggo.example.com
Users:
Count: 1 # 创建1个用户,默认用户名为User1
- Name: OrgJava
Domain: orgjava.example.com
EnableNodeOUs: true
Template:
Count: 2
Users:
Count: 1
第三步:生成证书,生成后的证书保存在crypto-config文件夹下。
cryptogen generate --config=crypto-config.yaml
crypto-config文件夹的目录结构如下图所示:
1.3 生成创世块文件和通道文件
第一步:新建configtx.yaml文件。
Organizations:
- &OrdererOrg
Name: OrdererOrg # Orderer组织的名称
ID: OrdererMSP # Orderer组织的ID
MSPDir: crypto-config/ordererOrganizations/example.com/msp # 组织的msp账号信息
- &OrgGo
Name: OrgGoMSP # OrgGo组织的名称
ID: OrgGoMSP # OrgGo组织的ID
MSPDir: crypto-config/peerOrganizations/orggo.example.com/msp
AnchorPeers: # 锚节点,代表所属组织和其他组织进行通信的节点
- Host: peer0.orggo.example.com
Port: 7051
- &OrgJava # 配置同上
Name: OrgJavaMSP
ID: OrgJavaMSP
MSPDir: crypto-config/peerOrganizations/orgjava.example.com/msp
AnchorPeers:
- Host: peer0.orgjava.example.com
Port: 7051
Capabilities:
Global: &ChannelCapabilities
V1_1: true
Orderer: &OrdererCapabilities
V1_1: true
Application: &ApplicationCapabilities
V1_2: true
Application: &ApplicationDefaults
Organizations:
Orderer: &OrdererDefaults
OrdererType: solo # 共识机制
Addresses:
- orderer.example.com:7050 # orderer节点的地址
BatchTimeout: 2s # 产生区块的时间
BatchSize:
MaxMessageCount: 100 # 产生区块的最大交易数据量
AbsoluteMaxBytes: 99 MB # 产生区块的最大数据量
PreferredMaxBytes: 512 KB
Kafka:
Brokers:
- 127.0.0.1:9092
Organizations:
Profiles:
TwoOrgsOrdererGenesis:
Capabilities:
<<: *ChannelCapabilities
Orderer:
<<: *OrdererDefaults
Organizations:
- *OrdererOrg
Capabilities:
<<: *OrdererCapabilities
Consortiums:
SampleConsortium:
Organizations:
- *OrgGo
- *OrgJava
TwoOrgsChannel:
Consortium: SampleConsortium
Application:
<<: *ApplicationDefaults
Organizations:
- *OrgGo
- *OrgJava
Capabilities:
<<: *ApplicationCapabilities
第二步:生成创世块文件。
# 新建一个目录,用于存储创世块文件和通道文件
mkdir ./channel-artifacts
# 生成创世块
configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
第三步:生成通道文件。
# 生成通道,通道名称为testchannel
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID testchannel
channel-artifacts目录结构如下图所示:
1.4 编写docker-compose配置文件
1.4.1 环境变量
- orderer节点使用的环境变量:
- ORDERER_GENERAL_LOGLEVEL=INFO # 日志级别
- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0 # orderer节点监听的地址
- ORDERER_GENERAL_GENESISMETHOD=file # 创始块的来源, 指定file来源就是文件中
# 创始块对应的文件, 这个不需要改
- ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block
- ORDERER_GENERAL_LOCALMSPID=OrdererMSP # orderer节点所属的组的ID
- ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp # 当前节点的msp账号路径
# enabled TLS
- ORDERER_GENERAL_TLS_ENABLED=true # 是否使用tls加密
- ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key # 私钥
- ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt # 证书
- ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt] # 根证书
- peer节点使用的环境变量:
- CORE_PEER_ID=peer0.orggo.example.com # 当前peer节点的名字, 自己起
# 当前peer节点的地址信息
- CORE_PEER_ADDRESS=peer0.orggo.example.com:7051
# 启动的时候, 指定连接谁, 一般写自己就行
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.orggo.example.com:7051
# 为了被其他节点感知到, 如果不设置别的节点不知有该节点的存在
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.orggo.example.com:7051
- CORE_PEER_LOCALMSPID=OrgGoMSP
# docker的本地套接字地址, 不需要改
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
# 当前节点属于哪个网络
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=network_default
- FABRIC_LOGGING_LEVEL=INFO
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_GOSSIP_USELEADERELECTION=true # 自动选举leader节点
- CORE_PEER_GOSSIP_ORGLEADER=false # 不手动指定当前节点为leader
- CORE_PEER_PROFILE_ENABLED=true # 在peer节点中有一个profile服务
- CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
- 客户端节点使用的环境变量:
- GOPATH=/opt/gopath # # 客户端docker容器启动之后, go的工作目录, 不需要修改
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock # docker容器启动之后, 对应的守护进程的本地套接字, 不需要修改
- FABRIC_LOGGING_LEVEL=INFO # 日志级别
- CORE_PEER_ID=cli # 当前客户端节点的ID, 自己指定
- CORE_PEER_ADDRESS=peer0.orggo.example.com:7051 # 客户端启动后连接的peer节点
- CORE_PEER_LOCALMSPID=OrgGoMSP # 组织ID
- CORE_PEER_TLS_ENABLED=true # 通信是否使用tls加密
- CORE_PEER_TLS_CERT_FILE= # 证书文件
/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.example.com/peers/peer0.orggo.example.com/tls/server.crt
- CORE_PEER_TLS_KEY_FILE= # 私钥文件
/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.example.com/peers/peer0.orggo.example.com/tls/server.key
-CORE_PEER_TLS_ROOTCERT_FILE= # 根证书文件
/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.example.com/peers/peer0.orggo.example.com/tls/ca.crt
# 指定当前客户端的身份
- CORE_PEER_MSPCONFIGPATH= /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.example.com/users/Admin@orggo.example.com/msp
客户端节点用于管理其他节点,比如说在节点上安装链代码。在实际开发中,一般使用sdk来操作各个节点。关于fabric-sdk介绍,后面再进行阐述。
1.4.2 新建docker-compose.yaml
docker-compose.yaml文件的完整配置如下所示:
version: '2'
networks:
byfn: # 网络名称
services:
orderer.example.com: # orderer节点配置
image: hyperledger/fabric-orderer:latest # 镜像名称:版本
container_name: orderer.example.com # 运行orderer节点的容器名称
environment:
- FABRIC_LOGGING_LEVEL=DEBUG # 日志级别:CRITICAL | ERROR | WARNING | NOTICE | INFO | DEBUG
- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0 # orderer节点监听的地址
- ORDERER_GENERAL_LISTENPORT=7050 # orderer节点监听的端口
- ORDERER_GENERAL_GENESISPROFILE=Orderer
- ORDERER_GENERAL_GENESISMETHOD=file # 创世块的来源,file表示来自于文件
- ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block # 指定创世块文件的路径
- ORDERER_GENERAL_LOCALMSPID=OrdererMSP # Orderer组织的MSP,与configtx.yaml文件配置保持一直
- ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp # 当前节点的 MSP 账号路径
- ORDERER_GENERAL_TLS_ENABLED=true # 通信时是否使用TLS加密
- ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key # 私钥文件
- ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt # 证书文件
- ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt] # 根证书文件
working_dir: /opt/gopath/src/github.com/hyperledger/fabric # orderer容器的工作目录
command: orderer
volumes: # 宿主机和容器之间的挂载配置
- ./channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block
- ./crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp:/var/hyperledger/orderer/msp
- ./crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls:/var/hyperledger/orderer/tls
ports:
- 7050:7050 # 宿主机端口:容器端口
networks:
- byfn # orderer节点所在网络
peer0.orggo.example.com: # orggo组织中peer0节点的配置
image: hyperledger/fabric-peer:latest # 镜像名称:版本
container_name: peer0.orggo.example.com # 容器名称
environment:
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_PEER_ID=peer0.orggo.example.com # peer 节点名字
- CORE_PEER_ADDRESS=peer0.orggo.example.com:7051 # 当前peer节点的地址信息
- CORE_VM_DOCKER_ATTACHSTDOUT=true
- FABRIC_LOGGING_LEVEL=DEBUG
- CORE_PEER_ADDRESSAUTODETECT=true
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.orggo.example.com:7051 # 如果不设置,其他节点不知道该节点的存在
- CORE_PEER_GOSSIP_USELEADERELECTION=true # 自动选举leader节点
- CORE_PEER_GOSSIP_ORGLEADER=false # 当前节点是不是 leader 节点
- CORE_PEER_GOSSIP_SKIPHANDSHAKE=true
- CORE_PEER_LOCALMSPID=OrgGoMSP # OrgGo组织的MSP,与configtx.yaml文件配置保持一直
- CORE_PEER_PROFILE_ENABLED=true
- CORE_PEER_MSPCONFIGPATH=/var/hyperledger/msp
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_TLS_SERVERHOSTOVERRIDE=peer0.orggo.example.com
- CORE_PEER_TLS_CERT_FILE=/var/hyperledger/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/var/hyperledger/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/var/hyperledger/tls/ca.crt
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
volumes:
- /var/run/:/host/var/run/
- ./crypto-config/peerOrganizations/orggo.example.com/peers/peer0.orggo.example.com/msp:/var/hyperledger/msp
- ./crypto-config/peerOrganizations/orggo.example.com/peers/peer0.orggo.example.com/tls:/var/hyperledger/tls
ports:
- 7051:7051
- 7053:7053
depends_on:
- orderer.example.com
links:
- orderer.example.com
networks:
- byfn
peer1.orggo.example.com:
image: hyperledger/fabric-peer:latest
container_name: peer1.orggo.example.com
environment:
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_VM_DOCKER_ATTACHSTDOUT=true
- FABRIC_LOGGING_LEVEL=DEBUG
- CORE_PEER_PROFILE_ENABLED=true
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_TLS_CERT_FILE=/var/hyperledger/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/var/hyperledger/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/var/hyperledger/tls/ca.crt
- CORE_PEER_ID=peer1.orggo.example.com
- CORE_PEER_ADDRESSAUTODETECT=true
- CORE_PEER_ADDRESS=peer1.orggo.example.com:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.orggo.example.com:7051
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
- CORE_PEER_GOSSIP_SKIPHANDSHAKE=true
- CORE_PEER_LOCALMSPID=OrgGoMSP # OrgGo组织的MSP,与configtx.yaml文件配置保持一直
- CORE_PEER_MSPCONFIGPATH=/var/hyperledger/msp
- CORE_PEER_TLS_SERVERHOSTOVERRIDE=peer1.orggo.example.com
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
volumes:
- /var/run/:/host/var/run/
- ./crypto-config/peerOrganizations/orggo.example.com/peers/peer1.orggo.example.com/msp:/var/hyperledger/msp
- ./crypto-config/peerOrganizations/orggo.example.com/peers/peer1.orggo.example.com/tls:/var/hyperledger/tls
ports:
- 8051:7051
- 8053:7053
depends_on:
- orderer.example.com
links:
- orderer.example.com
networks:
- byfn
peer0.orgjava.example.com:
image: hyperledger/fabric-peer:latest
container_name: peer0.orgjava.example.com
environment:
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_VM_DOCKER_ATTACHSTDOUT=true
- FABRIC_LOGGING_LEVEL=DEBUG
- CORE_PEER_PROFILE_ENABLED=true
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_TLS_CERT_FILE=/var/hyperledger/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/var/hyperledger/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/var/hyperledger/tls/ca.crt
- CORE_PEER_ID=peer0.orgojava.example.com
- CORE_PEER_ADDRESSAUTODETECT=true
- CORE_PEER_ADDRESS=peer0.orgjava.example.com:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.orgjava.example.com:7051
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
- CORE_PEER_GOSSIP_SKIPHANDSHAKE=true
- CORE_PEER_LOCALMSPID=OrgJavaMSP # OrgJava组织的MSP,与configtx.yaml文件配置保持一直
- CORE_PEER_MSPCONFIGPATH=/var/hyperledger/msp
- CORE_PEER_TLS_SERVERHOSTOVERRIDE=peer0.orgjava.example.com
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
volumes:
- /var/run/:/host/var/run/
- ./crypto-config/peerOrganizations/orgjava.example.com/peers/peer0.orgjava.example.com/msp:/var/hyperledger/msp
- ./crypto-config/peerOrganizations/orgjava.example.com/peers/peer0.orgjava.example.com/tls:/var/hyperledger/tls
ports:
- 9051:7051
- 9053:7053
depends_on:
- orderer.example.com
links:
- orderer.example.com
networks:
- byfn
peer1.orgjava.example.com:
image: hyperledger/fabric-peer:latest
container_name: peer1.orgjava.example.com
environment:
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_VM_DOCKER_ATTACHSTDOUT=true
- FABRIC_LOGGING_LEVEL=DEBUG
- CORE_PEER_PROFILE_ENABLED=true
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_TLS_CERT_FILE=/var/hyperledger/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/var/hyperledger/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/var/hyperledger/tls/ca.crt
- CORE_PEER_ID=peer1.orgjava.example.com
- CORE_PEER_ADDRESSAUTODETECT=true
- CORE_PEER_ADDRESS=peer1.orgjava.example.com:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.orgjava.example.com:7051
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
- CORE_PEER_GOSSIP_SKIPHANDSHAKE=true
- CORE_PEER_LOCALMSPID=OrgJavaMSP # OrgJava组织的MSP,与configtx.yaml文件配置保持一直
- CORE_PEER_MSPCONFIGPATH=/var/hyperledger/msp
- CORE_PEER_TLS_SERVERHOSTOVERRIDE=peer1.orgjava.example.com
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
volumes:
- /var/run/:/host/var/run/
- ./crypto-config/peerOrganizations/orgjava.example.com/peers/peer1.orgjava.example.com/msp:/var/hyperledger/msp
- ./crypto-config/peerOrganizations/orgjava.example.com/peers/peer1.orgjava.example.com/tls:/var/hyperledger/tls
ports:
- 10051:7051
- 10053:7053
depends_on:
- orderer.example.com
links:
- orderer.example.com
networks:
- byfn
cli:
container_name: cli
image: hyperledger/fabric-tools:latest
tty: true
stdin_open: true
environment:
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- FABRIC_LOGGING_LEVEL=DEBUG
- CORE_PEER_ID=cli
- CORE_PEER_ADDRESS=peer0.orggo.example.com:7051
- CORE_PEER_LOCALMSPID=OrgGoMSP
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.example.com/peers/peer0.orggo.example.com/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.example.com/peers/peer0.orggo.example.com/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.example.com/peers/peer0.orggo.example.com/tls/ca.crt
- CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.example.com/users/Admin@orggo.example.com/msp
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: /bin/bash
volumes:
- /var/run/:/host/var/run/
- ./chaincode/:/opt/gopath/src/github.com/chaincode
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
depends_on:
- orderer.example.com
- peer0.orggo.example.com
- peer1.orggo.example.com
- peer0.orgjava.example.com
- peer1.orgjava.example.com
networks:
- byfn
1.4.3 启动网络
docker-compose up -d
启动完成后,执行docker ps查看容器启动情况。
只有当所有容器的状态变为UP,代表网络启动成功。
1.5 创建和加入通道
第一步:进入cli容器。
docker exec -it cli bash
第二步:创建通道。
peer channel create -o orderer.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/msp/tlscacerts/tlsca.example.com-cert.pem
第三步:加入通道。
# 把testchannel.block文件放入./channel-artifacts目录中,以便其他节点上加入通道时使用该文件
mv testchannel.block ./channel-artifacts
# 把当前节点加入到testchannel通道中
peer channel join -b ./channel-artifacts/testchannel.block
可以执行peer channel list
查看当前节点是否加入到通道中。
第四步:切换到其他节点,并且把该节点加入到testchannel通道中。
- peer1.orggo.example.com加入通道:
# 通过修改环境变量切换到peer1.orggo.example.com节点
export CORE_PEER_ADDRESS=peer1.orggo.example.com:7051
export CORE_PEER_LOCALMSPID=OrgGoMSP
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.example.com/users/Admin@orggo.example.com/msp
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.example.com/peers/peer1.orggo.example.com/tls/ca.crt
export CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.example.com/peers/peer1.orggo.example.com/tls/server.crt
export CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.example.com/peers/peer1.orggo.example.com/tls/server.key
# 将节点加入testchannel通道中
peer channel join -b ./channel-artifacts/testchannel.block
- peer0.orgjava.example.com加入通道:
# 通过修改环境变量切换到peer1.orggo.example.com节点
export CORE_PEER_ADDRESS=peer0.orgjava.example.com:7051
export CORE_PEER_LOCALMSPID=OrgJavaMSP
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgjava.example.com/users/Admin@orgjava.example.com/msp
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgjava.example.com/peers/peer0.orgjava.example.com/tls/ca.crt
export CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgjava.example.com/peers/peer0.orgjava.example.com/tls/server.crt
export CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgjava.example.com/peers/peer0.orgjava.example.com/tls/server.key
# 将节点加入testchannel通道中
peer channel join -b ./channel-artifacts/testchannel.block
- peer1.orgjava.example.com加入通道:
export CORE_PEER_ADDRESS=peer1.orgjava.example.com:7051
export CORE_PEER_LOCALMSPID=OrgJavaMSP
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgjava.example.com/users/Admin@orgjava.example.com/msp
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgjava.example.com/peers/peer1.orgjava.example.com/tls/ca.crt
export CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgjava.example.com/peers/peer1.orgjava.example.com/tls/server.crt
export CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgjava.example.com/peers/peer1.orgjava.example.com/tls/server.key
# 将节点加入testchannel通道中
peer channel join -b ./channel-artifacts/testchannel.block
1.6 链代码的安装和实例化
链代码也称智能合约,是控制区块链网络中相关方相互交互的业务逻辑。链代码将业务网络交易封装在代码中,最终在一个 Docker 容器内运行。
- 链码安装的步骤:
第一步:准备chaincode文件。
在hyperledger-fabric/fabric-samples/chaincode/chaincode_example02/go目录下有两个chaincode文件,它们是官方提供了示例文件。我们可以把其中一个拷贝到~/fabric_demo01/chaincode目录中。
sudo cp chaincode_example02.go ~/fabric_demo1/chaincode/
第二步:在每一个节点上安装链码。
# 进入cli容器
docker exec -it cli bash
# 安装链码
peer chaincode install -n testcc -v 1.0 -l golang -p github.com/chaincode
安装成功后界面如下所示:
第三步:实例化链码。
# 实例化链码
peer chaincode instantiate -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C testchannel -n testcc -l golang -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "AND('OrgGoMSP.member'')"
注意: 每一个节点都需要安装链码,但是只需要在其中一个节点上实例化链码即可,不需要每个节点进行实例化操作。
1.7 测试
- 查询(显示a的值)
peer chaincode query -C testchannel -n testcc -c '{"Args":["query","a"]}'
结果显示100。
- 交易(a给b转账10)
peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C testchannel -n testcc --peerAddresses peer0.orggo.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.example.com/peers/peer0.orggo.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}'
转账完成后,重新查询a的值,这时候a的值为90。到目前为止,手动搭建hyperledge-fabric网络已经完成。