一、背景介绍
sentinel介绍:https://github.com/alibaba/Sentinel
本篇我们介绍一下sentinel-transport模块,从源码工程的README.md里
# Sentinel Transport The Sentinel transport module provides basic interfaces about Sentinel monitoring API server and client (`CommandCenter` and `HeartbeatSender`) as well implementations using different libraries or protocols.
sentinel transport module 提供了客户端&服务端相关的基础接口和监控api,以及不同的库/协议。主要分为原生socket/netty,使用时客户端选择一个依赖就可以了。
sentinel transport模块内部用了很多spi的方法进行类加载。内部主要实现的功能:
1. 客户端向服务端同步心跳包
2. 向服务端发起注册,客户端接受command命令
二、主要流程(simple-http模块为例)
核心类SimpleHttpCommandCenter
class SimpleHttpCommandCenter implements CommandCenter
该类实现了CommandCenter接口
public interface CommandCenter { void beforeStart() throws Exception; void start() throws Exception; void stop() throws Exception; }
那 Sentinel里的怎么加载到SimpleHttpCommandCenter这个类的?从Sentinel定义的Env的静态类加载开始
/** * Sentinel Env. This class will trigger all initialization for Sentinel. * * <p> * NOTE: to prevent deadlocks, other classes' static code block or static field should * NEVER refer to this class. * </p> * * @author jialiang.linjl */ public class Env { public static final Sph sph = new CtSph(); static { // If init fails, the process will exit. InitExecutor.doInit(); } }
然后我们看下加载流程图
然后我们再细节的了解下SimpleHttpCommandCenter类的两个核心方法beforeStart和start方法的内部细节
ok。。这里就差不多了。
三、后续
感觉用流程图的方式表达源码会更清晰易懂一些,比纯贴代码好一些。推荐大家打开源码跟着流程一起看
后续再分享一篇sentinel的责任链流程,责任链模式是sentinel的核心流程。