前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Cloud Gateway 结合配置中心限流

Spring Cloud Gateway 结合配置中心限流

作者头像
猿天地
发布2018-09-30 09:43:11
1.1K0
发布2018-09-30 09:43:11
举报
文章被收录于专栏:猿天地猿天地

前言

上篇文章《Spring Cloud Gateway 限流操作》我讲过复杂的限流场景可以通过扩展RedisRateLimiter来实现自己的限流策略。

假设你领导给你安排了一个任务,具体需求如下:

  • 针对具体的接口做限流
  • 不同接口限流的力度可以不同
  • 可以动态调整限流配置,实时生效

如果你接到上面的任务,你会怎么去设计+实现呢?

每个人看待问题的角度不同,自然思考出来的方案也不同,正所谓条条大路通罗马,能到达亩的地的路那就是一条好路。

如何分析需求

下面我给出我的实现方式,仅供各位参考,大牛请忽略。

具体问题具体分析,针对需求点,分别去做分析。

需求一 “如何针对具体的接口做限流” 这个在上篇文章中也有讲过,只需要让KeyResolver返回的是接口的URI即可,这样限流的维度那就是对这个接口进行限流。

需求二 “不同接口限流的力度可以不同” 这个通过配置的方式明显实现不了,配置中的replenishRate和burstCapacity都是配置死的,如果要做成动态的那么必须的自己通过扩展RedisRateLimiter来实现。

前提是必须有一个配置列表,这个配置列表就是每个接口对应的限流数值。有了这个配置我们就可以通过请求的接口获取这个接口对应的限流值。

需求三“可以动态调整限流配置,实时生效” 这个的话也比较容易,无论你是存文件,存数据库,存缓存只要每次都去读取,必然是实时生效的,但是性能问题我们不得不考虑啊。

存文件,读取文件,耗IO,主要是不方便修改 存数据库,可以通过web界面去修改,也可以直接改数据库,每次都要查询,性能不行 存分布式缓存(redis),性能比数据库有提高

对比下来肯定是缓存是最优的方案,还有更好的方案吗? 有,结合配置中心来做,我这边用自己的配置中心(https://github.com/yinjihuan/smconf)来讲解,换成其他的配置中心也是一样的思路。

配置中心的优点在于它本来就是用来存储配置的,配置在项目启动时加载完毕,当有修改时推送更新,每次读取都在本地对象中,性能好。

具体方案有了之后我们就可以开始撸代码了,但是你有想过这么多接口的限流值怎么初始化吗?手动一个个去加?

不同的服务维护的小组不同,当然也有可能是一个小组维护,从设计者的角度来思考,应该把设置的权利交给用户,交给我们的接口开发者,每个接口能够承受多少并发让用户来定,你的职责就是在网关进行限流。当然在公司中具体的限制量也不一定会由开发人员来定哈,这个得根据压测结果,做最好的调整。

话不多说-开始撸码

首先我们定义自己的RedisRateLimiter,复制源码稍微改造下即可, 这边只贴核心代码。

代码语言:javascript
复制
public class CustomRedisRateLimiter extends AbstractRateLimiter<CustomRedisRateLimiter.Config>
        implements ApplicationContextAware {

    public static final String CONFIGURATION_PROPERTY_NAME = "custom-redis-rate-limiter";
    public static final String REDIS_SCRIPT_NAME = "redisRequestRateLimiterScript";
    public static final String REMAINING_HEADER = "X-RateLimit-Remaining";
    public static final String REPLENISH_RATE_HEADER = "X-RateLimit-Replenish-Rate";
    public static final String BURST_CAPACITY_HEADER = "X-RateLimit-Burst-Capacity";

    public CustomRedisRateLimiter(ReactiveRedisTemplate<String, String> redisTemplate, RedisScript<List<Long>> script,
            Validator validator) {
        super(Config.class, CONFIGURATION_PROPERTY_NAME, validator);
        this.redisTemplate = redisTemplate;
        this.script = script;
        initialized.compareAndSet(false, true);
    }

    public CustomRedisRateLimiter(int defaultReplenishRate, int defaultBurstCapacity) {
        super(Config.class, CONFIGURATION_PROPERTY_NAME, null);
        this.defaultConfig = new Config().setReplenishRate(defaultReplenishRate).setBurstCapacity(defaultBurstCapacity);
    }

    // 限流配置
    private RateLimitConf rateLimitConf;

    @Override
    @SuppressWarnings("unchecked")
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        // 加载配置
        this.rateLimitConf = context.getBean(RateLimitConf.class);  
    }


    /**
     * This uses a basic token bucket algorithm and relies on the fact that
     * Redis scripts execute atomically. No other operations can run between
     * fetching the count and writing the new count.
     */
    @Override
    @SuppressWarnings("unchecked")
    public Mono<Response> isAllowed(String routeId, String id) {
        if (!this.initialized.get()) {
            throw new IllegalStateException("RedisRateLimiter is not initialized");
        }

        //Config routeConfig = getConfig().getOrDefault(routeId, defaultConfig);

        if (rateLimitConf == null) {
            throw new IllegalArgumentException("No Configuration found for route " + routeId);
        }
        Map<String,Integer> routeConfig = rateLimitConf.getLimitMap();

        // Key的格式:服务名称.接口URI.类型
        String replenishRateKey = routeId + "." + id + ".replenishRate";
        int replenishRate = routeConfig.get(replenishRateKey) == null ? routeConfig.get("default.replenishRate") : routeConfig.get(replenishRateKey);

        String burstCapacityKey = routeId + "." + id + ".burstCapacity";
        int burstCapacity = routeConfig.get(burstCapacityKey) == null ? routeConfig.get("default.burstCapacity") : routeConfig.get(burstCapacityKey);

        try {
            List<String> keys = getKeys(id);

            // The arguments to the LUA script. time() returns unixtime in
            // seconds.
            List<String> scriptArgs = Arrays.asList(replenishRate + "", burstCapacity + "",
                    Instant.now().getEpochSecond() + "", "1");
            // allowed, tokens_left = redis.eval(SCRIPT, keys, args)
            Flux<List<Long>> flux = this.redisTemplate.execute(this.script, keys, scriptArgs);
            // .log("redisratelimiter", Level.FINER);
            return flux.onErrorResume(throwable -> Flux.just(Arrays.asList(1L, -1L)))
                    .reduce(new ArrayList<Long>(), (longs, l) -> {
                        longs.addAll(l);
                        return longs;
                    }).map(results -> {
                        boolean allowed = results.get(0) == 1L;
                        Long tokensLeft = results.get(1);

                        Response response = new Response(allowed, getHeaders(replenishRate, burstCapacity, tokensLeft));

                        if (log.isDebugEnabled()) {
                            log.debug("response: " + response);
                        }
                        return response;
                    });
        } catch (Exception e) {
            /*
             * We don't want a hard dependency on Redis to allow traffic. Make
             * sure to set an alert so you know if this is happening too much.
             * Stripe's observed failure rate is 0.01%.
             */
            log.error("Error determining if user allowed from redis", e);
        }
        return Mono.just(new Response(true, getHeaders(replenishRate, burstCapacity, -1L)));
    }

    public HashMap<String, String> getHeaders(Integer replenishRate, Integer burstCapacity, Long tokensLeft) {
        HashMap<String, String> headers = new HashMap<>();
        headers.put(this.remainingHeader, tokensLeft.toString());
        headers.put(this.replenishRateHeader, String.valueOf(replenishRate));
        headers.put(this.burstCapacityHeader, String.valueOf(burstCapacity));
        return headers;
    }

}

需要在setApplicationContext中加载我们的配置类,配置类的定义如下:

代码语言:javascript
复制
@CxytianDiConf(system="fangjia-gateway")
public class RateLimitConf {
    // 限流配置
    @ConfField(value = "limitMap")
    private Map<String, Integer> limitMap = new HashMap<String, Integer>(){{
        put("default.replenishRate", 100);
        put("default.burstCapacity", 1000);
    }};
    public void setLimitMap(Map<String, Integer> limitMap) {
        this.limitMap = limitMap;
    }
    public Map<String, Integer> getLimitMap() {
        return limitMap;
    }
}

所有的接口对应的限流信息都在map中,有默认值,如果没有对应的配置就用默认的值对接口进行限流。

isAllowed方法中通过‘服务名称.接口URI.类型’组成一个Key, 通过这个Key去Map中获取对应的值。

类型的作用主要是用来区分replenishRate和burstCapacity两个值。

接下来就是配置CustomRedisRateLimiter:

代码语言:javascript
复制
@Bean
@Primary
public CustomRedisRateLimiter customRedisRateLimiter(
                  ReactiveRedisTemplate<String, String> redisTemplate,     
                  @Qualifier(CustomRedisRateLimiter.REDIS_SCRIPT_NAME)  RedisScript<List<Long>> redisScript,
                  Validator validator) {
    return new CustomRedisRateLimiter(redisTemplate, redisScript, validator);
}

网关这边的逻辑已经实现好了,接下来就是需要在具体的服务中自定义注解,然后将限流的参数初始化到我们的配置中心就可以了。

定义注解

代码语言:javascript
复制
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ApiRateLimit {

    /**
     * 速率
     * @return
     */
    int replenishRate() default 100;

    /**
     * 容积
     * @return
     */
    int burstCapacity() default 1000;

}

启动监听器,读取注解,初始化配置

代码语言:javascript
复制
/**
 * 初始化API网关需要进行并发限制的API
 * @author yinjihuan
 *
 */
public class InitGatewayApiLimitRateListener implements ApplicationListener<ApplicationReadyEvent> {

    // Controller包路径
    private String controllerPath;

    private RateLimitConf rateLimitConf;

    private ConfInit confInit;

    private String applicationName;

    public InitGatewayApiLimitRateListener(String controllerPath) {
        this.controllerPath = controllerPath;
    }

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        this.rateLimitConf = event.getApplicationContext().getBean(RateLimitConf.class);
        this.confInit = event.getApplicationContext().getBean(ConfInit.class);
        this.applicationName = event.getApplicationContext().getEnvironment().getProperty("spring.application.name");
        try {
            initLimitRateAPI();
        } catch (Exception e) {
            throw new RuntimeException("初始化需要进行并发限制的API异常", e);
        }
    }

    /**
     * 初始化需要进行并发限制的API
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void initLimitRateAPI() throws IOException, ClassNotFoundException {
        Map<String, Integer> limitMap = rateLimitConf.getLimitMap();
        ClasspathPackageScannerUtils scan = new ClasspathPackageScannerUtils(this.controllerPath);
        List<String> classList = scan.getFullyQualifiedClassNameList();
        for (String clazz : classList) {
            Class<?> clz = Class.forName(clazz);
            if (!clz.isAnnotationPresent(RestController.class)) {
                continue;
            }
            Method[] methods = clz.getDeclaredMethods();
            for (Method method : methods) {
                if (method.isAnnotationPresent(ApiRateLimit.class)) {
                    ApiRateLimit apiRateLimit = method.getAnnotation(ApiRateLimit.class);
                    String replenishRateKey = applicationName + "." + getApiUri(clz, method) + ".replenishRate";
                    String burstCapacityKey = applicationName + "." + getApiUri(clz, method) + ".burstCapacity";
                    limitMap.put(replenishRateKey, apiRateLimit.replenishRate());
                    limitMap.put(burstCapacityKey, apiRateLimit.burstCapacity());
                }
            }
        }
        rateLimitConf.setLimitMap(limitMap);
        // 初始化值到配置中心
        confInit.init(rateLimitConf);
    }

     private String getApiUri(Class<?> clz, Method method) {
            StringBuilder uri = new StringBuilder();
            uri.append(clz.getAnnotation(RequestMapping.class).value()[0]);
            if (method.isAnnotationPresent(GetMapping.class)) {
                uri.append(method.getAnnotation(GetMapping.class).value()[0]);
            } else if (method.isAnnotationPresent(PostMapping.class)) {
                uri.append(method.getAnnotation(PostMapping.class).value()[0]);
            } else if (method.isAnnotationPresent(RequestMapping.class)) {
                uri.append(method.getAnnotation(RequestMapping.class).value()[0]);
            }
            return uri.toString();
     }
}

配置监听器

代码语言:javascript
复制
SpringApplication application = new SpringApplication(FshHouseServiceApplication.class);
application.addListeners(new InitGatewayApiLimitRateListener("com.fangjia.fsh.house.controller"));
context = application.run(args);

最后使用就很简单了,只需要增加注解就可以了

代码语言:javascript
复制
@ApiRateLimit(replenishRate=10, burstCapacity=100)
@GetMapping("/data")
public HouseInfo getData(@RequestParam("name") String name) {
    return new HouseInfo(1L, "上海", "虹口", "东体小区");
}

我这边只是给大家提供一种去实现的思路,也许大家还有更好的方案。

我觉得只要不让每个开发都去关心这种非业务性质的功能,那就可以了,都在框架层面处理掉。当然实现原理可以跟大家分享下,会用很好,既会用又了解原理那就更好了。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-08-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 猿天地 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 如何分析需求
  • 话不多说-开始撸码
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档