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:upgrade api circuit breaker. #1438

Merged
merged 4 commits into from
Sep 26, 2024
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@
- [fix:fix contract reporting bug when using TSF.](https://github.com/Tencent/spring-cloud-tencent/pull/1373)
- [fix: fix TSF context bootstrap configuration](https://github.com/Tencent/spring-cloud-tencent/pull/1395)
- [fix: fix PolarisCircuitBreakerConfiguration not clear when gateway invoke by wildcard apis](https://github.com/Tencent/spring-cloud-tencent/pull/1418)
- [fix:fix actuator name warning from #1428 .](https://github.com/Tencent/spring-cloud-tencent/pull/1429)
- [fix:fix actuator name warning from #1428 .](https://github.com/Tencent/spring-cloud-tencent/pull/1429)
- [feat:upgrade api circuit breaker.](https://github.com/Tencent/spring-cloud-tencent/pull/1438)
6 changes: 0 additions & 6 deletions spring-cloud-starter-tencent-polaris-circuitbreaker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-stub-runner</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-test-common</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public class PolarisCircuitBreaker implements CircuitBreaker {
public PolarisCircuitBreaker(PolarisCircuitBreakerConfigBuilder.PolarisCircuitBreakerConfiguration conf,
ConsumerAPI consumerAPI,
CircuitBreakAPI circuitBreakAPI) {
FunctionalDecoratorRequest makeDecoratorRequest = new FunctionalDecoratorRequest(new ServiceKey(conf.getNamespace(), conf.getService()), conf.getMethod());
FunctionalDecoratorRequest makeDecoratorRequest = new FunctionalDecoratorRequest(
new ServiceKey(conf.getNamespace(), conf.getService()), conf.getProtocol(), conf.getMethod(), conf.getPath());
makeDecoratorRequest.setSourceService(new ServiceKey(conf.getSourceNamespace(), conf.getSourceService()));
makeDecoratorRequest.setResultToErrorCode(new PolarisResultToErrorCode());
this.consumerAPI = consumerAPI;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,25 @@
public class PolarisCircuitBreakerFactory
extends CircuitBreakerFactory<PolarisCircuitBreakerConfigBuilder.PolarisCircuitBreakerConfiguration, PolarisCircuitBreakerConfigBuilder> implements DisposableBean {

private final CircuitBreakAPI circuitBreakAPI;

private final ConsumerAPI consumerAPI;

private final ScheduledExecutorService cleanupService = Executors.newSingleThreadScheduledExecutor(
new NamedThreadFactory("sct-circuitbreaker-cleanup", true));

private Function<String, PolarisCircuitBreakerConfigBuilder.PolarisCircuitBreakerConfiguration> defaultConfiguration =
id -> {
String[] metadata = PolarisCircuitBreakerUtils.resolveCircuitBreakerId(id);
return new PolarisCircuitBreakerConfigBuilder()
.namespace(metadata[0])
.service(metadata[1])
.method(metadata[2])
.path(metadata[2])
.protocol(metadata[3])
.method(metadata[4])
.build();
};


private final CircuitBreakAPI circuitBreakAPI;

private final ConsumerAPI consumerAPI;

private final ScheduledExecutorService cleanupService = Executors.newSingleThreadScheduledExecutor(
new NamedThreadFactory("sct-circuitbreaker-cleanup", true));

public PolarisCircuitBreakerFactory(CircuitBreakAPI circuitBreakAPI, ConsumerAPI consumerAPI,
PolarisCircuitBreakerProperties polarisCircuitBreakerProperties) {
this.circuitBreakAPI = circuitBreakAPI;
Expand All @@ -83,7 +84,7 @@ public CircuitBreaker create(String id) {
@Override
protected PolarisCircuitBreakerConfigBuilder configBuilder(String id) {
String[] metadata = PolarisCircuitBreakerUtils.resolveCircuitBreakerId(id);
return new PolarisCircuitBreakerConfigBuilder(metadata[0], metadata[1], metadata[2]);
return new PolarisCircuitBreakerConfigBuilder(metadata[0], metadata[1], metadata[2], metadata[3], metadata[4]);
}

@Override
Expand All @@ -93,7 +94,7 @@ public void configureDefault(Function<String, PolarisCircuitBreakerConfigBuilder

@Override
public void destroy() {
ThreadPoolUtils.waitAndStopThreadPools(new ExecutorService[]{cleanupService});
ThreadPoolUtils.waitAndStopThreadPools(new ExecutorService[] {cleanupService});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public class ReactivePolarisCircuitBreaker implements ReactiveCircuitBreaker {
public ReactivePolarisCircuitBreaker(PolarisCircuitBreakerConfigBuilder.PolarisCircuitBreakerConfiguration conf,
ConsumerAPI consumerAPI,
CircuitBreakAPI circuitBreakAPI) {
InvokeContext.RequestContext requestContext = new FunctionalDecoratorRequest(new ServiceKey(conf.getNamespace(), conf.getService()), conf.getMethod());
InvokeContext.RequestContext requestContext = new FunctionalDecoratorRequest(
new ServiceKey(conf.getNamespace(), conf.getService()), conf.getProtocol(), conf.getMethod(), conf.getPath());
requestContext.setSourceService(new ServiceKey(conf.getSourceNamespace(), conf.getSourceService()));
requestContext.setResultToErrorCode(new PolarisResultToErrorCode());
this.consumerAPI = consumerAPI;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,25 @@
public class ReactivePolarisCircuitBreakerFactory extends
ReactiveCircuitBreakerFactory<PolarisCircuitBreakerConfigBuilder.PolarisCircuitBreakerConfiguration, PolarisCircuitBreakerConfigBuilder> implements DisposableBean {

private final CircuitBreakAPI circuitBreakAPI;

private final ConsumerAPI consumerAPI;

private final ScheduledExecutorService cleanupService = Executors.newSingleThreadScheduledExecutor(
new NamedThreadFactory("sct-reactive-circuitbreaker-cleanup", true));

private Function<String, PolarisCircuitBreakerConfigBuilder.PolarisCircuitBreakerConfiguration> defaultConfiguration =
id -> {
String[] metadata = PolarisCircuitBreakerUtils.resolveCircuitBreakerId(id);
return new PolarisCircuitBreakerConfigBuilder()
.namespace(metadata[0])
.service(metadata[1])
.method(metadata[2])
.path(metadata[2])
.protocol(metadata[3])
.method(metadata[4])
.build();
};

private final CircuitBreakAPI circuitBreakAPI;

private final ConsumerAPI consumerAPI;

private final ScheduledExecutorService cleanupService = Executors.newSingleThreadScheduledExecutor(
new NamedThreadFactory("sct-reactive-circuitbreaker-cleanup", true));

public ReactivePolarisCircuitBreakerFactory(CircuitBreakAPI circuitBreakAPI, ConsumerAPI consumerAPI,
PolarisCircuitBreakerProperties polarisCircuitBreakerProperties) {
this.circuitBreakAPI = circuitBreakAPI;
Expand All @@ -72,17 +74,17 @@ public ReactivePolarisCircuitBreakerFactory(CircuitBreakAPI circuitBreakAPI, Con
polarisCircuitBreakerProperties.getConfigurationCleanupInterval(), TimeUnit.MILLISECONDS);
}


@Override
public ReactiveCircuitBreaker create(String id) {
PolarisCircuitBreakerConfigBuilder.PolarisCircuitBreakerConfiguration conf = getConfigurations()
.computeIfAbsent(id, defaultConfiguration);
return new ReactivePolarisCircuitBreaker(conf, consumerAPI, circuitBreakAPI);
}

@Override
protected PolarisCircuitBreakerConfigBuilder configBuilder(String id) {
String[] metadata = PolarisCircuitBreakerUtils.resolveCircuitBreakerId(id);
return new PolarisCircuitBreakerConfigBuilder(metadata[0], metadata[1], metadata[2]);
return new PolarisCircuitBreakerConfigBuilder(metadata[0], metadata[1], metadata[2], metadata[3], metadata[4]);
}

@Override
Expand All @@ -93,6 +95,6 @@ public void configureDefault(

@Override
public void destroy() {
ThreadPoolUtils.waitAndStopThreadPools(new ExecutorService[]{cleanupService});
ThreadPoolUtils.waitAndStopThreadPools(new ExecutorService[] {cleanupService});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,21 @@ public class PolarisCircuitBreakerConfigBuilder implements ConfigBuilder<Polaris

private String service;

private String protocol;

private String method;

private String path;

public PolarisCircuitBreakerConfigBuilder() {

}

public PolarisCircuitBreakerConfigBuilder(String namespace, String service, String method) {
public PolarisCircuitBreakerConfigBuilder(String namespace, String service, String path, String protocol, String method) {
this.namespace = namespace;
this.service = service;
this.path = path;
this.protocol = protocol;
this.method = method;
}

Expand All @@ -54,17 +60,29 @@ public PolarisCircuitBreakerConfigBuilder service(String service) {
return this;
}

public PolarisCircuitBreakerConfigBuilder protocol(String protocol) {
this.protocol = protocol;
return this;
}

public PolarisCircuitBreakerConfigBuilder method(String method) {
this.method = method;
return this;
}

public PolarisCircuitBreakerConfigBuilder path(String path) {
this.path = path;
return this;
}

@Override
public PolarisCircuitBreakerConfiguration build() {
PolarisCircuitBreakerConfiguration conf = new PolarisCircuitBreakerConfiguration();
conf.setNamespace(namespace);
conf.setService(service);
conf.setProtocol(protocol);
conf.setMethod(method);
conf.setPath(path);
return conf;
}

Expand All @@ -78,8 +96,12 @@ public static class PolarisCircuitBreakerConfiguration {

private String service;

private String protocol;

private String method;

private String path;

public String getNamespace() {
return namespace;
}
Expand All @@ -96,6 +118,14 @@ public void setService(String service) {
this.service = service;
}

public String getProtocol() {
return protocol;
}

public void setProtocol(String protocol) {
this.protocol = protocol;
}

public String getMethod() {
return method;
}
Expand All @@ -104,6 +134,14 @@ public void setMethod(String method) {
this.method = method;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public String getSourceNamespace() {
return sourceNamespace;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
import java.net.URISyntaxException;

import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.polaris.api.utils.CollectionUtils;
import com.tencent.polaris.api.utils.StringUtils;
import feign.Request;
import feign.Target;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.cloud.openfeign.CircuitBreakerNameResolver;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import static org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation;

Expand All @@ -47,7 +50,7 @@ public String resolveCircuitBreakerName(String feignClientName, Target<?> target
String path = "";

// Get path in @FeignClient.
if (StringUtils.hasText(target.url())) {
if (StringUtils.isNotBlank(target.url())) {
URI uri = null;
try {
uri = new URI(target.url());
Expand All @@ -60,16 +63,24 @@ public String resolveCircuitBreakerName(String feignClientName, Target<?> target
}
}

// Get path in @RequestMapping.
// Get path and method in @RequestMapping.
RequestMapping requestMapping = findMergedAnnotation(method, RequestMapping.class);
String httpMethod = Request.HttpMethod.GET.name();
if (requestMapping != null) {
path += requestMapping.path().length == 0 ?
requestMapping.value().length == 0 ? "" : requestMapping.value()[0] :
requestMapping.path()[0];

RequestMethod[] requestMethods = requestMapping.method();
if (CollectionUtils.isNotEmpty(requestMethods)) {
httpMethod = requestMethods[0].name();
}
}
return "".equals(path) ?


return StringUtils.isBlank(path) ?
MetadataContext.LOCAL_NAMESPACE + "#" + serviceName :
MetadataContext.LOCAL_NAMESPACE + "#" + serviceName + "#" + path;
MetadataContext.LOCAL_NAMESPACE + "#" + serviceName + "#" + path + "#http#" + httpMethod;
}

}
Loading
Loading