Plugin init after application context refreshed
This commit is contained in:
@@ -21,6 +21,9 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
@@ -31,7 +34,6 @@ import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import we.Fizz;
|
||||
import we.config.AggregateRedisConfig;
|
||||
import we.config.SystemConfig;
|
||||
import we.flume.clients.log4j2appender.LogService;
|
||||
@@ -50,18 +52,19 @@ import java.util.regex.Pattern;
|
||||
*/
|
||||
|
||||
@Service
|
||||
public class ApiConfigService {
|
||||
public class ApiConfigService implements ApplicationListener<ContextRefreshedEvent> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ApiConfigService.class);
|
||||
|
||||
// private static final String macs = "macsT";
|
||||
|
||||
public Map<String, ServiceConfig> serviceConfigMap = new HashMap<>(128);
|
||||
|
||||
private Map<Integer, ApiConfig> apiConfigMap = new HashMap<>(128);
|
||||
|
||||
private Map<String, String> pluginConfigMap = new HashMap<>(32);
|
||||
|
||||
@Resource
|
||||
private ReactiveWebServerApplicationContext applicationContext;
|
||||
|
||||
@Resource
|
||||
private ApiConfigServiceProperties apiConfigServiceProperties;
|
||||
|
||||
@@ -83,17 +86,10 @@ public class ApiConfigService {
|
||||
@Autowired(required = false)
|
||||
private CustomAuth customAuth;
|
||||
|
||||
|
||||
@PostConstruct
|
||||
public void init() throws Throwable {
|
||||
this.init(this::lsnApiConfigChange);
|
||||
Result<?> result = initPlugin();
|
||||
if (result.code == Result.FAIL) {
|
||||
throw new RuntimeException(result.msg, result.t);
|
||||
}
|
||||
result = lsnPluginConfigChange();
|
||||
if (result.code == Result.FAIL) {
|
||||
throw new RuntimeException(result.msg, result.t);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: no need like this
|
||||
@@ -203,7 +199,7 @@ public class ApiConfigService {
|
||||
.defaultIfEmpty(Collections.emptyList())
|
||||
.flatMap(
|
||||
es -> {
|
||||
if (Fizz.context != null) {
|
||||
if (!es.isEmpty()) {
|
||||
String json = null;
|
||||
try {
|
||||
for (Map.Entry<Object, Object> e : es) {
|
||||
@@ -213,11 +209,11 @@ public class ApiConfigService {
|
||||
String pluginConfig = (String) map.get("fixedConfig");
|
||||
String currentPluginConfig = pluginConfigMap.get(plugin);
|
||||
if (currentPluginConfig == null || !currentPluginConfig.equals(pluginConfig)) {
|
||||
if (Fizz.context.containsBean(plugin)) {
|
||||
FizzPluginFilter pluginFilter = (FizzPluginFilter) Fizz.context.getBean(plugin);
|
||||
if (applicationContext.containsBean(plugin)) {
|
||||
FizzPluginFilter pluginFilter = (FizzPluginFilter) applicationContext.getBean(plugin);
|
||||
pluginFilter.init(pluginConfig);
|
||||
pluginConfigMap.put(plugin, pluginConfig);
|
||||
log.info("init {} with {}", plugin, pluginConfig);
|
||||
log.info("init plugin {} with {}", plugin, pluginConfig);
|
||||
} else {
|
||||
log.warn("no {} bean", plugin);
|
||||
}
|
||||
@@ -228,6 +224,8 @@ public class ApiConfigService {
|
||||
result.msg = "init plugin error, config: " + json;
|
||||
result.t = t;
|
||||
}
|
||||
} else {
|
||||
log.info("no plugin init");
|
||||
}
|
||||
return Mono.empty();
|
||||
}
|
||||
@@ -264,7 +262,6 @@ public class ApiConfigService {
|
||||
)
|
||||
.doOnNext(
|
||||
msg -> {
|
||||
if (Fizz.context != null) {
|
||||
String message = msg.getMessage();
|
||||
try {
|
||||
HashMap<?, ?> map = JacksonUtils.readValue(message, HashMap.class);
|
||||
@@ -272,8 +269,8 @@ public class ApiConfigService {
|
||||
String pluginConfig = (String) map.get("fixedConfig");
|
||||
String currentPluginConfig = pluginConfigMap.get(plugin);
|
||||
if (currentPluginConfig == null || !currentPluginConfig.equals(pluginConfig)) {
|
||||
if (Fizz.context.containsBean(plugin)) {
|
||||
FizzPluginFilter pluginFilter = (FizzPluginFilter) Fizz.context.getBean(plugin);
|
||||
if (applicationContext.containsBean(plugin)) {
|
||||
FizzPluginFilter pluginFilter = (FizzPluginFilter) applicationContext.getBean(plugin);
|
||||
pluginFilter.init(pluginConfig);
|
||||
pluginConfigMap.put(plugin, pluginConfig);
|
||||
log.info("init {} with {} again", plugin, pluginConfig);
|
||||
@@ -285,7 +282,6 @@ public class ApiConfigService {
|
||||
log.error("message: {}", message, t);
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
.subscribe();
|
||||
return result;
|
||||
@@ -311,6 +307,18 @@ public class ApiConfigService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||
Result<?> result = initPlugin();
|
||||
if (result.code == Result.FAIL) {
|
||||
throw new RuntimeException(result.msg, result.t);
|
||||
}
|
||||
result = lsnPluginConfigChange();
|
||||
if (result.code == Result.FAIL) {
|
||||
throw new RuntimeException(result.msg, result.t);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
@@ -422,7 +430,7 @@ public class ApiConfigService {
|
||||
ServerHttpRequest req = exchange.getRequest();
|
||||
HttpHeaders hdrs = req.getHeaders();
|
||||
LogService.setBizId(WebUtils.getTraceId(exchange));
|
||||
return auth(exchange, WebUtils.getAppId(exchange), WebUtils.getOriginIp(exchange), getTimestamp(hdrs), getSign(hdrs),
|
||||
return auth(exchange, WebUtils.getAppId(exchange), WebUtils.getOriginIp(exchange), WebUtils.getTimestamp(exchange), WebUtils.getSign(exchange),
|
||||
WebUtils.getClientService(exchange), req.getMethod(), WebUtils.getClientReqPath(exchange));
|
||||
}
|
||||
|
||||
@@ -525,27 +533,7 @@ public class ApiConfigService {
|
||||
return Mono.just(r);
|
||||
}
|
||||
|
||||
private String getTimestamp(HttpHeaders reqHdrs) {
|
||||
List<String> tsHdrs = systemConfig.getTimestampHeaders();
|
||||
for (int i = 0; i < tsHdrs.size(); i++) {
|
||||
String v = reqHdrs.getFirst(tsHdrs.get(i));
|
||||
if (v != null) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getSign(HttpHeaders reqHdrs) {
|
||||
List<String> signHdrs = systemConfig.getSignHeaders();
|
||||
for (int i = 0; i < signHdrs.size(); i++) {
|
||||
String v = reqHdrs.getFirst(signHdrs.get(i));
|
||||
if (v != null) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static class ApiConfigPathPatternComparator implements Comparator<ApiConfig> {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user