水下功夫做透,水上才能顺风顺水。

java 注解

   1. apollo配置以逗号分割的字符串,转化为list

@Value("#{'${huobi.actualAdjustSymbols:,}'.split(',')}")
private List<String> actualAdjustSymbols;

2.appollo配置转化为map
msg-type: "{1:'1',2:'1',3:'1',5:'1,2',15:'1',16:'1',17:'1,2',18:'1',19:'1',20:'1',21:'1',30:'1',31:'1',32:'1',33:'1',34:'1',35:'1',36:'1',37:'1',38:'1',39:'1',40:'1',41:'1',42:'1',43:'1',44:'1',45:'1',46:'1'}"

@Value("#{${etp.new-monitor.msg-type:{}}}")
private Map<Integer, String> monitorMsgType=new HashMap<>();

 

 3. @PostConstruct注解

@PostConstrct 注解的方法在启动的时候,执行这个方法,一般用于数据的常规化加载。

 4. 滑动窗口

        public Boolean ifAllowed(String key, Integer period, Integer maxCount){

            try{
                String script = "redis.call('zremrangeByScore', KEYS[1], 0, ARGV[1])\n" +
                    "local res = redis.call('zcard', KEYS[1])\n" +
                    "if res and (tonumber(res) < tonumber(ARGV[4])) then\n" +
                    "    redis.call('zadd', KEYS[1], ARGV[2], ARGV[3])\n" +
                    "    redis.call('expire',KEYS[1],ARGV[5]) \n" +
                    "    return 1\n" +
                    "else return 0 end\n";
                Long currentTime = HuobiDateUtil.currentTimeMillis();
                String args1 =  ""+(currentTime - period * 1000);//窗口初始时间
                String args2 = currentTime.toString();//scores
                String args3 = currentTime.toString();//value
                String args4 = maxCount.toString();  //最大次数
                String args5 = period.toString(); // 窗口时间
                List<String> keys = new ArrayList<>();
                keys.add(key);
                DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>(script,Long.class);
                RedisSerializer<String> stringRedisSerializer = newBizDbRedisTemplate.getStringSerializer();
                Long eval = (Long)newBizDbRedisTemplate.execute(redisScript, stringRedisSerializer,stringRedisSerializer,keys, args1, args2, args3, args4, args5);
                return eval.equals(1L);
            } catch (Exception ex) {
                log.error("[lua]滑动窗口限流失败", ex.getMessage());
            }
            return false;
        }

 








posted @ 2022-08-11 17:29  北方寒士  阅读(66)  评论(0编辑  收藏  举报