集群限流源碼分析(一)
- 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è)請求類型注解