@ -0,0 +1,37 @@
|
||||
# This workflow will build a Java project with Maven
|
||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
|
||||
|
||||
name: Codecov
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- 2021.0
|
||||
- 2020.0
|
||||
- greenwich
|
||||
pull_request:
|
||||
branches:
|
||||
- main
|
||||
- 2021.0
|
||||
- 2020.0
|
||||
- greenwich
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout codes
|
||||
uses: actions/checkout@v3
|
||||
- name: Set up JDK 8
|
||||
uses: actions/setup-java@v3
|
||||
with:
|
||||
distribution: 'temurin'
|
||||
java-version: 8
|
||||
- name: Test with Maven
|
||||
run: mvn -B test --file pom.xml
|
||||
- name: Upload coverage to Codecov
|
||||
uses: codecov/codecov-action@v3
|
||||
with:
|
||||
file: ${{ github.workspace }}/target/site/jacoco/jacoco.xml
|
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 |
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 |
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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.metadata.core;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.server.MockServerWebExchange;
|
||||
|
||||
/**
|
||||
* Test for {@link CustomTransitiveMetadataResolver}.
|
||||
*
|
||||
* @author quan
|
||||
*/
|
||||
public class CustomTransitiveMetadataResolverTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
MockServerHttpRequest.BaseBuilder<?> builder = MockServerHttpRequest.get("");
|
||||
builder.header("X-SCT-Metadata-Transitive-a", "test");
|
||||
MockServerWebExchange exchange = MockServerWebExchange.from(builder);
|
||||
Map<String, String> resolve = CustomTransitiveMetadataResolver.resolve(exchange);
|
||||
Assertions.assertThat(resolve.size()).isEqualTo(1);
|
||||
Assertions.assertThat(resolve.get("a")).isEqualTo("test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testServlet() {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("X-SCT-Metadata-Transitive-a", "test");
|
||||
Map<String, String> resolve = CustomTransitiveMetadataResolver.resolve(request);
|
||||
Assertions.assertThat(resolve.size()).isEqualTo(1);
|
||||
Assertions.assertThat(resolve.get("a")).isEqualTo("test");
|
||||
}
|
||||
}
|
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,83 @@
|
||||
/*
|
||||
* 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.metadata.core;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
import com.tencent.cloud.common.constant.MetadataConstant;
|
||||
import com.tencent.cloud.common.util.JacksonUtils;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.mock.web.server.MockServerWebExchange;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
/**
|
||||
* Test for {@link EncodeTransferMedataScgFilter}.
|
||||
*
|
||||
* @author quan
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = RANDOM_PORT,
|
||||
classes = EncodeTransferMedataScgFilterTest.TestApplication.class,
|
||||
properties = {"spring.config.location = classpath:application-test.yml", "spring.main.web-application-type = reactive"})
|
||||
public class EncodeTransferMedataScgFilterTest {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Mock
|
||||
private GatewayFilterChain chain;
|
||||
|
||||
@Test
|
||||
public void testTransitiveMetadataFromApplicationConfig() throws UnsupportedEncodingException {
|
||||
EncodeTransferMedataScgFilter filter = applicationContext.getBean(EncodeTransferMedataScgFilter.class);
|
||||
|
||||
// Mock Server Http Request
|
||||
MockServerHttpRequest.BaseBuilder<?> builder = MockServerHttpRequest.get("");
|
||||
MockServerWebExchange exchange = MockServerWebExchange.from(builder);
|
||||
filter.filter(exchange, chain);
|
||||
|
||||
// Check metadata str
|
||||
String metadata = exchange.getRequest().getHeaders().getFirst(MetadataConstant.HeaderName.CUSTOM_METADATA);
|
||||
Assertions.assertThat(metadata).isNotNull();
|
||||
|
||||
String decode = URLDecoder.decode(metadata, StandardCharsets.UTF_8.name());
|
||||
Map<String, String> transitiveMap = JacksonUtils.deserialize2Map(decode);
|
||||
Assertions.assertThat(transitiveMap.size()).isEqualTo(1);
|
||||
Assertions.assertThat(transitiveMap.get("b")).isEqualTo("2");
|
||||
}
|
||||
|
||||
@SpringBootApplication
|
||||
protected static class TestApplication {
|
||||
|
||||
}
|
||||
}
|
2
spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerBootstrapConfiguration.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisCircuitBreakerBootstrapConfiguration.java
7
spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/PolarisFeignClientAutoConfiguration.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisFeignClientAutoConfiguration.java
@ -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.circuitbreaker.config;
|
||||
|
||||
import com.tencent.cloud.polaris.circuitbreaker.resttemplate.PolarisResponseErrorHandler;
|
||||
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 com.tencent.polaris.api.core.ConsumerAPI;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* Auto configuration PolarisRestTemplateAutoConfiguration .
|
||||
*
|
||||
* @author wh 2022/6/21
|
||||
*/
|
||||
@ConditionalOnProperty(value = "spring.cloud.polaris.circuitbreaker.enabled",
|
||||
havingValue = "true", matchIfMissing = true)
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@AutoConfigureAfter(PolarisContextAutoConfiguration.class)
|
||||
public class PolarisRestTemplateAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(RestTemplate.class)
|
||||
public PolarisRestTemplateResponseErrorHandler polarisRestTemplateResponseErrorHandler(
|
||||
ConsumerAPI consumerAPI, @Autowired(required = false) PolarisResponseErrorHandler polarisResponseErrorHandler) {
|
||||
return new PolarisRestTemplateResponseErrorHandler(consumerAPI, polarisResponseErrorHandler);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(RestTemplate.class)
|
||||
public PolarisRestTemplateModifier polarisRestTemplateBeanPostProcessor(
|
||||
PolarisRestTemplateResponseErrorHandler restTemplateResponseErrorHandler) {
|
||||
return new PolarisRestTemplateModifier(restTemplateResponseErrorHandler);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* 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.resttemplate;
|
||||
|
||||
import org.springframework.web.client.ResponseErrorHandler;
|
||||
|
||||
/**
|
||||
* Polaris Response Error Handler Definition Of {@link ResponseErrorHandler}.
|
||||
*
|
||||
* @author wh 2022/6/21
|
||||
*/
|
||||
public interface PolarisResponseErrorHandler extends ResponseErrorHandler {
|
||||
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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.resttemplate;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.SmartInitializingSingleton;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* Auto configuration RestTemplate, Find the RestTemplate bean annotated with {@link LoadBalanced},
|
||||
* then replace {@link org.springframework.web.client.ResponseErrorHandler} with {@link PolarisRestTemplateResponseErrorHandler} .
|
||||
*
|
||||
* @author wh 2022/6/21
|
||||
*/
|
||||
public class PolarisRestTemplateModifier implements ApplicationContextAware, SmartInitializingSingleton {
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private final PolarisRestTemplateResponseErrorHandler polarisRestTemplateResponseErrorHandler;
|
||||
|
||||
public PolarisRestTemplateModifier(PolarisRestTemplateResponseErrorHandler polarisRestTemplateResponseErrorHandler) {
|
||||
this.polarisRestTemplateResponseErrorHandler = polarisRestTemplateResponseErrorHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterSingletonsInstantiated() {
|
||||
Map<String, Object> beans = this.applicationContext.getBeansWithAnnotation(LoadBalanced.class);
|
||||
if (!ObjectUtils.isEmpty(beans)) {
|
||||
beans.forEach(this::initRestTemplate);
|
||||
}
|
||||
}
|
||||
|
||||
private void initRestTemplate(String beanName, Object bean) {
|
||||
if (bean instanceof RestTemplate) {
|
||||
RestTemplate restTemplate = (RestTemplate) bean;
|
||||
restTemplate.setErrorHandler(polarisRestTemplateResponseErrorHandler);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
}
|
@ -0,0 +1,115 @@
|
||||
/*
|
||||
* 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.resttemplate;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.util.ReflectionUtils;
|
||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||
import com.tencent.polaris.api.pojo.RetStatus;
|
||||
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||
import com.tencent.polaris.api.rpc.ServiceCallResult;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.web.client.ResponseErrorHandler;
|
||||
|
||||
/**
|
||||
* Extend ResponseErrorHandler to get request information.
|
||||
*
|
||||
* @author wh 2022/6/21
|
||||
*/
|
||||
public class PolarisRestTemplateResponseErrorHandler implements ResponseErrorHandler {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(PolarisRestTemplateResponseErrorHandler.class);
|
||||
|
||||
private static final String FIELD_NAME = "connection";
|
||||
|
||||
private final ConsumerAPI consumerAPI;
|
||||
|
||||
private final PolarisResponseErrorHandler polarisResponseErrorHandler;
|
||||
|
||||
|
||||
public PolarisRestTemplateResponseErrorHandler(ConsumerAPI consumerAPI, PolarisResponseErrorHandler polarisResponseErrorHandler) {
|
||||
this.consumerAPI = consumerAPI;
|
||||
this.polarisResponseErrorHandler = polarisResponseErrorHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasError(@NonNull ClientHttpResponse response) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleError(@NonNull ClientHttpResponse response) throws IOException {
|
||||
if (Objects.nonNull(polarisResponseErrorHandler)) {
|
||||
if (polarisResponseErrorHandler.hasError(response)) {
|
||||
polarisResponseErrorHandler.handleError(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleError(@NonNull URI url, @NonNull HttpMethod method, @NonNull ClientHttpResponse response) throws IOException {
|
||||
ServiceCallResult resultRequest = createServiceCallResult(url);
|
||||
try {
|
||||
HttpURLConnection connection = (HttpURLConnection) ReflectionUtils.getFieldValue(response, FIELD_NAME);
|
||||
if (connection != null) {
|
||||
URL realURL = connection.getURL();
|
||||
resultRequest.setHost(realURL.getHost());
|
||||
resultRequest.setPort(realURL.getPort());
|
||||
}
|
||||
|
||||
if (response.getStatusCode().value() > 500) {
|
||||
resultRequest.setRetStatus(RetStatus.RetFail);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
LOG.error("Will report response of {} url {}", response, url, e);
|
||||
throw e;
|
||||
}
|
||||
finally {
|
||||
consumerAPI.updateServiceCallResult(resultRequest);
|
||||
}
|
||||
}
|
||||
|
||||
private ServiceCallResult createServiceCallResult(URI uri) {
|
||||
ServiceCallResult resultRequest = new ServiceCallResult();
|
||||
String serviceName = uri.getHost();
|
||||
resultRequest.setService(serviceName);
|
||||
resultRequest.setNamespace(MetadataContext.LOCAL_NAMESPACE);
|
||||
resultRequest.setMethod(uri.getPath());
|
||||
resultRequest.setRetStatus(RetStatus.RetSuccess);
|
||||
String sourceNamespace = MetadataContext.LOCAL_NAMESPACE;
|
||||
String sourceService = MetadataContext.LOCAL_SERVICE;
|
||||
if (StringUtils.isNotBlank(sourceNamespace) && StringUtils.isNotBlank(sourceService)) {
|
||||
resultRequest.setCallerService(new ServiceKey(sourceNamespace, sourceService));
|
||||
}
|
||||
return resultRequest;
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
|
||||
com.tencent.cloud.polaris.circuitbreaker.PolarisCircuitBreakerBootstrapConfiguration
|
||||
com.tencent.cloud.polaris.circuitbreaker.config.PolarisCircuitBreakerBootstrapConfiguration
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.tencent.cloud.polaris.circuitbreaker.PolarisFeignClientAutoConfiguration
|
||||
com.tencent.cloud.polaris.circuitbreaker.config.PolarisFeignClientAutoConfiguration,\
|
||||
com.tencent.cloud.polaris.circuitbreaker.config.PolarisRestTemplateAutoConfiguration
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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.resttemplate;
|
||||
|
||||
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
|
||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test For {@link PolarisRestTemplateResponseErrorHandler}.
|
||||
*
|
||||
* @author wh 2022/6/22
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = PolarisRestTemplateResponseErrorHandlerTest.TestApplication.class,
|
||||
properties = {"spring.cloud.polaris.namespace=Test", "spring.cloud.polaris.service=TestApp"})
|
||||
public class PolarisRestTemplateResponseErrorHandlerTest {
|
||||
|
||||
@Test
|
||||
public void handleError() throws Exception {
|
||||
ConsumerAPI consumerAPI = mock(ConsumerAPI.class);
|
||||
PolarisRestTemplateResponseErrorHandler polarisRestTemplateResponseErrorHandler = new PolarisRestTemplateResponseErrorHandler(consumerAPI, null);
|
||||
URI uri = mock(URI.class);
|
||||
when(uri.getPath()).thenReturn("/test");
|
||||
when(uri.getHost()).thenReturn("host");
|
||||
HttpURLConnection httpURLConnection = mock(HttpURLConnection.class);
|
||||
URL url = mock(URL.class);
|
||||
when(httpURLConnection.getURL()).thenReturn(url);
|
||||
when(url.getHost()).thenReturn("127.0.0.1");
|
||||
when(url.getPort()).thenReturn(8080);
|
||||
when(httpURLConnection.getResponseCode()).thenReturn(200);
|
||||
SimpleClientHttpResponseTest clientHttpResponse = new SimpleClientHttpResponseTest(httpURLConnection);
|
||||
polarisRestTemplateResponseErrorHandler.handleError(uri, HttpMethod.GET, clientHttpResponse);
|
||||
when(consumerAPI.unWatchService(null)).thenReturn(true);
|
||||
}
|
||||
|
||||
@SpringBootApplication
|
||||
protected static class TestApplication {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,106 @@
|
||||
/*
|
||||
* 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.resttemplate;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.client.AbstractClientHttpResponse;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
|
||||
/**
|
||||
* Mock Test for {@link AbstractClientHttpResponse}.
|
||||
*
|
||||
* @author wh 2022/6/22
|
||||
*/
|
||||
public class SimpleClientHttpResponseTest extends AbstractClientHttpResponse {
|
||||
|
||||
private final HttpURLConnection connection;
|
||||
|
||||
@Nullable
|
||||
private HttpHeaders headers;
|
||||
|
||||
@Nullable
|
||||
private InputStream responseStream;
|
||||
|
||||
|
||||
SimpleClientHttpResponseTest(HttpURLConnection connection) {
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int getRawStatusCode() throws IOException {
|
||||
return this.connection.getResponseCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStatusText() throws IOException {
|
||||
String result = this.connection.getResponseMessage();
|
||||
return (result != null) ? result : "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
if (this.headers == null) {
|
||||
this.headers = new HttpHeaders();
|
||||
// Header field 0 is the status line for most HttpURLConnections, but not on GAE
|
||||
String name = this.connection.getHeaderFieldKey(0);
|
||||
if (StringUtils.hasLength(name)) {
|
||||
this.headers.add(name, this.connection.getHeaderField(0));
|
||||
}
|
||||
int i = 1;
|
||||
while (true) {
|
||||
name = this.connection.getHeaderFieldKey(i);
|
||||
if (!StringUtils.hasLength(name)) {
|
||||
break;
|
||||
}
|
||||
this.headers.add(name, this.connection.getHeaderField(i));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getBody() throws IOException {
|
||||
InputStream errorStream = this.connection.getErrorStream();
|
||||
this.responseStream = (errorStream != null ? errorStream : this.connection.getInputStream());
|
||||
return this.responseStream;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
try {
|
||||
if (this.responseStream == null) {
|
||||
getBody();
|
||||
}
|
||||
StreamUtils.drain(this.responseStream);
|
||||
this.responseStream.close();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1 +1 @@
|
||||
just for test
|
||||
just for test
|
@ -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);
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration
|
||||
com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration,\
|
||||
com.tencent.cloud.polaris.context.config.PolarisContextPostConfiguration
|
||||
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
|
||||
com.tencent.cloud.polaris.context.PolarisContextBootstrapAutoConfiguration
|
||||
com.tencent.cloud.polaris.context.config.PolarisContextBootstrapAutoConfiguration
|
||||
|