Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement circuit breaker in enhance plugin, support listen config group, support refresh single config in refresh_context mode. #1490

Merged
merged 5 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@
- [feat:support smooth upgrade from tsf.](https://github.com/Tencent/spring-cloud-tencent/pull/1472)
- [fix:fix caller disposable metadata handle when using tracing.](https://github.com/Tencent/spring-cloud-tencent/pull/1476)
- [refactor:update registry status.](https://github.com/Tencent/spring-cloud-tencent/pull/1486)
- [feat: implement circuit breaker in enhance plugin, support listen config group, support refresh single config in refresh_context mode.](https://github.com/Tencent/spring-cloud-tencent/pull/1490)
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
<revision>2.0.1.0-2021.0.9-SNAPSHOT</revision>

<!-- Spring Framework -->
<spring.framework.version>5.3.31</spring.framework.version>
<spring.framework.version>5.3.39</spring.framework.version>

<!-- Spring Boot -->
<spring.boot.version>2.7.18</spring.boot.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
package com.tencent.cloud.metadata.core;

import java.net.URI;
import java.util.Arrays;
import java.util.Map;

import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.rpc.enhancement.instrument.resttemplate.EnhancedRestTemplateInterceptor;
import com.tencent.cloud.rpc.enhancement.plugin.DefaultEnhancedPluginRunner;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

Expand Down Expand Up @@ -79,13 +76,7 @@ protected static class TestApplication {

@Bean
public RestTemplate restTemplate() {

EncodeTransferMedataRestTemplateEnhancedPlugin plugin = new EncodeTransferMedataRestTemplateEnhancedPlugin();
EnhancedRestTemplateInterceptor interceptor = new EnhancedRestTemplateInterceptor(
new DefaultEnhancedPluginRunner(Arrays.asList(plugin), new MockRegistration(), null));
RestTemplate template = new RestTemplate();
template.setInterceptors(Arrays.asList(interceptor));
return template;
return new RestTemplate();
}

@RequestMapping("/test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI;
import com.tencent.polaris.circuitbreak.api.FunctionalDecorator;
import com.tencent.polaris.circuitbreak.api.InvokeHandler;
import com.tencent.polaris.circuitbreak.api.pojo.FunctionalDecoratorRequest;
import com.tencent.polaris.circuitbreak.api.pojo.InvokeContext;
import com.tencent.polaris.circuitbreak.client.exception.CallAbortedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -39,7 +41,7 @@
*
* @author seanyu 2023-02-27
*/
public class PolarisCircuitBreaker implements CircuitBreaker {
public class PolarisCircuitBreaker implements CircuitBreaker, InvokeHandler {

private static final Logger LOGGER = LoggerFactory.getLogger(PolarisCircuitBreaker.class);

Expand All @@ -49,6 +51,8 @@ public class PolarisCircuitBreaker implements CircuitBreaker {

private final ConsumerAPI consumerAPI;

private final InvokeHandler invokeHandler;

public PolarisCircuitBreaker(PolarisCircuitBreakerConfigBuilder.PolarisCircuitBreakerConfiguration conf,
ConsumerAPI consumerAPI,
CircuitBreakAPI circuitBreakAPI) {
Expand All @@ -59,6 +63,7 @@ public PolarisCircuitBreaker(PolarisCircuitBreakerConfigBuilder.PolarisCircuitBr
this.consumerAPI = consumerAPI;
this.conf = conf;
this.decorator = circuitBreakAPI.makeFunctionalDecorator(makeDecoratorRequest);
this.invokeHandler = circuitBreakAPI.makeInvokeHandler(makeDecoratorRequest);
}

@Override
Expand All @@ -77,4 +82,18 @@ public <T> T run(Supplier<T> toRun, Function<Throwable, T> fallback) {
}
}

@Override
public void acquirePermission() {
invokeHandler.acquirePermission();
}

@Override
public void onSuccess(InvokeContext.ResponseContext responseContext) {
invokeHandler.onSuccess(responseContext);
}

@Override
public void onError(InvokeContext.ResponseContext responseContext) {
invokeHandler.onError(responseContext);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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.beanprocessor;

import com.tencent.cloud.polaris.circuitbreaker.instrument.resttemplate.PolarisLoadBalancerInterceptor;
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginRunner;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor;
import org.springframework.cloud.client.loadbalancer.LoadBalancerRequestFactory;
import org.springframework.core.Ordered;
import org.springframework.lang.NonNull;

/**
* LoadBalancerInterceptorBeanPostProcessor is used to wrap the default LoadBalancerInterceptor implementation and returns a custom PolarisLoadBalancerInterceptor.
*
* @author Shedfree Wu
*/
public class LoadBalancerInterceptorBeanPostProcessor implements BeanPostProcessor, BeanFactoryAware, Ordered {
/**
* The order of the bean post processor. if user want to wrap it(CustomLoadBalancerInterceptor -> PolarisLoadBalancerInterceptor), CustomLoadBalancerInterceptorBeanPostProcessor's order should be bigger than ${@link POLARIS_LOAD_BALANCER_INTERCEPTOR_POST_PROCESSOR_ORDER}.
*/
public static final int POLARIS_LOAD_BALANCER_INTERCEPTOR_POST_PROCESSOR_ORDER = 0;

private BeanFactory factory;

@Override
public void setBeanFactory(@NonNull BeanFactory beanFactory) throws BeansException {
this.factory = beanFactory;
}

@Override
public Object postProcessBeforeInitialization(@NonNull Object bean, @NonNull String beanName) throws BeansException {
if (bean instanceof LoadBalancerInterceptor) {
// Support rest template router.
// Replaces the default LoadBalancerInterceptor implementation and returns a custom PolarisLoadBalancerInterceptor
LoadBalancerRequestFactory requestFactory = this.factory.getBean(LoadBalancerRequestFactory.class);
LoadBalancerClient loadBalancerClient = this.factory.getBean(LoadBalancerClient.class);
EnhancedPluginRunner pluginRunner = this.factory.getBean(EnhancedPluginRunner.class);
return new PolarisLoadBalancerInterceptor(loadBalancerClient, requestFactory, pluginRunner);
}
return bean;
}

@Override
public int getOrder() {
return POLARIS_LOAD_BALANCER_INTERCEPTOR_POST_PROCESSOR_ORDER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.tencent.cloud.polaris.circuitbreaker.common;

import com.tencent.cloud.polaris.context.CircuitBreakerStatusCodeException;
import com.tencent.polaris.circuitbreak.api.pojo.ResultToErrorCode;
import feign.FeignException;

Expand Down Expand Up @@ -49,6 +50,9 @@ else if (checkClassExist("org.springframework.web.reactive.function.client.WebCl
&& e instanceof WebClientResponseException) {
return ((WebClientResponseException) e).getRawStatusCode();
}
else if (e instanceof CircuitBreakerStatusCodeException) {
return ((CircuitBreakerStatusCodeException) e).getRawStatusCode();
}
return -1;
}

Expand Down
Loading
Loading