Sentinel之集群限流源碼分析(一)

集群限流源碼分析(一)

  • sentinel-cluster-common-default: 公共模塊食侮,包含公共接口和實(shí)體
  • sentinel-cluster-client-default: 默認(rèn)集群流控 client 模塊,使用 Netty 進(jìn)行通信,提供接口方便序列化協(xié)議擴(kuò)展
  • sentinel-cluster-server-default: 默認(rèn)集群流控 server 模塊衡查,使用 Netty 進(jìn)行通信,提供接口方便序列化協(xié)議擴(kuò)展;
    同時(shí)提供擴(kuò)展接口對接規(guī)則判斷的具體實(shí)現(xiàn)(TokenService),默認(rèn)實(shí)現(xiàn)是復(fù)用 sentinel-core 的相關(guān)邏輯

sentinel-cluster-common-default

源碼內(nèi)容如下:


image.png

可以看到該模塊下主要定義公共的注解盯串、接口公共方法。

codec包

數(shù)據(jù)流接口

該包下定義EntityWriter和EntityDecoder兩個(gè)頂層接口戒良。

  • EntityWriter:用于對目標(biāo)數(shù)據(jù)的序列化
public interface EntityWriter<E, T> {

    /**
     * Write the provided entity to target stream.
     *
     * @param entity entity to publish
     * @param target the target stream
     */
    void writeTo(E entity, T target);
}
  • EntityDecoder:對響應(yīng)數(shù)據(jù)的反序列接口
public interface EntityDecoder<S, T> {

    /**
     * Decode target object from source stream.
     *
     * @param source source stream
     * @return decoded target object
     */
    T decode(S source);
}
  • RequestEntityWriter:客戶端請求的數(shù)據(jù)序列化處理接口体捏,繼承自EntityWriter;
public interface RequestEntityWriter<E extends Request, T> extends EntityWriter<E, T> {

}

請求實(shí)體對象繼承自Request

  • RequestEntityDecoder:客戶端對服務(wù)返回的數(shù)據(jù)反序列化接口,繼續(xù)自EntityDecoder:
public interface RequestEntityDecoder<S, T extends Request> extends EntityDecoder<S, T> {

}
  • ResponseEntityDecoder:服務(wù)端對請求客戶端的數(shù)據(jù)反序列化接口,繼續(xù)自EntityDecoder:
public interface ResponseEntityDecoder<S, T extends Response> extends EntityDecoder<S, T> {

}

響應(yīng)實(shí)體對象繼承自Response

  • ResponseEntityWriter:服務(wù)端對響應(yīng)數(shù)據(jù)的序列化接口几缭,繼承自EntityWriter河泳。
public interface ResponseEntityWriter<E extends Response, T> extends EntityWriter<E, T> {

}

request包

請求對象接口Request

public interface Request {

    /**
     * Get request type.
     *
     * @return request type
     */
    int getType();

    /**
     * Get request ID.
     *
     * @return unique request ID
     */
    int getId();
}
  • 該接口定義兩個(gè)方法:getType()getId();用于獲取請求的類型和Id;實(shí)際上就是ClusterRequest實(shí)體對象的get方法年栓。

ClusterRequest

public class ClusterRequest<T> implements Request {

    private int id;
    private int type;

    private T data;

    public ClusterRequest() {}

    public ClusterRequest(int id, int type, T data) {
        this.id = id;
        this.type = type;
        this.data = data;
    }
    //省略部分代碼
}
  • ClusterRequest:集群客戶端請求對象拆挥,對象實(shí)體是泛型T表示

FlowRequestData、ParamFlowRequestData
這兩個(gè)對象表示正常限流請求對象某抓,以及熱點(diǎn)參數(shù)限流對象纸兔;源碼如下:

public class FlowRequestData {
    //限流id,規(guī)則id
    private long flowId;
    private int count;
    private boolean priority;
}

public class ParamFlowRequestData {

    private long flowId;
    private int count;
    private Collection<Object> params;
}
  • 兩個(gè)主體具體用法后面分析搪缨;

response包

響應(yīng)對象接口Response

public interface Response {
    /**
     * Get response ID.
     *
     * @return response ID
     */
    int getId();

    /**
     * Get response type.
     *
     * @return response type
     */
    int getType();

    /**
     * Get response status.
     *
     * @return response status
     */
    int getStatus();
}
  • 和Request接口一樣食拜,定義了幾個(gè)獲取Id鸵熟,type,status的方法副编。
    ClusterResponse:集群響應(yīng)對象
public class ClusterResponse<T> implements Response {

    private int id;
    private int type;
    private int status;

    //響應(yīng)的具體數(shù)據(jù)
    private T data;

    public ClusterResponse() {}
    
    //省略部分代碼
}

FlowTokenResponseData:服務(wù)端響應(yīng)的data

public class FlowTokenResponseData {

    //剩余次數(shù)
    private int remainingCount;
    //等待時(shí)間
    private int waitInMs;
}

ClusterTransportClient接口

public interface ClusterTransportClient {

    /**
     * Start the client.
     *
     * @throws Exception some error occurred (e.g. initialization failed)
     */
     //啟動客戶端
    void start() throws Exception;

    /**
     * Stop the client.
     *
     * @throws Exception some error occurred (e.g. shutdown failed)
     */
     //停止客戶端
    void stop() throws Exception;

    /**
     * Send request to remote server and get response.
     *
     * @param request Sentinel cluster request
     * @return response from remote server
     * @throws Exception some error occurs
     */
     //發(fā)送請求
    ClusterResponse sendRequest(ClusterRequest request) throws Exception;

    /**
     * Check whether the client has been started and ready for sending requests.
     *
     * @return true if the client is ready to send requests, otherwise false
     */
     //判斷客戶端是否準(zhǔn)備好
    boolean isReady();
}
  • ClusterTransportClient客戶端傳送接口,定義4個(gè)方法如上流强;默認(rèn)實(shí)現(xiàn)類NettyTransportClient在client模塊中痹届,后面講解。

ConfigSupplierRegistry配置注冊

public final class ConfigSupplierRegistry {

    /**
     * The default namespace supplier provides appName as namespace.
     */
    private static final Supplier<String> DEFAULT_APP_NAME_SUPPLIER = new Supplier<String>() {
        @Override
        public String get() {
            return AppNameUtil.getAppName();
        }
    };
    /**
     * Registered namespace supplier.
     */
    private static Supplier<String> namespaceSupplier = DEFAULT_APP_NAME_SUPPLIER;

    /**
     * Get the registered namespace supplier.
     *
     * @return the registered namespace supplier
     */
    public static Supplier<String> getNamespaceSupplier() {
        return namespaceSupplier;
    }

    public static void setNamespaceSupplier(Supplier<String> namespaceSupplier) {
        AssertUtil.notNull(namespaceSupplier, "namespaceSupplier cannot be null");
        ConfigSupplierRegistry.namespaceSupplier = namespaceSupplier;
        RecordLog.info("[ConfigSupplierRegistry] New namespace supplier provided, current supplied: "
            + namespaceSupplier.get());
    }

    private ConfigSupplierRegistry() {}
  • 集群限流服務(wù)端作用設(shè)置或獲取打月,默認(rèn)是服務(wù)的name

其他

  • ClusterConstants:常量定義
  • ClusterErrorMessages:器群錯(cuò)誤消息定義
  • SentinelClusterException:定義了SentinelCluster異常
  • RequestType:定義了一個(gè)請求類型注解
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末队腐,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子奏篙,更是在濱河造成了極大的恐慌柴淘,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,273評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件秘通,死亡現(xiàn)場離奇詭異为严,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)肺稀,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評論 3 398
  • 文/潘曉璐 我一進(jìn)店門第股,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人话原,你說我怎么就攤上這事夕吻。” “怎么了繁仁?”我有些...
    開封第一講書人閱讀 167,709評論 0 360
  • 文/不壞的土叔 我叫張陵涉馅,是天一觀的道長。 經(jīng)常有香客問我黄虱,道長控漠,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,520評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮盐捷,結(jié)果婚禮上偶翅,老公的妹妹穿的比我還像新娘。我一直安慰自己碉渡,他們只是感情好聚谁,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,515評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著滞诺,像睡著了一般形导。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上习霹,一...
    開封第一講書人閱讀 52,158評論 1 308
  • 那天朵耕,我揣著相機(jī)與錄音,去河邊找鬼淋叶。 笑死阎曹,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的煞檩。 我是一名探鬼主播处嫌,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼斟湃!你這毒婦竟也來了熏迹?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤凝赛,失蹤者是張志新(化名)和其女友劉穎注暗,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體墓猎,經(jīng)...
    沈念sama閱讀 46,203評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡捆昏,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,287評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了陶衅。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片么介。...
    茶點(diǎn)故事閱讀 40,427評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡谷徙,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情熙侍,我是刑警寧澤弛针,帶...
    沈念sama閱讀 36,122評論 5 349
  • 正文 年R本政府宣布裳朋,位于F島的核電站匪蝙,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏门烂。R本人自食惡果不足惜乳愉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,801評論 3 333
  • 文/蒙蒙 一兄淫、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蔓姚,春花似錦捕虽、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至备闲,卻和暖如春晌端,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背恬砂。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工咧纠, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人泻骤。 一個(gè)月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓漆羔,卻偏偏與公主長得像,于是被迫代替她去往敵國和親瞪讼。 傳聞我的和親對象是個(gè)殘疾皇子钧椰,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,440評論 2 359

推薦閱讀更多精彩內(nèi)容