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

相关文章

SpringBoot实现不同接口指定上传文件大小的具体步骤

《SpringBoot实现不同接口指定上传文件大小的具体步骤》:本文主要介绍在SpringBoot中通过自定义注解、AOP拦截和配置文件实现不同接口上传文件大小限制的方法,强调需设置全局阈值远大于... 目录一  springboot实现不同接口指定文件大小1.1 思路说明1.2 工程启动说明二 具体实施2

基于Redisson实现分布式系统下的接口限流

《基于Redisson实现分布式系统下的接口限流》在高并发场景下,接口限流是保障系统稳定性的重要手段,本文将介绍利用Redisson结合Redis实现分布式环境下的接口限流,具有一定的参考价值,感兴趣... 目录分布式限流的核心挑战基于 Redisson 的分布式限流设计思路实现步骤引入依赖定义限流注解实现

SpringBoot实现RSA+AES自动接口解密的实战指南

《SpringBoot实现RSA+AES自动接口解密的实战指南》在当今数据泄露频发的网络环境中,接口安全已成为开发者不可忽视的核心议题,RSA+AES混合加密方案因其安全性高、性能优越而被广泛采用,本... 目录一、项目依赖与环境准备1.1 Maven依赖配置1.2 密钥生成与配置二、加密工具类实现2.1

使用Python的requests库调用API接口的详细步骤

《使用Python的requests库调用API接口的详细步骤》使用Python的requests库调用API接口是开发中最常用的方式之一,它简化了HTTP请求的处理流程,以下是详细步骤和实战示例,涵... 目录一、准备工作:安装 requests 库二、基本调用流程(以 RESTful API 为例)1.

Olingo分析和实践之ODataImpl详细分析(重要方法详解)

《Olingo分析和实践之ODataImpl详细分析(重要方法详解)》ODataImpl.java是ApacheOlingoOData框架的核心工厂类,负责创建序列化器、反序列化器和处理器等组件,... 目录概述主要职责类结构与继承关系核心功能分析1. 序列化器管理2. 反序列化器管理3. 处理器管理重要方

SpringBoot+Redis防止接口重复提交问题

《SpringBoot+Redis防止接口重复提交问题》:本文主要介绍SpringBoot+Redis防止接口重复提交问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录前言实现思路代码示例测试总结前言在项目的使用使用过程中,经常会出现某些操作在短时间内频繁提交。例

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

MybatisPlus service接口功能介绍

《MybatisPlusservice接口功能介绍》:本文主要介绍MybatisPlusservice接口功能介绍,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录Service接口基本用法进阶用法总结:Lambda方法Service接口基本用法MyBATisP

Java中的Closeable接口及常见问题

《Java中的Closeable接口及常见问题》Closeable是Java中的一个标记接口,用于表示可以被关闭的对象,它定义了一个标准的方法来释放对象占用的系统资源,下面给大家介绍Java中的Clo... 目录1. Closeable接口概述2. 主要用途3. 实现类4. 使用方法5. 实现自定义Clos