From f11dd97e8fdf26fb594691129f65e5328c444457 Mon Sep 17 00:00:00 2001 From: dxfeng10 Date: Mon, 18 Jan 2021 19:44:35 +0800 Subject: [PATCH] add flow statistic and rate limit config (#29) add flow statistic and rate limit config --- src/main/resources/application.yml | 9 +++++- .../we/stats/ratelimit/RateLimitTests.java | 8 ++--- .../ResourceRateLimitConfigServiceTests.java | 29 +++++-------------- 3 files changed, 19 insertions(+), 27 deletions(-) diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index bd38346..0722d74 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -85,4 +85,11 @@ stat: open: true send-log: # switch for push log data - open: true \ No newline at end of file + open: true +sched: + executors: 2 +flowControl: true +flow-stat-sched: + cron: 2/10 * * * * ? + dest: redis + queue: fizz_resource_access_stat \ No newline at end of file diff --git a/src/test/java/we/stats/ratelimit/RateLimitTests.java b/src/test/java/we/stats/ratelimit/RateLimitTests.java index f3be29a..9baf539 100644 --- a/src/test/java/we/stats/ratelimit/RateLimitTests.java +++ b/src/test/java/we/stats/ratelimit/RateLimitTests.java @@ -138,16 +138,16 @@ public class RateLimitTests { long incrTime = DateTimeUtils.toMillis("2021-01-08 21:28:42.000", Constants.DatetimePattern.DP23); boolean success = flowStat.incrRequest("resourceX", incrTime, Long.MAX_VALUE, Long.MAX_VALUE); - System.err.println("incrTime: " + incrTime + ", success: " + success); + // System.err.println("incrTime: " + incrTime + ", success: " + success); long startTimeSlot = DateTimeUtils.toMillis("2021-01-08 21:28:41.000", Constants.DatetimePattern.DP23); long endTimeSlot = DateTimeUtils.toMillis("2021-01-08 21:28:44.000", Constants.DatetimePattern.DP23); List resourceTimeWindowStats = flowStat.getResourceTimeWindowStats(null, startTimeSlot, endTimeSlot, 3); if (resourceTimeWindowStats == null || resourceTimeWindowStats.isEmpty()) { - System.err.println(toDP19(startTimeSlot) + " - " + toDP19(endTimeSlot) + " no flow stat data"); + // System.err.println(toDP19(startTimeSlot) + " - " + toDP19(endTimeSlot) + " no flow stat data"); } else { - System.err.println(JacksonUtils.writeValueAsString(resourceTimeWindowStats)); + // System.err.println(JacksonUtils.writeValueAsString(resourceTimeWindowStats)); } } @@ -160,6 +160,6 @@ public class RateLimitTests { FlowStat flowStat = new FlowStat(); boolean success = flowStat.incrRequest("resourceX", 1610181704000l, Long.MAX_VALUE, Long.MAX_VALUE); List r = flowStat.getResourceTimeWindowStats("resourceX", 1610181681000l, 1610181711000l, 30); - System.err.println("r: " + JacksonUtils.writeValueAsString(r)); + // System.err.println("r: " + JacksonUtils.writeValueAsString(r)); } } diff --git a/src/test/java/we/stats/ratelimit/ResourceRateLimitConfigServiceTests.java b/src/test/java/we/stats/ratelimit/ResourceRateLimitConfigServiceTests.java index 6a90aaa..8b59782 100644 --- a/src/test/java/we/stats/ratelimit/ResourceRateLimitConfigServiceTests.java +++ b/src/test/java/we/stats/ratelimit/ResourceRateLimitConfigServiceTests.java @@ -1,23 +1,17 @@ package we.stats.ratelimit; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.ReactiveStringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.event.annotation.BeforeTestMethod; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; -import org.springframework.util.ReflectionUtils; import we.redis.RedisProperties; import we.redis.RedisServerConfiguration; import we.redis.RedisTemplateConfiguration; import javax.annotation.Resource; -import java.lang.reflect.Field; - import static org.junit.jupiter.api.Assertions.assertEquals; /** @@ -46,27 +40,18 @@ public class ResourceRateLimitConfigServiceTests { // Field rt = ResourceRateLimitConfigService.class.getField("rt"); // ReflectionUtils.makeAccessible(rt); // ReflectionUtils.setField(rt, resourceRateLimitConfigService, reactiveStringRedisTemplate); - resourceRateLimitConfigService.setReactiveStringRedisTemplate( reactiveStringRedisTemplate); + resourceRateLimitConfigService.setReactiveStringRedisTemplate(reactiveStringRedisTemplate); } @Test void initTest() throws Throwable { - // System.err.println(redisProperties); - // System.err.println(stringRedisTemplate); - // System.err.println(reactiveStringRedisTemplate); - - // stringRedisTemplate.opsForValue().set("name", "F-22"); - // Thread.sleep(2000); - // String name = stringRedisTemplate.opsForValue().get("name"); - // assertEquals(name, "F-22"); - // System.err.println(name); - - // stringRedisTemplate.opsForHash().put("fizz_rate_limit", "2", "{\"concurrents\":100,\"enable\":1,\"id\":2,\"isDeleted\":0,\"resource\":\"service_default\",\"type\":2}"); - // resourceRateLimitConfigService.init(); - // ResourceRateLimitConfig resourceRateLimitConfig = resourceRateLimitConfigService.getResourceRateLimitConfig("service_default"); - // + stringRedisTemplate.opsForHash().put("fizz_rate_limit", "2", "{\"concurrents\":66,\"enable\":1,\"id\":2,\"isDeleted\":0,\"resource\":\"service_default\",\"type\":2}"); + resourceRateLimitConfigService.init(); + ResourceRateLimitConfig resourceRateLimitConfig = resourceRateLimitConfigService.getResourceRateLimitConfig("service_default"); + assertEquals(resourceRateLimitConfig.concurrents, 66); // System.err.println(resourceRateLimitConfig); - // System.err.println("init test end"); // Thread.currentThread().join(); + Thread.sleep(4000); + // System.err.println("init test end"); } }