服务热线
135-6963-3175
通过继承AbstractRule抽象类实现Rule规则的自定义扩展
@Data public class RateLimitConfig extends AbstractRule implements Serializable { private static final long serialVersionUID = 8714167652947345144L; private boolean forUser;//扩展按用户限流 private boolean forIp;//按ip限流 private String serviceId; private boolean global;//是否全局规则 private String parseResource; private Long id; public RateLimitConfig() { super(); setLimitApp(RuleConstant.LIMIT_APP_DEFAULT); } public RateLimitConfig(String resourceName) { super(); setResource(resourceName); setLimitApp(RuleConstant.LIMIT_APP_DEFAULT); } /** * The threshold type of flow control (0: thread count, 1: QPS). */ private int grade = RuleConstant.FLOW_GRADE_QPS; /** * Flow control threshold count. */ private double count; /** * Flow control strategy based on invocation chain. * 直接(默认):接口达到限流条件时,开启限流 * 关联:当关联的资源达到限流条件时,开启限流,适合做应用让步。例如为一个查询的接口添加关联流控,关联资源为一个更新的 接口,当更新的接口达到阈值时,开启查询接口的限流,为更新接口让步服务器资源。 * 链路:当从某个接口过来的资源达到限流条件时,开启限流 * {@link RuleConstant#STRATEGY_DIRECT} for direct flow control (by origin); * {@link RuleConstant#STRATEGY_RELATE} for relevant flow control (with relevant resource); * {@link RuleConstant#STRATEGY_CHAIN} for chain flow control (by entrance resource). */ private int strategy = RuleConstant.STRATEGY_DIRECT; /** * 关联资源 * Reference resource in flow control with relevant resource or context. */ private String refResource; /** * Rate limiter control behavior. * 0. default(reject directly), 1. warm up, 2. rate limiter, 3. warm up + rate limiter */ private int controlBehavior = RuleConstant.CONTROL_BEHAVIOR_DEFAULT; private int warmUpPeriodSec = 10; /** * Max queueing time in rate limiter behavior. */ private int maxQueueingTimeMs = 500; private boolean clusterMode; /** * Flow rule config for cluster mode. */ private ClusterFlowConfig clusterConfig; /** * The traffic shaping (throttling) controller. */ @JsonIgnore private TrafficShapingController controller; public int getControlBehavior() { return controlBehavior; } public RateLimitConfig setControlBehavior(int controlBehavior) { this.controlBehavior = controlBehavior; return this; } public int getMaxQueueingTimeMs() { return maxQueueingTimeMs; } public RateLimitConfig setMaxQueueingTimeMs(int maxQueueingTimeMs) { this.maxQueueingTimeMs = maxQueueingTimeMs; return this; } RateLimitConfig setRater(TrafficShapingController rater) { this.controller = rater; return this; } TrafficShapingController getRater() { return controller; } public int getWarmUpPeriodSec() { return warmUpPeriodSec; } public RateLimitConfig setWarmUpPeriodSec(int warmUpPeriodSec) { this.warmUpPeriodSec = warmUpPeriodSec; return this; } public int getGrade() { return grade; } public RateLimitConfig setGrade(int grade) { this.grade = grade; return this; } public double getCount() { return count; } public RateLimitConfig setCount(double count) { this.count = count; return this; } public int getStrategy() { return strategy; } public RateLimitConfig setStrategy(int strategy) { this.strategy = strategy; return this; } public String getRefResource() { return refResource; } public RateLimitConfig setRefResource(String refResource) { this.refResource = refResource; return this; } public boolean isClusterMode() { return clusterMode; } public RateLimitConfig setClusterMode(boolean clusterMode) { this.clusterMode = clusterMode; return this; } public ClusterFlowConfig getClusterConfig() { return clusterConfig; } public RateLimitConfig setClusterConfig(ClusterFlowConfig clusterConfig) { this.clusterConfig = clusterConfig; return this; } @Override public boolean passCheck(Context context, DefaultNode node, int acquireCount, Object... args) { return true; } }
扩展完自定义字段后,然后在限流规则匹配时就可按相关维度进行判断,组装限流id。
待补充...