基于Raft共识算法搭建多机Fabric1.4.1网络
在fabric1.4.1的版本中,提供了基于raft共识的raft排序服务。raft的模型可以容忍奔溃,如果有节点故障掉线可以正常运行。前提是要有大多数存活,也就是要保证1/2以上的节点个数正常运行。raft共识是“主从模型”,主节点通过动态选举决定,从节点是主节点的复制。raft排序服务比kafka排序服务易于设置和管理。并且raft的设计允许不同的组织贡献节点来共同组成排序服务。raft排序是fabric实现拜占庭容错排序服务的第⼀步,如我们所见,开发raft排序服务的决定也是基于此的。raft节点之间通过使⽤用TLS认证身份,如果一个攻击者想要伪造raft节点,就必须要获得⼀个有效的证书和对应的私钥。所以,没有⼀个有效的TLS证书,是不可能运行raft节点的。 要使用raft共识,需要修改两个地⽅:
1. 本地配置:⽤用来控制节点的特性,例例如TLS配置,备份个数和⽂文件存储。
2. 通道配置:⽤用来定义raft集群的成员关系,以及raft协议相关的参数,例例如⼼心跳间隔、leader节点超时时间等。
3. 需要注意的是,每个channel有属于它⾃己的raft集群。因此,在chennel中要指定raft节点,指定的方式是把raft节点的tls证书配置到channel的配置文件中。在系统通道和应用通道中的配置中,每个排序以consenter的形式列出来。下⾯有configtx.yaml中关于raft节点的配置。
4. 必须开启TLS才能使用Raft排序
上文中已经把搭建fabric网络所需要的环境部署工作已经完成。
1、搭建基于Raft共识的多机fabric网络环境的说明与准备
在本次基于Raft共识搭建的fabric1.4.1网络环境中,准备搭建五个orderer节点、一个组织四个peer,未加入CouchDB和fabric-ca.这里要说明一下,raft共识中同步的节点必须为奇数,因为在整个共识环境中每个节点都是follower,当他们感受到网络中没有leader节点向他们发送heartbeat的时候,他们就会变成candidater,这时候需要他们之间相互投票才能将自己由candidater变成leader,达成一致的过程需要整个网络中有n/2+1个节点达成一致,整个网络才会达成一致,所以需要奇数个同步节点,在leader选举出来之后通过leader与客户端交互,将本地的log同步到各个follower,点击这里了解更多的raft算法,。
本文一共用到四台华为云主机,每台主机均是ubuntu18.04.2系统,各个主机的IP以及节点分配情况见下表:
下表中是各个节点分配情况以及对应的主机IP
各个主机的节点分配情况 | 对应主机的IP地址 |
---|---|
orderer0.example.com,peer0.org1.example.com ,cli | 192.168.1.16 |
orderer1.example.com,peer1.org1.example.com,cli | 192.168.1.17 |
orderer2.example.com,peer2.org1.example.com,cli | 192.168.1.173 |
orderer3.example.com,orderer4.example.com,peer3.org1.example.com,cli | 192.168.1.190 |
2、基于Raft共识搭建多机fabric网络
这一节是对每台华为云主机的各个节点进行网络配置,主要涉及的是配置文件。
- 因为在四台主机的项目目录必须一样,所有在四台主机的家目录下执行相同的命令mkdir lpgsy来创建项目目录,并进入项目目录。
第一台主机 192.168.1.16
-
在项目目录中执行命令vim crypto-config.yaml,并将下列代码块中的内容粘贴到文件中(192.168.1.16),生成yaml文件。
OrdererOrgs: # 排序节点的组织定义 - Name: Orderer # orderer节点的名称 Domain: example.com # orderer节点的根域名 Specs: # orderer节点的主机名 - Hostname: orderer0 - Hostname: orderer1 - Hostname: orderer2 - Hostname: orderer3 - Hostname: orderer4 PeerOrgs: # peer节点的组织定义 - Name: Org1 # 组织1的名称 Domain: org1.example.com # 组织1的根域名 EnableNodeOUs: true # 是否显示节点的详细信息 Template: Count: 4 # 组织1中的节点(peer)数目 Users: Count: 4 # 组织1中的用户数目
-
执行命令cryptogen generate --config ./crypto-config.yaml生成各个节点的证书(192.168.1.16)。
-
执行命令vim configtx.yaml,并将下列代码块中的内容粘贴到文件中(192.168.1.16)。
--- Organizations: - &OrdererOrg # orderer节点配置信息 Name: OrdererOrg # orderer节点名称 ID: OrdererMSP # orderer节点ID MSPDir: crypto-config/ordererOrganizations/example.com/msp # msp文件路径 Policies: Readers: Type: Signature Rule: "OR('OrdererMSP.member')" Writers: Type: Signature Rule: "OR('OrdererMSP.member')" Admins: Type: Signature Rule: "OR('OrdererMSP.admin')" - &Org1 # 组织 Name: Org1MSP # 组织名称 ID: Org1MSP # 组织ID MSPDir: crypto-config/peerOrganizations/org1.example.com/msp # 组织msp文件路径 AnchorPeers: # 访问组织的域名和端口 - Host: peer0.org1.example.com Port: 7051 Policies: Readers: Type: Signature Rule: "OR('Org1MSP.admin','Org1MSP.peer','Org1MSP.client')" Writers: Type: Signature Rule: "OR('Org1MSP.admin','Org1MSP.client')" Admins: Type: Signature Rule: "OR('Org1MSP.admin')" Capabilities: Channel: &ChannelCapabilities V1_3: true Orderer: &OrdererCapabilities V1_1: true Application: &ApplicationCapabilities V1_3: true V1_2: false V1_1: false Application: &ApplicationDefaults Organizations: Policies: Readers: Type: ImplicitMeta Rule: "ANY Readers" Writers: Type: ImplicitMeta Rule: "ANY Writers" Admins: Type: ImplicitMeta Rule: "MAJORITY Admins" Capabilities: <<: *ApplicationCapabilities Orderer: &OrdererDefaults OrdererType: solo Addresses: - orderer0.example.com:7050 BatchTimeout: 2s BatchSize: MaxMessageCount: 200 AbsoluteMaxBytes: 2 MB PreferredMaxBytes: 512 KB Kafka: Brokers: - 127.0.0.1:9092 Organizations: Policies: Readers: Type: ImplicitMeta Rule: "ANY Readers" Writers: Type: ImplicitMeta Rule: "ANY Writers" Admins: Type: ImplicitMeta Rule: "MAJORITY Admins" BlockValidation: Type: ImplicitMeta Rule: "ANY Writers" Channel: &ChannelDefaults Policies: Readers: Type: ImplicitMeta Rule: "ANY Readers" Writers: Type: ImplicitMeta Rule: "ANY Writers" Admins: Type: ImplicitMeta Rule: "MAJORITY Admins" Capabilities: <<: *ChannelCapabilities Profiles: # 定义系统通道和应用通道配置信息,从Profiles获取-profile后边的参数 TwoOrgsOrdererGenesis: #组织定义标识符,可自定义 # 系统通道配置标识符,自定义 <<: *ChannelDefaults # 引用上面 ChannelDefaults 的属性 # 具体通道相关配置信息引用ChannelDefaults Capabilities: <<: *ChannelCapabilities # 引用 ChannelCapabilities 的属性 Orderer: # 配置属性,系统关键字,不能修改 # 系统通道配置信息 <<: *OrdererDefaults # 引用上面 OrdererDefaults 的属性 # 具体配置信息引用OrdererDefaults OrdererType: etcdraft #指定排序共识 EtcdRaft: Consenters: - Host: orderer0.example.com Port: 7050 ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/tls/server.crt ServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/tls/server.crt - Host: orderer1.example.com Port: 7050 ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer1.example.com/tls/server.crt ServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer1.example.com/tls/server.crt - Host: orderer2.example.com Port: 7050 ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer2.example.com/tls/server.crt ServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer2.example.com/tls/server.crt - Host: orderer3.example.com Port: 7050 ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer3.example.com/tls/server.crt ServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer3.example.com/tls/server.crt - Host: orderer4.example.com Port: 8050 ClientTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer4.example.com/tls/server.crt ServerTLSCert: crypto-config/ordererOrganizations/example.com/orderers/orderer4.example.com/tls/server.crt Addresses: - orderer0.example.com:7050 - orderer1.example.com:7050 - orderer2.example.com:7050 - orderer3.example.com:7050 - orderer4.example.com:8050 Organizations: # 系统通道组织 - *OrdererOrg # 引用上面为 OrdererOrg 的属性 # 具体配置信息引用OrdererOrg Capabilities: # 定义系统通道全局功能特性 <<: *OrdererCapabilities #引用上面为 OrdererCapabilities 的属性 # 具体通道排序配置信息引用OrdererCapabilities Application: # 系统通道信息 <<: *ApplicationDefaults # 具体应用通道配置信息引用ApplicationDefaults Organizations: # 系统通道组织 - <<: *OrdererOrg # 具体配置信息引用OrdererOrg Consortiums: # 定义了系统中包含的组织 # 联盟列表 SampleConsortium: # 联盟名称 Org