# Conflicts: # CHANGELOG.mdpull/399/head
@ -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
|
@ -0,0 +1,4 @@
|
||||
# Change Log
|
||||
---
|
||||
|
||||
- [fix not load application.yml bug & fix guava version conflict bug](https://github.com/Tencent/spring-cloud-tencent/pull/287)
|
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");
|
||||
}
|
||||
}
|
6
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,84 @@
|
||||
/*
|
||||
* 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.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 com.tencent.cloud.common.constant.ContextConstant.UTF_8;
|
||||
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, UTF_8);
|
||||
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 {
|
||||
|
||||
}
|
||||
}
|
@ -1,62 +0,0 @@
|
||||
/*
|
||||
* 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.feign.PolarisFeignBeanPostProcessor;
|
||||
import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration;
|
||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||
import com.tencent.polaris.client.api.SDKContext;
|
||||
import com.tencent.polaris.factory.api.DiscoveryAPIFactory;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;
|
||||
|
||||
/**
|
||||
* Configuration for Polaris {@link feign.Feign} which can automatically bring in the call
|
||||
* results for reporting.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
@ConditionalOnProperty(value = "spring.cloud.polaris.circuitbreaker.enabled", havingValue = "true",
|
||||
matchIfMissing = true)
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(name = "org.springframework.cloud.openfeign.FeignAutoConfiguration")
|
||||
@AutoConfigureAfter(PolarisContextAutoConfiguration.class)
|
||||
@AutoConfigureBefore(FeignAutoConfiguration.class)
|
||||
public class PolarisFeignClientAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public ConsumerAPI consumerAPI(SDKContext context) {
|
||||
return DiscoveryAPIFactory.createConsumerAPIByContext(context);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Order(HIGHEST_PRECEDENCE)
|
||||
public PolarisFeignBeanPostProcessor polarisFeignBeanPostProcessor(ConsumerAPI consumerAPI) {
|
||||
return new PolarisFeignBeanPostProcessor(consumerAPI);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
/*
|
||||
* 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.feign.PolarisFeignBeanPostProcessor;
|
||||
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 com.tencent.polaris.client.api.SDKContext;
|
||||
import com.tencent.polaris.factory.api.DiscoveryAPIFactory;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;
|
||||
|
||||
/**
|
||||
* Auto Configuration for Polaris {@link feign.Feign} OR {@link RestTemplate} which can automatically bring in the call
|
||||
* results for reporting.
|
||||
*
|
||||
* @author <a href="mailto:iskp.me@gmail.com">Palmer.Xu</a> 2022-06-29
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
public class PolarisCircuitBreakerAutoConfiguration {
|
||||
|
||||
/**
|
||||
* Configuration for Polaris {@link feign.Feign} which can automatically bring in the call
|
||||
* results for reporting.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(name = "org.springframework.cloud.openfeign.FeignAutoConfiguration")
|
||||
@AutoConfigureAfter(PolarisContextAutoConfiguration.class)
|
||||
@AutoConfigureBefore(FeignAutoConfiguration.class)
|
||||
@ConditionalOnProperty(value = "spring.cloud.polaris.circuitbreaker.enabled", havingValue = "true", matchIfMissing = true)
|
||||
protected static class PolarisFeignClientAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public ConsumerAPI consumerAPI(SDKContext context) {
|
||||
return DiscoveryAPIFactory.createConsumerAPIByContext(context);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Order(HIGHEST_PRECEDENCE)
|
||||
public PolarisFeignBeanPostProcessor polarisFeignBeanPostProcessor(ConsumerAPI consumerAPI) {
|
||||
return new PolarisFeignBeanPostProcessor(consumerAPI);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuration for Polaris {@link RestTemplate} which can automatically bring in the call
|
||||
* results for reporting.
|
||||
*
|
||||
* @author wh 2022/6/21
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@AutoConfigureAfter(PolarisContextAutoConfiguration.class)
|
||||
@ConditionalOnClass(RestTemplate.class)
|
||||
@ConditionalOnProperty(value = "spring.cloud.polaris.circuitbreaker.enabled", havingValue = "true", matchIfMissing = true)
|
||||
protected static class PolarisRestTemplateAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public PolarisRestTemplateResponseErrorHandler polarisRestTemplateResponseErrorHandler(
|
||||
ConsumerAPI consumerAPI, @Autowired(required = false) PolarisResponseErrorHandler polarisResponseErrorHandler) {
|
||||
return new PolarisRestTemplateResponseErrorHandler(consumerAPI, polarisResponseErrorHandler);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PolarisRestTemplateModifier polarisRestTemplateBeanPostProcessor(
|
||||
PolarisRestTemplateResponseErrorHandler restTemplateResponseErrorHandler) {
|
||||
return new PolarisRestTemplateModifier(restTemplateResponseErrorHandler);
|
||||
}
|
||||
}
|
||||
}
|
6
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
@ -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 final PolarisRestTemplateResponseErrorHandler polarisRestTemplateResponseErrorHandler;
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
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,114 @@
|
||||
/*
|
||||
* 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,4 @@
|
||||
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.PolarisCircuitBreakerAutoConfiguration
|
||||
|
@ -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.circuitbreaker;
|
||||
|
||||
import com.tencent.cloud.polaris.circuitbreaker.config.PolarisCircuitBreakerAutoConfiguration;
|
||||
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 PolarisCircuitBreakerAutoConfiguration} .
|
||||
*
|
||||
* @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,
|
||||
PolarisCircuitBreakerAutoConfiguration.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(PolarisCircuitBreakerAutoConfiguration.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,105 @@
|
||||
/*
|
||||
* 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,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.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,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.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,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.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> polarisDiscoveryInfo = new HashMap<>();
|
||||
polarisDiscoveryInfo.put("PolarisDiscoveryProperties", polarisDiscoveryProperties);
|
||||
|
||||
List<ServiceInstances> serviceInstancesInfoList = new ArrayList<>();
|
||||
|
||||
if (StringUtils.isNotEmpty(serviceId)) {
|
||||
ServiceInstances serviceInstances = getServiceInstances(serviceId);
|
||||
serviceInstancesInfoList.add(serviceInstances);
|
||||
polarisDiscoveryInfo.put("ServiceInstances", serviceInstancesInfoList);
|
||||
return polarisDiscoveryInfo;
|
||||
}
|
||||
|
||||
for (String service : polarisDiscoveryClient.getServices()) {
|
||||
ServiceInstances serviceInstances = getServiceInstances(service);
|
||||
serviceInstancesInfoList.add(serviceInstances);
|
||||
}
|
||||
|
||||
polarisDiscoveryInfo.put("ServiceInstances", serviceInstancesInfoList);
|
||||
return polarisDiscoveryInfo;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|