etcd网络通讯层(一)——rafthttp中重要的接口

2024-01-05 17:18

本文主要是介绍etcd网络通讯层(一)——rafthttp中重要的接口,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

       前面我们提到etcd-raft模块,etcd-raft模块并未提供网络层的相关实现,而是将待发送的消息封装进Ready实例返回给上层模块,然后由上层模块决定如何将这些消息发送到集群中的其他节点。etcd将网络层的相关实现单独封装成一个模块,也就是etcd-rafthttp模块。从而降低了etcd-raft和etcd-rafthttp之间的耦合,提高程序的可扩展性。

      etcd-rafthttp的模块主要在https://github.com/etcd-io/etcd/tree/master/etcdserver/api/rafthttp 目录下。

      在etcd集群中,每个节点启动时都会与集群中的其他节点建立连接(每个节点即是服务端,也是客户端),这里以三个节点为例,最终形成如图的网络结构。

      在etcd-rafthttp模块中,主要使用两个消息传输通道,Stream和Pipeline消息通道。

      Stream消息通道维护的HTTP长连接,主要负责传输数据量较小、发送比较频繁的消息,例如,MsgApp消息、MsgHeartbeat消息、MsgVote消息等。

     而Pipeline消息通道在传输数据完成后立即关闭连接,主要负责传输数据量较大、发送频率较低的消息,例如,MsgSnap消息等。

 

一、rafthttp.Transporter接口

Transporter接口是rafthttp包的核心接口,它定义了etcd网络层的核心功能,其具体定义如下:

 type Transporter interface {// Start starts the given Transporter.// Start MUST be called before calling other functions in the interface.Start() error  //初始化操作// Handler returns the HTTP handler of the transporter.// A transporter HTTP handler handles the HTTP requests// from remote peers.// The handler MUST be used to handle RaftPrefix(/raft)// endpoint.Handler() http.Handler		//创建Handler实例,并关联到指定URL上// Send sends out the given messages to the remote peers.// Each message has a To field, which is an id that maps// to an existing peer in the transport.// If the id cannot be found in the transport, the message// will be ignored.Send(m []raftpb.Message)  //发送消息// SendSnapshot sends out the given snapshot message to a remote peer.// The behavior of SendSnapshot is similar to Send.SendSnapshot(m snap.Message)   //发送快照数据// AddRemote adds a remote with given peer urls into the transport.// A remote helps newly joined member to catch up the progress of cluster,// and will not be used after that.// It is the caller's responsibility to ensure the urls are all valid,// or it panics.AddRemote(id types.ID, urls []string)   //在集群中添加一个节点时,其他节点会通过该方法添加该新加入节点的信息// AddPeer adds a peer with given peer urls into the transport.// It is the caller's responsibility to ensure the urls are all valid,// or it panics.// Peer urls are used to connect to the remote peer.//Peer接口是当前节点对集群中其他节点的抽象表示,而结构体Peer则是Peer接口的一个具体实现//下面几个方法是对Peer的操作AddPeer(id types.ID, urls []string)// RemovePeer removes the peer with given id.RemovePeer(id types.ID)// RemoveAllPeers removes all the existing peers in the transport.RemoveAllPeers()// UpdatePeer updates the peer urls of the peer with the given id.// It is the caller's responsibility to ensure the urls are all valid,// or it panics.UpdatePeer(id types.ID, urls []string)// ActiveSince returns the time that the connection with the peer// of the given id becomes active.// If the connection is active since peer was added, it returns the adding time.// If the connection is currently inactive, it returns zero time.ActiveSince(id types.ID) time.Time// ActivePeers returns the number of active peers.ActivePeers() int// Stop closes the connections and stops the transporter.Stop()  //关闭操作
}

 Start()方法完成初始化操作

 Handler() 创建Handler实例,并关联到指定的URL上,创建HTTP服务时使用

 Send()方法用于给对端发送消息

 SendSnapshot()用于向对端发送快照数据

 Peer接口是当前节点对集群中其他节点(对端)的抽象表示,AddPeer、RemovePeer、RemoveAllPeer、UpdatePeer分别用于添加节点、删除节点、删除所有节点和更新节点的操作。

 

二、Raft接口 

     Raft接口也是rafthttp包中非常重要的接口,用于集群中当前节点的网络层(etcd-rafthttp)向etcd-raft层发送消息。

type Raft interface {Process(ctx context.Context, m raftpb.Message) error  //将指定消息传递到etcd-raft模块进行处理IsIDRemoved(id uint64) bool     //检测当前节点是否从当前集群中被移除ReportUnreachable(id uint64)  //通知底层的etcd-raft模块,当前节点与指定的节点无法连通ReportSnapshot(id uint64, status raft.SnapshotStatus)  //通知底层的etcd-raft模块,快照数据是否发送成功
}

Raft接口提供了当前节点的网络层向底层的etcd-raft模块发送消息的功能

 

三、Peer接口

type Peer interface {// send sends the message to the remote peer. The function is non-blocking// and has no promise that the message will be received by the remote.// When it fails to send message out, it will report the status to underlying// raft.//发送单个消息给对端,该方法时非阻塞的,如果出现发送失败,则会将失败信息报告给底层的Raft接口send(m raftpb.Message)// sendSnap sends the merged snapshot message to the remote peer. Its behavior// is similar to send.//发送snap.Message,其他行为与上面的send()方法类似sendSnap(m snap.Message)// update updates the urls of remote peer.//更新对应节点暴露的URL地址update(urls types.URLs)// attachOutgoingConn attaches the outgoing connection to the peer for// stream usage. After the call, the ownership of the outgoing// connection hands over to the peer. The peer will close the connection// when it is no longer used./*将指定连接与当前的Peer绑定,Peer会将该连接作为Stream消息通道使用当Peer不再使用该连接时,会将该连接关闭*/attachOutgoingConn(conn *outgoingConn)// activeSince returns the time that the connection with the// peer becomes active./*activeSince返回与其他端(peer)的连接变为active状态的时间。*/activeSince() time.Time// stop performs any necessary finalization and terminates the peer// elegantly./*关闭当前Peer实例,会关闭底层的网络连接*/stop()
}

 Peer接口是当前节点对集群中其他节点(对端)的抽象表示,该接口主要作用是向对端(集群中的其他节点)发送消息的功能。

后面会对这些接口的实现进行详细分析。

这篇关于etcd网络通讯层(一)——rafthttp中重要的接口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/573591

相关文章

go中空接口的具体使用

《go中空接口的具体使用》空接口是一种特殊的接口类型,它不包含任何方法,本文主要介绍了go中空接口的具体使用,具有一定的参考价值,感兴趣的可以了解一下... 目录接口-空接口1. 什么是空接口?2. 如何使用空接口?第一,第二,第三,3. 空接口几个要注意的坑坑1:坑2:坑3:接口-空接口1. 什么是空接

SpringBoot整合jasypt实现重要数据加密

《SpringBoot整合jasypt实现重要数据加密》Jasypt是一个专注于简化Java加密操作的开源工具,:本文主要介绍详细介绍了如何使用jasypt实现重要数据加密,感兴趣的小伙伴可... 目录jasypt简介 jasypt的优点SpringBoot使用jasypt创建mapper接口配置文件加密

如何用java对接微信小程序下单后的发货接口

《如何用java对接微信小程序下单后的发货接口》:本文主要介绍在微信小程序后台实现发货通知的步骤,包括获取Access_token、使用RestTemplate调用发货接口、处理AccessTok... 目录配置参数 调用代码获取Access_token调用发货的接口类注意点总结配置参数 首先需要获取Ac

讯飞webapi语音识别接口调用示例代码(python)

《讯飞webapi语音识别接口调用示例代码(python)》:本文主要介绍如何使用Python3调用讯飞WebAPI语音识别接口,重点解决了在处理语音识别结果时判断是否为最后一帧的问题,通过运行代... 目录前言一、环境二、引入库三、代码实例四、运行结果五、总结前言基于python3 讯飞webAPI语音

MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析

《MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析》本文将详细讲解MyBatis-Plus中的lambdaUpdate用法,并提供丰富的案例来帮助读者更好地理解和应... 目录深入探索MyBATis-Plus中Service接口的lambdaUpdate用法及示例案例背景

Java8需要知道的4个函数式接口简单教程

《Java8需要知道的4个函数式接口简单教程》:本文主要介绍Java8中引入的函数式接口,包括Consumer、Supplier、Predicate和Function,以及它们的用法和特点,文中... 目录什么是函数是接口?Consumer接口定义核心特点注意事项常见用法1.基本用法2.结合andThen链

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

MyBatis-Flex BaseMapper的接口基本用法小结

《MyBatis-FlexBaseMapper的接口基本用法小结》本文主要介绍了MyBatis-FlexBaseMapper的接口基本用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具... 目录MyBATis-Flex简单介绍特性基础方法INSERT① insert② insertSelec

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

Idea实现接口的方法上无法添加@Override注解的解决方案

《Idea实现接口的方法上无法添加@Override注解的解决方案》文章介绍了在IDEA中实现接口方法时无法添加@Override注解的问题及其解决方法,主要步骤包括更改项目结构中的Languagel... 目录Idea实现接China编程口的方法上无法添加@javascriptOverride注解错误原因解决方