diff --git a/CHANGELOG.md b/CHANGELOG.md index 0dcaf116..0a2a3634 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,3 +35,4 @@ - [Optimize: optimize report call result for restTemplate.](https://github.com/Tencent/spring-cloud-tencent/pull/565) - [feat:add spring-cloud-starter-tencent-all and quickstart examples.](https://github.com/Tencent/spring-cloud-tencent/pull/569) - [refactor:optimize project and code.](https://github.com/Tencent/spring-cloud-tencent/pull/571) +- - [Optimize:change default dynamic config refresh type to reflect.](https://github.com/Tencent/spring-cloud-tencent/pull/575) 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 c5c893c6..b6dd0876 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 a2fe878f..b9ffc5cd 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 @@ -63,7 +63,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 13214ad5..40002aba 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 @@ -57,7 +57,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 182883ab..8ee9ab9d 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 00000000..e676cda9 --- /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 02920c29..eecf3291 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 65ba0b54..2f1bd923 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 00000000..a0c6b792 --- /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 a690a23c..0baf9e20 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 f2e845e7..60442040 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 4e7a0003..546e503a 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 fbe20fc8..00000000 --- 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