服务监控和治理

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

2020-03-21  本文已影响0人  橘子_好多灰

集群限流源码分析(一)

sentinel-cluster-common-default

源码内容如下:


image.png

可以看到该模块下主要定义公共的注解、接口公共方法。

codec包

数据流接口

该包下定义EntityWriter和EntityDecoder两个顶层接口。

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);
}
public interface EntityDecoder<S, T> {

    /**
     * Decode target object from source stream.
     *
     * @param source source stream
     * @return decoded target object
     */
    T decode(S source);
}
public interface RequestEntityWriter<E extends Request, T> extends EntityWriter<E, T> {

}

请求实体对象继承自Request

public interface RequestEntityDecoder<S, T extends Request> extends EntityDecoder<S, T> {

}
public interface ResponseEntityDecoder<S, T extends Response> extends EntityDecoder<S, T> {

}

响应实体对象继承自Response

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();
}

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;
    }
    //省略部分代码
}

FlowRequestData、ParamFlowRequestData
这两个对象表示正常限流请求对象,以及热点参数限流对象;源码如下:

public class FlowRequestData {
    //限流id,规则id
    private long flowId;
    private int count;
    private boolean priority;
}

public class ParamFlowRequestData {

    private long flowId;
    private int count;
    private Collection<Object> params;
}

response包

响应对象接口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();
}
public class ClusterResponse<T> implements Response {

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

    //响应的具体数据
    private T data;

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

FlowTokenResponseData:服务端响应的data

public class FlowTokenResponseData {

    //剩余次数
    private int remainingCount;
    //等待时间
    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
     */
     //发送请求
    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
     */
     //判断客户端是否准备好
    boolean isReady();
}

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() {}

其他

上一篇下一篇

猜你喜欢

热点阅读