update: add ApiConifg2appsServiceTests
This commit is contained in:
@@ -21,17 +21,17 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
|
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
import we.config.AggregateRedisConfig;
|
import we.config.AggregateRedisConfig;
|
||||||
import we.flume.clients.log4j2appender.LogService;
|
import we.flume.clients.log4j2appender.LogService;
|
||||||
import we.util.Constants;
|
import we.util.Constants;
|
||||||
import we.util.JacksonUtils;
|
import we.util.JacksonUtils;
|
||||||
|
import we.util.ReactorUtils;
|
||||||
|
import we.util.ThreadContext;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.HashMap;
|
import java.util.*;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author hongqiaowei
|
* @author hongqiaowei
|
||||||
@@ -40,9 +40,13 @@ import java.util.Set;
|
|||||||
@Service
|
@Service
|
||||||
public class ApiConifg2appsService {
|
public class ApiConifg2appsService {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(ApiConifg2appsService.class);
|
private static final Logger log = LoggerFactory.getLogger(ApiConifg2appsService.class);
|
||||||
|
|
||||||
private static final String fizzApiConfigAppChannel = "fizz_api_config_app_channel";
|
private static final String fizzApiConfigAppSetSize = "fizz_api_config_app_set_size";
|
||||||
|
|
||||||
|
private static final String fizzApiConfigAppKeyPrefix = "fizz_api_config_app:";
|
||||||
|
|
||||||
|
private static final String fizzApiConfigAppChannel = "fizz_api_config_app_channel";
|
||||||
|
|
||||||
private Map<Integer/* api config id */, Set<String/* app */>> apiConfig2appsMap = new HashMap<>(128);
|
private Map<Integer/* api config id */, Set<String/* app */>> apiConfig2appsMap = new HashMap<>(128);
|
||||||
|
|
||||||
@@ -51,6 +55,90 @@ public class ApiConifg2appsService {
|
|||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() throws Throwable {
|
public void init() throws Throwable {
|
||||||
|
rt.opsForHash().entries(fizzApiConfigAppSetSize)
|
||||||
|
.reduce(
|
||||||
|
new ArrayList<Map.Entry<Object, Object>>(),
|
||||||
|
(collector, e) -> {
|
||||||
|
collector.add(e);
|
||||||
|
return collector;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.map(
|
||||||
|
es -> {
|
||||||
|
log(es);
|
||||||
|
Mono initiateFlux = ReactorUtils.getInitiateMono();
|
||||||
|
for (Map.Entry<Object, Object> e : es) {
|
||||||
|
Integer apiConfigId = Integer.parseInt( (String) e.getKey() );
|
||||||
|
int appSetCount = Integer.parseInt( (String) e.getValue() );
|
||||||
|
for (int i = 0; i < appSetCount; i++) {
|
||||||
|
int iFinal = i;
|
||||||
|
initiateFlux = initiateFlux.flatMap(
|
||||||
|
o -> {
|
||||||
|
return
|
||||||
|
rt.opsForSet().members(fizzApiConfigAppKeyPrefix + apiConfigId + '_' + iFinal)
|
||||||
|
.reduce(
|
||||||
|
new ArrayList<String>(),
|
||||||
|
(collector, a) -> {
|
||||||
|
collector.add(a);
|
||||||
|
return collector;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.map(
|
||||||
|
as -> {
|
||||||
|
save(apiConfigId, as);
|
||||||
|
return ReactorUtils.NULL;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return initiateFlux;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.subscribe(
|
||||||
|
m -> {
|
||||||
|
m.subscribe(
|
||||||
|
e -> {
|
||||||
|
lsnChannel();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void log(ArrayList<Map.Entry<Object, Object>> es) {
|
||||||
|
StringBuilder b = ThreadContext.getStringBuilder();
|
||||||
|
b.append(fizzApiConfigAppSetSize).append('\n');
|
||||||
|
for (Map.Entry<Object, Object> e : es) {
|
||||||
|
String key = (String) e.getKey();
|
||||||
|
String value = (String) e.getValue();
|
||||||
|
b.append(key).append(":").append(value).append(' ');
|
||||||
|
}
|
||||||
|
log.info(b.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void save(Integer apiConfigId, ArrayList<String> as) {
|
||||||
|
Set<String> appSet = apiConfig2appsMap.get(apiConfigId);
|
||||||
|
if (appSet == null) {
|
||||||
|
appSet = new HashSet<>();
|
||||||
|
apiConfig2appsMap.put(apiConfigId, appSet);
|
||||||
|
}
|
||||||
|
appSet.addAll(as);
|
||||||
|
log(apiConfigId, as);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void log(Integer apiConfigId, ArrayList<String> apps) {
|
||||||
|
StringBuilder b = ThreadContext.getStringBuilder();
|
||||||
|
b.append(apiConfigId).append(" add: ");
|
||||||
|
for (String a : apps) {
|
||||||
|
b.append(a).append(' ');
|
||||||
|
}
|
||||||
|
log.info(b.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void lsnChannel() {
|
||||||
rt.listenToChannel(fizzApiConfigAppChannel)
|
rt.listenToChannel(fizzApiConfigAppChannel)
|
||||||
.doOnError(
|
.doOnError(
|
||||||
t -> {
|
t -> {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
package we.util;
|
package we.util;
|
||||||
|
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,13 +26,17 @@ import reactor.core.publisher.Mono;
|
|||||||
|
|
||||||
public interface ReactorUtils {
|
public interface ReactorUtils {
|
||||||
|
|
||||||
static final Object OBJ = new Object();
|
static final Object OBJ = new Object();
|
||||||
|
|
||||||
static final Object NULL = OBJ;
|
static final Object NULL = OBJ;
|
||||||
|
|
||||||
static final Mono<Object> INITIATE = Mono.just(NULL);
|
static final Throwable EMPTY_THROWABLE = new Throwable(null, null, false, false) {}; // XXX
|
||||||
|
|
||||||
static final Mono<Object> EMPTY_ASYNC_TASK = INITIATE;
|
static Mono getInitiateMono() {
|
||||||
|
return Mono.just(OBJ);
|
||||||
|
}
|
||||||
|
|
||||||
static final Throwable EMPTY_THROWABLE = new Throwable(null, null, false, false) {};
|
static Flux getInitiateFlux() {
|
||||||
|
return Flux.just(OBJ);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,65 @@
|
|||||||
package we.plugin.auth;
|
package we.plugin.auth;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
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.junit.jupiter.SpringJUnitConfig;
|
||||||
|
import we.redis.RedisProperties;
|
||||||
|
import we.redis.RedisServerConfiguration;
|
||||||
|
import we.redis.RedisTemplateConfiguration;
|
||||||
|
import we.stats.ratelimit.ResourceRateLimitConfigService;
|
||||||
import we.util.JacksonUtils;
|
import we.util.JacksonUtils;
|
||||||
|
import we.util.ReflectionUtils;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author hongqiaowei
|
* @author hongqiaowei
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@TestPropertySource("/application.properties")
|
||||||
|
@SpringJUnitConfig(classes = {RedisProperties.class, RedisTemplateConfiguration.class, RedisServerConfiguration.class})
|
||||||
public class ApiConifg2appsServiceTests {
|
public class ApiConifg2appsServiceTests {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
StringRedisTemplate stringRedisTemplate;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
ReactiveStringRedisTemplate reactiveStringRedisTemplate;
|
||||||
|
|
||||||
|
ApiConifg2appsService apiConifg2appsService;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void beforeEach() throws NoSuchFieldException {
|
||||||
|
apiConifg2appsService = new ApiConifg2appsService();
|
||||||
|
ReflectionUtils.set(apiConifg2appsService, "rt", reactiveStringRedisTemplate);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test() {
|
void initTest() throws Throwable {
|
||||||
ApiConfig2apps apiConfig2apps = new ApiConfig2apps();
|
Map<String, String> apiConfigId2appSetCountMap = new HashMap<>();
|
||||||
apiConfig2apps.id = 0;
|
apiConfigId2appSetCountMap.put("60", "1");
|
||||||
apiConfig2apps.isDeleted = 0;
|
apiConfigId2appSetCountMap.put("61", "2");
|
||||||
apiConfig2apps.apps = Arrays.asList("app0", "app1");
|
stringRedisTemplate.opsForHash().putAll("fizz_api_config_app_set_size", apiConfigId2appSetCountMap);
|
||||||
String apiConfig2appsJson = JacksonUtils.writeValueAsString(apiConfig2apps);
|
|
||||||
// System.err.println(apiConfig2appsJson);
|
stringRedisTemplate.opsForSet().add("fizz_api_config_app:60_0", "app_a");
|
||||||
|
stringRedisTemplate.opsForSet().add("fizz_api_config_app:61_0", "app_b", "app_c");
|
||||||
|
stringRedisTemplate.opsForSet().add("fizz_api_config_app:61_1", "app_d");
|
||||||
|
|
||||||
|
apiConifg2appsService.init();
|
||||||
|
Thread.sleep(4000);
|
||||||
|
|
||||||
|
Map<Integer, Set<String>> apiConfig2appsMap = apiConifg2appsService.getApiConfig2appsMap();
|
||||||
|
System.err.println("r: " + JacksonUtils.writeValueAsString(apiConfig2appsMap));
|
||||||
|
assertTrue(apiConfig2appsMap.get(61).contains("app_c"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
18
src/test/resources/log4j2-test.xml
Normal file
18
src/test/resources/log4j2-test.xml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<Configuration status="info">
|
||||||
|
<properties>
|
||||||
|
<property name="APP_NAME">fizz-gateway</property>
|
||||||
|
</properties>
|
||||||
|
<Appenders>
|
||||||
|
<Console name="Console" target="SYSTEM_OUT">
|
||||||
|
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %level %logger{36} - %X{traceId} %msg%n" />
|
||||||
|
</Console>
|
||||||
|
</Appenders>
|
||||||
|
<Loggers>
|
||||||
|
<Root level="warn">
|
||||||
|
<AppenderRef ref="Console" />
|
||||||
|
</Root>
|
||||||
|
<Logger name="we" level="DEBUG"/>
|
||||||
|
</Loggers>
|
||||||
|
</Configuration>
|
||||||
Reference in New Issue
Block a user