Skip to content

fix count circuit breaker in gateway & return 404 when context api does not match. #1509

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

Merged
merged 3 commits into from
Feb 28, 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 @@ -52,3 +52,4 @@
- [feat:use polaris-all for shading third-party dependencies.](https://github.com/Tencent/spring-cloud-tencent/pull/1505)
- [feat:support default instance circuit breaker rule.](https://github.com/Tencent/spring-cloud-tencent/pull/1506)
- [docs:update JDK version configuration in GitHub Actions.](https://github.com/Tencent/spring-cloud-tencent/pull/1507)
- [fix: fix count circuit breaker in gateway & return 404 when context api does not match.](https://github.com/Tencent/spring-cloud-tencent/pull/1509)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
spring:
cloud:
tencent:
gateway:
groups:
group-scg2p:
predicate:
apiType: ms
context: /group1
namespace:
key: null
position: PATH
service:
key: null
position: PATH
routes:
- host: null
metadata: {}
method: GET
namespace: default
path: /echo/{param}
service: provider-demo
routes:
group1:
filters:
- Context=group1
order: -1
predicates:
- Context=group1
- Path=/group1/**
uri: lb://group1
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static class ContextPluginConfiguration {
@Value("${spring.cloud.polaris.discovery.eager-load.enabled:#{'true'}}")
private boolean commonEagerLoadEnabled;

@Value("${spring.cloud.polaris.discovery.eager-load.gateway.enabled:#{'true'}}")
@Value("${spring.cloud.polaris.discovery.eager-load.gateway.enabled:#{'false'}}")
private boolean gatewayEagerLoadEnabled;

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public int getOrder() {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
// restore context from exchange
MetadataContext metadataContext = (MetadataContext) exchange.getAttributes().get(
MetadataContext metadataContext = exchange.getAttribute(
MetadataConstant.HeaderName.METADATA_CONTEXT);
if (metadataContext != null) {
MetadataContextHolder.set(metadataContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
import org.springframework.core.Ordered;

public class PolarisReactiveLoadBalancerClientFilterBeanPostProcessor implements BeanPostProcessor, Ordered {
/**
* Order of this bean post processor.
*/
public static final int ORDER = 0;

private ApplicationContext applicationContext;

Expand All @@ -46,6 +50,6 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw

@Override
public int getOrder() {
return 0;
return ORDER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.support.NotFoundException;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;

import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR;


public class ContextGatewayFilter implements GatewayFilter, Ordered {

private static final Logger logger = LoggerFactory.getLogger(ContextGatewayFilter.class);
Expand Down Expand Up @@ -69,44 +71,48 @@ private Mono<Void> externalFilter(ServerWebExchange exchange, GatewayFilterChain
String[] apis = rebuildExternalApi(request, request.getPath().value());
GroupContext.ContextRoute contextRoute = manager.getGroupPathRoute(config.getGroup(), apis[0]);
if (contextRoute == null) {
throw new RuntimeException(String.format("Can't find context route for group: %s, path: %s, origin path: %s", config.getGroup(), apis[0], request.getPath()));
String msg = String.format("[externalFilter] Can't find context route for group: %s, path: %s, origin path: %s", config.getGroup(), apis[0], request.getPath());
logger.warn(msg);
throw NotFoundException.create(true, msg);
}
updateRouteMetadata(exchange, contextRoute);

URI requestUri = URI.create(contextRoute.getHost() + apis[1]);
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, requestUri);
// 调整为正确路径
// to correct path
ServerHttpRequest newRequest = request.mutate().path(apis[1]).build();
return chain.filter(exchange.mutate().request(newRequest).build());
}

private Mono<Void> msFilter(ServerWebExchange exchange, GatewayFilterChain chain, GroupContext groupContext) {
ServerHttpRequest request = exchange.getRequest();
String[] apis = rebuildMsApi(request, groupContext, request.getPath().value());
// 判断 api 是否匹配
// check api
GroupContext.ContextRoute contextRoute = manager.getGroupPathRoute(config.getGroup(), apis[0]);
if (contextRoute == null) {
throw new RuntimeException(String.format("Can't find context route for group: %s, path: %s, origin path: %s", config.getGroup(), apis[0], request.getPath()));
String msg = String.format("[msFilter] Can't find context route for group: %s, path: %s, origin path: %s", config.getGroup(), apis[0], request.getPath());
logger.warn(msg);
throw NotFoundException.create(true, msg);
}
updateRouteMetadata(exchange, contextRoute);

MetadataContext metadataContext = (MetadataContext) exchange.getAttributes().get(
MetadataContext metadataContext = exchange.getAttribute(
MetadataConstant.HeaderName.METADATA_CONTEXT);

metadataContext.putFragmentContext(MetadataContext.FRAGMENT_APPLICATION_NONE,
MetadataConstant.POLARIS_TARGET_NAMESPACE, contextRoute.getNamespace());

if (metadataContext != null) {
metadataContext.putFragmentContext(MetadataContext.FRAGMENT_APPLICATION_NONE,
MetadataConstant.POLARIS_TARGET_NAMESPACE, contextRoute.getNamespace());
}
URI requestUri = URI.create("lb://" + contextRoute.getService() + apis[1]);
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, requestUri);
// 调整为正确路径
// to correct path
ServerHttpRequest newRequest = request.mutate().path(apis[1]).build();
return chain.filter(exchange.mutate().request(newRequest).build());
}

/**
* e.g. "/context/api/test" → [ "GET|/api/test", "/api/test"]
*/
private String[] rebuildExternalApi(ServerHttpRequest request, String path) {
String[] rebuildExternalApi(ServerHttpRequest request, String path) {
String[] pathSegments = path.split("/");
StringBuilder matchPath = new StringBuilder();
StringBuilder realPath = new StringBuilder();
Expand All @@ -127,7 +133,7 @@ private String[] rebuildExternalApi(ServerHttpRequest request, String path) {
* returns an array of two strings, the first is the match path, the second is the real path.
* e.g. "/context/namespace/svc/api/test" → [ "GET|/namespace/svc/api/test", "/api/test"]
*/
private String[] rebuildMsApi(ServerHttpRequest request, GroupContext groupContext, String path) {
String[] rebuildMsApi(ServerHttpRequest request, GroupContext groupContext, String path) {
String[] pathSegments = path.split("/");
StringBuilder matchPath = new StringBuilder();
int index = 2;
Expand Down Expand Up @@ -181,7 +187,7 @@ private void updateRouteMetadata(ServerWebExchange exchange, GroupContext.Contex
}

Route route = (Route) exchange.getAttributes().get(GATEWAY_ROUTE_ATTR);
Constructor constructor = Route.class.getDeclaredConstructors()[1];
Constructor constructor = Route.class.getDeclaredConstructors()[0];
constructor.setAccessible(true);
try {
HashMap<String, Object> metadata = new HashMap<>(route.getMetadata());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,4 @@ public void setGroup(String group) {
this.group = group;
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public Map<String, Map<String, GroupContext.ContextRoute>> getGroupPathRouteMap(
return groupPathRouteMap;
}

public Map<String, Map<String, GroupContext.ContextRoute>> getGroupWildcardPathRouteMap() {
return groupWildcardPathRouteMap;
}

public void setGroupRouteMap(Map<String, GroupContext> groups) {

ConcurrentHashMap<String, Map<String, GroupContext.ContextRoute>> newGroupPathRouteMap = new ConcurrentHashMap<>();
Expand All @@ -63,7 +67,7 @@ public void setGroupRouteMap(Map<String, GroupContext> groups) {
for (GroupContext.ContextRoute route : entry.getValue().getRoutes()) {
String path = route.getPath();
// convert path parameter to group wildcard path
if (path.contains("{") && path.contains("}") || path.contains("*")) {
if (path.contains("{") && path.contains("}") || path.contains("*") || path.contains("?")) {
newGroupWildcardPathRoute.put(buildPathKey(entry.getValue(), route), route);
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ public GatewayConfigChangeListener(ContextGatewayPropertiesManager manager,
public void onChangeTencentGatewayProperties(ConfigChangeEvent event) {
Binder binder = Binder.get(environment);
BindResult<ContextGatewayProperties> result = binder.bind(ContextGatewayProperties.PREFIX, ContextGatewayProperties.class);
manager.setGroupRouteMap(result.get().getGroups());
this.publisher.publishEvent(new RefreshRoutesEvent(event));
if (result.isBound()) {
manager.setGroupRouteMap(result.get().getGroups());
this.publisher.publishEvent(new RefreshRoutesEvent(event));
}
}

@PolarisConfigKVFileChangeListener(interestedKeyPrefixes = GatewayProperties.PREFIX)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public class GroupContext {

private String comment;

private ApiType apiType;

private ContextPredicate predicate;

private List<ContextRoute> routes;
Expand All @@ -38,14 +36,6 @@ public void setComment(String comment) {
this.comment = comment;
}

public ApiType getApiType() {
return apiType;
}

public void setApiType(ApiType apiType) {
this.apiType = apiType;
}

public ContextPredicate getPredicate() {
return predicate;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"properties": [
{
"name": "spring.cloud.tencent.gateway.routes",
"type": "java.util.Map<java.lang.String, org.springframework.cloud.gateway.route.RouteDefinition>",
"description": "Route definitions map for gateway context",
"sourceType": "com.tencent.cloud.plugin.gateway.context.ContextGatewayProperties",
"defaultValue": {}
},
{
"name": "spring.cloud.tencent.gateway.groups",
"type": "java.util.Map<java.lang.String, com.tencent.cloud.plugin.gateway.context.GroupContext>",
"description": "Group contexts configuration",
"sourceType": "com.tencent.cloud.plugin.gateway.context.ContextGatewayProperties",
"defaultValue": {}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* 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.plugin.gateway;

import com.tencent.cloud.plugin.gateway.context.ContextGatewayFilterFactory;
import com.tencent.cloud.plugin.gateway.context.ContextGatewayProperties;
import com.tencent.cloud.plugin.gateway.context.ContextGatewayPropertiesManager;
import com.tencent.cloud.plugin.gateway.context.ContextPropertiesRouteDefinitionLocator;
import com.tencent.cloud.plugin.gateway.context.ContextRoutePredicateFactory;
import com.tencent.cloud.plugin.gateway.context.GatewayConfigChangeListener;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.listener.ConfigChangeEvent;
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClient;
import com.tencent.cloud.polaris.discovery.reactive.PolarisReactiveDiscoveryClient;
import org.junit.jupiter.api.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.gateway.config.GatewayAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

/**
* Tests for {@link GatewayPluginAutoConfiguration}.
*/
class GatewayPluginAutoConfigurationTest {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
ConfigurationPropertiesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
PolarisContextAutoConfiguration.class,
GatewayAutoConfiguration.class,
GatewayPluginAutoConfiguration.class
))
.withPropertyValues(
"spring.cloud.gateway.enabled=false", // not needed for this test
"spring.cloud.tencent.plugin.scg.enabled=true",
"spring.cloud.tencent.plugin.scg.context.enabled=true",
"spring.cloud.tencent.gateway.routes.test_group.uri=lb://test-group"
)
.withUserConfiguration(MockPolarisClientsConfiguration.class);

@Test
void shouldCreateBeansWhenConditionsMet() {
contextRunner.run(context -> {
// Verify core beans
assertThat(context).hasSingleBean(ContextGatewayFilterFactory.class);
assertThat(context).hasSingleBean(ContextPropertiesRouteDefinitionLocator.class);
assertThat(context).hasSingleBean(ContextRoutePredicateFactory.class);
assertThat(context).hasSingleBean(ContextGatewayPropertiesManager.class);
assertThat(context).hasSingleBean(GatewayRegistrationCustomizer.class);
assertThat(context).hasSingleBean(GatewayConfigChangeListener.class);
assertThat(context).hasSingleBean(PolarisReactiveLoadBalancerClientFilterBeanPostProcessor.class);


GatewayPluginAutoConfiguration.ContextPluginConfiguration pluginConfiguration =
context.getBean(GatewayPluginAutoConfiguration.ContextPluginConfiguration.class);
assertThat(pluginConfiguration).hasFieldOrPropertyWithValue("commonEagerLoadEnabled", true)
.hasFieldOrPropertyWithValue("gatewayEagerLoadEnabled", false);

ContextGatewayProperties properties = context.getBean(ContextGatewayProperties.class);
properties.setRoutes(properties.getRoutes());
properties.setGroups(properties.getGroups());
properties.toString();

// test listener
GatewayConfigChangeListener listener = context.getBean(GatewayConfigChangeListener.class);
listener.onChangeTencentGatewayProperties(new ConfigChangeEvent(null, null));
listener.onChangeGatewayConfigChangeListener(new ConfigChangeEvent(null, null));

});
}

@Test
void shouldEagerLoad() {
contextRunner
.withPropertyValues("spring.cloud.polaris.discovery.eager-load.gateway.enabled=true")
.run(context -> {
// Verify eager loading
GatewayPluginAutoConfiguration.ContextPluginConfiguration pluginConfiguration = context.getBean(GatewayPluginAutoConfiguration.ContextPluginConfiguration.class);
assertThat(pluginConfiguration).hasFieldOrPropertyWithValue("commonEagerLoadEnabled", true)
.hasFieldOrPropertyWithValue("gatewayEagerLoadEnabled", true);
});
}

@Test
void shouldNotCreateBeansWhenPluginDisabled() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(GatewayPluginAutoConfiguration.class))
.withPropertyValues("spring.cloud.tencent.plugin.scg.enabled=false")
.run(context -> assertThat(context).doesNotHaveBean(ContextGatewayFilterFactory.class));
}

@Test
void shouldNotCreateContextBeansWhenContextPluginDisabled() {
contextRunner
.withPropertyValues("spring.cloud.tencent.plugin.scg.context.enabled=false")
.run(context -> {
assertThat(context).doesNotHaveBean(ContextGatewayFilterFactory.class);
assertThat(context).doesNotHaveBean(ContextPropertiesRouteDefinitionLocator.class);
});
}

@Configuration
static class MockPolarisClientsConfiguration {
@Bean
PolarisDiscoveryClient polarisDiscoveryClient() {
return mock(PolarisDiscoveryClient.class);
}

@Bean
PolarisReactiveDiscoveryClient polarisReactiveDiscoveryClient() {
return mock(PolarisReactiveDiscoveryClient.class);
}

@Bean
PolarisConfigProperties polarisConfigProperties() {
return new PolarisConfigProperties();
}
}
}
Loading