diff --git a/CHANGELOG.md b/CHANGELOG.md index c76ad2eb..b4c0cdcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,4 +8,5 @@ - [Bugfix: update byte-buddy scope test to compile](https://github.com/Tencent/spring-cloud-tencent/pull/495) - [feature:add @ConditionalOnConfigReflectEnabled annotation](https://github.com/Tencent/spring-cloud-tencent/pull/496) - [Feature: zuul supports polaris router](https://github.com/Tencent/spring-cloud-tencent/pull/502) +- [Refactor : optimize project and code](https://github.com/Tencent/spring-cloud-tencent/pull/506) - [Fix typo & Code optimization](https://github.com/Tencent/spring-cloud-tencent/pull/507) 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 fa14d9ae..a8966025 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 @@ -152,7 +152,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 6e56bd3e..906597e9 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 @@ -94,7 +94,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/PolarisConfigAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java index 60201c72..197db3e0 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java @@ -80,8 +80,8 @@ public class PolarisConfigAutoConfiguration { return new PolarisRefreshContextConfigPropertyAutoRefresher(polarisConfigProperties, polarisPropertySourceManager, contextRefresher); } - @ConditionalOnConfigReflectEnabled @Configuration(proxyBeanMethods = false) + @ConditionalOnConfigReflectEnabled @AutoConfigureBefore(PolarisConfigAutoConfiguration.class) public static class PolarisReflectRefresherAutoConfiguration { @Bean 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 7c7afdf8..9c87f69a 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.PolarisConfigFileLocator; 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 091f09be..a713cdb7 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 @@ -31,6 +31,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; @@ -122,33 +123,44 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { // priority: application-${profile} > application > boostrap-${profile} > boostrap String[] activeProfiles = environment.getActiveProfiles(); + buildInternalConfigFiles(internalConfigFiles, namespace, serviceName, activeProfiles); - for (String activeProfile : activeProfiles) { - if (!StringUtils.hasText(activeProfile)) { - continue; - } - - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + activeProfile + ".properties")); - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + activeProfile + ".yml")); + // Compatible with defaultProfiles configuration + if (ArrayUtils.isEmpty(activeProfiles)) { + String[] defaultProfiles = environment.getDefaultProfiles(); + buildInternalConfigFiles(internalConfigFiles, namespace, serviceName, defaultProfiles); } + // build default config properties files . internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.properties")); internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.yml")); + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.properties")); + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.yml")); - for (String activeProfile : activeProfiles) { - if (!StringUtils.hasText(activeProfile)) { - continue; - } + return internalConfigFiles; + } - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + activeProfile + ".properties")); - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + activeProfile + ".yml")); - } + private void buildInternalConfigFiles(List internalConfigFiles, String namespace, String serviceName, String[] profiles) { - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.properties")); - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.yml")); + if (profiles != null) { + for (String profile : profiles) { + if (!StringUtils.hasText(profile)) { + continue; + } + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + profile + ".properties")); + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + profile + ".yml")); + } - return internalConfigFiles; + for (String profile : profiles) { + if (!StringUtils.hasText(profile)) { + continue; + } + + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + profile + ".properties")); + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + profile + ".yml")); + } + } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigPropertyAutoRefresher.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigPropertyAutoRefresher.java index 275258c8..2b1ccbb7 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigPropertyAutoRefresher.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigPropertyAutoRefresher.java @@ -30,6 +30,7 @@ import org.slf4j.LoggerFactory; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.ApplicationListener; +import org.springframework.lang.NonNull; import org.springframework.util.CollectionUtils; /** @@ -57,7 +58,7 @@ public abstract class PolarisConfigPropertyAutoRefresher } @Override - public void onApplicationEvent(ApplicationReadyEvent event) { + public void onApplicationEvent(@NonNull ApplicationReadyEvent event) { registerPolarisConfigPublishEvent(); } 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/adapter/PolarisReflectConfigPropertyAutoRefresher.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisReflectConfigPropertyAutoRefresher.java index e597d680..5a22e46d 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisReflectConfigPropertyAutoRefresher.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisReflectConfigPropertyAutoRefresher.java @@ -36,6 +36,7 @@ import org.springframework.cloud.context.environment.EnvironmentChangeEvent; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.lang.NonNull; /** * PolarisReflectConfigPropertyAutoRefresher to refresh config in reflect type @@ -107,14 +108,10 @@ public class PolarisReflectConfigPropertyAutoRefresher extends PolarisConfigProp Object value = placeholderHelper .resolvePropertyValue(beanFactory, springValue.getBeanName(), springValue.getPlaceholder()); - if (springValue.isJson()) { - value = parseJsonValue((String) value, springValue.getTargetType()); - } - else { - value = springValue.isField() ? this.typeConverter.convertIfNecessary(value, springValue.getTargetType(), springValue.getField()) : - this.typeConverter.convertIfNecessary(value, springValue.getTargetType(), - springValue.getMethodParameter()); - } + value = springValue.isField() ? this.typeConverter.convertIfNecessary(value, springValue.getTargetType(), springValue.getField()) : + this.typeConverter.convertIfNecessary(value, springValue.getTargetType(), + springValue.getMethodParameter()); + return value; } @@ -129,7 +126,7 @@ public class PolarisReflectConfigPropertyAutoRefresher extends PolarisConfigProp } @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException { this.context = (ConfigurableApplicationContext) applicationContext; this.beanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory(); this.typeConverter = this.beanFactory.getTypeConverter(); diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshContextConfigPropertyAutoRefresher.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshContextConfigPropertyAutoRefresher.java index b3a79a08..7a52e8dd 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshContextConfigPropertyAutoRefresher.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshContextConfigPropertyAutoRefresher.java @@ -40,11 +40,6 @@ public class PolarisRefreshContextConfigPropertyAutoRefresher extends PolarisCon this.contextRefresher = contextRefresher; } - @Override - public void refreshSpringValue(String changedKey) { - // do nothing,all config will be refreshed by contextRefresher.refresh - } - @Override public void refreshConfigurationProperties(Set changeKeys) { contextRefresher.refresh(); diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/SmartConfigurationPropertiesRebinder.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/SmartConfigurationPropertiesRebinder.java index bc275482..c035d88e 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/SmartConfigurationPropertiesRebinder.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/SmartConfigurationPropertiesRebinder.java @@ -23,6 +23,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; @@ -102,6 +103,7 @@ public class SmartConfigurationPropertiesRebinder extends ConfigurationPropertie case SPECIFIC_BEAN: rebindSpecificBean(event); break; + case ALL_BEANS: default: rebind(); break; @@ -112,7 +114,7 @@ public class SmartConfigurationPropertiesRebinder extends ConfigurationPropertie private void rebindSpecificBean(EnvironmentChangeEvent event) { Set refreshedSet = new HashSet<>(); beanMap.forEach((name, bean) -> event.getKeys().forEach(changeKey -> { - String prefix = AnnotationUtils.getValue(bean.getAnnotation()).toString(); + String prefix = Objects.requireNonNull(AnnotationUtils.getValue(bean.getAnnotation())).toString(); // prevent multiple refresh one ConfigurationPropertiesBean. if (changeKey.startsWith(prefix) && refreshedSet.add(name)) { rebind(name); diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ConditionalOnConfigReflectEnabled.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ConditionalOnConfigReflectEnabled.java index 05c80dfa..8fc8e43d 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ConditionalOnConfigReflectEnabled.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ConditionalOnConfigReflectEnabled.java @@ -34,4 +34,5 @@ import org.springframework.context.annotation.Conditional; @Target({ElementType.TYPE, ElementType.METHOD}) @Conditional(ConfigReflectCondition.class) public @interface ConditionalOnConfigReflectEnabled { + } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/NonDefaultBehaviorCondition.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/NonDefaultBehaviorCondition.java index 98b14318..48484133 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/NonDefaultBehaviorCondition.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/NonDefaultBehaviorCondition.java @@ -46,12 +46,10 @@ public class NonDefaultBehaviorCondition extends SpringBootCondition { public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { RefreshBehavior behavior = context.getEnvironment().getProperty( - POLARIS_CONFIG_REFRESH_BEHAVIOR, RefreshBehavior.class, - DEFAULT_REFRESH_BEHAVIOR); + POLARIS_CONFIG_REFRESH_BEHAVIOR, RefreshBehavior.class, DEFAULT_REFRESH_BEHAVIOR); if (DEFAULT_REFRESH_BEHAVIOR == behavior) { 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/config/ConfigFileGroup.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/ConfigFileGroup.java index b52ffd13..0b5c2c33 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/ConfigFileGroup.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/ConfigFileGroup.java @@ -15,6 +15,7 @@ * specific language governing permissions and limitations under the License. * */ + package com.tencent.cloud.polaris.config.config; import java.util.List; diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java index bdd1821e..19af6caf 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java @@ -15,6 +15,7 @@ * specific language governing permissions and limitations under the License. * */ + package com.tencent.cloud.polaris.config.config; import java.util.List; diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/endpoint/PolarisConfigEndpointAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/endpoint/PolarisConfigEndpointAutoConfiguration.java index af2a64cb..ecef5313 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/endpoint/PolarisConfigEndpointAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/endpoint/PolarisConfigEndpointAutoConfiguration.java @@ -42,7 +42,8 @@ public class PolarisConfigEndpointAutoConfiguration { @Bean @ConditionalOnAvailableEndpoint @ConditionalOnMissingBean - public PolarisConfigEndpoint polarisConfigEndpoint(PolarisConfigProperties polarisConfigProperties, PolarisPropertySourceManager polarisPropertySourceManager) { + public PolarisConfigEndpoint polarisConfigEndpoint(PolarisConfigProperties polarisConfigProperties, + PolarisPropertySourceManager polarisPropertySourceManager) { return new PolarisConfigEndpoint(polarisConfigProperties, polarisPropertySourceManager); } } 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 12abd79c..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; @@ -36,5 +35,4 @@ public enum RefreshBehavior { * Refresh specific {@link ConfigurationPropertiesBean} base on change key. */ SPECIFIC_BEAN, - } 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 3058e5eb..f0ee2753 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,21 @@ +/* + * 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 d7d09912..da71bd70 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,21 @@ +/* + * 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; @@ -27,6 +45,7 @@ import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.annotation.Bean; +import org.springframework.lang.NonNull; /** * Spring value processor of field or method which has @Value and xml config placeholders. @@ -59,7 +78,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 @@ -119,11 +138,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, " @@ -136,9 +155,9 @@ 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; } @@ -146,14 +165,17 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanFactor try { PropertyDescriptor pd = BeanUtils .getPropertyDescriptor(bean.getClass(), definition.getPropertyName()); - Method method = pd.getWriteMethod(); - if (method == null) { - continue; + + 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(), @@ -166,7 +188,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 1f039611..c092b96d 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; @@ -44,23 +43,17 @@ public class SpringValue { private final String key; private final String placeholder; private final Class targetType; - private Type genericType; - private final boolean isJson; - 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..c6682d51 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,7 +60,7 @@ 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; @@ -67,14 +68,14 @@ public class SpringValueDefinitionProcessor implements BeanDefinitionRegistryPos } @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 { } 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/PolarisPropertiesSourceAutoRefresherTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertiesSourceAutoRefresherTest.java index a2fdef9f..6f738afa 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/client/PolarisClient.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/client/PolarisClient.java index 6ab0adbf..dddf5279 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/client/PolarisClient.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/client/PolarisClient.java @@ -46,5 +46,4 @@ public @interface PolarisClient { * @return namespace */ String namespace() default ""; - } 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 a5ce7078..048b5c14 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; @@ -32,6 +33,7 @@ import org.slf4j.LoggerFactory; import org.springframework.cloud.client.discovery.event.HeartbeatEvent; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisherAware; +import org.springframework.lang.NonNull; import org.springframework.util.CollectionUtils; /** @@ -93,7 +95,7 @@ public class PolarisServiceStatusChangeListener extends AbstractResourceEventLis } @Override - public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { + public void setApplicationEventPublisher(@NonNull ApplicationEventPublisher applicationEventPublisher) { this.publisher = applicationEventPublisher; } } 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 d99dfcbf..1e0738f1 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 @@ -51,7 +51,7 @@ import static org.springframework.util.ReflectionUtils.rethrowRuntimeException; */ public class PolarisServiceRegistry implements ServiceRegistry { - private static final Logger LOG = LoggerFactory.getLogger(PolarisServiceRegistry.class); + private static final Logger LOGGER = LoggerFactory.getLogger(PolarisServiceRegistry.class); private final PolarisDiscoveryProperties polarisDiscoveryProperties; @@ -80,7 +80,7 @@ public class PolarisServiceRegistry implements ServiceRegistry { @Override public void register(Registration registration) { if (StringUtils.isEmpty(registration.getServiceId())) { - LOG.warn("No service to register for polaris client..."); + LOGGER.warn("No service to register for polaris client..."); return; } // Register instance. @@ -103,7 +103,7 @@ public class PolarisServiceRegistry implements ServiceRegistry { try { ProviderAPI providerClient = polarisDiscoveryHandler.getProviderAPI(); providerClient.register(instanceRegisterRequest); - LOG.info("polaris registry, {} {} {}:{} {} register finished", + LOGGER.info("polaris registry, {} {} {}:{} {} register finished", polarisDiscoveryProperties.getNamespace(), registration.getServiceId(), registration.getHost(), registration.getPort(), staticMetadataManager.getMergedStaticMetadata()); @@ -116,17 +116,17 @@ public class PolarisServiceRegistry implements ServiceRegistry { } } catch (Exception e) { - LOG.error("polaris registry, {} register failed...{},", registration.getServiceId(), registration, e); + LOGGER.error("polaris registry, {} register failed...{},", registration.getServiceId(), registration, e); rethrowRuntimeException(e); } } @Override public void deregister(Registration registration) { - LOG.info("De-registering from Polaris Server now..."); + LOGGER.info("De-registering from Polaris Server now..."); if (StringUtils.isEmpty(registration.getServiceId())) { - LOG.warn("No dom to de-register for polaris client..."); + LOGGER.warn("No dom to de-register for polaris client..."); return; } @@ -142,14 +142,14 @@ public class PolarisServiceRegistry implements ServiceRegistry { providerClient.deRegister(deRegisterRequest); } catch (Exception e) { - LOG.error("ERR_POLARIS_DEREGISTER, de-register failed...{},", registration, e); + LOGGER.error("ERR_POLARIS_DEREGISTER, de-register failed...{},", registration, e); } finally { if (null != heartbeatExecutor) { heartbeatExecutor.shutdown(); } } - LOG.info("De-registration finished."); + LOGGER.info("De-registration finished."); } @Override @@ -201,7 +201,7 @@ public class PolarisServiceRegistry implements ServiceRegistry { heartbeatRequest.getHost(), heartbeatRequest.getPort(), healthCheckEndpoint); if (!OkHttpUtil.get(healthCheckUrl, null)) { - LOG.error("backend service health check failed. health check endpoint = {}", healthCheckEndpoint); + LOGGER.error("backend service health check failed. health check endpoint = {}", healthCheckEndpoint); return; } } @@ -209,10 +209,10 @@ public class PolarisServiceRegistry implements ServiceRegistry { polarisDiscoveryHandler.getProviderAPI().heartbeat(heartbeatRequest); } catch (PolarisException e) { - LOG.error("polaris heartbeat[{}]", e.getCode(), e); + LOGGER.error("polaris heartbeat[{}]", e.getCode(), e); } catch (Exception e) { - LOG.error("polaris heartbeat runtime error", e); + LOGGER.error("polaris heartbeat runtime error", e); } }, polarisDiscoveryProperties.getHeartbeatInterval(), polarisDiscoveryProperties.getHeartbeatInterval(), MILLISECONDS); } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisServerList.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisServerList.java index 6e5b4a00..1f650df2 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisServerList.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisServerList.java @@ -39,7 +39,7 @@ public class PolarisServerList extends AbstractServerList { private String serviceId; - private PolarisDiscoveryHandler polarisDiscoveryHandler; + private final PolarisDiscoveryHandler polarisDiscoveryHandler; public PolarisServerList(PolarisDiscoveryHandler polarisDiscoveryHandler) { this.polarisDiscoveryHandler = polarisDiscoveryHandler; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/util/OkHttpUtil.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/util/OkHttpUtil.java index b5a275c9..759b78c4 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/util/OkHttpUtil.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/util/OkHttpUtil.java @@ -36,7 +36,7 @@ public final class OkHttpUtil { /** * Logger. */ - public final static Logger logger = LoggerFactory.getLogger(OkHttpUtil.class); + public final static Logger LOGGER = LoggerFactory.getLogger(OkHttpUtil.class); /** * client. @@ -62,12 +62,12 @@ public final class OkHttpUtil { if (response.isSuccessful() && Objects.nonNull(response.body())) { String result = response.body().string(); - logger.debug("exec get request, url: {} success,response data: {}", url, result); + LOGGER.debug("exec get request, url: {} success,response data: {}", url, result); return true; } } catch (Exception e) { - logger.error("exec get request,url: {} failed!", url, e); + LOGGER.error("exec get request,url: {} failed!", url, e); } return false; } 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..efc81049 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 @@ -27,6 +27,13 @@ import org.springframework.core.Ordered; */ public final class RateLimitConstant { + + /** + * Default Private Constructor . + */ + private RateLimitConstant() { + } + /** * Order of filter. */ @@ -41,7 +48,4 @@ public final class RateLimitConstant { * The build in label method. */ public static String LABEL_METHOD = "method"; - - 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 d41ebce8..f5a442e3 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 @@ -76,7 +76,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/endpoint/PolarisRateLimitRuleEndpointAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/endpoint/PolarisRateLimitRuleEndpointAutoConfiguration.java index d1683298..de5b6bfe 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/endpoint/PolarisRateLimitRuleEndpointAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/endpoint/PolarisRateLimitRuleEndpointAutoConfiguration.java @@ -46,5 +46,4 @@ public class PolarisRateLimitRuleEndpointAutoConfiguration { public PolarisRateLimitRuleEndpoint polarisRateLimitRuleEndpoint(ServiceRuleManager serviceRuleManager, PolarisRateLimitProperties properties) { return new PolarisRateLimitRuleEndpoint(serviceRuleManager, properties); } - } 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 cb95857b..8a1162ee 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..57c6a50b 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; @@ -91,7 +92,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 f0e5ec10..d7e4aaae 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/main/java/com/tencent/cloud/polaris/router/RouterConstants.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/RouterConstants.java index cff733e5..d074c523 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/RouterConstants.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/RouterConstants.java @@ -25,11 +25,15 @@ package com.tencent.cloud.polaris.router; */ public final class RouterConstants { + /** - * the header of router label. + * Default Private Constructor. */ - public static final String ROUTER_LABEL_HEADER = "internal-router-label"; - private RouterConstants() { } + + /** + * the header of router label. + */ + public static final String ROUTER_LABEL_HEADER = "internal-router-label"; } diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/beanprocessor/PolarisLoadBalancerCompositeRuleBeanPostProcessor.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/beanprocessor/PolarisLoadBalancerCompositeRuleBeanPostProcessor.java index b6092be3..7b9e8dc6 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/beanprocessor/PolarisLoadBalancerCompositeRuleBeanPostProcessor.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/beanprocessor/PolarisLoadBalancerCompositeRuleBeanPostProcessor.java @@ -33,6 +33,7 @@ import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.lang.NonNull; /** * Decorate IRule with PolarisLoadBalancerCompositeRule. @@ -44,7 +45,7 @@ public class PolarisLoadBalancerCompositeRuleBeanPostProcessor implements BeanPo private BeanFactory beanFactory; @Override - public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { + public Object postProcessBeforeInitialization(@NonNull Object bean, @NonNull String beanName) throws BeansException { if (bean instanceof AbstractLoadBalancerRule) { RouterAPI routerAPI = beanFactory.getBean(RouterAPI.class); PolarisLoadBalancerProperties polarisLoadBalancerProperties = beanFactory.getBean(PolarisLoadBalancerProperties.class); @@ -59,7 +60,7 @@ public class PolarisLoadBalancerCompositeRuleBeanPostProcessor implements BeanPo } @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-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterAutoConfiguration.java index ae9a37e1..4555719a 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterAutoConfiguration.java @@ -34,9 +34,11 @@ import com.tencent.cloud.polaris.router.interceptor.RuleBasedRouterRequestInterc import com.tencent.cloud.polaris.router.spi.ServletRouterLabelResolver; import com.tencent.cloud.polaris.router.zuul.PolarisRibbonRoutingFilter; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.cloud.netflix.ribbon.RibbonClients; +import org.springframework.cloud.netflix.zuul.ZuulServerAutoConfiguration; import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper; import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandFactory; import org.springframework.context.annotation.Bean; @@ -93,14 +95,22 @@ public class RouterAutoConfiguration { return new RuleBasedRouterRequestInterceptor(polarisRuleBasedRouterProperties); } - @Bean(initMethod = "init") + /** + * AutoConfiguration for router module integrate for zuul. + */ + @Configuration(proxyBeanMethods = false) @ConditionalOnClass(name = "org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter") - public PolarisRibbonRoutingFilter ribbonRoutingFilter(ProxyRequestHelper helper, - RibbonCommandFactory ribbonCommandFactory, - MetadataLocalProperties metadataLocalProperties, - RouterRuleLabelResolver routerRuleLabelResolver, - List routerLabelResolvers) { - return new PolarisRibbonRoutingFilter(helper, ribbonCommandFactory, metadataLocalProperties, - routerRuleLabelResolver, routerLabelResolvers); + @AutoConfigureAfter(ZuulServerAutoConfiguration.class) + public static class ZuulRouterAutoConfiguration { + + @Bean(initMethod = "init") + public PolarisRibbonRoutingFilter ribbonRoutingFilter(ProxyRequestHelper helper, + RibbonCommandFactory ribbonCommandFactory, + MetadataLocalProperties metadataLocalProperties, + RouterRuleLabelResolver routerRuleLabelResolver, + List routerLabelResolvers) { + return new PolarisRibbonRoutingFilter(helper, ribbonCommandFactory, metadataLocalProperties, + routerRuleLabelResolver, routerLabelResolvers); + } } } diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/zuul/PolarisRibbonRoutingFilter.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/zuul/PolarisRibbonRoutingFilter.java index e9e73762..bd48d735 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/zuul/PolarisRibbonRoutingFilter.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/zuul/PolarisRibbonRoutingFilter.java @@ -49,6 +49,7 @@ import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper; import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandFactory; import org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter; import org.springframework.core.Ordered; +import org.springframework.lang.NonNull; import org.springframework.util.CollectionUtils; import org.springframework.util.MultiValueMap; @@ -177,7 +178,7 @@ public class PolarisRibbonRoutingFilter extends RibbonRoutingFilter implements B } @Override - public void setBeanFactory(BeanFactory beanFactory) throws BeansException { + public void setBeanFactory(@NonNull BeanFactory beanFactory) throws BeansException { this.factory = beanFactory; } diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/PolarisLoadBalancerCompositeRuleTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/PolarisLoadBalancerCompositeRuleTest.java index bfe70fdf..ffc7123c 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/PolarisLoadBalancerCompositeRuleTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/PolarisLoadBalancerCompositeRuleTest.java @@ -80,7 +80,7 @@ import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class PolarisLoadBalancerCompositeRuleTest { - private static AtomicBoolean initTransitiveMetadata = new AtomicBoolean(false); + private static final AtomicBoolean initTransitiveMetadata = new AtomicBoolean(false); @Mock private PolarisLoadBalancerProperties polarisLoadBalancerProperties; @Mock 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 e16785c5..3ae3261f 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-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/beanprocessor/PolarisLoadBalancerCompositeRuleBeanPostProcessorTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/beanprocessor/PolarisLoadBalancerCompositeRuleBeanPostProcessorTest.java index 5765ad7e..009d6a77 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/beanprocessor/PolarisLoadBalancerCompositeRuleBeanPostProcessorTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/beanprocessor/PolarisLoadBalancerCompositeRuleBeanPostProcessorTest.java @@ -1,3 +1,21 @@ +/* + * 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.router.beanprocessor; import com.netflix.loadbalancer.AbstractLoadBalancerRule; @@ -25,7 +43,6 @@ import org.springframework.cloud.netflix.ribbon.SpringClientFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; - /** * Test for {@link PolarisLoadBalancerCompositeRuleBeanPostProcessor}. * diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/resttemplate/PolarisLoadBalancerInterceptorTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/resttemplate/PolarisLoadBalancerInterceptorTest.java index bff5116a..15a3934e 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/resttemplate/PolarisLoadBalancerInterceptorTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/resttemplate/PolarisLoadBalancerInterceptorTest.java @@ -18,7 +18,6 @@ package com.tencent.cloud.polaris.router.resttemplate; - import java.io.IOException; import java.net.URI; import java.util.Collections; diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/scg/PolarisLoadBalancerClientFilterTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/scg/PolarisLoadBalancerClientFilterTest.java index 2f201507..544e55bb 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/scg/PolarisLoadBalancerClientFilterTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/scg/PolarisLoadBalancerClientFilterTest.java @@ -18,7 +18,6 @@ package com.tencent.cloud.polaris.router.scg; - import java.net.URI; import java.util.HashMap; import java.util.Map; diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/zuul/PolarisRibbonRoutingFilterTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/zuul/PolarisRibbonRoutingFilterTest.java index 537e4eca..bc6bb843 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/zuul/PolarisRibbonRoutingFilterTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/zuul/PolarisRibbonRoutingFilterTest.java @@ -369,5 +369,4 @@ public class PolarisRibbonRoutingFilterTest { verify(polarisLoadBalancer).chooseServer(routerContext); verify(metadataLocalProperties, times(1)).getContent(); } - } diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/BeanFactoryUtils.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/BeanFactoryUtils.java index a15febe0..bddde813 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/BeanFactoryUtils.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/BeanFactoryUtils.java @@ -33,7 +33,6 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory; public final class BeanFactoryUtils { private BeanFactoryUtils() { - } public static List getBeans(BeanFactory beanFactory, Class requiredType) { 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 a7a1e551..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) { 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 97722fb0..eab0ad5e 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 @@ -63,8 +63,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 e34419a4..6241318f 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 @@ -14,11 +14,9 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ -// CHECKSTYLE:OFF package com.tencent.cloud.polaris.loadbalancer; - import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -52,7 +50,6 @@ import org.springframework.cloud.netflix.ribbon.StaticServerList; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; - /** * Test for {@link PolarisLoadBalancer}. * 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 e9dfa989..a03a8eed 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 @@ -31,6 +31,7 @@ import org.springframework.cloud.netflix.ribbon.SpringClientFactory; import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient; import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory; import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient; +import org.springframework.lang.NonNull; /** * Wrap Spring Bean and decorating proxy for Feign Client. @@ -48,7 +49,7 @@ public class EnhancedFeignBeanPostProcessor implements BeanPostProcessor, BeanFa } @Override - public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { + public Object postProcessBeforeInitialization(@NonNull Object bean, @NonNull String beanName) throws BeansException { return wrapper(bean); } diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateModifier.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateModifier.java index e4cf0211..0740e0ad 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateModifier.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateModifier.java @@ -24,6 +24,7 @@ import org.springframework.beans.factory.SmartInitializingSingleton; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; +import org.springframework.lang.NonNull; import org.springframework.util.ObjectUtils; import org.springframework.web.client.RestTemplate; @@ -59,7 +60,7 @@ public class EnhancedRestTemplateModifier implements ApplicationContextAware, Sm } @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } } diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporter.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporter.java index 46b945dc..c4a17ced 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporter.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporter.java @@ -17,7 +17,6 @@ package com.tencent.cloud.rpc.enhancement.resttemplate; -import java.io.IOException; import java.net.HttpURLConnection; import java.net.URI; import java.net.URL; @@ -46,7 +45,7 @@ import org.springframework.web.client.ResponseErrorHandler; */ public class EnhancedRestTemplateReporter extends AbstractPolarisReporterAdapter implements ResponseErrorHandler { - private static final Logger LOG = LoggerFactory.getLogger(EnhancedRestTemplateReporter.class); + private static final Logger LOGGER = LoggerFactory.getLogger(EnhancedRestTemplateReporter.class); private static final String FIELD_NAME = "connection"; @@ -67,10 +66,10 @@ public class EnhancedRestTemplateReporter extends AbstractPolarisReporterAdapter } @Override - public void handleError(@NonNull URI url, @NonNull HttpMethod method, @NonNull ClientHttpResponse response) - throws IOException { + public void handleError(@NonNull URI url, @NonNull HttpMethod method, @NonNull ClientHttpResponse response) { ServiceCallResult resultRequest = createServiceCallResult(url); try { + HttpURLConnection connection = (HttpURLConnection) ReflectionUtils.getFieldValue(response, FIELD_NAME); if (connection != null) { URL realURL = connection.getURL(); @@ -78,20 +77,19 @@ public class EnhancedRestTemplateReporter extends AbstractPolarisReporterAdapter resultRequest.setPort(realURL.getPort()); } + // checking response http status code if (apply(response.getStatusCode())) { resultRequest.setRetStatus(RetStatus.RetFail); } - } - catch (Exception e) { - LOG.error("Will report response of {} url {}", response, url, e); - resultRequest.setRetStatus(RetStatus.RetFail); - throw e; - } - finally { - LOG.debug("Will report result of {}. URL=[{}]. Response=[{}].", resultRequest.getRetStatus().name(), + + // processing report with consumerAPI . + LOGGER.debug("Will report result of {}. URL=[{}]. Response=[{}].", resultRequest.getRetStatus().name(), url, response); consumerAPI.updateServiceCallResult(resultRequest); } + catch (Exception e) { + LOGGER.error("RestTemplate response reporter execute failed of {} url {}", response, url, e); + } } private ServiceCallResult createServiceCallResult(URI uri) {