add switch for report call result and default false (#550)

pull/559/head
lepdou 2 years ago committed by GitHub
parent 74287e3001
commit 99d9f59fd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -28,3 +28,4 @@
- [add feature-env plugin & add spring cloud gateway staining plugin](https://github.com/Tencent/spring-cloud-tencent/pull/533)
- [feature:add @ConditionalOnConfigReflectEnabled annotation](https://github.com/Tencent/spring-cloud-tencent/pull/539)
- [fix:set error handler named EnhancedRestTemplateReporter for RestTemplate](https://github.com/Tencent/spring-cloud-tencent/pull/545)
- [Optimize: add switch for report call result and default false](https://github.com/Tencent/spring-cloud-tencent/pull/550)

@ -20,10 +20,13 @@ package com.tencent.cloud.polaris.circuitbreaker.config;
import com.tencent.cloud.common.constant.ContextConstant;
import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled;
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementAutoConfiguration;
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
import com.tencent.polaris.api.config.consumer.ServiceRouterConfig;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.plugins.router.healthy.RecoverRouterConfig;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -36,17 +39,26 @@ import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
@ConditionalOnPolarisEnabled
@ConditionalOnProperty(value = "spring.cloud.polaris.circuitbreaker.enabled", havingValue = "true", matchIfMissing = true)
@AutoConfigureAfter(RpcEnhancementAutoConfiguration.class)
public class PolarisCircuitBreakerAutoConfiguration {
@Bean
public CircuitBreakerConfigModifier circuitBreakerConfigModifier() {
return new CircuitBreakerConfigModifier();
public CircuitBreakerConfigModifier circuitBreakerConfigModifier(RpcEnhancementReporterProperties properties) {
return new CircuitBreakerConfigModifier(properties);
}
public static class CircuitBreakerConfigModifier implements PolarisConfigModifier {
private final RpcEnhancementReporterProperties properties;
public CircuitBreakerConfigModifier(RpcEnhancementReporterProperties properties) {
this.properties = properties;
}
@Override
public void modify(ConfigurationImpl configuration) {
properties.setEnabled(true);
// Turn on circuitbreaker configuration
configuration.getConsumer().getCircuitBreaker().setEnable(true);

@ -17,10 +17,13 @@
package com.tencent.cloud.polaris.circuitbreaker.config;
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementAutoConfiguration;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
@ -31,7 +34,12 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class PolarisCircuitBreakerAutoConfigurationTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisCircuitBreakerAutoConfiguration.class))
.withConfiguration(AutoConfigurations.of(
PolarisContextAutoConfiguration.class,
RpcEnhancementAutoConfiguration.class,
LoadBalancerAutoConfiguration.class,
RpcEnhancementAutoConfiguration.class,
PolarisCircuitBreakerAutoConfiguration.class))
.withPropertyValues("spring.cloud.polaris.circuitbreaker.enabled=true");
@Test

@ -17,10 +17,13 @@
package com.tencent.cloud.polaris.circuitbreaker.config;
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementAutoConfiguration;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
@ -31,7 +34,11 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class PolarisCircuitBreakerBootstrapConfigurationTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisCircuitBreakerBootstrapConfiguration.class))
.withConfiguration(AutoConfigurations.of(PolarisContextAutoConfiguration.class,
RpcEnhancementAutoConfiguration.class,
LoadBalancerAutoConfiguration.class,
RpcEnhancementAutoConfiguration.class,
PolarisCircuitBreakerBootstrapConfiguration.class))
.withPropertyValues("spring.cloud.polaris.enabled=true")
.withPropertyValues("spring.cloud.polaris.circuitbreaker.enabled=true");

@ -86,8 +86,8 @@ public class RpcEnhancementAutoConfiguration {
}
@Bean
public ExceptionPolarisReporter exceptionPolarisReporter() {
return new ExceptionPolarisReporter();
public ExceptionPolarisReporter exceptionPolarisReporter(RpcEnhancementReporterProperties properties) {
return new ExceptionPolarisReporter(properties);
}
}
}

@ -0,0 +1,15 @@
package com.tencent.cloud.rpc.enhancement.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* Bootstrap configuration for rpc enhancement.
* @author lepdou 2022-08-24
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty("spring.cloud.polaris.enabled")
@Import(RpcEnhancementAutoConfiguration.class)
public class RpcEnhancementBootstrapConfiguration {
}

@ -32,6 +32,11 @@ import org.springframework.http.HttpStatus;
@ConfigurationProperties("spring.cloud.tencent.rpc-enhancement.reporter")
public class RpcEnhancementReporterProperties {
/**
* Whether report call result to polaris.
*/
private boolean enabled;
/**
* Specify the Http status code(s) that needs to be reported as FAILED.
*/
@ -60,6 +65,14 @@ public class RpcEnhancementReporterProperties {
return new ArrayList<>(Arrays.asList(items));
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public List<HttpStatus> getStatuses() {
return statuses;
}

@ -19,6 +19,7 @@ package com.tencent.cloud.rpc.enhancement.feign.plugin.reporter;
import java.net.SocketTimeoutException;
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
import com.tencent.cloud.rpc.enhancement.feign.plugin.EnhancedFeignContext;
import com.tencent.cloud.rpc.enhancement.feign.plugin.EnhancedFeignPlugin;
import com.tencent.cloud.rpc.enhancement.feign.plugin.EnhancedFeignPluginType;
@ -45,6 +46,12 @@ public class ExceptionPolarisReporter implements EnhancedFeignPlugin {
@Autowired(required = false)
private ConsumerAPI consumerAPI;
private RpcEnhancementReporterProperties reporterProperties;
public ExceptionPolarisReporter(RpcEnhancementReporterProperties reporterProperties) {
this.reporterProperties = reporterProperties;
}
@Override
public String getName() {
return ExceptionPolarisReporter.class.getName();
@ -57,6 +64,10 @@ public class ExceptionPolarisReporter implements EnhancedFeignPlugin {
@Override
public void run(EnhancedFeignContext context) {
if (!reporterProperties.isEnabled()) {
return;
}
if (consumerAPI != null) {
Request request = context.getRequest();
Response response = context.getResponse();

@ -61,6 +61,10 @@ public class SuccessPolarisReporter extends AbstractPolarisReporterAdapter imple
@Override
public void run(EnhancedFeignContext context) {
if (!properties.isEnabled()) {
return;
}
if (consumerAPI != null) {
Request request = context.getRequest();
Response response = context.getResponse();

@ -77,8 +77,11 @@ 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) {
if (!properties.isEnabled()) {
return;
}
ServiceCallResult resultRequest = createServiceCallResult(url);
try {
HttpURLConnection connection = (HttpURLConnection) ReflectionUtils.getFieldValue(response, FILE_NAME);
@ -95,7 +98,6 @@ public class EnhancedRestTemplateReporter extends AbstractPolarisReporterAdapter
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(),

@ -6,6 +6,12 @@
"defaultValue": true,
"description": "If rpc enhancement enabled."
},
{
"name": "spring.cloud.tencent.rpc-enhancement.reporter.enabled",
"type": "java.lang.Boolean",
"defaultValue": false,
"description": "Whether report call result to polaris."
},
{
"name": "spring.cloud.tencent.rpc-enhancement.reporter.ignore-internal-server-error",
"type": "java.lang.Boolean",

@ -1,5 +1,6 @@
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatPropertiesBootstrapConfiguration
com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatPropertiesBootstrapConfiguration,\
com.tencent.cloud.rpc.enhancement.config.RpcEnhancementBootstrapConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tencent.cloud.rpc.enhancement.config.RpcEnhancementAutoConfiguration,\
com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatPropertiesAutoConfiguration

@ -17,6 +17,7 @@
package com.tencent.cloud.rpc.enhancement.feign.plugin.reporter;
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
import com.tencent.cloud.rpc.enhancement.feign.plugin.EnhancedFeignContext;
import com.tencent.cloud.rpc.enhancement.feign.plugin.EnhancedFeignPluginType;
import com.tencent.polaris.api.core.ConsumerAPI;
@ -48,6 +49,8 @@ public class ExceptionPolarisReporterTest {
private static MockedStatic<ReporterUtils> mockedReporterUtils;
@Mock
private ConsumerAPI consumerAPI;
@Mock
private RpcEnhancementReporterProperties reporterProperties;
@InjectMocks
private ExceptionPolarisReporter exceptionPolarisReporter;

@ -17,6 +17,7 @@
package com.tencent.cloud.rpc.enhancement.feign.plugin.reporter;
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
import com.tencent.cloud.rpc.enhancement.feign.plugin.EnhancedFeignContext;
import com.tencent.cloud.rpc.enhancement.feign.plugin.EnhancedFeignPluginType;
import com.tencent.polaris.api.core.ConsumerAPI;
@ -49,6 +50,8 @@ public class SuccessPolarisReporterTest {
private static MockedStatic<ReporterUtils> mockedReporterUtils;
@Mock
private ConsumerAPI consumerAPI;
@Mock
private RpcEnhancementReporterProperties reporterProperties;
@InjectMocks
private SuccessPolarisReporter successPolarisReporter;

Loading…
Cancel
Save