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下载接口限速功能实现》通过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

java对接第三方接口的三种实现方式

《java对接第三方接口的三种实现方式》:本文主要介绍java对接第三方接口的三种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录HttpURLConnection调用方法CloseableHttpClient调用RestTemplate调用总结在日常工作

Java 的 Condition 接口与等待通知机制详解

《Java的Condition接口与等待通知机制详解》在Java并发编程里,实现线程间的协作与同步是极为关键的任务,本文将深入探究Condition接口及其背后的等待通知机制,感兴趣的朋友一起看... 目录一、引言二、Condition 接口概述2.1 基本概念2.2 与 Object 类等待通知方法的区别

SpringBoot实现接口数据加解密的三种实战方案

《SpringBoot实现接口数据加解密的三种实战方案》在金融支付、用户隐私信息传输等场景中,接口数据若以明文传输,极易被中间人攻击窃取,SpringBoot提供了多种优雅的加解密实现方案,本文将从原... 目录一、为什么需要接口数据加解密?二、核心加解密算法选择1. 对称加密(AES)2. 非对称加密(R

Java对接Dify API接口的完整流程

《Java对接DifyAPI接口的完整流程》Dify是一款AI应用开发平台,提供多种自然语言处理能力,通过调用Dify开放API,开发者可以快速集成智能对话、文本生成等功能到自己的Java应用中,本... 目录Java对接Dify API接口完整指南一、Dify API简介二、准备工作三、基础对接实现1.

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

usb接口驱动异常问题常用解决方案

《usb接口驱动异常问题常用解决方案》当遇到USB接口驱动异常时,可以通过多种方法来解决,其中主要就包括重装USB控制器、禁用USB选择性暂停设置、更新或安装新的主板驱动等... usb接口驱动异常怎么办,USB接口驱动异常是常见问题,通常由驱动损坏、系统更新冲突、硬件故障或电源管理设置导致。以下是常用解决