Fallback and go on when nacos server down

This commit is contained in:
hongqiaowei
2022-06-09 18:37:27 +08:00
parent 9533dd45c5
commit dc2b28f6fa
12 changed files with 229 additions and 44 deletions

View File

@@ -63,7 +63,7 @@ public abstract class PropertiesUtils {
public static void setBeanPropertyValue(Object bean, Properties properties, Map<String, Class<?>> propertyTypeHint) {
BeanWrapperImpl beanWrapper = new BeanWrapperImpl(bean);
for (String propertyName : properties.stringPropertyNames()) {
/*for (String propertyName : properties.stringPropertyNames()) {
if (beanWrapper.isWritableProperty(propertyName)) {
beanWrapper.setPropertyValue(propertyName, properties.get(propertyName));
} else if (propertyTypeHint != null) {
@@ -77,6 +77,24 @@ public abstract class PropertiesUtils {
}
}
}
}
}*/
properties.forEach(
(n, v) -> {
String propertyName = (String) n;
if (beanWrapper.isWritableProperty(propertyName)) {
beanWrapper.setPropertyValue(propertyName, properties.get(propertyName));
} else if (propertyTypeHint != null) {
int dotPos = propertyName.lastIndexOf(Consts.S.DOT);
if (dotPos > -1) {
String prefix = propertyName.substring(0, dotPos);
Class<?> aClass = propertyTypeHint.get(prefix);
if (aClass != null && Map.class.isAssignableFrom(aClass)) {
String newPropertyName = prefix + PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR + propertyName.substring(dotPos + 1) + PropertyAccessor.PROPERTY_KEY_SUFFIX_CHAR;
beanWrapper.setPropertyValue(newPropertyName, properties.get(propertyName));
}
}
}
}
);
}
}

View File

@@ -18,6 +18,7 @@
package we.util;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* @author hongqiaowei
@@ -39,4 +40,10 @@ public abstract class ReflectionUtils extends org.springframework.util.Reflectio
makeAccessible(f);
return getField(f, target);
}
public static Object invokeMethod(String method, Object target, Object... args) {
Method m = findMethod(target.getClass(), method);
makeAccessible(m);
return invokeMethod(m, target, args);
}
}