From 1af5b70581663513ff2c3b60ec0ca8ec6268b732 Mon Sep 17 00:00:00 2001 From: lepdou Date: Thu, 8 Sep 2022 19:51:04 +0800 Subject: [PATCH] change default dynamic config refresh type to reflect (#574) --- CHANGELOG.md | 1 + .../ReflectRefreshTypeCondition.java | 9 +-- .../config/PolarisConfigProperties.java | 2 +- ...itional-spring-configuration-metadata.json | 2 +- .../ConditionalOnReflectRefreshTypeTest.java | 1 + .../callee/DataSourceProperties.java | 65 +++++++++++++++++++ .../callee/QuickstartCalleeController.java | 8 ++- .../src/main/resources/bootstrap.yml | 1 - .../callee/DataSourceProperties.java | 65 +++++++++++++++++++ .../callee/QuickstartCalleeController.java | 8 ++- .../src/main/resources/bootstrap.yml | 1 - .../QuickstartCalleeServiceFallback.java | 2 +- .../src/main/resources/polaris.yml | 14 ---- 13 files changed, 152 insertions(+), 27 deletions(-) create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/DataSourceProperties.java create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/DataSourceProperties.java delete mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/polaris.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index eb4a85ddc..5451c93cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,3 +34,4 @@ - [Optimize: optimize report call result for restTemplate.](https://github.com/Tencent/spring-cloud-tencent/pull/564) - [feat:add spring-cloud-starter-tencent-all and quickstart examples.](https://github.com/Tencent/spring-cloud-tencent/pull/568) - [refactor:optimize project and code.](https://github.com/Tencent/spring-cloud-tencent/pull/570) +- [Optimize:change default dynamic config refresh type to reflect.](https://github.com/Tencent/spring-cloud-tencent/pull/574) diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ReflectRefreshTypeCondition.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ReflectRefreshTypeCondition.java index c5c893c6e..b6dd08764 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ReflectRefreshTypeCondition.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ReflectRefreshTypeCondition.java @@ -37,16 +37,17 @@ public class ReflectRefreshTypeCondition extends SpringBootCondition { */ public static final String POLARIS_CONFIG_REFRESH_TYPE = "spring.cloud.polaris.config.refresh-type"; - private static final RefreshType DEFAULT_REFRESH_TYPE = RefreshType.REFRESH_CONTEXT; + private static final RefreshType DEFAULT_REFRESH_TYPE = RefreshType.REFLECT; @Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { RefreshType refreshType = context.getEnvironment() .getProperty(POLARIS_CONFIG_REFRESH_TYPE, RefreshType.class, DEFAULT_REFRESH_TYPE); - if (refreshType == DEFAULT_REFRESH_TYPE) { - return ConditionOutcome.noMatch("no matched"); + if (refreshType == RefreshType.REFLECT) { + return ConditionOutcome.match("matched"); } - return ConditionOutcome.match("matched"); + + return ConditionOutcome.noMatch("no matched"); } } 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 235b40cc9..3bc06f7c0 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 @@ -57,7 +57,7 @@ public class PolarisConfigProperties { /** * Attribute refresh type. */ - private RefreshType refreshType = RefreshType.REFRESH_CONTEXT; + private RefreshType refreshType = RefreshType.REFLECT; /** * List of injected configuration files. diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json index b394c540a..64172e637 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -63,7 +63,7 @@ { "name": "spring.cloud.polaris.config.refresh-type", "type": "com.tencent.cloud.polaris.config.enums.RefreshType", - "defaultValue": "refresh_context", + "defaultValue": "reflect", "description": "Attribute refresh type when config updated. refresh_context or reflect." }, { diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnReflectRefreshTypeTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnReflectRefreshTypeTest.java index 182883ab2..8ee9ab9d2 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnReflectRefreshTypeTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnReflectRefreshTypeTest.java @@ -100,6 +100,7 @@ public class ConditionalOnReflectRefreshTypeTest { .withPropertyValues("spring.application.name=" + "conditionalOnConfigReflectEnabledTest") .withPropertyValues("server.port=" + 8080) .withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081") + .withPropertyValues("spring.cloud.polaris.config.refresh-type=" + RefreshType.REFRESH_CONTEXT) .withPropertyValues("spring.cloud.polaris.config.enabled=true"); contextRunner.run(context -> { assertThat(context).hasSingleBean(PolarisConfigProperties.class); diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/DataSourceProperties.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/DataSourceProperties.java new file mode 100644 index 000000000..e676cda9c --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/DataSourceProperties.java @@ -0,0 +1,65 @@ +/* + * 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.quickstart.callee; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + * Example for ConfigurationProperties. + * @author lepdou 2022-09-08 + */ +@Component +@ConfigurationProperties("datasource") +public class DataSourceProperties { + + private String jdbcUrl; + private String username; + private String password; + + public String getJdbcUrl() { + return jdbcUrl; + } + + public void setJdbcUrl(String jdbcUrl) { + this.jdbcUrl = jdbcUrl; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + @Override + public String toString() { + return "jdbcUrl='" + jdbcUrl + '\'' + + ", username='" + username + '\'' + + ", password='" + password + '\''; + } +} diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java index 02920c291..eecf3291f 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java @@ -20,6 +20,7 @@ package com.tencent.cloud.quickstart.callee; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -43,6 +44,9 @@ public class QuickstartCalleeController { @Value("${appName:Callee}") private String appName; + @Autowired + private DataSourceProperties dataSourceProperties; + /** * Get sum of two value. * @param value1 value 1 @@ -61,8 +65,8 @@ public class QuickstartCalleeController { */ @GetMapping("/info") public String info() { - LOG.info("Quickstart [{}] Service [{}] is called.", appName, port); - return String.format("Quickstart [%s] Service [%s] is called.", appName, port); + LOG.info("Quickstart [{}] Service [{}] is called. datasource = {}", appName, port, dataSourceProperties); + return String.format("Quickstart [%s] Service [%s] is called. datasource = [%s]", appName, port, dataSourceProperties); } /** diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/bootstrap.yml index 65ba0b54c..2f1bd923f 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/bootstrap.yml @@ -17,7 +17,6 @@ spring: groups: - name: ${spring.application.name} files: [ "config/callee.properties" ] - refresh-type: reflect ratelimit: enabled: true maxQueuingTime: 500 diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/DataSourceProperties.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/DataSourceProperties.java new file mode 100644 index 000000000..a0c6b7922 --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/DataSourceProperties.java @@ -0,0 +1,65 @@ +/* + * 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.quickstart.callee; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + * Example for ConfigurationProperties. + * @author lepdou 2022-09-08 + */ +@Component +@ConfigurationProperties("datasource") +public class DataSourceProperties { + + private String jdbcUrl; + private String username; + private String password; + + public String getJdbcUrl() { + return jdbcUrl; + } + + public void setJdbcUrl(String jdbcUrl) { + this.jdbcUrl = jdbcUrl; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + @Override + public String toString() { + return "jdbcUrl='" + jdbcUrl + '\'' + + ", username='" + username + '\'' + + ", password='" + password + '\''; + } +} diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java index a690a23c2..0baf9e208 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java @@ -20,6 +20,7 @@ package com.tencent.cloud.quickstart.callee; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.GetMapping; @@ -45,6 +46,9 @@ public class QuickstartCalleeController { @Value("${appName:Callee}") private String appName; + @Autowired + private DataSourceProperties dataSourceProperties; + /** * Get sum of two value. * @param value1 value 1 @@ -63,8 +67,8 @@ public class QuickstartCalleeController { */ @GetMapping("/info") public String info() { - LOG.info("Quickstart [{}] Service [{}] is called.", appName, port); - return String.format("Quickstart [%s] Service [%s] is called.", appName, port); + LOG.info("Quickstart [{}] Service [{}] is called. datasource = {}", appName, port, dataSourceProperties); + return String.format("Quickstart [%s] Service [%s] is called. datasource = [%s]", appName, port, dataSourceProperties); } /** diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/resources/bootstrap.yml index f2e845e73..60442040b 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/resources/bootstrap.yml @@ -17,7 +17,6 @@ spring: groups: - name: ${spring.application.name} files: [ "config/callee.properties" ] - refresh-type: reflect ratelimit: enabled: true maxQueuingTime: 500 diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCalleeServiceFallback.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCalleeServiceFallback.java index 4e7a00035..546e503aa 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCalleeServiceFallback.java +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCalleeServiceFallback.java @@ -34,6 +34,6 @@ public class QuickstartCalleeServiceFallback implements QuickstartCalleeService @Override public String circuitBreak() { - return "CircuitBreak is triggered."; + return "An exception occurred in the service call and fallback"; } } diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/polaris.yml b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/polaris.yml deleted file mode 100644 index fbe20fc8f..000000000 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/polaris.yml +++ /dev/null @@ -1,14 +0,0 @@ -consumer: - circuitBreaker: - checkPeriod: 100ms - chain: - - errorCount - - errorRate - plugin: - errorCount: - continuousErrorThreshold: 1 - metricNumBuckets: 1 - errorRate: - errorRateThreshold: 100 - metricStatTimeWindow: 1s - requestVolumeThreshold: 1