Skip to content

feat: support spring.cloud.polaris.router.nearby-router.matchLevel to config nearyby level #1298

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 9 commits into from
May 15, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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;

import com.tencent.cloud.common.constant.OrderConstant;
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties;
import com.tencent.polaris.api.config.consumer.ServiceRouterConfig;
import com.tencent.polaris.api.plugin.route.LocationLevel;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.plugins.router.nearby.NearbyRouterConfig;
import org.apache.commons.lang.StringUtils;

/**
* RouterConfigModifier.
*
* @author sean yu
*/
public class RouterConfigModifier implements PolarisConfigModifier {

private final PolarisNearByRouterProperties polarisNearByRouterProperties;

public RouterConfigModifier(PolarisNearByRouterProperties polarisNearByRouterProperties) {
this.polarisNearByRouterProperties = polarisNearByRouterProperties;
}

@Override
public void modify(ConfigurationImpl configuration) {
if (StringUtils.isNotBlank(polarisNearByRouterProperties.getMatchLevel())) {
LocationLevel locationLevel = LocationLevel.valueOf(polarisNearByRouterProperties.getMatchLevel());
NearbyRouterConfig nearbyRouterConfig = configuration.getConsumer().getServiceRouter().getPluginConfig(
ServiceRouterConfig.DEFAULT_ROUTER_NEARBY, NearbyRouterConfig.class);
nearbyRouterConfig.setMatchLevel(locationLevel);
configuration.getConsumer().getServiceRouter()
.setPluginConfig(ServiceRouterConfig.DEFAULT_ROUTER_NEARBY, nearbyRouterConfig);
}

}

@Override
public int getOrder() {
return OrderConstant.Modifier.ROUTER_ORDER;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
* RouterBootstrapAutoConfiguration.
*
* @author sean yu
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty("spring.cloud.polaris.enabled")
@Import(RouterConfigModifierAutoConfiguration.class)
public class RouterBootstrapAutoConfiguration {


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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;

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

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
* RouterConfigModifierAutoConfiguration.
*
* @author sean yu
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnPolarisRouterEnabled
@Import({PolarisNearByRouterProperties.class, PolarisMetadataRouterProperties.class, PolarisRuleBasedRouterProperties.class})
public class RouterConfigModifierAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public RouterConfigModifier routerConfigModifier(PolarisNearByRouterProperties polarisNearByRouterProperties) {
return new RouterConfigModifier(polarisNearByRouterProperties);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class PolarisNearByRouterProperties {

private boolean enabled = true;

private String matchLevel;

public boolean isEnabled() {
return enabled;
}
Expand All @@ -38,10 +40,19 @@ public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public String getMatchLevel() {
return matchLevel;
}

public void setMatchLevel(String matchLevel) {
this.matchLevel = matchLevel;
}

@Override
public String toString() {
return "PolarisNearByRouterProperties{" +
"enabled=" + enabled +
", matchLevel='" + matchLevel + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
"defaultValue": true,
"description": "the switch for near by 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."
},
{
"name": "spring.cloud.polaris.router.rule-router.enabled",
"type": "java.lang.Boolean",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tencent.cloud.polaris.router.config.RouterAutoConfiguration,\
com.tencent.cloud.polaris.router.config.FeignAutoConfiguration,\
com.tencent.cloud.polaris.router.endpoint.PolarisRouterEndpointAutoConfiguration
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.tencent.cloud.polaris.router.config.RouterBootstrapAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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;

import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
import com.tencent.cloud.polaris.router.RouterConfigModifier;
import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties;
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementAutoConfiguration;
import com.tencent.polaris.api.config.Configuration;
import com.tencent.polaris.api.config.consumer.ServiceRouterConfig;
import com.tencent.polaris.factory.ConfigAPIFactory;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.plugins.router.nearby.NearbyRouterConfig;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

import static org.assertj.core.api.Assertions.assertThat;

/**
* RouterBootstrapAutoConfigurationTest.
*
* @author sean yu
*/
public class RouterBootstrapAutoConfigurationTest {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
PolarisContextAutoConfiguration.class,
PolarisNearByRouterProperties.class,
RpcEnhancementAutoConfiguration.class,
RouterBootstrapAutoConfiguration.class))
.withPropertyValues("spring.cloud.polaris.enabled=true")
.withPropertyValues("spring.cloud.polaris.router.nearby-router.matchLevel=campus")
.withPropertyValues("spring.cloud.polaris.circuitbreaker.enabled=true");

@Test
public void testDefaultInitialization() {
this.contextRunner.run(context -> {
assertThat(context).hasSingleBean(RouterConfigModifier.class);
RouterConfigModifier routerConfigModifier = (RouterConfigModifier) context.getBean("routerConfigModifier");
Configuration configuration = ConfigAPIFactory.defaultConfig();
routerConfigModifier.modify((ConfigurationImpl) configuration);
NearbyRouterConfig nearbyRouterConfig = configuration.getConsumer().getServiceRouter().getPluginConfig(
ServiceRouterConfig.DEFAULT_ROUTER_NEARBY, NearbyRouterConfig.class);
Assertions.assertEquals("campus", nearbyRouterConfig.getMatchLevel().name());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ public void setEnabled() {
@Test
public void testToString() {
assertThat(properties.toString())
.isEqualTo("PolarisNearByRouterProperties{enabled=true}");
.isEqualTo("PolarisNearByRouterProperties{enabled=true, matchLevel='null'}");
}
}
Loading