feat:support reading configuration from application.yml or application.properties. (#259)

pull/264/head
Haotian Zhang 2 years ago committed by GitHub
parent 7e672d175a
commit 49a03f8355
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,8 +1,11 @@
# Change Log # Change Log
--- ---
[UT: Add config module unit test](https://github.com/Tencent/spring-cloud-tencent/pull/229)
[Feature: Add config change listener feature support](https://github.com/Tencent/spring-cloud-tencent/pull/220) - [UT: Add config module unit test](https://github.com/Tencent/spring-cloud-tencent/pull/229)
[Feature: Support spring cloud gateway routers](https://github.com/Tencent/spring-cloud-tencent/pull/230) - [Feature: Add config change listener feature support](https://github.com/Tencent/spring-cloud-tencent/pull/220)
[Feature: Add instance metadata spi for registration](https://github.com/Tencent/spring-cloud-tencent/pull/244) - [Feature: Support spring cloud gateway routers](https://github.com/Tencent/spring-cloud-tencent/pull/230)
[Bugfix: fix guava version conflict bug & fix router strong dependency on LoadBalancerClientFilter](https://github.com/Tencent/spring-cloud-tencent/pull/236) - [Feature: Add instance metadata spi for registration](https://github.com/Tencent/spring-cloud-tencent/pull/244)
[Upgrade: fix third-party lib CVEs & upgrade core spring libs version](https://github.com/Tencent/spring-cloud-tencent/pull/237) - [Bugfix: fix guava version conflict bug & fix router strong dependency on LoadBalancerClientFilter](https://github.com/Tencent/spring-cloud-tencent/pull/236)
- [Upgrade: fix third-party lib CVEs & upgrade core spring libs version](https://github.com/Tencent/spring-cloud-tencent/pull/237)
- [change escape way into encode](https://github.com/Tencent/spring-cloud-tencent/pull/251)
- [feat:support reading configuration from application.yml or application.properties.](https://github.com/Tencent/spring-cloud-tencent/pull/259)

@ -55,7 +55,7 @@ import static javax.servlet.DispatcherType.REQUEST;
@ConditionalOnPolarisEnabled @ConditionalOnPolarisEnabled
@AutoConfigureAfter(PolarisContextAutoConfiguration.class) @AutoConfigureAfter(PolarisContextAutoConfiguration.class)
@ConditionalOnProperty(name = "spring.cloud.polaris.ratelimit.enabled", matchIfMissing = true) @ConditionalOnProperty(name = "spring.cloud.polaris.ratelimit.enabled", matchIfMissing = true)
public class PolarisRateLimitConfiguration { public class PolarisRateLimitAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean

@ -17,59 +17,26 @@
package com.tencent.cloud.polaris.ratelimit.config; package com.tencent.cloud.polaris.ratelimit.config;
import com.tencent.cloud.common.constant.ContextConstant;
import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled;
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/** /**
* Autoconfiguration of rate limit at bootstrap phase. * Properties auto configuration of rate limit.
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ConditionalOnPolarisEnabled @ConditionalOnPolarisEnabled
@ConditionalOnProperty(name = "spring.cloud.polaris.ratelimit.enabled", matchIfMissing = true) @Import({PolarisRateLimitProperties.class})
public class PolarisRateLimitBootstrapConfiguration { public class PolarisRateLimitPropertiesAutoConfiguration {
@Bean
public PolarisRateLimitProperties polarisRateLimitProperties() {
return new PolarisRateLimitProperties();
}
@Bean @Bean
@ConditionalOnMissingBean
public RateLimitConfigModifier rateLimitConfigModifier(PolarisRateLimitProperties polarisRateLimitProperties) { public RateLimitConfigModifier rateLimitConfigModifier(PolarisRateLimitProperties polarisRateLimitProperties) {
return new RateLimitConfigModifier(polarisRateLimitProperties); return new RateLimitConfigModifier(polarisRateLimitProperties);
} }
/**
* Config modifier for rate limit.
*
* @author Haotian Zhang
*/
public static class RateLimitConfigModifier implements PolarisConfigModifier {
private PolarisRateLimitProperties polarisRateLimitProperties;
public RateLimitConfigModifier(PolarisRateLimitProperties polarisRateLimitProperties) {
this.polarisRateLimitProperties = polarisRateLimitProperties;
}
@Override
public void modify(ConfigurationImpl configuration) {
// Update MaxQueuingTime.
configuration.getProvider().getRateLimit()
.setMaxQueuingTime(polarisRateLimitProperties.getMaxQueuingTime());
}
@Override
public int getOrder() {
return ContextConstant.ModifierOrder.CIRCUIT_BREAKER_ORDER;
}
}
} }

@ -0,0 +1,34 @@
/*
* 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.ratelimit.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* Autoconfiguration of rate limit at bootstrap phase.
*
* @author Haotian Zhang
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty("spring.cloud.polaris.enabled")
@Import(PolarisRateLimitPropertiesAutoConfiguration.class)
public class PolarisRateLimitPropertiesBootstrapConfiguration {
}

@ -0,0 +1,49 @@
/*
* 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.ratelimit.config;
import com.tencent.cloud.common.constant.ContextConstant;
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.polaris.factory.config.ConfigurationImpl;
/**
* Config modifier for rate limit.
*
* @author Haotian Zhang
*/
public class RateLimitConfigModifier implements PolarisConfigModifier {
private PolarisRateLimitProperties polarisRateLimitProperties;
public RateLimitConfigModifier(PolarisRateLimitProperties polarisRateLimitProperties) {
this.polarisRateLimitProperties = polarisRateLimitProperties;
}
@Override
public void modify(ConfigurationImpl configuration) {
// Update MaxQueuingTime.
configuration.getProvider().getRateLimit()
.setMaxQueuingTime(polarisRateLimitProperties.getMaxQueuingTime());
}
@Override
public int getOrder() {
return ContextConstant.ModifierOrder.CIRCUIT_BREAKER_ORDER;
}
}

@ -1,4 +1,5 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitConfiguration com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitAutoConfiguration,\
com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitPropertiesAutoConfiguration
org.springframework.cloud.bootstrap.BootstrapConfiguration=\ org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitBootstrapConfiguration com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitPropertiesBootstrapConfiguration

@ -33,11 +33,11 @@ import org.springframework.boot.web.servlet.FilterRegistrationBean;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Test for {@link PolarisRateLimitConfiguration}. * Test for {@link PolarisRateLimitAutoConfiguration}.
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
public class PolarisRateLimitConfigurationTest { public class PolarisRateLimitAutoConfigurationTest {
private ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner(); private ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner();
@ -51,14 +51,14 @@ public class PolarisRateLimitConfigurationTest {
.withConfiguration(AutoConfigurations.of( .withConfiguration(AutoConfigurations.of(
PolarisContextAutoConfiguration.class, PolarisContextAutoConfiguration.class,
PolarisRateLimitProperties.class, PolarisRateLimitProperties.class,
PolarisRateLimitConfiguration.class)) PolarisRateLimitAutoConfiguration.class))
.run(context -> { .run(context -> {
assertThat(context).hasSingleBean(LimitAPI.class); assertThat(context).hasSingleBean(LimitAPI.class);
assertThat(context).hasSingleBean(RateLimitRuleLabelResolver.class); assertThat(context).hasSingleBean(RateLimitRuleLabelResolver.class);
assertThat(context).doesNotHaveBean(PolarisRateLimitConfiguration.QuotaCheckFilterConfig.class); assertThat(context).doesNotHaveBean(PolarisRateLimitAutoConfiguration.QuotaCheckFilterConfig.class);
assertThat(context).doesNotHaveBean(QuotaCheckServletFilter.class); assertThat(context).doesNotHaveBean(QuotaCheckServletFilter.class);
assertThat(context).doesNotHaveBean(FilterRegistrationBean.class); assertThat(context).doesNotHaveBean(FilterRegistrationBean.class);
assertThat(context).doesNotHaveBean(PolarisRateLimitConfiguration.MetadataReactiveFilterConfig.class); assertThat(context).doesNotHaveBean(PolarisRateLimitAutoConfiguration.MetadataReactiveFilterConfig.class);
assertThat(context).doesNotHaveBean(QuotaCheckReactiveFilter.class); assertThat(context).doesNotHaveBean(QuotaCheckReactiveFilter.class);
}); });
} }
@ -68,14 +68,14 @@ public class PolarisRateLimitConfigurationTest {
this.webApplicationContextRunner this.webApplicationContextRunner
.withConfiguration(AutoConfigurations.of(PolarisContextAutoConfiguration.class, .withConfiguration(AutoConfigurations.of(PolarisContextAutoConfiguration.class,
PolarisRateLimitProperties.class, PolarisRateLimitProperties.class,
PolarisRateLimitConfiguration.class)) PolarisRateLimitAutoConfiguration.class))
.run(context -> { .run(context -> {
assertThat(context).hasSingleBean(LimitAPI.class); assertThat(context).hasSingleBean(LimitAPI.class);
assertThat(context).hasSingleBean(RateLimitRuleLabelResolver.class); assertThat(context).hasSingleBean(RateLimitRuleLabelResolver.class);
assertThat(context).hasSingleBean(PolarisRateLimitConfiguration.QuotaCheckFilterConfig.class); assertThat(context).hasSingleBean(PolarisRateLimitAutoConfiguration.QuotaCheckFilterConfig.class);
assertThat(context).hasSingleBean(QuotaCheckServletFilter.class); assertThat(context).hasSingleBean(QuotaCheckServletFilter.class);
assertThat(context).hasSingleBean(FilterRegistrationBean.class); assertThat(context).hasSingleBean(FilterRegistrationBean.class);
assertThat(context).doesNotHaveBean(PolarisRateLimitConfiguration.MetadataReactiveFilterConfig.class); assertThat(context).doesNotHaveBean(PolarisRateLimitAutoConfiguration.MetadataReactiveFilterConfig.class);
assertThat(context).doesNotHaveBean(QuotaCheckReactiveFilter.class); assertThat(context).doesNotHaveBean(QuotaCheckReactiveFilter.class);
}); });
} }
@ -85,14 +85,14 @@ public class PolarisRateLimitConfigurationTest {
this.reactiveWebApplicationContextRunner this.reactiveWebApplicationContextRunner
.withConfiguration(AutoConfigurations.of(PolarisContextAutoConfiguration.class, .withConfiguration(AutoConfigurations.of(PolarisContextAutoConfiguration.class,
PolarisRateLimitProperties.class, PolarisRateLimitProperties.class,
PolarisRateLimitConfiguration.class)) PolarisRateLimitAutoConfiguration.class))
.run(context -> { .run(context -> {
assertThat(context).hasSingleBean(LimitAPI.class); assertThat(context).hasSingleBean(LimitAPI.class);
assertThat(context).hasSingleBean(RateLimitRuleLabelResolver.class); assertThat(context).hasSingleBean(RateLimitRuleLabelResolver.class);
assertThat(context).doesNotHaveBean(PolarisRateLimitConfiguration.QuotaCheckFilterConfig.class); assertThat(context).doesNotHaveBean(PolarisRateLimitAutoConfiguration.QuotaCheckFilterConfig.class);
assertThat(context).doesNotHaveBean(QuotaCheckServletFilter.class); assertThat(context).doesNotHaveBean(QuotaCheckServletFilter.class);
assertThat(context).doesNotHaveBean(FilterRegistrationBean.class); assertThat(context).doesNotHaveBean(FilterRegistrationBean.class);
assertThat(context).hasSingleBean(PolarisRateLimitConfiguration.MetadataReactiveFilterConfig.class); assertThat(context).hasSingleBean(PolarisRateLimitAutoConfiguration.MetadataReactiveFilterConfig.class);
assertThat(context).hasSingleBean(QuotaCheckReactiveFilter.class); assertThat(context).hasSingleBean(QuotaCheckReactiveFilter.class);
}); });
} }

@ -25,22 +25,21 @@ import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Test for {@link PolarisRateLimitBootstrapConfiguration}. * Test for {@link PolarisRateLimitPropertiesAutoConfiguration}.
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
public class PolarisRateLimitBootstrapConfigurationTest { public class PolarisRateLimitPropertiesAutoConfigurationTest {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner() private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration( .withConfiguration(
AutoConfigurations.of(PolarisRateLimitBootstrapConfiguration.class)) AutoConfigurations.of(PolarisRateLimitPropertiesAutoConfiguration.class));
.withPropertyValues("spring.cloud.polaris.ratelimit.enabled=true");
@Test @Test
public void testDefaultInitialization() { public void testDefaultInitialization() {
this.contextRunner.run(context -> { this.contextRunner.run(context -> {
assertThat(context).hasSingleBean(PolarisRateLimitProperties.class); assertThat(context).hasSingleBean(PolarisRateLimitProperties.class);
assertThat(context).hasSingleBean(PolarisRateLimitBootstrapConfiguration.RateLimitConfigModifier.class); assertThat(context).hasSingleBean(RateLimitConfigModifier.class);
}); });
} }
} }

@ -0,0 +1,46 @@
/*
* 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.ratelimit.config;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link PolarisRateLimitPropertiesBootstrapConfiguration}.
*
* @author Haotian Zhang
*/
public class PolarisRateLimitPropertiesBootstrapConfigurationTest {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(PolarisRateLimitPropertiesBootstrapConfiguration.class))
.withPropertyValues("spring.cloud.polaris.enabled=true");
@Test
public void testDefaultInitialization() {
this.contextRunner.run(context -> {
assertThat(context).hasSingleBean(PolarisRateLimitProperties.class);
assertThat(context).hasSingleBean(RateLimitConfigModifier.class);
});
}
}

@ -18,11 +18,13 @@
package com.tencent.cloud.polaris.ratelimit.config; package com.tencent.cloud.polaris.ratelimit.config;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration; import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -31,29 +33,25 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = PolarisRateLimitPropertiesTest.TestApplication.class)
@ActiveProfiles("test")
public class PolarisRateLimitPropertiesTest { public class PolarisRateLimitPropertiesTest {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner() @Autowired
.withConfiguration(AutoConfigurations.of(PolarisRateLimitPropertiesAutoConfiguration.class, PolarisRateLimitProperties.class)) private PolarisRateLimitProperties polarisRateLimitProperties;
.withPropertyValues("spring.cloud.polaris.ratelimit.rejectRequestTips=xxx")
.withPropertyValues("spring.cloud.polaris.ratelimit.rejectRequestTipsFilePath=/index.html")
.withPropertyValues("spring.cloud.polaris.ratelimit.rejectHttpCode=419")
.withPropertyValues("spring.cloud.polaris.ratelimit.maxQueuingTime=500");
@Test @Test
public void testDefaultInitialization() { public void testDefaultInitialization() {
this.contextRunner.run(context -> { assertThat(polarisRateLimitProperties).isNotNull();
PolarisRateLimitProperties polarisRateLimitProperties = context.getBean(PolarisRateLimitProperties.class); assertThat(polarisRateLimitProperties.getRejectRequestTips()).isEqualTo("xxx");
assertThat(polarisRateLimitProperties.getRejectRequestTips()).isEqualTo("xxx"); assertThat(polarisRateLimitProperties.getRejectRequestTipsFilePath()).isEqualTo("/index.html");
assertThat(polarisRateLimitProperties.getRejectRequestTipsFilePath()).isEqualTo("/index.html"); assertThat(polarisRateLimitProperties.getRejectHttpCode()).isEqualTo(419);
assertThat(polarisRateLimitProperties.getRejectHttpCode()).isEqualTo(419); assertThat(polarisRateLimitProperties.getMaxQueuingTime()).isEqualTo(500L);
assertThat(polarisRateLimitProperties.getMaxQueuingTime()).isEqualTo(500L);
});
} }
@Configuration @SpringBootApplication
@EnableAutoConfiguration protected static class TestApplication {
static class PolarisRateLimitPropertiesAutoConfiguration {
} }
} }

@ -0,0 +1,4 @@
spring.cloud.polaris.ratelimit.rejectRequestTips=xxx
spring.cloud.polaris.ratelimit.rejectRequestTipsFilePath=/index.html
spring.cloud.polaris.ratelimit.rejectHttpCode=419
spring.cloud.polaris.ratelimit.maxQueuingTime=500
Loading…
Cancel
Save