feat:support default instance circuit breaker rule. ()

pull/1508/head
Haotian Zhang 1 month ago committed by GitHub
parent 79e549ea60
commit 4dbf8125d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -6,3 +6,4 @@
- [feat:support circuit breaker metrics reporting.](https://github.com/Tencent/spring-cloud-tencent/pull/1495)
- [feat: support gateway context, feign eager-load support default value.](https://github.com/Tencent/spring-cloud-tencent/pull/1496)
- [feat:use polaris-all for shading third-party dependencies.](https://github.com/Tencent/spring-cloud-tencent/pull/1498)
- [feat:support default instance circuit breaker rule.](https://github.com/Tencent/spring-cloud-tencent/pull/1499)

@ -18,6 +18,7 @@
package com.tencent.cloud.polaris.circuitbreaker.common;
import com.tencent.cloud.common.constant.OrderConstant;
import com.tencent.cloud.polaris.circuitbreaker.config.PolarisCircuitBreakerProperties;
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
import com.tencent.polaris.api.config.consumer.ServiceRouterConfig;
@ -33,8 +34,12 @@ public class CircuitBreakerConfigModifier implements PolarisConfigModifier {
private final RpcEnhancementReporterProperties properties;
public CircuitBreakerConfigModifier(RpcEnhancementReporterProperties properties) {
private final PolarisCircuitBreakerProperties polarisCircuitBreakerProperties;
public CircuitBreakerConfigModifier(RpcEnhancementReporterProperties properties,
PolarisCircuitBreakerProperties polarisCircuitBreakerProperties) {
this.properties = properties;
this.polarisCircuitBreakerProperties = polarisCircuitBreakerProperties;
}
@Override
@ -53,6 +58,17 @@ public class CircuitBreakerConfigModifier implements PolarisConfigModifier {
// Update modified config to source properties
configuration.getConsumer().getServiceRouter()
.setPluginConfig(ServiceRouterConfig.DEFAULT_ROUTER_RECOVER, recoverRouterConfig);
configuration.getConsumer().getCircuitBreaker()
.setDefaultRuleEnable(polarisCircuitBreakerProperties.isDefaultRuleEnabled());
configuration.getConsumer().getCircuitBreaker()
.setDefaultErrorCount(polarisCircuitBreakerProperties.getDefaultErrorCount());
configuration.getConsumer().getCircuitBreaker()
.setDefaultErrorPercent(polarisCircuitBreakerProperties.getDefaultErrorPercent());
configuration.getConsumer().getCircuitBreaker()
.setDefaultInterval(polarisCircuitBreakerProperties.getDefaultInterval());
configuration.getConsumer().getCircuitBreaker()
.setDefaultMinimumRequest(polarisCircuitBreakerProperties.getDefaultMinimumRequest());
}
@Override

@ -87,8 +87,9 @@ public class PolarisCircuitBreakerAutoConfiguration {
@Bean
@ConditionalOnMissingBean(CircuitBreakerConfigModifier.class)
public CircuitBreakerConfigModifier circuitBreakerConfigModifier(RpcEnhancementReporterProperties properties) {
return new CircuitBreakerConfigModifier(properties);
public CircuitBreakerConfigModifier circuitBreakerConfigModifier(RpcEnhancementReporterProperties properties,
PolarisCircuitBreakerProperties polarisCircuitBreakerProperties) {
return new CircuitBreakerConfigModifier(properties, polarisCircuitBreakerProperties);
}
@Bean

@ -39,6 +39,31 @@ public class PolarisCircuitBreakerProperties {
@Value("${spring.cloud.polaris.circuitbreaker.configuration-cleanup-interval:#{300000}}")
private long configurationCleanupInterval = 300000;
/**
* If default circuit breaker rule is enabled. Default is true.
*/
private boolean defaultRuleEnabled = true;
/**
* Consecutive error count circuit-breaker default error count. Default is 10.
*/
private int defaultErrorCount = 10;
/**
* Error rate circuit-breaker default error rate percent. Default is 50.
*/
private int defaultErrorPercent = 50;
/**
* Error rate circuit-breaker default interval(ms). Default is 60000.
*/
private int defaultInterval = 60000;
/**
* Error rate circuit-breaker default minimum request. Default is 10.
*/
private int defaultMinimumRequest = 10;
public boolean isEnabled() {
return enabled;
}
@ -54,4 +79,57 @@ public class PolarisCircuitBreakerProperties {
public void setConfigurationCleanupInterval(long configurationCleanupInterval) {
this.configurationCleanupInterval = configurationCleanupInterval;
}
public boolean isDefaultRuleEnabled() {
return defaultRuleEnabled;
}
public void setDefaultRuleEnabled(boolean defaultRuleEnabled) {
this.defaultRuleEnabled = defaultRuleEnabled;
}
public int getDefaultErrorCount() {
return defaultErrorCount;
}
public void setDefaultErrorCount(int defaultErrorCount) {
this.defaultErrorCount = defaultErrorCount;
}
public int getDefaultErrorPercent() {
return defaultErrorPercent;
}
public void setDefaultErrorPercent(int defaultErrorPercent) {
this.defaultErrorPercent = defaultErrorPercent;
}
public int getDefaultInterval() {
return defaultInterval;
}
public void setDefaultInterval(int defaultInterval) {
this.defaultInterval = defaultInterval;
}
public int getDefaultMinimumRequest() {
return defaultMinimumRequest;
}
public void setDefaultMinimumRequest(int defaultMinimumRequest) {
this.defaultMinimumRequest = defaultMinimumRequest;
}
@Override
public String toString() {
return "PolarisCircuitBreakerProperties{" +
"enabled=" + enabled +
", configurationCleanupInterval=" + configurationCleanupInterval +
", defaultRuleEnabled=" + defaultRuleEnabled +
", defaultErrorCount=" + defaultErrorCount +
", defaultErrorPercent=" + defaultErrorPercent +
", defaultInterval=" + defaultInterval +
", defaultMinimumRequest=" + defaultMinimumRequest +
'}';
}
}

@ -11,6 +11,36 @@
"type": "java.lang.Long",
"defaultValue": "300000",
"description": "Interval to clean up PolarisCircuitBreakerConfiguration, unit millisecond."
},
{
"name": "spring.cloud.polaris.circuitbreaker.default-rule-enabled",
"type": "java.lang.Boolean",
"defaultValue": "true",
"description": "If default circuit breaker rule is enabled."
},
{
"name": "spring.cloud.polaris.circuitbreaker.default-error-count",
"type": "java.lang.Integer",
"defaultValue": 10,
"description": "Consecutive error count circuit-breaker default error count."
},
{
"name": "spring.cloud.polaris.circuitbreaker.default-error-percent",
"type": "java.lang.Integer",
"defaultValue": 50,
"description": "Error rate circuit-breaker default error rate percent."
},
{
"name": "spring.cloud.polaris.circuitbreaker.default-interval",
"type": "java.lang.Integer",
"defaultValue": 60000,
"description": "Error rate circuit-breaker default interval(ms)."
},
{
"name": "spring.cloud.polaris.circuitbreaker.default-minimum-request",
"type": "java.lang.Integer",
"defaultValue": 10,
"description": "Error rate circuit-breaker default minimum request."
}
],
"hints": []

@ -0,0 +1,68 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 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.circuitbreaker.config;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link PolarisCircuitBreakerProperties}.
*
* @author Haotian Zhang
*/
public class PolarisCircuitBreakerPropertiesTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(TestApplication.TestConfig.class))
.withPropertyValues("spring.cloud.polaris.circuitbreaker.enabled=true")
.withPropertyValues("spring.cloud.polaris.circuitbreaker.default-rule-enabled=false")
.withPropertyValues("spring.cloud.polaris.circuitbreaker.default-error-count=1")
.withPropertyValues("spring.cloud.polaris.circuitbreaker.default-error-percent=2")
.withPropertyValues("spring.cloud.polaris.circuitbreaker.default-interval=3")
.withPropertyValues("spring.cloud.polaris.circuitbreaker.default-minimum-request=4");
@Test
public void testDefaultInitialization() {
this.contextRunner.run(context -> {
assertThat(context).hasSingleBean(PolarisCircuitBreakerProperties.class);
PolarisCircuitBreakerProperties polarisCircuitBreakerProperties = context.getBean(PolarisCircuitBreakerProperties.class);
assertThat(polarisCircuitBreakerProperties.isDefaultRuleEnabled()).isFalse();
assertThat(polarisCircuitBreakerProperties.getDefaultErrorCount()).isEqualTo(1);
assertThat(polarisCircuitBreakerProperties.getDefaultErrorPercent()).isEqualTo(2);
assertThat(polarisCircuitBreakerProperties.getDefaultInterval()).isEqualTo(3);
assertThat(polarisCircuitBreakerProperties.getDefaultMinimumRequest()).isEqualTo(4);
});
}
@EnableAutoConfiguration
protected static class TestApplication {
@Configuration
@EnableConfigurationProperties(PolarisCircuitBreakerProperties.class)
protected static class TestConfig {
}
}
}
Loading…
Cancel
Save