Merge branch 'main' of https://github.com/Tencent/spring-cloud-tencent into add_ribbon_testcase
Conflicts: spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerAutoConfigurationTest.javapull/373/head
After Width: | Height: | Size: 41 KiB |
After Width: | Height: | Size: 13 KiB |
After Width: | Height: | Size: 42 KiB |
After Width: | Height: | Size: 13 KiB |
After Width: | Height: | Size: 31 KiB |
After Width: | Height: | Size: 13 KiB |
After Width: | Height: | Size: 31 KiB |
After Width: | Height: | Size: 13 KiB |
Before Width: | Height: | Size: 16 KiB |
After Width: | Height: | Size: 17 KiB |
After Width: | Height: | Size: 1.6 KiB |
After Width: | Height: | Size: 18 KiB |
After Width: | Height: | Size: 1.7 KiB |
4
spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/intercepter/EncodeTransferMedataRestTemplateInterceptorTest.java → spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/EncodeTransferMedataRestTemplateInterceptorTest.java
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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.circuitbreaker;
|
||||
|
||||
import com.tencent.cloud.polaris.circuitbreaker.config.PolarisRestTemplateAutoConfiguration;
|
||||
import com.tencent.cloud.polaris.circuitbreaker.resttemplate.PolarisRestTemplateModifier;
|
||||
import com.tencent.cloud.polaris.circuitbreaker.resttemplate.PolarisRestTemplateResponseErrorHandler;
|
||||
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test For {@link PolarisRestTemplateAutoConfiguration} .
|
||||
*
|
||||
* @author <a href="mailto:iskp.me@gmail.com">Palmer Xu</a> 2022-06-28
|
||||
*/
|
||||
public class PolarisRestTemplateAutoConfigurationTest {
|
||||
|
||||
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
|
||||
.withConfiguration(
|
||||
AutoConfigurations.of(
|
||||
PolarisRestTemplateAutoConfigurationTester.class,
|
||||
PolarisContextAutoConfiguration.class,
|
||||
PolarisRestTemplateAutoConfiguration.class))
|
||||
.withPropertyValues("spring.cloud.polaris.circuitbreaker.enabled=true");
|
||||
|
||||
@Test
|
||||
public void testInitialization() {
|
||||
this.contextRunner
|
||||
.run(context -> {
|
||||
assertThat(context).hasSingleBean(PolarisRestTemplateModifier.class);
|
||||
assertThat(context).hasSingleBean(PolarisRestTemplateResponseErrorHandler.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@AutoConfigureBefore(PolarisRestTemplateAutoConfiguration.class)
|
||||
static class PolarisRestTemplateAutoConfigurationTester {
|
||||
|
||||
@Bean
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
9
spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/PolarisRestTemplateResponseErrorHandlerTest.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisRestTemplateResponseErrorHandlerTest.java
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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.config.endpoint;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource;
|
||||
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
|
||||
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||
|
||||
/**
|
||||
* Endpoint of polaris config.
|
||||
*
|
||||
* @author shuiqingliu
|
||||
**/
|
||||
@Endpoint(id = "polaris-config")
|
||||
public class PolarisConfigEndpoint {
|
||||
|
||||
private final PolarisConfigProperties polarisConfigProperties;
|
||||
private final PolarisPropertySourceManager polarisPropertySourceManager;
|
||||
|
||||
public PolarisConfigEndpoint(PolarisConfigProperties polarisConfigProperties, PolarisPropertySourceManager polarisPropertySourceManager) {
|
||||
this.polarisConfigProperties = polarisConfigProperties;
|
||||
this.polarisPropertySourceManager = polarisPropertySourceManager;
|
||||
}
|
||||
|
||||
@ReadOperation
|
||||
public Map<String, Object> polarisConfig() {
|
||||
Map<String, Object> configInfo = new HashMap<>();
|
||||
configInfo.put("PolarisConfigProperties", polarisConfigProperties);
|
||||
|
||||
List<PolarisPropertySource> propertySourceList = polarisPropertySourceManager.getAllPropertySources();
|
||||
configInfo.put("PolarisPropertySource", propertySourceList);
|
||||
|
||||
return configInfo;
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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.config.endpoint;
|
||||
|
||||
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
|
||||
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* The AutoConfiguration for Polaris Config's endpoint.
|
||||
*
|
||||
* @author shuiqingliu
|
||||
**/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(Endpoint.class)
|
||||
@ConditionalOnProperty(value = "spring.cloud.polaris.config.enabled",
|
||||
matchIfMissing = true)
|
||||
public class PolarisConfigEndpointAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnAvailableEndpoint
|
||||
@ConditionalOnMissingBean
|
||||
public PolarisConfigEndpoint polarisConfigEndpoint(PolarisConfigProperties polarisConfigProperties, PolarisPropertySourceManager polarisPropertySourceManager) {
|
||||
return new PolarisConfigEndpoint(polarisConfigProperties, polarisPropertySourceManager);
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
|
||||
com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration
|
||||
com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration,\
|
||||
com.tencent.cloud.polaris.config.endpoint.PolarisConfigEndpointAutoConfiguration
|
||||
|
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* 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.config.endpoint;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.tencent.cloud.polaris.config.adapter.MockedConfigKVFile;
|
||||
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource;
|
||||
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
|
||||
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test for polaris config endpoint.
|
||||
*
|
||||
* @author shuiqingliu
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class PolarisConfigEndpointTest {
|
||||
|
||||
private final String testNamespace = "testNamespace";
|
||||
private final String testServiceName = "testServiceName";
|
||||
private final String testFileName = "application.properties";
|
||||
|
||||
@Mock
|
||||
private PolarisConfigProperties polarisConfigProperties;
|
||||
@Mock
|
||||
private PolarisPropertySourceManager polarisPropertySourceManager;
|
||||
|
||||
@Test
|
||||
public void testPolarisConfigEndpoint() {
|
||||
Map<String, Object> content = new HashMap<>();
|
||||
content.put("k1", "v1");
|
||||
content.put("k2", "v2");
|
||||
content.put("k3", "v3");
|
||||
MockedConfigKVFile file = new MockedConfigKVFile(content);
|
||||
PolarisPropertySource polarisPropertySource = new PolarisPropertySource(testNamespace, testServiceName, testFileName,
|
||||
file, content);
|
||||
when(polarisPropertySourceManager.getAllPropertySources()).thenReturn(Lists.newArrayList(polarisPropertySource));
|
||||
|
||||
PolarisConfigEndpoint endpoint = new PolarisConfigEndpoint(polarisConfigProperties, polarisPropertySourceManager);
|
||||
Map<String, Object> info = endpoint.polarisConfig();
|
||||
assertThat(polarisConfigProperties).isEqualTo(info.get("PolarisConfigProperties"));
|
||||
assertThat(Lists.newArrayList(polarisPropertySource)).isEqualTo(info.get("PolarisPropertySource"));
|
||||
}
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
/*
|
||||
* 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.endpoint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
|
||||
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
|
||||
import com.tencent.polaris.api.pojo.ServiceInstances;
|
||||
import com.tencent.polaris.api.rpc.InstancesResponse;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Selector;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
|
||||
/**
|
||||
* Endpoint of polaris discovery, include discovery properties and service instance.
|
||||
*
|
||||
* @author shuiqingliu
|
||||
*/
|
||||
@Endpoint(id = "polaris-discovery")
|
||||
public class PolarisDiscoveryEndPoint {
|
||||
|
||||
private final PolarisDiscoveryProperties polarisDiscoveryProperties;
|
||||
private final DiscoveryClient polarisDiscoveryClient;
|
||||
private final PolarisDiscoveryHandler polarisDiscoveryHandler;
|
||||
|
||||
public PolarisDiscoveryEndPoint(PolarisDiscoveryProperties polarisDiscoveryProperties, DiscoveryClient polarisDiscoveryClient, PolarisDiscoveryHandler polarisDiscoveryHandler) {
|
||||
this.polarisDiscoveryProperties = polarisDiscoveryProperties;
|
||||
this.polarisDiscoveryClient = polarisDiscoveryClient;
|
||||
this.polarisDiscoveryHandler = polarisDiscoveryHandler;
|
||||
}
|
||||
|
||||
@ReadOperation
|
||||
public Map<String, Object> polarisDiscovery(@Selector String serviceId) {
|
||||
Map<String, Object> polarisDisConveryInfo = new HashMap<>();
|
||||
polarisDisConveryInfo.put("PolarisDiscoveryProperties", polarisDiscoveryProperties);
|
||||
|
||||
List<ServiceInstances> serviceInstancesInfoList = new ArrayList<>();
|
||||
|
||||
if (StringUtils.isNotEmpty(serviceId)) {
|
||||
ServiceInstances serviceInstances = getServiceInstances(serviceId);
|
||||
serviceInstancesInfoList.add(serviceInstances);
|
||||
polarisDisConveryInfo.put("ServiceInstances", serviceInstancesInfoList);
|
||||
return polarisDisConveryInfo;
|
||||
}
|
||||
|
||||
for (String service : polarisDiscoveryClient.getServices()) {
|
||||
ServiceInstances serviceInstances = getServiceInstances(service);
|
||||
serviceInstancesInfoList.add(serviceInstances);
|
||||
}
|
||||
|
||||
polarisDisConveryInfo.put("ServiceInstances", serviceInstancesInfoList);
|
||||
return polarisDisConveryInfo;
|
||||
}
|
||||
|
||||
private ServiceInstances getServiceInstances(String serviceId) {
|
||||
InstancesResponse instancesResponse = polarisDiscoveryHandler.getHealthyInstances(serviceId);
|
||||
ServiceInstances serviceInstances = instancesResponse.toServiceInstances();
|
||||
return serviceInstances;
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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.endpoint;
|
||||
|
||||
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
|
||||
import com.tencent.cloud.polaris.discovery.ConditionalOnPolarisDiscoveryEnabled;
|
||||
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* The AutoConfiguration for Polaris Discovery's Endpoint.
|
||||
*
|
||||
* @author shuiqingliu
|
||||
**/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(Endpoint.class)
|
||||
@ConditionalOnPolarisDiscoveryEnabled
|
||||
public class PolarisDiscoveryEndpointAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnAvailableEndpoint
|
||||
public PolarisDiscoveryEndPoint polarisDiscoveryEndPoint(PolarisDiscoveryProperties polarisDiscoveryProperties,
|
||||
DiscoveryClient discoveryClient, PolarisDiscoveryHandler polarisDiscoveryHandler) {
|
||||
return new PolarisDiscoveryEndPoint(polarisDiscoveryProperties, discoveryClient, polarisDiscoveryHandler);
|
||||
}
|
||||
}
|
@ -0,0 +1,102 @@
|
||||
/*
|
||||
* 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.endpoint;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
|
||||
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration;
|
||||
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClient;
|
||||
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration;
|
||||
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
|
||||
import com.tencent.polaris.test.mock.discovery.NamingServer;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||
import static com.tencent.polaris.test.common.Consts.PORT;
|
||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test for polaris discovery endpoint.
|
||||
*
|
||||
* @author shuiqingliu
|
||||
*/
|
||||
public class PolarisDiscoveryEndPointTest {
|
||||
|
||||
private static NamingServer namingServer;
|
||||
|
||||
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(
|
||||
PolarisDiscoveryEndPointTest.PolarisPropertiesConfiguration.class,
|
||||
PolarisDiscoveryClientConfiguration.class,
|
||||
PolarisDiscoveryAutoConfiguration.class,
|
||||
PolarisDiscoveryEndpointAutoConfiguration.class))
|
||||
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
|
||||
.withPropertyValues("server.port=" + PORT)
|
||||
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081")
|
||||
.withPropertyValues(
|
||||
"spring.cloud.polaris.discovery.namespace=" + NAMESPACE_TEST)
|
||||
.withPropertyValues("spring.cloud.polaris.discovery.token=xxxxxx");
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() throws Exception {
|
||||
namingServer = NamingServer.startNamingServer(10081);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() throws Exception {
|
||||
if (null != namingServer) {
|
||||
namingServer.terminate();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPolarisDiscoveryEndpoint() {
|
||||
this.contextRunner.run(context -> {
|
||||
PolarisDiscoveryProperties polarisDiscoveryProperties = context
|
||||
.getBean(PolarisDiscoveryProperties.class);
|
||||
DiscoveryClient discoveryClient = context
|
||||
.getBean(PolarisDiscoveryClient.class);
|
||||
PolarisDiscoveryHandler polarisDiscoveryHandler = context.getBean(PolarisDiscoveryHandler.class);
|
||||
PolarisDiscoveryEndPoint polarisDiscoveryEndPoint = new PolarisDiscoveryEndPoint(polarisDiscoveryProperties, discoveryClient, polarisDiscoveryHandler);
|
||||
|
||||
Map<String, Object> mapInfo = polarisDiscoveryEndPoint.polarisDiscovery("java_provider_test");
|
||||
|
||||
assertThat(polarisDiscoveryProperties).isEqualTo(mapInfo.get("PolarisDiscoveryProperties"));
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@EnableDiscoveryClient
|
||||
static class PolarisPropertiesConfiguration {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,101 @@
|
||||
/*
|
||||
* 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.ratelimit.endpoint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.tencent.cloud.polaris.context.ServiceRuleManager;
|
||||
import com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitProperties;
|
||||
import com.tencent.polaris.client.pb.RateLimitProto;
|
||||
import com.tencent.polaris.client.pb.RoutingProto;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Selector;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* Endpoint of Polaris RateLimit rule.
|
||||
*
|
||||
* @author shuiqingliu
|
||||
**/
|
||||
@Endpoint(id = "polaris-ratelimit")
|
||||
public class PolarisRateLimitRuleEndpoint {
|
||||
|
||||
private final ServiceRuleManager serviceRuleManager;
|
||||
private final PolarisRateLimitProperties polarisRateLimitProperties;
|
||||
|
||||
public PolarisRateLimitRuleEndpoint(ServiceRuleManager serviceRuleManager, PolarisRateLimitProperties polarisRateLimitProperties) {
|
||||
this.serviceRuleManager = serviceRuleManager;
|
||||
this.polarisRateLimitProperties = polarisRateLimitProperties;
|
||||
}
|
||||
|
||||
@ReadOperation
|
||||
public Map<String, Object> rateLimit(@Selector String namespace, @Selector String service, @Nullable String dstService) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
RateLimitProto.RateLimit rateLimit = serviceRuleManager.getServiceRateLimitRule(namespace, service);
|
||||
result.put("properties", polarisRateLimitProperties);
|
||||
result.put("namespace", namespace);
|
||||
result.put("service", service);
|
||||
result.put("rateLimits", parseRateLimitRule(rateLimit));
|
||||
|
||||
if (StringUtils.isEmpty(dstService)) {
|
||||
return result;
|
||||
}
|
||||
List<RoutingProto.Route> routes = serviceRuleManager.getServiceRouterRule(namespace, service, dstService);
|
||||
result.put("routes", routes);
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<Object> parseRateLimitRule(RateLimitProto.RateLimit rateLimit) {
|
||||
List<Object> rateLimitRule = new ArrayList<>();
|
||||
if (rateLimit == null || CollectionUtils.isEmpty(rateLimit.getRulesList())) {
|
||||
return rateLimitRule;
|
||||
}
|
||||
|
||||
for (RateLimitProto.Rule rule : rateLimit.getRulesList()) {
|
||||
Map<String, Object> ruleMap = new HashMap<>();
|
||||
ruleMap.put("id", rule.getId());
|
||||
ruleMap.put("priority", rule.getPriority());
|
||||
ruleMap.put("resource", rule.getResource());
|
||||
ruleMap.put("type", rule.getType());
|
||||
ruleMap.put("labels", rule.getLabelsMap());
|
||||
ruleMap.put("amounts", rule.getAmountsList());
|
||||
ruleMap.put("action", rule.getAction());
|
||||
ruleMap.put("disable", rule.getDisable());
|
||||
ruleMap.put("report", rule.getReport());
|
||||
ruleMap.put("create_time", rule.getCtime());
|
||||
ruleMap.put("modify_time", rule.getMtime());
|
||||
ruleMap.put("revision", rule.getRevision());
|
||||
ruleMap.put("service_token", rule.getServiceToken());
|
||||
ruleMap.put("adjuster", rule.getAdjuster());
|
||||
ruleMap.put("regex_combine", rule.getRegexCombine());
|
||||
ruleMap.put("amount_mode", rule.getAmountMode());
|
||||
ruleMap.put("failover", rule.getFailover());
|
||||
ruleMap.put("cluster", rule.getCluster());
|
||||
rateLimitRule.add(ruleMap);
|
||||
}
|
||||
return rateLimitRule;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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.ratelimit.endpoint;
|
||||
|
||||
import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled;
|
||||
import com.tencent.cloud.polaris.context.ServiceRuleManager;
|
||||
import com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitProperties;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* The AutoConfiguration for Polaris RateLimit endpoint.
|
||||
*
|
||||
* @author shuiqingliu
|
||||
**/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(Endpoint.class)
|
||||
@ConditionalOnPolarisEnabled
|
||||
@ConditionalOnProperty(name = "spring.cloud.polaris.ratelimit.enabled", matchIfMissing = true)
|
||||
public class PolarisRateLimitRuleEndpointAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnAvailableEndpoint
|
||||
public PolarisRateLimitRuleEndpoint polarisRateLimitRuleEndpoint(ServiceRuleManager serviceRuleManager, PolarisRateLimitProperties properties) {
|
||||
return new PolarisRateLimitRuleEndpoint(serviceRuleManager, properties);
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitAutoConfiguration,\
|
||||
com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitPropertiesAutoConfiguration
|
||||
com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitPropertiesAutoConfiguration,\
|
||||
com.tencent.cloud.polaris.ratelimit.endpoint.PolarisRateLimitRuleEndpointAutoConfiguration
|
||||
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
|
||||
com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitPropertiesBootstrapConfiguration
|
||||
|
@ -0,0 +1,110 @@
|
||||
/*
|
||||
* 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.ratelimit.endpoint;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.protobuf.StringValue;
|
||||
import com.tencent.cloud.polaris.context.ServiceRuleManager;
|
||||
import com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitProperties;
|
||||
import com.tencent.polaris.client.pb.ModelProto;
|
||||
import com.tencent.polaris.client.pb.RateLimitProto;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||
import static com.tencent.polaris.test.common.Consts.PORT;
|
||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test for polaris rete limit rule endpoint.
|
||||
*
|
||||
* @author shuiqingliu
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class PolarisRateLimitRuleEndpointTests {
|
||||
|
||||
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(
|
||||
PolarisRateLimitRuleEndpointTests.PolarisRateLimitAutoConfiguration.class,
|
||||
PolarisRateLimitRuleEndpointAutoConfiguration.class,
|
||||
PolarisRateLimitAutoConfiguration.class,
|
||||
PolarisRateLimitAutoConfiguration.class))
|
||||
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
|
||||
.withPropertyValues("server.port=" + PORT)
|
||||
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081")
|
||||
.withPropertyValues(
|
||||
"spring.cloud.polaris.discovery.namespace=" + NAMESPACE_TEST)
|
||||
.withPropertyValues("spring.cloud.polaris.discovery.token=xxxxxx");
|
||||
|
||||
private ServiceRuleManager serviceRuleManager;
|
||||
private PolarisRateLimitProperties polarisRateLimitProperties;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
serviceRuleManager = mock(ServiceRuleManager.class);
|
||||
when(serviceRuleManager.getServiceRateLimitRule(any(), anyString())).thenAnswer(invocationOnMock -> {
|
||||
String serviceName = invocationOnMock.getArgument(1).toString();
|
||||
if (serviceName.equals("TestApp1")) {
|
||||
return null;
|
||||
}
|
||||
else if (serviceName.equals("TestApp2")) {
|
||||
return RateLimitProto.RateLimit.newBuilder().build();
|
||||
}
|
||||
else if (serviceName.equals("TestApp3")) {
|
||||
RateLimitProto.Rule rule = RateLimitProto.Rule.newBuilder().build();
|
||||
return RateLimitProto.RateLimit.newBuilder().addRules(rule).build();
|
||||
}
|
||||
else {
|
||||
ModelProto.MatchString matchString = ModelProto.MatchString.newBuilder()
|
||||
.setType(ModelProto.MatchString.MatchStringType.EXACT)
|
||||
.setValue(StringValue.of("value"))
|
||||
.setValueType(ModelProto.MatchString.ValueType.TEXT).build();
|
||||
RateLimitProto.Rule rule = RateLimitProto.Rule.newBuilder()
|
||||
.putLabels("${http.method}", matchString).build();
|
||||
return RateLimitProto.RateLimit.newBuilder().addRules(rule).build();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPolarisRateLimit() {
|
||||
this.contextRunner.run(context -> polarisRateLimitProperties = context.getBean(PolarisRateLimitProperties.class));
|
||||
PolarisRateLimitRuleEndpoint polarisRateLimitRuleEndpoint = new PolarisRateLimitRuleEndpoint(serviceRuleManager, polarisRateLimitProperties);
|
||||
Map<String, Object> rateLimit = polarisRateLimitRuleEndpoint.rateLimit("namespaceTest", "TestApp2", "TestApp3");
|
||||
assertThat(polarisRateLimitProperties).isEqualTo(rateLimit.get("properties"));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
static class PolarisRateLimitAutoConfiguration {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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.common.metadata.endpoint;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.tencent.cloud.common.metadata.StaticMetadataManager;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||
|
||||
/**
|
||||
* Endpoint of polaris's metadata.
|
||||
*
|
||||
* @author shuiqingliu
|
||||
**/
|
||||
@Endpoint(id = "polaris-metadata")
|
||||
public class PolarisMetadataEndpoint {
|
||||
|
||||
private final StaticMetadataManager staticMetadataManager;
|
||||
|
||||
public PolarisMetadataEndpoint(StaticMetadataManager staticMetadataManager) {
|
||||
this.staticMetadataManager = staticMetadataManager;
|
||||
}
|
||||
|
||||
@ReadOperation
|
||||
public Map<String, Object> metadata() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("Env", staticMetadataManager.getAllEnvMetadata());
|
||||
result.put("EnvTransitive", staticMetadataManager.getEnvTransitiveMetadata());
|
||||
result.put("ConfigTransitive", staticMetadataManager.getConfigTransitiveMetadata());
|
||||
result.put("Config", staticMetadataManager.getAllConfigMetadata());
|
||||
result.put("MergeStatic", staticMetadataManager.getMergedStaticMetadata());
|
||||
result.put("CustomSPI", staticMetadataManager.getCustomSPITransitiveMetadata());
|
||||
result.put("zone", staticMetadataManager.getZone());
|
||||
result.put("region", staticMetadataManager.getRegion());
|
||||
result.put("campus", staticMetadataManager.getCampus());
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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.common.metadata.endpoint;
|
||||
|
||||
import com.tencent.cloud.common.metadata.StaticMetadataManager;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* The AutoConfiguration for metadata endpoint.
|
||||
*
|
||||
* @author shuiqingliu
|
||||
**/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(Endpoint.class)
|
||||
public class PolarisMetadataEndpointAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnAvailableEndpoint
|
||||
public PolarisMetadataEndpoint metadataEndpoint(StaticMetadataManager staticMetadataManager) {
|
||||
return new PolarisMetadataEndpoint(staticMetadataManager);
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.tencent.cloud.common.util.ApplicationContextAwareUtils,\
|
||||
com.tencent.cloud.common.metadata.config.MetadataAutoConfiguration
|
||||
com.tencent.cloud.common.metadata.config.MetadataAutoConfiguration,\
|
||||
com.tencent.cloud.common.metadata.endpoint.PolarisMetadataEndpointAutoConfiguration
|
||||
|
@ -0,0 +1,186 @@
|
||||
/*
|
||||
* 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.common.metadata;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
|
||||
import com.tencent.cloud.common.spi.InstanceMetadataProvider;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
||||
/**
|
||||
* test for {@link StaticMetadataManager}
|
||||
*@author lepdou 2022-06-27
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class StaticMetadataManagerTest {
|
||||
|
||||
@Mock
|
||||
private MetadataLocalProperties metadataLocalProperties;
|
||||
|
||||
@Test
|
||||
public void testParseConfigMetadata() {
|
||||
Map<String, String> content = new HashMap<>();
|
||||
content.put("k1", "v1");
|
||||
content.put("k2", "v22");
|
||||
content.put("zone", "zone1");
|
||||
content.put("region", "region1");
|
||||
|
||||
when(metadataLocalProperties.getContent()).thenReturn(content);
|
||||
when(metadataLocalProperties.getTransitive()).thenReturn(Collections.singletonList("k1"));
|
||||
|
||||
StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties, null);
|
||||
|
||||
Map<String, String> metadata = metadataManager.getAllConfigMetadata();
|
||||
Assert.assertEquals(4, metadata.size());
|
||||
Assert.assertEquals("v1", metadata.get("k1"));
|
||||
Assert.assertEquals("v22", metadata.get("k2"));
|
||||
|
||||
Map<String, String> transitiveMetadata = metadataManager.getConfigTransitiveMetadata();
|
||||
Assert.assertEquals(1, transitiveMetadata.size());
|
||||
Assert.assertEquals("v1", transitiveMetadata.get("k1"));
|
||||
|
||||
Assert.assertEquals("zone1", metadataManager.getZone());
|
||||
Assert.assertEquals("region1", metadataManager.getRegion());
|
||||
|
||||
Map<String, String> locationInfo = metadataManager.getLocationMetadata();
|
||||
Assert.assertEquals("zone1", locationInfo.get("zone"));
|
||||
Assert.assertEquals("region1", locationInfo.get("region"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomSPIMetadata() {
|
||||
Map<String, String> content = new HashMap<>();
|
||||
content.put("k1", "v1");
|
||||
content.put("k2", "v2");
|
||||
|
||||
when(metadataLocalProperties.getContent()).thenReturn(content);
|
||||
when(metadataLocalProperties.getTransitive()).thenReturn(Collections.singletonList("k1"));
|
||||
|
||||
StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties,
|
||||
new MockedMetadataProvider());
|
||||
|
||||
Map<String, String> metadata = metadataManager.getAllCustomMetadata();
|
||||
Assert.assertEquals(3, metadata.size());
|
||||
Assert.assertEquals("v1", metadata.get("k1"));
|
||||
Assert.assertEquals("v22", metadata.get("k2"));
|
||||
Assert.assertEquals("v33", metadata.get("k3"));
|
||||
|
||||
Map<String, String> transitiveMetadata = metadataManager.getCustomSPITransitiveMetadata();
|
||||
Assert.assertEquals(1, transitiveMetadata.size());
|
||||
Assert.assertEquals("v22", metadata.get("k2"));
|
||||
|
||||
Assert.assertEquals("zone2", metadataManager.getZone());
|
||||
Assert.assertEquals("region1", metadataManager.getRegion());
|
||||
|
||||
Map<String, String> locationInfo = metadataManager.getLocationMetadata();
|
||||
Assert.assertEquals("zone2", locationInfo.get("zone"));
|
||||
Assert.assertEquals("region1", locationInfo.get("region"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMergedMetadata() {
|
||||
Map<String, String> content = new HashMap<>();
|
||||
content.put("k1", "v1");
|
||||
content.put("k2", "v2");
|
||||
content.put("zone", "zone1");
|
||||
content.put("region", "region1");
|
||||
content.put("campus", "campus1");
|
||||
|
||||
when(metadataLocalProperties.getContent()).thenReturn(content);
|
||||
when(metadataLocalProperties.getTransitive()).thenReturn(Collections.singletonList("k1"));
|
||||
|
||||
StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties,
|
||||
new MockedMetadataProvider());
|
||||
|
||||
Map<String, String> metadata = metadataManager.getMergedStaticMetadata();
|
||||
Assert.assertEquals(6, metadata.size());
|
||||
Assert.assertEquals("v1", metadata.get("k1"));
|
||||
Assert.assertEquals("v22", metadata.get("k2"));
|
||||
Assert.assertEquals("v33", metadata.get("k3"));
|
||||
Assert.assertEquals("zone2", metadata.get("zone"));
|
||||
Assert.assertEquals("region1", metadata.get("region"));
|
||||
Assert.assertEquals("campus1", metadata.get("campus"));
|
||||
|
||||
Map<String, String> transitiveMetadata = metadataManager.getMergedStaticTransitiveMetadata();
|
||||
Assert.assertEquals(2, transitiveMetadata.size());
|
||||
Assert.assertEquals("v1", metadata.get("k1"));
|
||||
Assert.assertEquals("v22", metadata.get("k2"));
|
||||
|
||||
Assert.assertEquals("zone2", metadataManager.getZone());
|
||||
Assert.assertEquals("region1", metadataManager.getRegion());
|
||||
|
||||
Assert.assertTrue(CollectionUtils.isEmpty(metadataManager.getAllEnvMetadata()));
|
||||
Assert.assertTrue(CollectionUtils.isEmpty(metadataManager.getEnvTransitiveMetadata()));
|
||||
|
||||
Map<String, String> locationInfo = metadataManager.getLocationMetadata();
|
||||
Assert.assertEquals("zone2", locationInfo.get("zone"));
|
||||
Assert.assertEquals("region1", locationInfo.get("region"));
|
||||
Assert.assertEquals("campus1", locationInfo.get("campus"));
|
||||
|
||||
}
|
||||
|
||||
static class MockedMetadataProvider implements InstanceMetadataProvider {
|
||||
|
||||
@Override
|
||||
public Map<String, String> getMetadata() {
|
||||
Map<String, String> metadata = new HashMap<>();
|
||||
metadata.put("k1", "v1");
|
||||
metadata.put("k2", "v22");
|
||||
metadata.put("k3", "v33");
|
||||
return metadata;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getTransitiveMetadataKeys() {
|
||||
Set<String> transitiveKeys = new HashSet<>();
|
||||
transitiveKeys.add("k2");
|
||||
return transitiveKeys;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRegion() {
|
||||
return "region1";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getZone() {
|
||||
return "zone2";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCampus() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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.common.metadata.endpoint;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.tencent.cloud.common.metadata.StaticMetadataManager;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test for polaris metadata endpoint.
|
||||
*
|
||||
* @author shuiqingliu
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class PolarisMetadataEndpointTests {
|
||||
|
||||
@Mock
|
||||
private StaticMetadataManager staticMetadataManager;
|
||||
|
||||
@Test
|
||||
public void testPolarisMetadataEndpoint() {
|
||||
Map<String, String> envMetadata = new HashMap<>();
|
||||
envMetadata.put("k1", "v1");
|
||||
envMetadata.put("k2", "v2");
|
||||
envMetadata.put("k3", "v3");
|
||||
|
||||
when(staticMetadataManager.getAllEnvMetadata()).thenReturn(envMetadata);
|
||||
|
||||
PolarisMetadataEndpoint polarisMetadataEndpoint = new PolarisMetadataEndpoint(staticMetadataManager);
|
||||
Map<String, Object> metaMap = polarisMetadataEndpoint.metadata();
|
||||
assertThat(envMetadata).isEqualTo(metaMap.get("Env"));
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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.context;
|
||||
|
||||
import com.tencent.cloud.common.metadata.StaticMetadataManager;
|
||||
import com.tencent.polaris.api.plugin.common.ValueContext;
|
||||
import com.tencent.polaris.api.plugin.route.LocationLevel;
|
||||
import com.tencent.polaris.client.api.SDKContext;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
/**
|
||||
* After all configurations are loaded, post-initialize SDKContext.
|
||||
*@author lepdou 2022-06-28
|
||||
*/
|
||||
public class PostInitPolarisSDKContext {
|
||||
|
||||
public PostInitPolarisSDKContext(SDKContext sdkContext, StaticMetadataManager staticMetadataManager) {
|
||||
// set instance's location info
|
||||
String region = staticMetadataManager.getRegion();
|
||||
String zone = staticMetadataManager.getZone();
|
||||
String campus = staticMetadataManager.getCampus();
|
||||
|
||||
ValueContext valueContext = sdkContext.getValueContext();
|
||||
if (StringUtils.isNotBlank(region)) {
|
||||
valueContext.setValue(LocationLevel.region.name(), region);
|
||||
}
|
||||
if (StringUtils.isNotBlank(zone)) {
|
||||
valueContext.setValue(LocationLevel.zone.name(), zone);
|
||||
}
|
||||
if (StringUtils.isNotBlank(campus)) {
|
||||
valueContext.setValue(LocationLevel.campus.name(), campus);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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.context.config;
|
||||
|
||||
import com.tencent.cloud.common.metadata.StaticMetadataManager;
|
||||
import com.tencent.cloud.polaris.context.PostInitPolarisSDKContext;
|
||||
import com.tencent.polaris.client.api.SDKContext;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Post-initialization operations after the application initialization phase is completed.
|
||||
*@author lepdou 2022-06-28
|
||||
*/
|
||||
@Configuration
|
||||
public class PolarisContextPostConfiguration {
|
||||
|
||||
@Bean
|
||||
public PostInitPolarisSDKContext postInitPolarisSDKContext(SDKContext sdkContext, StaticMetadataManager staticMetadataManager) {
|
||||
return new PostInitPolarisSDKContext(sdkContext, staticMetadataManager);
|
||||
}
|
||||
}
|