Skip to content

feat:upgrade nearby router and add namespace nearby router. #1404

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 1 commit into from
Aug 20, 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 @@ -24,4 +24,5 @@
- [fix: memory cost too many when using wildcard feign calls](https://github.com/Tencent/spring-cloud-tencent/pull/1400)
- [feat:support consul config data.](https://github.com/Tencent/spring-cloud-tencent/pull/1401)
- [feat: support otel trace](https://github.com/Tencent/spring-cloud-tencent/pull/1402)
- [feat:support TSF router.](https://github.com/Tencent/spring-cloud-tencent/pull/1403)
- [feat:support TSF router.](https://github.com/Tencent/spring-cloud-tencent/pull/1403)
- [feat:upgrade nearby router and add namespace nearby router.](https://github.com/Tencent/spring-cloud-tencent/pull/1404)
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain web
MetadataContextHolder.get());

TransHeadersTransfer.transfer(serverHttpRequest);

return webFilterChain.filter(serverWebExchange)
.doOnError(throwable -> LOG.error("handle metadata[{}] error.",
MetadataContextHolder.get(), throwable))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ protected void doFilterInternal(@NonNull HttpServletRequest httpServletRequest,
MetadataContextHolder.init(mergedTransitiveMetadata, mergedDisposableMetadata, mergedApplicationMetadata, callerMessageMetadataProvider);

TransHeadersTransfer.transfer(httpServletRequest);

try {
filterChain.doFilter(httpServletRequest, httpServletResponse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* 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.metadata.core;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/

package com.tencent.cloud.metadata.provider;

import javax.servlet.http.HttpServletRequest;

import com.tencent.cloud.common.util.UrlUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* 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.metadata.core;
Expand Down
4 changes: 4 additions & 0 deletions spring-cloud-starter-tencent-polaris-circuitbreaker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
<groupId>com.tencent.polaris</groupId>
<artifactId>router-nearby</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-namespace</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-metadata</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public static void afterAll() {
public void testCircuitBreaker() {
Configuration configuration = TestUtils.configWithEnvAddress();
CircuitBreakAPI circuitBreakAPI = CircuitBreakAPIFactory.createCircuitBreakAPIByConfig(configuration);

ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration);

PolarisCircuitBreakerFactory polarisCircuitBreakerFactory = new PolarisCircuitBreakerFactory(circuitBreakAPI, consumerAPI);
CircuitBreaker cb = polarisCircuitBreakerFactory.create(SERVICE_CIRCUIT_BREAKER);

Expand Down
8 changes: 6 additions & 2 deletions spring-cloud-starter-tencent-polaris-config/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-tencent</artifactId>
Expand Down Expand Up @@ -34,6 +34,10 @@
<groupId>com.tencent.polaris</groupId>
<artifactId>router-nearby</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-namespace</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-metadata</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@
* @author weihubeats
*/
public class AffectedConfigurationPropertiesRebinder extends ConfigurationPropertiesRebinder {

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

private ApplicationContext applicationContext;
private Map<String, ConfigurationPropertiesBean> propertiesBeans = new HashMap<>();

private final Map<String, Map<String, Object>> propertiesBeanDefaultValues = new ConcurrentHashMap<>();

public AffectedConfigurationPropertiesRebinder(ConfigurationPropertiesBeans beans) {
Expand Down Expand Up @@ -126,8 +128,7 @@ private void initPropertiesBeanDefaultValues(Map<String, ConfigurationProperties
for (ConfigurationPropertiesBean propertiesBean : propertiesBeans.values()) {
Map<String, Object> defaultValues = new HashMap<>();
try {
Object instance = propertiesBean.getInstance().getClass().getDeclaredConstructor((Class<?>[]) null)
.newInstance();
Object instance = propertiesBean.getInstance().getClass().getDeclaredConstructor((Class<?>[]) null).newInstance();
ReflectionUtils.doWithFields(instance.getClass(), field -> {
try {
field.setAccessible(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public final class PolarisConfigChangeEventListener implements ApplicationListen
*/
@Override
public void onApplicationEvent(@NonNull ApplicationEvent event) {

// Initialize application all environment properties .
if (event instanceof ApplicationStartedEvent && started.compareAndSet(false, true)) {
ApplicationStartedEvent applicationStartedEvent = (ApplicationStartedEvent) event;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@

/**
* test for {@link PolarisConfigFileLocator}.
*
* @author lepdou 2022-06-11
*@author lepdou 2022-06-11
*/
@ExtendWith(MockitoExtension.class)
public class PolarisConfigFileLocatorTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ public void register(PolarisRegistration registration) {
heartbeat(heartbeatRequest);
}
registration.setInstanceId(instanceRegisterResponse.getInstanceId());
LOGGER.info("polaris registry, {} {} {} {}:{} {} register finished", polarisDiscoveryProperties.getNamespace(),
LOGGER.info("polaris registry, {} {} {} {}:{} {} {} {} {} register finished", polarisDiscoveryProperties.getNamespace(),
registration.getServiceId(), registration.getInstanceId(), registration.getHost(), registration.getPort(),
staticMetadataManager.getRegion(), staticMetadataManager.getZone(), staticMetadataManager.getCampus(),
staticMetadataManager.getMergedStaticMetadata());
if (Objects.nonNull(polarisStatProperties) && polarisStatProperties.isEnabled()) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ void setUp() {
doReturn(true).when(consulContextProperties).isEnabled();
doReturn(true).when(consulContextProperties).isRegister();

// mock NacosContextProperties
// mock NacosContextProperties
nacosContextProperties = mock(NacosContextProperties.class);
doReturn(true).when(nacosContextProperties).isEnabled();
Expand Down
4 changes: 4 additions & 0 deletions spring-cloud-starter-tencent-polaris-ratelimit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
<groupId>com.tencent.polaris</groupId>
<artifactId>router-nearby</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-namespace</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-canary</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions spring-cloud-starter-tencent-polaris-router/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
<groupId>com.tencent.polaris</groupId>
<artifactId>router-nearby</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-namespace</artifactId>
</dependency>
<!-- Polaris dependencies end -->

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import java.util.List;

import com.tencent.cloud.polaris.router.config.properties.PolarisMetadataRouterProperties;
import com.tencent.cloud.polaris.router.config.properties.PolarisNamespaceRouterProperties;
import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties;
import com.tencent.cloud.polaris.router.config.properties.PolarisRuleBasedRouterProperties;
import com.tencent.cloud.polaris.router.interceptor.MetadataRouterRequestInterceptor;
import com.tencent.cloud.polaris.router.interceptor.NamespaceRouterRequestInterceptor;
import com.tencent.cloud.polaris.router.interceptor.NearbyRouterRequestInterceptor;
import com.tencent.cloud.polaris.router.interceptor.RuleBasedRouterRequestInterceptor;
import com.tencent.cloud.polaris.router.resttemplate.RouterLabelRestTemplateInterceptor;
Expand Down Expand Up @@ -70,6 +72,11 @@ public RuleBasedRouterRequestInterceptor ruleBasedRouterRequestInterceptor(Polar
return new RuleBasedRouterRequestInterceptor(polarisRuleBasedRouterProperties);
}

@Bean
@ConditionalOnProperty(value = "spring.cloud.polaris.router.namespace-router.enabled", matchIfMissing = true)
public NamespaceRouterRequestInterceptor namespaceRouterRequestInterceptor(PolarisNamespaceRouterProperties polarisNamespaceRouterProperties) {
return new NamespaceRouterRequestInterceptor(polarisNamespaceRouterProperties);
}

/**
* Create when gateway application is SCG.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.tencent.cloud.polaris.router.RouterConfigModifier;
import com.tencent.cloud.polaris.router.config.properties.PolarisMetadataRouterProperties;
import com.tencent.cloud.polaris.router.config.properties.PolarisNamespaceRouterProperties;
import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties;
import com.tencent.cloud.polaris.router.config.properties.PolarisRuleBasedRouterProperties;

Expand All @@ -35,7 +36,8 @@
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnPolarisRouterEnabled
@Import({PolarisNearByRouterProperties.class, PolarisMetadataRouterProperties.class, PolarisRuleBasedRouterProperties.class})
@Import({PolarisNearByRouterProperties.class, PolarisMetadataRouterProperties.class, PolarisRuleBasedRouterProperties.class,
PolarisNamespaceRouterProperties.class})
public class RouterConfigModifierAutoConfiguration {

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 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.router.config.properties;

import com.tencent.polaris.api.rpc.NamespaceRouterFailoverType;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* the configuration for namespace router.
*
* @author lepdou 2022-05-23
*/
@ConfigurationProperties(prefix = "spring.cloud.polaris.router.namespace-router")
public class PolarisNamespaceRouterProperties {

private boolean enabled = false;

private NamespaceRouterFailoverType failOver = NamespaceRouterFailoverType.all;

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public NamespaceRouterFailoverType getFailOver() {
return failOver;
}

public void setFailOver(NamespaceRouterFailoverType failOver) {
this.failOver = failOver;
}

@Override
public String toString() {
return "PolarisNamespaceRouterProperties{" +
"enabled=" + enabled +
", failOver=" + failOver +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@ConfigurationProperties(prefix = "spring.cloud.polaris.router.nearby-router")
public class PolarisNearByRouterProperties {

private boolean enabled = true;
private boolean enabled = false;

private String matchLevel;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 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.router.interceptor;

import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.polaris.router.PolarisRouterContext;
import com.tencent.cloud.polaris.router.config.properties.PolarisNamespaceRouterProperties;
import com.tencent.cloud.polaris.router.spi.RouterRequestInterceptor;
import com.tencent.polaris.metadata.core.MetadataContainer;
import com.tencent.polaris.metadata.core.MetadataType;
import com.tencent.polaris.metadata.core.TransitiveType;
import com.tencent.polaris.plugins.router.namespace.NamespaceRouter;
import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest;

/**
* Router request interceptor for namespace router.
*
* @author Hoatian Zhang
*/
public class NamespaceRouterRequestInterceptor implements RouterRequestInterceptor {

private final PolarisNamespaceRouterProperties polarisNamespaceRouterProperties;

public NamespaceRouterRequestInterceptor(PolarisNamespaceRouterProperties polarisNamespaceRouterProperties) {
this.polarisNamespaceRouterProperties = polarisNamespaceRouterProperties;
}

@Override
public void apply(ProcessRoutersRequest request, PolarisRouterContext routerContext) {
// set namespace router enable
MetadataContainer metadataContainer = MetadataContextHolder.get()
.getMetadataContainer(MetadataType.CUSTOM, false);
metadataContainer.putMetadataMapValue(NamespaceRouter.ROUTER_TYPE_NAMESPACE, NamespaceRouter.ROUTER_ENABLED,
String.valueOf(polarisNamespaceRouterProperties.isEnabled()), TransitiveType.NONE);

// set namespace router fail over type.
request.setNamespaceRouterFailoverType(polarisNamespaceRouterProperties.getFailOver());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
{
"name": "spring.cloud.polaris.router.nearby-router.enabled",
"type": "java.lang.Boolean",
"defaultValue": true,
"defaultValue": false,
"description": "the switch for nearby router."
},
{
"name": "spring.cloud.polaris.router.nearby-router.matchLevel",
"type": "java.lang.String",
"defaultValue": "zone",
"description": "the match level for nearby router, options can be region/zone/campus."
"defaultValue": "ZONE",
"description": "the match level for nearby router, options can be REGION/ZONE/CAMPUS."
},
{
"name": "spring.cloud.polaris.router.rule-router.enabled",
Expand All @@ -30,11 +30,42 @@
"defaultValue": "all",
"description": "the fail over type for rule based router."
},
{
"name": "spring.cloud.polaris.router.namespace-router.enabled",
"type": "java.lang.Boolean",
"defaultValue": false,
"description": "the switch for namespace router."
},
{
"name": "spring.cloud.polaris.router.namespace-router.failOver",
"type": "java.lang.String",
"defaultValue": "all",
"description": "the fail over type for namespace router."
},
{
"name": "spring.cloud.polaris.router.enabled",
"type": "java.lang.Boolean",
"defaultValue": true,
"description": "the switch for router module."
}
],
"hints": [
{
"name": "spring.cloud.polaris.router.nearby-router.matchLevel",
"values": [
{
"value": "CAMPUS"
},
{
"value": "ZONE"
},
{
"value": "REGION"
},
{
"value": "ALL"
}
]
}
]
}
Loading
Loading