From 8c9d0decb72c576e24b1e88ad3a8c2977311084c Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Wed, 7 Sep 2022 20:09:09 +0800 Subject: [PATCH] refactor:optimize project and code. (#571) --- CHANGELOG.md | 1 + .../MetadataTransferAutoConfiguration.java | 2 +- ...MetadataTransferAutoConfigurationTest.java | 2 +- ...larisConfigBootstrapAutoConfiguration.java | 1 + .../adapter/PolarisConfigFileLocator.java | 4 +- .../adapter/PolarisConfigFilePuller.java | 97 ++++++++++++------- .../PolarisConfigPropertyRefresher.java | 4 +- .../ReflectRefreshTypeCondition.java | 1 - .../configdata/PolarisConfigDataLoader.java | 18 ++-- .../PolarisConfigDataLocationResolver.java | 18 ++-- .../polaris/config/enums/RefreshBehavior.java | 1 - .../polaris/config/enums/RefreshType.java | 1 + .../spring/annotation/PolarisProcessor.java | 17 ++++ .../annotation/SpringValueProcessor.java | 49 +++++++--- .../config/spring/property/SpringValue.java | 27 +----- .../SpringValueDefinitionProcessor.java | 25 ++--- .../spring/property/SpringValueRegistry.java | 10 +- .../adapter/PolarisConfigFilePullerTest.java | 13 ++- ...arisPropertiesSourceAutoRefresherTest.java | 2 +- ...yPropertiesBootstrapAutoConfiguration.java | 1 + ...sRefreshApplicationReadyEventListener.java | 1 + .../refresh/PolarisRefreshConfiguration.java | 1 + .../PolarisServiceStatusChangeListener.java | 1 + .../registry/PolarisServiceRegistry.java | 3 +- .../ratelimit/constant/RateLimitConstant.java | 5 +- .../PolarisRateLimitRuleEndpoint.java | 2 +- .../filter/QuotaCheckReactiveFilter.java | 8 +- .../filter/QuotaCheckServletFilter.java | 8 +- .../ratelimit/utils/QuotaCheckUtils.java | 1 + .../router/RouterRuleLabelResolverTest.java | 1 - .../common/constant/RouterConstants.java | 4 + .../src/main/resources/bootstrap.yml | 2 + .../cloud/polaris/context/ModifyAddress.java | 13 +-- .../PolarisContextAutoConfiguration.java | 4 +- .../PolarisLoggingApplicationListener.java | 3 +- .../loadbalancer/PolarisLoadBalancerTest.java | 6 +- .../feign/EnhancedFeignBeanPostProcessor.java | 2 +- 37 files changed, 200 insertions(+), 159 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6d21dbb..f324038b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,3 +33,4 @@ - [Report the labels in request when report the result of invocation by Feign](https://github.com/Tencent/spring-cloud-tencent/pull/556) - [fix:fix heartbeat interval different configuration from polaris-java SDK.](https://github.com/Tencent/spring-cloud-tencent/pull/559) - [Optimize: optimize report call result for restTemplate.](https://github.com/Tencent/spring-cloud-tencent/pull/565) +- [refactor:optimize project and code.](https://github.com/Tencent/spring-cloud-tencent/pull/571) diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfiguration.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfiguration.java index ed5d9c8f..7b2aa8f9 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfiguration.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfiguration.java @@ -132,7 +132,7 @@ public class MetadataTransferAutoConfiguration { } @Bean - public SmartInitializingSingleton addEncodeTransferMedataInterceptorForRestTemplate(EncodeTransferMedataRestTemplateInterceptor interceptor) { + public SmartInitializingSingleton addEncodeTransferMetadataInterceptorForRestTemplate(EncodeTransferMedataRestTemplateInterceptor interceptor) { return () -> restTemplates.forEach(restTemplate -> { List list = new ArrayList<>(restTemplate.getInterceptors()); list.add(interceptor); diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfigurationTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfigurationTest.java index f95036fe..2ef9a6b1 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfigurationTest.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/config/MetadataTransferAutoConfigurationTest.java @@ -85,7 +85,7 @@ public class MetadataTransferAutoConfigurationTest { .stream() .filter(interceptor -> Objects.equals(interceptor, encodeTransferMedataRestTemplateInterceptor)) .collect(Collectors.toList()); - //EncodeTransferMedataFeignInterceptor is not added repeatedly + //EncodeTransferMetadataFeignInterceptor is not added repeatedly Assertions.assertThat(encodeTransferMedataFeignInterceptorList.size()).isEqualTo(1); } }); diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigBootstrapAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigBootstrapAutoConfiguration.java index 43d81844..0e877d7b 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigBootstrapAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigBootstrapAutoConfiguration.java @@ -15,6 +15,7 @@ * specific language governing permissions and limitations under the License. * */ + package com.tencent.cloud.polaris.config; import com.tencent.cloud.polaris.config.adapter.AffectedConfigurationPropertiesRebinder; diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java index b37568f9..93ea9663 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java @@ -62,8 +62,10 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { // load spring boot default config files String[] activeProfiles = environment.getActiveProfiles(); + String[] defaultProfiles = environment.getDefaultProfiles(); String serviceName = environment.getProperty("spring.application.name"); - polarisConfigFilePuller.initInternalConfigFiles(compositePropertySource, activeProfiles, serviceName); + polarisConfigFilePuller.initInternalConfigFiles( + compositePropertySource, activeProfiles, defaultProfiles, serviceName); // load custom config files List configFileGroups = polarisConfigProperties.getGroups(); diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFilePuller.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFilePuller.java index f6b5631a..2399ab2f 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFilePuller.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFilePuller.java @@ -17,6 +17,8 @@ package com.tencent.cloud.polaris.config.adapter; +import java.util.ArrayList; +import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -30,6 +32,7 @@ import com.tencent.polaris.configuration.api.core.ConfigFileMetadata; import com.tencent.polaris.configuration.api.core.ConfigFileService; import com.tencent.polaris.configuration.api.core.ConfigKVFile; import com.tencent.polaris.configuration.client.internal.DefaultConfigFileMetadata; +import org.apache.commons.lang.ArrayUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -55,6 +58,24 @@ public final class PolarisConfigFilePuller { private PolarisConfigFilePuller() { } + /** + * Factory method to create PolarisConfigFilePuller for + * {@link PolarisConfigDataLoader},{@link PolarisConfigFileLocator}. + * + * @param polarisContextProperties polarisContextProperties + * @param configFileService configFileService + * @param polarisPropertySourceManager polarisPropertySourceManager + * @return PolarisConfigFilePuller instance + */ + public static PolarisConfigFilePuller get(PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, + PolarisPropertySourceManager polarisPropertySourceManager) { + PolarisConfigFilePuller puller = new PolarisConfigFilePuller(); + puller.polarisContextProperties = polarisContextProperties; + puller.configFileService = configFileService; + puller.polarisPropertySourceManager = polarisPropertySourceManager; + return puller; + } + /** * InitInternalConfigFiles for {@link PolarisConfigDataLoader}. * @@ -62,8 +83,9 @@ public final class PolarisConfigFilePuller { * @param activeProfiles activeProfiles * @param serviceName serviceName */ - public void initInternalConfigFiles(CompositePropertySource compositePropertySource, String[] activeProfiles, String serviceName) { - List internalConfigFiles = getInternalConfigFiles(activeProfiles, serviceName); + public void initInternalConfigFiles(CompositePropertySource compositePropertySource, String[] activeProfiles, + String[] defaultProfiles, String serviceName) { + List internalConfigFiles = getInternalConfigFiles(activeProfiles, defaultProfiles, serviceName); for (ConfigFileMetadata configFile : internalConfigFiles) { PolarisPropertySource polarisPropertySource = loadPolarisPropertySource( configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName()); @@ -137,59 +159,60 @@ public final class PolarisConfigFilePuller { return new PolarisPropertySource(namespace, group, fileName, configKVFile, map); } - - private List getInternalConfigFiles(String[] activeProfiles, String serviceName) { + private List getInternalConfigFiles( + String[] activeProfiles, String[] defaultProfiles, String serviceName) { String namespace = polarisContextProperties.getNamespace(); if (StringUtils.hasText(polarisContextProperties.getService())) { serviceName = polarisContextProperties.getService(); } // priority: application-${profile} > application > boostrap-${profile} > boostrap - return getInternalConfigFiles(activeProfiles, namespace, serviceName); + return getInternalConfigFiles(activeProfiles, defaultProfiles, namespace, serviceName); } - private List getInternalConfigFiles(String[] activeProfiles, String namespace, String serviceName) { + private List getInternalConfigFiles( + String[] activeProfiles, String[] defaultProfiles, String namespace, String serviceName) { + List profileList = new ArrayList<>(); + if (ArrayUtils.isNotEmpty(activeProfiles)) { + profileList.addAll(Arrays.asList(activeProfiles)); + } + else if (ArrayUtils.isNotEmpty(defaultProfiles)) { + profileList.addAll(Arrays.asList(defaultProfiles)); + } + List internalConfigFiles = new LinkedList<>(); - for (String activeProfile : activeProfiles) { - if (!StringUtils.hasText(activeProfile)) { + // build application config files + buildInternalApplicationConfigFiles(internalConfigFiles, namespace, serviceName, profileList); + // build bootstrap config files + buildInternalBootstrapConfigFiles(internalConfigFiles, namespace, serviceName, profileList); + + return internalConfigFiles; + } + + private void buildInternalApplicationConfigFiles( + List internalConfigFiles, String namespace, String serviceName, List profiles) { + for (String profile : profiles) { + if (!StringUtils.hasText(profile)) { continue; } - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + activeProfile + ".properties")); - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + activeProfile + ".yml")); + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + profile + ".properties")); + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + profile + ".yml")); } - + // build default config properties files. internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.properties")); internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.yml")); + } - for (String activeProfile : activeProfiles) { - if (!StringUtils.hasText(activeProfile)) { + private void buildInternalBootstrapConfigFiles( + List internalConfigFiles, String namespace, String serviceName, List profiles) { + for (String profile : profiles) { + if (!StringUtils.hasText(profile)) { continue; } - - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + activeProfile + ".properties")); - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + activeProfile + ".yml")); + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + profile + ".properties")); + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + profile + ".yml")); } - + // build default config properties files. internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.properties")); internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.yml")); - - return internalConfigFiles; - } - - /** - * Factory method to create PolarisConfigFilePuller for - * {@link PolarisConfigDataLoader},{@link PolarisConfigFileLocator}. - * - * @param polarisContextProperties polarisContextProperties - * @param configFileService configFileService - * @param polarisPropertySourceManager polarisPropertySourceManager - * @return PolarisConfigFilePuller instance - */ - public static PolarisConfigFilePuller get(PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, - PolarisPropertySourceManager polarisPropertySourceManager) { - PolarisConfigFilePuller puller = new PolarisConfigFilePuller(); - puller.polarisContextProperties = polarisContextProperties; - puller.configFileService = configFileService; - puller.polarisPropertySourceManager = polarisPropertySourceManager; - return puller; } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigPropertyRefresher.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigPropertyRefresher.java index 39351e7b..4b63c1c7 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigPropertyRefresher.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigPropertyRefresher.java @@ -33,7 +33,9 @@ public interface PolarisConfigPropertyRefresher { * * @param changedKey changedKey */ - void refreshSpringValue(String changedKey); + default void refreshSpringValue(String changedKey) { + // do nothing,all config will be refreshed by contextRefresher.refresh + } /** * refresh @ConfigurationProperties beans. diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ReflectRefreshTypeCondition.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ReflectRefreshTypeCondition.java index 9db7d5b4..c5c893c6 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ReflectRefreshTypeCondition.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ReflectRefreshTypeCondition.java @@ -47,7 +47,6 @@ public class ReflectRefreshTypeCondition extends SpringBootCondition { if (refreshType == DEFAULT_REFRESH_TYPE) { return ConditionOutcome.noMatch("no matched"); } - return ConditionOutcome.match("matched"); } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLoader.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLoader.java index 9b0f0855..2e6473df 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLoader.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLoader.java @@ -56,18 +56,13 @@ import static org.springframework.boot.context.config.ConfigData.Option.PROFILE_ */ public class PolarisConfigDataLoader implements ConfigDataLoader { + static final AtomicBoolean INTERNAL_CONFIG_FILES_LOADED = new AtomicBoolean(false); + static final AtomicBoolean CUSTOM_POLARIS_CONFIG_FILE_LOADED = new AtomicBoolean(false); private static final String POLARIS_CONFIG_PROPERTY_SOURCE_NAME = "polaris-config"; - private final Log log; - private ConfigFileService configFileService; - private PolarisConfigFilePuller puller; - static final AtomicBoolean INTERNAL_CONFIG_FILES_LOADED = new AtomicBoolean(false); - - static final AtomicBoolean CUSTOM_POLARIS_CONFIG_FILE_LOADED = new AtomicBoolean(false); - public PolarisConfigDataLoader(DeferredLogFactory logFactory) { this.log = logFactory.getLog(getClass()); } @@ -107,9 +102,10 @@ public class PolarisConfigDataLoader implements ConfigDataLoader profilesActive = profiles.getActive(); - String[] activeProfiles = profilesActive.toArray(new String[]{}); - this.puller.initInternalConfigFiles(compositePropertySource, activeProfiles, resource.getServiceName()); + String[] activeProfiles = profiles.getActive().toArray(new String[] {}); + String[] defaultProfiles = profiles.getDefault().toArray(new String[] {}); + this.puller.initInternalConfigFiles( + compositePropertySource, activeProfiles, defaultProfiles, resource.getServiceName()); } PolarisConfigProperties polarisConfigProperties = resource.getPolarisConfigProperties(); @@ -136,7 +132,7 @@ public class PolarisConfigDataLoader implements ConfigDataLoader loadConfigDataResources(ConfigDataLocationResolverContext resolverContext, - ConfigDataLocation location, - Profiles profiles, - PolarisConfigProperties polarisConfigProperties, - PolarisContextProperties polarisContextProperties) { + ConfigDataLocation location, + Profiles profiles, + PolarisConfigProperties polarisConfigProperties, + PolarisContextProperties polarisContextProperties) { List result = new ArrayList<>(); boolean optional = location.isOptional(); String groupFileName = getRealGroupFileName(location); @@ -245,8 +246,8 @@ public class PolarisConfigDataLocationResolver implements } private void prepareAndInitEarlierPolarisSdkContext(ConfigDataLocationResolverContext resolverContext, - PolarisConfigProperties polarisConfigProperties, - PolarisContextProperties polarisContextProperties) { + PolarisConfigProperties polarisConfigProperties, + PolarisContextProperties polarisContextProperties) { ConfigurableBootstrapContext bootstrapContext = resolverContext.getBootstrapContext(); if (!bootstrapContext.isRegistered(SDKContext.class)) { SDKContext sdkContext = sdkContext(resolverContext, @@ -270,8 +271,7 @@ public class PolarisConfigDataLocationResolver implements PolarisContextProperties polarisContextProperties) { // add ModifyAddress and ConfigurationModifier to load SDKContext List modifierList = new ArrayList<>(); - ModifyAddress modifyAddress = new ModifyAddress(); - modifyAddress.setProperties(polarisContextProperties); + ModifyAddress modifyAddress = new ModifyAddress(polarisContextProperties); ConfigurationModifier configurationModifier = new ConfigurationModifier(polarisConfigProperties, polarisContextProperties); diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/enums/RefreshBehavior.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/enums/RefreshBehavior.java index 198c6619..b6e414ed 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/enums/RefreshBehavior.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/enums/RefreshBehavior.java @@ -16,7 +16,6 @@ * */ - package com.tencent.cloud.polaris.config.enums; import org.springframework.boot.context.properties.ConfigurationPropertiesBean; diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/enums/RefreshType.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/enums/RefreshType.java index 748ef206..4c9ff21c 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/enums/RefreshType.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/enums/RefreshType.java @@ -15,6 +15,7 @@ * specific language governing permissions and limitations under the License. * */ + package com.tencent.cloud.polaris.config.enums; /** diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/PolarisProcessor.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/PolarisProcessor.java index 6ff8d8ed..cfbc5385 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/PolarisProcessor.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/PolarisProcessor.java @@ -1,3 +1,20 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + package com.tencent.cloud.polaris.config.spring.annotation; import java.lang.reflect.Field; diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessor.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessor.java index 2532f2b9..0edd584f 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessor.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessor.java @@ -1,3 +1,20 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + package com.tencent.cloud.polaris.config.spring.annotation; import java.beans.PropertyDescriptor; @@ -60,7 +77,7 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanFactor } @Override - public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) + public void postProcessBeanFactory(@NonNull ConfigurableListableBeanFactory beanFactory) throws BeansException { if (polarisConfigProperties.isAutoRefresh() && beanFactory instanceof BeanDefinitionRegistry) { beanName2SpringValueDefinitions = SpringValueDefinitionProcessor @@ -120,11 +137,11 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanFactor SpringValue springValue; if (member instanceof Field) { Field field = (Field) member; - springValue = new SpringValue(key, value.value(), bean, beanName, field, false); + springValue = new SpringValue(key, value.value(), bean, beanName, field); } else if (member instanceof Method) { Method method = (Method) member; - springValue = new SpringValue(key, value.value(), bean, beanName, method, false); + springValue = new SpringValue(key, value.value(), bean, beanName, method); } else { LOGGER.error("Polaris @Value annotation currently only support to be used on methods and fields, " @@ -137,24 +154,24 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanFactor } private void processBeanPropertyValues(Object bean, String beanName) { - Collection propertySpringValues = beanName2SpringValueDefinitions - .get(beanName); - if (propertySpringValues == null || propertySpringValues.isEmpty()) { + Collection propertySpringValues = beanName2SpringValueDefinitions.get(beanName); + if (propertySpringValues.isEmpty()) { return; } for (SpringValueDefinition definition : propertySpringValues) { try { - PropertyDescriptor pd = BeanUtils - .getPropertyDescriptor(bean.getClass(), definition.getPropertyName()); - Method method = pd.getWriteMethod(); - if (method == null) { - continue; + PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(bean.getClass(), definition.getPropertyName()); + if (pd != null) { + Method method = pd.getWriteMethod(); + if (method == null) { + continue; + } + SpringValue springValue = new SpringValue(definition.getKey(), definition.getPlaceholder(), + bean, beanName, method); + springValueRegistry.register(beanFactory, definition.getKey(), springValue); + LOGGER.debug("Monitoring {}", springValue); } - SpringValue springValue = new SpringValue(definition.getKey(), definition.getPlaceholder(), - bean, beanName, method, false); - springValueRegistry.register(beanFactory, definition.getKey(), springValue); - LOGGER.debug("Monitoring {}", springValue); } catch (Throwable ex) { LOGGER.error("Failed to enable auto update feature for {}.{}", bean.getClass(), @@ -167,7 +184,7 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanFactor } @Override - public void setBeanFactory(BeanFactory beanFactory) throws BeansException { + public void setBeanFactory(@NonNull BeanFactory beanFactory) throws BeansException { this.beanFactory = beanFactory; } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValue.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValue.java index ea602a92..06556d92 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValue.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValue.java @@ -21,7 +21,6 @@ import java.lang.ref.WeakReference; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.lang.reflect.Type; import org.springframework.core.MethodParameter; @@ -37,30 +36,24 @@ import org.springframework.core.MethodParameter; */ public class SpringValue { - private MethodParameter methodParameter; - private Field field; private final WeakReference beanRef; private final String beanName; private final String key; private final String placeholder; private final Class targetType; - private Type genericType; - private final boolean isJson; + private MethodParameter methodParameter; + private Field field; - public SpringValue(String key, String placeholder, Object bean, String beanName, Field field, boolean isJson) { + public SpringValue(String key, String placeholder, Object bean, String beanName, Field field) { this.beanRef = new WeakReference<>(bean); this.beanName = beanName; this.field = field; this.key = key; this.placeholder = placeholder; this.targetType = field.getType(); - this.isJson = isJson; - if (isJson) { - this.genericType = field.getGenericType(); - } } - public SpringValue(String key, String placeholder, Object bean, String beanName, Method method, boolean isJson) { + public SpringValue(String key, String placeholder, Object bean, String beanName, Method method) { this.beanRef = new WeakReference<>(bean); this.beanName = beanName; this.methodParameter = new MethodParameter(method, 0); @@ -68,10 +61,6 @@ public class SpringValue { this.placeholder = placeholder; Class[] paramTps = method.getParameterTypes(); this.targetType = paramTps[0]; - this.isJson = isJson; - if (isJson) { - this.genericType = method.getGenericParameterTypes()[0]; - } } public void update(Object newVal) throws IllegalAccessException, InvocationTargetException { @@ -127,14 +116,6 @@ public class SpringValue { return field; } - public Type getGenericType() { - return genericType; - } - - public boolean isJson() { - return isJson; - } - boolean isTargetBeanValid() { return beanRef.get() != null; } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinitionProcessor.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinitionProcessor.java index 56ed6239..9064667c 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinitionProcessor.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinitionProcessor.java @@ -35,6 +35,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.TypedStringValue; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; +import org.springframework.lang.NonNull; /** * To process xml config placeholders, e.g. @@ -59,34 +60,34 @@ public class SpringValueDefinitionProcessor implements BeanDefinitionRegistryPos private final PlaceholderHelper placeholderHelper; - private PolarisConfigProperties polarisConfigProperties; + private final PolarisConfigProperties polarisConfigProperties; public SpringValueDefinitionProcessor(PlaceholderHelper placeholderHelper, PolarisConfigProperties polarisConfigProperties) { this.polarisConfigProperties = polarisConfigProperties; this.placeholderHelper = placeholderHelper; } + public static Multimap getBeanName2SpringValueDefinitions(BeanDefinitionRegistry registry) { + Multimap springValueDefinitions = beanName2SpringValueDefinitions.get(registry); + if (springValueDefinitions == null) { + springValueDefinitions = LinkedListMultimap.create(); + } + + return springValueDefinitions; + } + @Override - public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { + public void postProcessBeanDefinitionRegistry(@NonNull BeanDefinitionRegistry registry) throws BeansException { if (polarisConfigProperties.isAutoRefresh()) { processPropertyValues(registry); } } @Override - public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { + public void postProcessBeanFactory(@NonNull ConfigurableListableBeanFactory beanFactory) throws BeansException { } - public static Multimap getBeanName2SpringValueDefinitions(BeanDefinitionRegistry registry) { - Multimap springValueDefinitions = beanName2SpringValueDefinitions.get(registry); - if (springValueDefinitions == null) { - springValueDefinitions = LinkedListMultimap.create(); - } - - return springValueDefinitions; - } - private void processPropertyValues(BeanDefinitionRegistry beanRegistry) { if (!PROPERTY_VALUES_PROCESSED_BEAN_FACTORIES.add(beanRegistry)) { // already initialized diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueRegistry.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueRegistry.java index 9155c923..78ed172f 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueRegistry.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueRegistry.java @@ -94,14 +94,8 @@ public class SpringValueRegistry { Iterator> iterator = registry.values().iterator(); while (!Thread.currentThread().isInterrupted() && iterator.hasNext()) { Multimap springValues = iterator.next(); - Iterator> springValueIterator = springValues.entries().iterator(); - while (springValueIterator.hasNext()) { - Map.Entry springValue = springValueIterator.next(); - if (!springValue.getValue().isTargetBeanValid()) { - // clear unused spring values - springValueIterator.remove(); - } - } + // clear unused spring values + springValues.entries().removeIf(springValue -> !springValue.getValue().isTargetBeanValid()); } } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFilePullerTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFilePullerTest.java index 3d5ef742..8e195ff0 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFilePullerTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFilePullerTest.java @@ -45,6 +45,9 @@ import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class PolarisConfigFilePullerTest { + private final String testNamespace = "testNamespace"; + private final String testServiceName = "testServiceName"; + private final String polarisConfigPropertySourceName = "polaris-config"; @Mock private PolarisContextProperties polarisContextProperties; @Mock @@ -52,10 +55,6 @@ public class PolarisConfigFilePullerTest { @Mock private PolarisPropertySourceManager polarisPropertySourceManager; - private final String testNamespace = "testNamespace"; - private final String testServiceName = "testServiceName"; - private final String polarisConfigPropertySourceName = "polaris-config"; - @Test public void testPullInternalConfigFiles() { PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService, @@ -80,7 +79,7 @@ public class PolarisConfigFilePullerTest { when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile); CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName); - puller.initInternalConfigFiles(compositePropertySource, new String[]{}, testServiceName); + puller.initInternalConfigFiles(compositePropertySource, new String[] {}, new String[] {}, testServiceName); Assert.assertEquals("v1", compositePropertySource.getProperty("k1")); Assert.assertEquals("v2", compositePropertySource.getProperty("k2")); @@ -121,9 +120,9 @@ public class PolarisConfigFilePullerTest { when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap-dev.yml")).thenReturn(emptyConfigFile); List active = new ArrayList<>(); active.add("dev"); - String[] activeProfiles = active.toArray(new String[]{}); + String[] activeProfiles = active.toArray(new String[] {}); CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName); - puller.initInternalConfigFiles(compositePropertySource, activeProfiles, testServiceName); + puller.initInternalConfigFiles(compositePropertySource, activeProfiles, new String[] {}, testServiceName); Assert.assertEquals("v11", compositePropertySource.getProperty("k1")); Assert.assertEquals("v2", compositePropertySource.getProperty("k2")); diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertiesSourceAutoRefresherTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertiesSourceAutoRefresherTest.java index 6df40aee..912a12ac 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertiesSourceAutoRefresherTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertiesSourceAutoRefresherTest.java @@ -83,7 +83,7 @@ public class PolarisPropertiesSourceAutoRefresherTest { MockedConfigChange mockedConfigChange = new MockedConfigChange(); mockedConfigChange.setK1("v1"); Field field = mockedConfigChange.getClass().getDeclaredField("k1"); - SpringValue springValue = new SpringValue("v1", "placeholder", mockedConfigChange, "mockedConfigChange", field, false); + SpringValue springValue = new SpringValue("v1", "placeholder", mockedConfigChange, "mockedConfigChange", field); springValues.add(springValue); diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesBootstrapAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesBootstrapAutoConfiguration.java index f04663c1..e441aff2 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesBootstrapAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesBootstrapAutoConfiguration.java @@ -15,6 +15,7 @@ * specific language governing permissions and limitations under the License. * */ + package com.tencent.cloud.polaris; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisRefreshApplicationReadyEventListener.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisRefreshApplicationReadyEventListener.java index d8773ce7..cab57298 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisRefreshApplicationReadyEventListener.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisRefreshApplicationReadyEventListener.java @@ -14,6 +14,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.tencent.cloud.polaris.discovery.refresh; import java.util.concurrent.Executors; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisRefreshConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisRefreshConfiguration.java index acbe54b7..c0523fa5 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisRefreshConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisRefreshConfiguration.java @@ -14,6 +14,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.tencent.cloud.polaris.discovery.refresh; import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisServiceStatusChangeListener.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisServiceStatusChangeListener.java index c189058e..61aba789 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisServiceStatusChangeListener.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/refresh/PolarisServiceStatusChangeListener.java @@ -14,6 +14,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.tencent.cloud.polaris.discovery.refresh; import java.util.Set; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java index 82761d40..5a78e913 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java @@ -123,7 +123,6 @@ public class PolarisServiceRegistry implements ServiceRegistry { @Override public void deregister(Registration registration) { - LOGGER.info("De-registering from Polaris Server now..."); if (StringUtils.isEmpty(registration.getServiceId())) { @@ -155,10 +154,12 @@ public class PolarisServiceRegistry implements ServiceRegistry { @Override public void close() { + } @Override public void setStatus(Registration registration, String status) { + } @Override diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/constant/RateLimitConstant.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/constant/RateLimitConstant.java index 814bed56..cdbf527a 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/constant/RateLimitConstant.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/constant/RateLimitConstant.java @@ -31,17 +31,18 @@ public final class RateLimitConstant { * Order of filter. */ public static final int FILTER_ORDER = Ordered.HIGHEST_PRECEDENCE + 10; - /** * Info of rate limit. */ public static String QUOTA_LIMITED_INFO = "The request is denied by rate limit because the throttling threshold is reached"; - /** * The build in label method. */ public static String LABEL_METHOD = "method"; + /** + * Default Private Constructor. + */ private RateLimitConstant() { } } diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/endpoint/PolarisRateLimitRuleEndpoint.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/endpoint/PolarisRateLimitRuleEndpoint.java index fe888ed8..a97069c2 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/endpoint/PolarisRateLimitRuleEndpoint.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/endpoint/PolarisRateLimitRuleEndpoint.java @@ -77,7 +77,7 @@ public class PolarisRateLimitRuleEndpoint { } for (RateLimitProto.Rule rule : rateLimit.getRulesList()) { - String ruleJson = ""; + String ruleJson; try { ruleJson = JsonFormat.printer().print(rule); } diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java index 74269570..30a55248 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java @@ -60,7 +60,7 @@ import static com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant.LAB */ public class QuotaCheckReactiveFilter implements WebFilter, Ordered { - private static final Logger LOG = LoggerFactory.getLogger(QuotaCheckReactiveFilter.class); + private static final Logger LOGGER = LoggerFactory.getLogger(QuotaCheckReactiveFilter.class); private final LimitAPI limitAPI; @@ -114,14 +114,14 @@ public class QuotaCheckReactiveFilter implements WebFilter, Ordered { } // Unirate if (quotaResponse.getCode() == QuotaResultCode.QuotaResultOk && quotaResponse.getWaitMs() > 0) { - LOG.debug("The request of [{}] will waiting for {}ms.", path, quotaResponse.getWaitMs()); + LOGGER.debug("The request of [{}] will waiting for {}ms.", path, quotaResponse.getWaitMs()); return Mono.delay(Duration.ofMillis(quotaResponse.getWaitMs())).flatMap(e -> chain.filter(exchange)); } } catch (Throwable t) { // An exception occurs in the rate limiting API call, // which should not affect the call of the business process. - LOG.error("fail to invoke getQuota, service is " + localService, t); + LOGGER.error("fail to invoke getQuota, service is " + localService, t); } return chain.filter(exchange); @@ -153,7 +153,7 @@ public class QuotaCheckReactiveFilter implements WebFilter, Ordered { return labelResolver.resolve(exchange); } catch (Throwable e) { - LOG.error("resolve custom label failed. resolver = {}", labelResolver.getClass().getName(), e); + LOGGER.error("resolve custom label failed. resolver = {}", labelResolver.getClass().getName(), e); } } return Maps.newHashMap(); diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckServletFilter.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckServletFilter.java index 82c3d791..c75ed1e0 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckServletFilter.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckServletFilter.java @@ -46,6 +46,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.annotation.Order; +import org.springframework.lang.NonNull; import org.springframework.web.filter.OncePerRequestFilter; import static com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant.LABEL_METHOD; @@ -58,13 +59,11 @@ import static com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant.LAB @Order(RateLimitConstant.FILTER_ORDER) public class QuotaCheckServletFilter extends OncePerRequestFilter { - private static final Logger LOG = LoggerFactory.getLogger(QuotaCheckServletFilter.class); - /** * Default Filter Registration Bean Name Defined . */ public static final String QUOTA_FILTER_BEAN_NAME = "quotaFilterRegistrationBean"; - + private static final Logger LOG = LoggerFactory.getLogger(QuotaCheckServletFilter.class); private final LimitAPI limitAPI; private final PolarisRateLimiterLabelServletResolver labelResolver; @@ -91,7 +90,8 @@ public class QuotaCheckServletFilter extends OncePerRequestFilter { } @Override - protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) + protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, + @NonNull FilterChain filterChain) throws ServletException, IOException { String localNamespace = MetadataContext.LOCAL_NAMESPACE; String localService = MetadataContext.LOCAL_SERVICE; diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/utils/QuotaCheckUtils.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/utils/QuotaCheckUtils.java index ff08fe34..faac3490 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/utils/QuotaCheckUtils.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/utils/QuotaCheckUtils.java @@ -14,6 +14,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.tencent.cloud.polaris.ratelimit.utils; import java.util.Map; diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/RouterRuleLabelResolverTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/RouterRuleLabelResolverTest.java index f7c32a0b..449174eb 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/RouterRuleLabelResolverTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/RouterRuleLabelResolverTest.java @@ -18,7 +18,6 @@ package com.tencent.cloud.polaris.router; - import java.util.HashMap; import java.util.LinkedList; import java.util.List; diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/RouterConstants.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/RouterConstants.java index 7fbe0f1b..55eee75e 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/RouterConstants.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/RouterConstants.java @@ -24,11 +24,15 @@ package com.tencent.cloud.common.constant; * @author lepdou 2022-05-17 */ public final class RouterConstants { + /** * the header of router label. */ public static final String ROUTER_LABEL_HEADER = "internal-router-label"; + /** + * Default Private Constructor. + */ private RouterConstants() { } } diff --git a/spring-cloud-tencent-examples/polaris-config-example/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-config-example/src/main/resources/bootstrap.yml index 30cdb066..25447b73 100644 --- a/spring-cloud-tencent-examples/polaris-config-example/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-config-example/src/main/resources/bootstrap.yml @@ -3,6 +3,8 @@ server: spring: application: name: polaris-config-example + profiles: + active: abc cloud: polaris: address: grpc://183.47.111.80:8091 diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ModifyAddress.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ModifyAddress.java index 806c44a7..1e79f751 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ModifyAddress.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ModifyAddress.java @@ -26,8 +26,6 @@ import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.polaris.factory.config.ConfigurationImpl; import org.apache.commons.lang.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; - /** * Modify polaris server address. * @@ -35,8 +33,11 @@ import org.springframework.beans.factory.annotation.Autowired; */ public class ModifyAddress implements PolarisConfigModifier { - @Autowired - private PolarisContextProperties properties; + private final PolarisContextProperties properties; + + public ModifyAddress(PolarisContextProperties properties) { + this.properties = properties; + } @Override public void modify(ConfigurationImpl configuration) { @@ -53,8 +54,4 @@ public class ModifyAddress implements PolarisConfigModifier { public int getOrder() { return ContextConstant.ModifierOrder.FIRST; } - - public void setProperties(PolarisContextProperties properties) { - this.properties = properties; - } } diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java index af919cab..36fa9371 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java @@ -68,8 +68,8 @@ public class PolarisContextAutoConfiguration { @Bean @ConditionalOnMissingBean - public ModifyAddress polarisConfigModifier() { - return new ModifyAddress(); + public ModifyAddress polarisConfigModifier(PolarisContextProperties properties) { + return new ModifyAddress(properties); } @Bean diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/logging/PolarisLoggingApplicationListener.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/logging/PolarisLoggingApplicationListener.java index 52f1948a..a75087ec 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/logging/PolarisLoggingApplicationListener.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/logging/PolarisLoggingApplicationListener.java @@ -24,6 +24,7 @@ import org.springframework.boot.context.logging.LoggingApplicationListener; import org.springframework.context.ApplicationEvent; import org.springframework.context.event.GenericApplicationListener; import org.springframework.core.ResolvableType; +import org.springframework.lang.NonNull; /** * Reload of Polaris logging configuration. @@ -50,7 +51,7 @@ public class PolarisLoggingApplicationListener implements GenericApplicationList } @Override - public void onApplicationEvent(ApplicationEvent applicationEvent) { + public void onApplicationEvent(@NonNull ApplicationEvent applicationEvent) { PolarisLogging.getInstance().loadConfiguration(); } } diff --git a/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancerTest.java b/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancerTest.java index d80888a7..54b2ac26 100644 --- a/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancerTest.java +++ b/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancerTest.java @@ -52,7 +52,6 @@ import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; - /** * Test for {@link PolarisLoadBalancer}. * @@ -61,6 +60,8 @@ import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class PolarisLoadBalancerTest { + private static MockedStatic mockedApplicationContextAwareUtils; + private static Instance testInstance; @Mock private RouterAPI routerAPI; @Mock @@ -68,9 +69,6 @@ public class PolarisLoadBalancerTest { @Mock private PolarisLoadBalancerProperties loadBalancerProperties; - private static MockedStatic mockedApplicationContextAwareUtils; - private static Instance testInstance; - @BeforeClass public static void beforeClass() { mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class); diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignBeanPostProcessor.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignBeanPostProcessor.java index 488b9e09..1ddbe814 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignBeanPostProcessor.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignBeanPostProcessor.java @@ -35,7 +35,7 @@ import org.springframework.cloud.openfeign.loadbalancer.RetryableFeignBlockingLo */ public class EnhancedFeignBeanPostProcessor implements BeanPostProcessor, BeanFactoryAware { - private EnhancedFeignPluginRunner pluginRunner; + private final EnhancedFeignPluginRunner pluginRunner; private BeanFactory factory;