Add schedule to refresh registry center config
This commit is contained in:
@@ -41,8 +41,17 @@ public abstract class ReflectionUtils extends org.springframework.util.Reflectio
|
||||
return getField(f, target);
|
||||
}
|
||||
|
||||
public static Object invokeMethod(String method, Object target, Object... args) {
|
||||
Method m = findMethod(target.getClass(), method);
|
||||
public static Object invokeMethod(String method, Object target) {
|
||||
return invokeMethod(method, target, null, null);
|
||||
}
|
||||
|
||||
public static Object invokeMethod(String method, Object target, Class<?>[] argTypes, Object[] args) {
|
||||
Method m = null;
|
||||
if (args == null) {
|
||||
m = findMethod(target.getClass(), method);
|
||||
} else {
|
||||
m = findMethod(target.getClass(), method, argTypes);
|
||||
}
|
||||
makeAccessible(m);
|
||||
return invokeMethod(m, target, args);
|
||||
}
|
||||
|
||||
@@ -21,13 +21,15 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import we.fizz.ConfigLoader;
|
||||
import we.plugin.auth.ApiConfigService;
|
||||
import we.plugin.auth.ApiConfig2appsService;
|
||||
import we.plugin.auth.ApiConfigService;
|
||||
import we.plugin.auth.AppService;
|
||||
import we.plugin.auth.GatewayGroupService;
|
||||
import we.proxy.RpcInstanceService;
|
||||
import we.service_registry.RegistryCenterService;
|
||||
import we.stats.degrade.DegradeRuleService;
|
||||
import we.stats.ratelimit.ResourceRateLimitConfigService;
|
||||
import we.util.Result;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@@ -75,6 +77,9 @@ public class RefreshLocalCacheConfig {
|
||||
@Resource
|
||||
private FizzMangerConfig fizzMangerConfig;
|
||||
|
||||
@Resource
|
||||
private RegistryCenterService registryCenterService;
|
||||
|
||||
// @Resource
|
||||
// private DegradeRuleService degradeRuleService;
|
||||
|
||||
@@ -153,6 +158,15 @@ public class RefreshLocalCacheConfig {
|
||||
// }
|
||||
// }
|
||||
|
||||
if (refreshLocalCacheConfigProperties.isRegistryCenterCacheRefreshEnabled()) {
|
||||
Result<?> result = registryCenterService.initRegistryCenter();
|
||||
if (result.code == Result.SUCC) {
|
||||
LOGGER.info("refresh registry center local cache done");
|
||||
} else {
|
||||
LOGGER.warn("fail to refresh registry center local cache: {}", result.msg, result.t);
|
||||
}
|
||||
}
|
||||
|
||||
fizzMangerConfig.updateMangerUrl();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,4 +54,7 @@ public class RefreshLocalCacheConfigProperties {
|
||||
|
||||
@Value("${refresh-local-cache.degrade-rule-enabled:false}")
|
||||
private boolean degradeRuleCacheRefreshEnabled;
|
||||
|
||||
@Value("${refresh-local-cache.registry-center-enabled:false}")
|
||||
private boolean registryCenterCacheRefreshEnabled;
|
||||
}
|
||||
|
||||
@@ -23,6 +23,8 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import we.util.JacksonUtils;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
@@ -86,6 +88,14 @@ public class RegistryCenter {
|
||||
return fizzServiceRegistration.getInstance(service);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
RegistryCenter that = (RegistryCenter) o;
|
||||
return id == that.id && type == that.type && clientConfigFormat == that.clientConfigFormat && Objects.equals(name, that.name) && Objects.equals(clientConfig, that.clientConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JacksonUtils.writeValueAsString(this);
|
||||
|
||||
@@ -67,7 +67,7 @@ public class RegistryCenterService implements ApplicationListener<ContextRefresh
|
||||
}
|
||||
}
|
||||
|
||||
private Result<?> initRegistryCenter() {
|
||||
public Result<?> initRegistryCenter() {
|
||||
Result<?> result = Result.succ();
|
||||
Flux<Map.Entry<Object, Object>> registryCenterEntries = rt.opsForHash().entries("fizz_registry");
|
||||
registryCenterEntries.collectList()
|
||||
@@ -80,19 +80,12 @@ public class RegistryCenterService implements ApplicationListener<ContextRefresh
|
||||
for (Map.Entry<Object, Object> e : es) {
|
||||
json = (String) e.getValue();
|
||||
RegistryCenter rc = JacksonUtils.readValue(json, RegistryCenter.class);
|
||||
registryCenterMap.put(rc.name, rc);
|
||||
rc.initFizzServiceRegistration(applicationContext);
|
||||
FizzServiceRegistration fizzServiceRegistration = rc.getFizzServiceRegistration();
|
||||
try {
|
||||
fizzServiceRegistration.register();
|
||||
LOGGER.info("success to init registry center {}", rc);
|
||||
} catch (Throwable throwable) {
|
||||
if (systemConfig.isFastFailWhenRegistryCenterDown()) {
|
||||
throw throwable;
|
||||
} else {
|
||||
LOGGER.warn("fail to init registry center {}, fast fail when registry center down is false, so continue", rc, throwable);
|
||||
fizzServiceRegistration.close();
|
||||
}
|
||||
RegistryCenter currentRegistryCenter = registryCenterMap.get(rc.name);
|
||||
if (currentRegistryCenter == null) {
|
||||
register(rc);
|
||||
} else if (!rc.equals(currentRegistryCenter)) {
|
||||
deregister(currentRegistryCenter);
|
||||
register(rc);
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
@@ -119,6 +112,55 @@ public class RegistryCenterService implements ApplicationListener<ContextRefresh
|
||||
return result;
|
||||
}
|
||||
|
||||
private void deregister(RegistryCenter rc) throws Throwable {
|
||||
FizzServiceRegistration fizzServiceRegistration = rc.getFizzServiceRegistration();
|
||||
Throwable error = null;
|
||||
try {
|
||||
fizzServiceRegistration.deregister();
|
||||
registryCenterMap.remove(rc.name);
|
||||
} catch (Throwable t) {
|
||||
LOGGER.error("deregister {}", rc, t);
|
||||
error = t;
|
||||
} finally {
|
||||
try {
|
||||
fizzServiceRegistration.close();
|
||||
} catch (Throwable t) {
|
||||
LOGGER.error("close {}", rc, t);
|
||||
error = t;
|
||||
}
|
||||
}
|
||||
if (error != null) {
|
||||
if (systemConfig.isFastFailWhenRegistryCenterDown()) {
|
||||
throw error;
|
||||
} else {
|
||||
LOGGER.warn("fail to deregister {}, fast fail when registry center down is false, so continue", rc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void register(RegistryCenter rc) throws Throwable {
|
||||
rc.initFizzServiceRegistration(applicationContext);
|
||||
FizzServiceRegistration fizzServiceRegistration = rc.getFizzServiceRegistration();
|
||||
try {
|
||||
fizzServiceRegistration.register();
|
||||
registryCenterMap.put(rc.name, rc);
|
||||
} catch (Throwable t0) {
|
||||
Throwable error = t0;
|
||||
LOGGER.error("register {}", rc, t0);
|
||||
try {
|
||||
fizzServiceRegistration.close();
|
||||
} catch (Throwable t1) {
|
||||
error = t1;
|
||||
LOGGER.error("close {}", rc, t1);
|
||||
}
|
||||
if (systemConfig.isFastFailWhenRegistryCenterDown()) {
|
||||
throw error;
|
||||
} else {
|
||||
LOGGER.warn("fail to register {}, fast fail when registry center down is false, so continue", rc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Result<?> lsnRegistryCenterChange() {
|
||||
Result<?> result = Result.succ();
|
||||
String channel = "fizz_registry_channel";
|
||||
@@ -141,29 +183,14 @@ public class RegistryCenterService implements ApplicationListener<ContextRefresh
|
||||
String message = msg.getMessage();
|
||||
try {
|
||||
RegistryCenter rc = JacksonUtils.readValue(message, RegistryCenter.class);
|
||||
RegistryCenter prev = null;
|
||||
RegistryCenter prev = registryCenterMap.get(rc.name);
|
||||
if (rc.isDeleted) {
|
||||
prev = registryCenterMap.remove(rc.name);
|
||||
LOGGER.info("remove registry center {}", prev);
|
||||
FizzServiceRegistration fizzServiceRegistration = prev.getFizzServiceRegistration();
|
||||
fizzServiceRegistration.deregister();
|
||||
fizzServiceRegistration.close();
|
||||
deregister(prev);
|
||||
} else {
|
||||
prev = registryCenterMap.put(rc.name, rc);
|
||||
LOGGER.info("update registry center {} with {}", prev, rc);
|
||||
if (prev != null) {
|
||||
FizzServiceRegistration fizzServiceRegistration = prev.getFizzServiceRegistration();
|
||||
fizzServiceRegistration.deregister();
|
||||
fizzServiceRegistration.close();
|
||||
}
|
||||
rc.initFizzServiceRegistration(applicationContext);
|
||||
FizzServiceRegistration fsr = rc.getFizzServiceRegistration();
|
||||
try {
|
||||
fsr.register();
|
||||
} catch (Throwable throwable) {
|
||||
LOGGER.error("fail to update registry center {}", rc, throwable);
|
||||
fsr.close();
|
||||
deregister(prev);
|
||||
}
|
||||
register(rc);
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
LOGGER.error("update registry center error, {}", message, t);
|
||||
|
||||
@@ -109,7 +109,8 @@ public class FizzNacosServiceRegistration extends FizzServiceRegistration {
|
||||
|
||||
Connection currentConnection = (Connection) ReflectionUtils.get(grpcClient, "currentConnection");
|
||||
if (currentConnection != null) {
|
||||
ReflectionUtils.invokeMethod("closeConnection", grpcClient, currentConnection);
|
||||
currentConnection.setAbandon(true);
|
||||
ReflectionUtils.invokeMethod("closeConnection", grpcClient, new Class[]{Connection.class}, new Object[]{currentConnection});
|
||||
LOGGER.info("close {} current connection {}", getId(), currentConnection.getConnectionId());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user