当前位置:网站首页>The design idea of DMicro, the Go microservice development framework
The design idea of DMicro, the Go microservice development framework
2022-08-05 02:31:00 【A female programmer who can't write code】
Go 微服务开发框架 DMicro 的设计思路
DMicro 源码地址:
- Gitee:
- dmicro: dmicro是一个高效、Extensible and easy to use micro service framework.包含drpc,dserver等
背景
DMicro 诞生的背景,Because I wrote 10 来年的 PHP,Want to be in company internal promotion Go, Internal components and rpc 协议都是基于 swoole 定制化开发的.The research on the market all kinds of framework,包括 beego,goframe,gin,go-micro,go-zero,erpc 等等,May be I was technical ability is limited,Doesn't make these frameworks good fit our business.
We have several business development pain points,在当时 golang A set of solution cannot be found in the ecological.
- Micro service application and monomer application development at the same time.
- 高性能,The high availability of network communication.
- Need a custom application layer protocol (重点).
- Need to be flexible plug-in extension mechanism,Convenient adaptation of existing system (重点).
- Concept of server and client,Can use the same each other api 调用对方.
- 支持 Push 消息.
- 连接 / 会话管理.
- 高效率的开发,支持通过 proto 生成代码.
- 支持多种网络协议,
tcp,websocket,quic,unixsocket. - 兼容 http 协议.
- Can be more rapid positioning problem.
- 更便捷的增加新特性.
In the commonly used open source frameworks do the simple research later,Discovery is not an appropriate framework can meet all my needs.After serious thinking,发现 erpc 和 goframe The combination of the two frameworks fitness to satisfy my needs,So was born the r DMicro.
概述
DMicro 中的 drpc Thought is the reference component erpc 实现,Even it's successor.
drpc 组件是 DMicro 框架的一部分,为了适配 DMicro 框架,在 erpc On the basis of a deeply extension development.
整个 DMicro 大量使用 goframe 中的组件,如果业务使用 goframe 框架,可以无缝接入.
DRpc 特性列表:
对等通信,对等Api高性能,非阻塞异步IO自定义Proto,,兼容http协议,自定义CodecHook点,插件系统,Push消息,session管理,Socket抽象,断线重连,过载保护,负载均衡,心跳机制,平滑重启...
DServer 特性列表:
快速构建,平滑重启,多进程支持,单/Multiple processes consistentPredefined command line,ctrl命令管理服务可观测,可控制,应用沙盒
DMicro Have built-in component:
- [x]
Registry服务注册 - [x]
Selector服务发现 - [x]
Eventbus事件总线 - [x]
Supervisor进程管理 - [ ]
Code gen代码生成 - [ ]
Tracing链路追踪 - [ ]
MetricsStatistics the alarm - [ ]
Broker限流熔断 - [ ]
OpenAPI文档自动生成
架构

设计理念
对 DMicro 框架的设计,From the beginning of the design is in the pursuit of flexibility,适应性.Under the premise that ensure the stability of the micro service,Pursuing the development of the project efficiency.
- 面向接口设计,保证代码稳定,Provide flexible customization.
- Abstract interface components,高内聚,低耦合.
- 分层设计,Step by step a top-down assembly,Conducive to stability and maintain.
- 高性能,高可用,低消耗.
- 对开发友好,封装复杂度.
- Provide a rich component and function,让开发专注业务.
Countless write DMicro Day and night,I remember that developed three principles:
Clarity(清晰)Simplicity(简单)Productivity(生产力)
无论工作,Open source projects or do,Should keep the three principles,养成良好的习惯.
面向接口设计
DMicro The principle of taking everything interface,Provide the framework unparalleled extensibility.
Below is a message sent by the circulation flow,可以看到,All function points are abstracted to interface,The realization of each function point provides different.

会话 Session
大多数的 Rpc Framework does not emphasize the session (session) 的概念,Because of its application scenario does not need to use the session (session). 那么 drpc Why need to abstract the session (session) 呢?
Endpoint融合了Client和Server, 需要提供相同的Api.服务端Need to客户端发送消息,And access to the client's response.服务端支持对多个客户端批量发送消息.- Asynchronous active disconnect
一个或多个会话. - Access to the session at the bottom of the
文件描述符, For performance tuning. - For each session can bind special
数据/属性.
Session Abstract the whole drpc Framework of the session,把 Socket,Message,Context All mixed together.开发者只需要对 session 进行操作,Can achieve most demand.
- 获取连接信息
- The life cycle of control connection (超时时间)
- Control of a single request life cycle (超时时间)
- 接收消息
- 发送消息
- Create a message context
- Binding session information (如用户信息)
- 断线重连
- Take the initiative to disconnect session.
- 健康检查
- Get connection is closed event
- As the session set separate id
Session Interface can be subdivided into 4 个 interface{}, 分别是 EarlySession,BaseSession,CtxSession,Session. Corresponding to the application of different life stages session (Session) Have different properties.
EarlySessionSaid just generate the session,尚未启动 goroutine Read the data of phase.BaseSessionOnly the most basic way,Used to close the connection when the plug-in parameters.CtxSessionIn the handler context transfer session object.SessionFull-featured session object.
正常情况下,Developers are used Session,CtxSession 这两个接口,其他 2 An interface is in the plug-in to use.
消息 Message
消息 Message 包含消息头 Header, 消息体 Body, Is the client and the server communication between entities.
Message interface{} Abstract to the operation of the communication entity.
Size消息的长度Transfer-Filter-PipelinePacket filtering data processing pipelineSeq序列号MType消息类型ServiceMethod资源标识符Meta消息的元数据BodyCodecThe body of the message encoding formatBody消息体

协议 Proto
协议是对消息Message Object serialization and reverse serialization,框架提供 Proto 接口.只需要实现该接口,Developers can customize in accordance with business requirements custom agreement,Thus promotes the flexibility of the frame.
接口的定义如下:
type Proto interface {
Version() (byte, string)
Pack(Message) error
Unpack(Message) error
}
Version()Return the agreement id 和名字,Two of the only version number.Pack对消息Message对象进行序列化.UnpackThe byte stream deserialization,生成一个消息Message对象.
At present, the framework has support Http,Json,Raw,Protobuf,JsonRpc 这 5 个协议.
RAW Agreement of the following:

Other agreements can reference code.
编码 Codec
As a general framework of,The supported protocols can have a variety of,The body of the codec can also have many kinds of. drpc 使用 Codec Interface to the body Body 进行编解码.
接口的定义如下:
type Codec interface {
ID() byte
Name() string
Marshal(interface{}) ([]byte, error)
Unmarshal([]byte, interface{}) error
}
IDReturn to weave Codec 的 idNameReturn to weave Codec 的名字,Name is for developers to more easily identify.MarshalTo encode the message contentUnmarshalTo decrypt the message content
At present, the framework has support Form,Json,plain,Protobuf,XML 这 5 A codec.
连接 Socket
Socket 扩展了 net.Conn, And abstract the interface,Convenient framework to the underlying network protocol integration.
Socket The interface part of Session 接口的功能,Session Some methods of interface call,Is actually a forwarding call Socket 中的方法.
The layered realization,让 Socket Have the ability to integrate other agreement.
TCP V4,TCP V6Unix SocketKCPQUIC
Support performance tuning of the connection in the.
SetKeepAliveOpen links to keep aliveSetKeepAlivePeriodLink to keep alive intervalSetReadBufferSet link to read buffer sizeSetWriteBufferFor a link to write buffer sizeSetNoDelay开启关闭 no delay 算法ControlFDSupport operation links of the original handle
有机的组合
前面讲到,DMicro Framework and all the interface,分层 + 接口的设计,让 DMicro Of a flexible efficient and accord with the actual situation of the business ability.
Next we want to talk about the basis of these skills.插件系统.
插件 Plugin
Plug-in system brought great extensibility and flexibility to framework,Is a soul of the framework module,有了它,The framework has infinite possibility.
What kind of plug-in system to be elegant?我能想到的有以下几点:
- Reasonable and rich
hook位置,Can cover the whole framework of life cycle,Through the communication of each link. - 每个
hookLocation into and out of the refs are carefully designed. - Each plug-in can use multiple
hook位置,每个hookPosition can be used by multiple plug-ins. - Design concise enough,优雅.Can be convenient for secondary development custom.
在 drpc 中,Throughout the hook with the Endpoint 的生命周期,Is it one of the most important aspects of the integral.
通过这些钩子 Hook 点,Given the plug-in infinite possibility.
组件
有了插件,Can through the combination of plug-ins,Write the comprehensive function of the component,The framework provides some of the built-in components,
服务端 Rpc Server客户端 Rpc Client服务注册 Registry服务发现 Selector事件总线 EventBus进程管理 Supervisor
即将提供:
链路追踪 TracingStatistics the alarm Metrics限流熔断 Broker.
限于篇幅的原因,The realization of the specific components,这里就不深入讲解,请关注后续的文章.
未来展望
如果把 DMicro Compared to life,Now the stage of growth is still at the teenager,Only completed the basic part of architecture design and components development.
The next direction is mainly to develop in the direction of ease of use and reliability.
易用性:
- Project performance tools
dmctl工具的开发,包括代码生成,The project structure generated,打包,Compile, etc. - 符合 openapi The development of document components of the definition of.
- More complete documentation and usage examples.
可靠性:
- 可观测性
- 链路追踪
- 指标信息
- 日志流
- 生产可用
- 测试用例的完善
- 代码覆盖率
- 性能调优
希望 DMicro Can growing under everybody's care and spur growth.
边栏推荐
- 如何模拟后台API调用场景,很细!
- iNFTnews | What can NFTs bring to the sports industry and fans?
- Understand the recommendation system in one article: Recall 06: Two-tower model - model structure, training method, the recall model is a late fusion feature, and the sorting model is an early fusion
- 如何看待自己的羞愧感
- Access Characteristics of Constructor under Inheritance Relationship
- 如何基于OpenVINO POT工具简单实现对模型的量化压缩
- lua learning
- Jincang database KingbaseES V8 GIS data migration solution (3. Data migration based on ArcGIS platform to KES)
- 编译预处理等细节
- 【C语言】详解栈和队列(定义、销毁、数据的操作)
猜你喜欢

How to simply implement the quantization and compression of the model based on the OpenVINO POT tool

Tree search (bintree)

在这个超连接的世界里,你的数据安全吗

海量服务实例动态化管理

2022-08-04:输入:去重数组arr,里面的数只包含0~9。limit,一个数字。 返回:要求比limit小的情况下,能够用arr拼出来的最大数字。 来自字节。

2022-08-04: Input: deduplicated array arr, the numbers in it only contain 0~9.limit, a number.Return: The maximum number that can be spelled out with arr if the requirement is smaller than limit.from

.Net C# Console Create a window using Win32 API

Jincang database KingbaseES V8 GIS data migration solution (3. Data migration based on ArcGIS platform to KES)

Live preview | 30 minutes started quickly!Look at credible distributed AI chain oar architectural design

shell statement to modify txt file or sh file
随机推荐
后期学习计划
C语言日记 9 if的3种语句
Short domain name bypass and xss related knowledge
Greenplum Database Fault Analysis - Can a Soft Connection Be Made to the Database Base Folder?
[ROS] (10) ROS Communication - Service Communication
力扣-相同的树
Unleashing the engine of technological innovation, Intel joins hands with ecological partners to promote the vigorous development of smart retail
[LeetCode Brush Questions] - Sum of Numbers topic (more topics to be added)
C language implements a simple number guessing game
C student management system Insert the student node at the specified location
Dotnet 6 Why does the network request not follow the change of the system network proxy and dynamically switch the proxy?
【解密】OpenSea免费创造的NFT都没上链竟能出现在我的钱包里?
海量服务实例动态化管理
The 2022 EdgeX China Challenge will be grandly opened on August 3
[Decryption] Can the NFTs created by OpenSea for free appear in my wallet without being chained?
02 【开发服务器 资源模块】
树形查找(二叉查找树)
CPDA|运营人如何从负基础学会数据分析(SQL)
【存储】曙光存储DS800-G35 ISCSI各映射LUN给服务器
【日常训练】1403. 非递增顺序的最小子序列