对图中peer节点启动之后的peer address 172.17.0.3:7051灰色部分的的代码提取分析,7051:peer gRPC 服务监听端口。下面是Hyperledger中相关监听的服务端口
默认包括:以下监听的服务端口对应hyperledger0.6版本,与其他版本无关。
7050: REST 服务端口
7051:peer gRPC 服务监听端口
7052:peer CLI 端口
7053:peer 事件服务端口
7054:eCAP7055:eCAA
7056:tCAP
7057:tCAA
7058:tlsCAP
7059:tlsCAA
图中的CacheConfiguration()这个函数到底做了什么?请看下面
// CacheConfiguration计算和缓存经常使用的常量且计算常量做为包变量,按照惯例前面的全局变量
// 已经被嵌入在这里为了保留原始的抽象状态
func CacheConfiguration() (err error) {
// getLocalAddress 返回正在操作的本地peer的address:port,受到env:peer.addressAutoDetect的影响
getLocalAddress := func() (peerAddress string, err error) {
if viper.GetBool("peer.addressAutoDetect") {
// 需要从peer.address设置中获取端口号,并将其添加到已经确定的主机ip后
_, port, err := net.SplitHostPort(viper.GetString("peer.address"))
if err != nil {
err = fmt.Errorf("Error auto detecting Peer's address: %s", err)
return "", err
}
peerAddress = net.JoinHostPort(GetLocalIP(), port)
peerLogger.Infof("Auto detected peer address: %s", peerAddress)
} else {
peerAddress = viper.GetString("peer.address")
}
return
}
// getPeerEndpoint 对于这个Peer实例来说,返回PeerEndpoint,受到env:peer.addressAutoDetect的影响
getPeerEndpoint := func() (*pb.PeerEndpoint, error) {
var peerAddress string
var peerType pb.PeerEndpoint_Type
peerAddress, err := getLocalAddress()
if err != nil {
return nil, err
}
if viper.GetBool("peer.validator.enabled") {
peerType = pb.PeerEndpoint_VALIDATOR
} else {
peerType = pb.PeerEndpoint_NON_VALIDATOR
}
return &pb.PeerEndpoint{ID: &pb.PeerID{Name: viper.GetString("peer.id")}, Address: peerAddress, Type: peerType}, nil
}
localAddress, localAddressError = getLocalAddress()
peerEndpoint, peerEndpointError = getPeerEndpoint()
syncStateSnapshotChannelSize = viper.GetInt("peer.sync.state.snapshot.channelSize")
syncStateDeltasChannelSize = viper.GetInt("peer.sync.state.deltas.channelSize")
syncBlocksChannelSize = viper.GetInt("peer.sync.blocks.channelSize")
validatorEnabled = viper.GetBool("peer.validator.enabled")
securityEnabled = viper.GetBool("security.enabled")
configurationCached = true
if localAddressError != nil {
return localAddressError
} else if peerEndpointError != nil {
return peerEndpointError
}
return
}
// cacheConfiguration如果检查失败打一个错误日志
func cacheConfiguration() {
if err := CacheConfiguration(); err != nil {
peerLogger.Errorf("Execution continues after CacheConfiguration() failure : %s", err)
}
}// GetPeerEndpoint 从缓存配置中返回peerEndpoint
func GetPeerEndpoint() (*pb.PeerEndpoint, error) {
if !configurationCached {
cacheConfiguration()
}
return peerEndpoint, peerEndpointError
}
// cacheConfiguration如果检查失败打一个错误日志
func cacheConfiguration() {
if err := CacheConfiguration(); err != nil {
peerLogger.Errorf("Execution continues after CacheConfiguration() failure : %s", err)
}