[ISSUE #11231]Optimize the handleSpringBinder method in PropertiesUtil. (#11240)

This commit is contained in:
阿魁 2023-10-11 09:23:31 +08:00 committed by GitHub
parent 7911eb03db
commit d85e3f7f31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 42 deletions

View File

@ -107,12 +107,14 @@ public class AuthConfigs extends Subscriber<ServerConfigChangeEvent> {
try {
Map<String, Properties> newProperties = new HashMap<>(1);
Properties properties = PropertiesUtil.getPropertiesWithPrefix(EnvUtil.getEnvironment(), PREFIX);
for (String each : properties.stringPropertyNames()) {
int typeIndex = each.indexOf('.');
String type = each.substring(0, typeIndex);
String subKey = each.substring(typeIndex + 1);
newProperties.computeIfAbsent(type, key -> new Properties())
.setProperty(subKey, properties.getProperty(each));
if (properties != null) {
for (String each : properties.stringPropertyNames()) {
int typeIndex = each.indexOf('.');
String type = each.substring(0, typeIndex);
String subKey = each.substring(typeIndex + 1);
newProperties.computeIfAbsent(type, key -> new Properties())
.setProperty(subKey, properties.getProperty(each));
}
}
authPluginProperties = newProperties;
} catch (Exception e) {
@ -177,8 +179,8 @@ public class AuthConfigs extends Subscriber<ServerConfigChangeEvent> {
cachingEnabled = EnvUtil.getProperty(Constants.Auth.NACOS_CORE_AUTH_CACHING_ENABLED, Boolean.class, true);
serverIdentityKey = EnvUtil.getProperty(Constants.Auth.NACOS_CORE_AUTH_SERVER_IDENTITY_KEY, "");
serverIdentityValue = EnvUtil.getProperty(Constants.Auth.NACOS_CORE_AUTH_SERVER_IDENTITY_VALUE, "");
enableUserAgentAuthWhite = EnvUtil
.getProperty(Constants.Auth.NACOS_CORE_AUTH_ENABLE_USER_AGENT_AUTH_WHITE, Boolean.class, false);
enableUserAgentAuthWhite = EnvUtil.getProperty(Constants.Auth.NACOS_CORE_AUTH_ENABLE_USER_AGENT_AUTH_WHITE,
Boolean.class, false);
nacosAuthSystemType = EnvUtil.getProperty(Constants.Auth.NACOS_CORE_AUTH_SYSTEM_TYPE, "");
refreshPluginProperties();
ModuleStateHolder.getInstance().getModuleState(AuthModuleStateBuilder.AUTH_MODULE)

View File

@ -54,12 +54,14 @@ public class ConfigChangeConfigs extends Subscriber<ServerConfigChangeEvent> {
try {
Map<String, Properties> newProperties = new HashMap<>(3);
Properties properties = PropertiesUtil.getPropertiesWithPrefix(EnvUtil.getEnvironment(), PREFIX);
for (String each : properties.stringPropertyNames()) {
int typeIndex = each.indexOf('.');
String type = each.substring(0, typeIndex);
String subKey = each.substring(typeIndex + 1);
newProperties.computeIfAbsent(type, key -> new Properties())
.setProperty(subKey, properties.getProperty(each));
if (properties != null) {
for (String each : properties.stringPropertyNames()) {
int typeIndex = each.indexOf('.');
String type = each.substring(0, typeIndex);
String subKey = each.substring(typeIndex + 1);
newProperties.computeIfAbsent(type, key -> new Properties())
.setProperty(subKey, properties.getProperty(each));
}
}
configPluginProperties = newProperties;
} catch (Exception e) {

View File

@ -22,8 +22,6 @@ import com.alibaba.nacos.core.utils.Loggers;
import com.alibaba.nacos.sys.env.EnvUtil;
import com.alibaba.nacos.sys.utils.PropertiesUtil;
import java.lang.reflect.InvocationTargetException;
/**
* Grpc config.
*
@ -41,11 +39,9 @@ public class RpcServerTlsConfig extends TlsConfig {
public static synchronized RpcServerTlsConfig getInstance() {
if (null == instance) {
try {
instance = PropertiesUtil
.handleSpringBinder(EnvUtil.getEnvironment(), PREFIX, RpcServerTlsConfig.class);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) {
Loggers.REMOTE.warn("TLS config bind failed, use default value", e);
instance = PropertiesUtil.handleSpringBinder(EnvUtil.getEnvironment(), PREFIX, RpcServerTlsConfig.class);
if (instance == null) {
Loggers.REMOTE.debug("TLS configuration is empty, use default value");
instance = new RpcServerTlsConfig();
}
}

View File

@ -16,10 +16,10 @@
package com.alibaba.nacos.sys.utils;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.env.Environment;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Properties;
@ -30,13 +30,11 @@ import java.util.Properties;
*/
public class PropertiesUtil {
public static Properties getPropertiesWithPrefix(Environment environment, String prefix)
throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
public static Properties getPropertiesWithPrefix(Environment environment, String prefix) {
return handleSpringBinder(environment, prefix, Properties.class);
}
public static Map<String, Object> getPropertiesWithPrefixForMap(Environment environment, String prefix)
throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
public static Map<String, Object> getPropertiesWithPrefixForMap(Environment environment, String prefix) {
return handleSpringBinder(environment, prefix, Map.class);
}
@ -49,16 +47,8 @@ public class PropertiesUtil {
* @param <T> target class
* @return binder object
*/
@SuppressWarnings("unchecked")
public static <T> T handleSpringBinder(Environment environment, String prefix, Class<T> targetClass)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, ClassNotFoundException {
Class<?> binderClass = Class.forName("org.springframework.boot.context.properties.bind.Binder");
Method getMethod = binderClass.getDeclaredMethod("get", Environment.class);
Method bindMethod = binderClass.getDeclaredMethod("bind", String.class, Class.class);
Object binderObject = getMethod.invoke(null, environment);
public static <T> T handleSpringBinder(Environment environment, String prefix, Class<T> targetClass) {
String prefixParam = prefix.endsWith(".") ? prefix.substring(0, prefix.length() - 1) : prefix;
Object bindResultObject = bindMethod.invoke(binderObject, prefixParam, targetClass);
Method resultGetMethod = bindResultObject.getClass().getDeclaredMethod("get");
return (T) resultGetMethod.invoke(bindResultObject);
return Binder.get(environment).bind(prefixParam, Bindable.of(targetClass)).orElse(null);
}
}

View File

@ -24,7 +24,6 @@ import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Properties;
@ -40,8 +39,7 @@ public class PropertiesUtilTest {
@Test
@SuppressWarnings("unchecked")
public void testGetPropertiesWithPrefixForMap()
throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
public void testGetPropertiesWithPrefixForMap() {
Map<String, Object> actual = PropertiesUtil.getPropertiesWithPrefixForMap(environment, "nacos.prefix");
assertEquals(3, actual.size());
for (Map.Entry<String, Object> entry : actual.entrySet()) {
@ -64,9 +62,14 @@ public class PropertiesUtilTest {
}
@Test
public void testGetPropertiesWithPrefix()
throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
public void testGetPropertiesWithPrefix() {
Properties actual = PropertiesUtil.getPropertiesWithPrefix(environment, "nacos.prefix");
assertEquals(3, actual.size());
}
@Test
public void testHandleSpringBinder() {
Map properties = PropertiesUtil.handleSpringBinder(environment, "nacos.prefix", Map.class);
assertEquals(3, properties.size());
}
}