feat: support spring.cloud.polaris.router.nearby-router.matchLevel to config nearyby level (#1298)
parent
8309602d96
commit
556635386a
@ -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;
|
||||
}
|
||||
}
|
@ -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 {
|
||||
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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());
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in new issue