commit
a626ca2015
@ -0,0 +1,27 @@
|
|||||||
|
name: Release
|
||||||
|
|
||||||
|
on:
|
||||||
|
release:
|
||||||
|
types: [ published ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
release:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- name: Set up JDK 17
|
||||||
|
uses: actions/setup-java@v3
|
||||||
|
with:
|
||||||
|
java-version: '17'
|
||||||
|
distribution: 'temurin'
|
||||||
|
server-id: nexus-releases
|
||||||
|
server-username: MAVEN_USERNAME
|
||||||
|
server-password: MAVEN_PASSWORD
|
||||||
|
|
||||||
|
- name: Publish package
|
||||||
|
env:
|
||||||
|
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
|
||||||
|
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
|
||||||
|
run: |
|
||||||
|
cat <(echo -e "${{ secrets.OSSRH_GPG_SECRET_KEY }}") | gpg --batch --import;
|
||||||
|
mvn clean deploy -U -P release -Dgpg.passphrase=${{ secrets.OSSRH_GPG_SECRET_KEY_PASSWORD }} -DskipTests
|
@ -1,3 +1,20 @@
|
|||||||
# Change Log
|
# Change Log
|
||||||
---
|
---
|
||||||
|
|
||||||
|
- feature: support reactive discovery client health indicator.
|
||||||
|
- feature: Enhance default configuration to support `application*.yaml` and `bootstrap*.yaml`.
|
||||||
|
- feat:adapt for nacos instance.
|
||||||
|
- Refactoring: Refactor Circuitbreaker ut.
|
||||||
|
- refactor:refactor rpc enhancement.
|
||||||
|
- feat:refactor SDKContext as static.
|
||||||
|
- docs:add release GitHub Action.
|
||||||
|
- docs:update Polaris test environment ip.
|
||||||
|
- fix:fix custom fallback exception.
|
||||||
|
- feat:sct-all package is now available as a shaded uber-jar.
|
||||||
|
- fix:use path parameter in `@FeignClient` for circuit-breaker.
|
||||||
|
- build(deps): bump guava in /spring-cloud-tencent-dependencies
|
||||||
|
- fix:fix reporting bug when port is -1.
|
||||||
|
- fix:update guava version.
|
||||||
|
- fix:fix circuit breaker bean load order bug when using Nacos discovery.
|
||||||
|
- refactor:refactor Polaris registration.
|
||||||
|
- feat:added automatic optimization for dynamic config refresh type.
|
||||||
|
10
spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/CircuitBreakerPolarisAutoConfigurationTest.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisCircuitBreakerAutoConfigurationTest.java
10
spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/CircuitBreakerPolarisAutoConfigurationTest.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisCircuitBreakerAutoConfigurationTest.java
6
spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerBootstrapConfigurationTest.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisCircuitBreakerBootstrapConfigurationTest.java
6
spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerBootstrapConfigurationTest.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisCircuitBreakerBootstrapConfigurationTest.java
117
spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerFeignIntegrationTest.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisCircuitBreakerFeignIntegrationTest.java
117
spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerFeignIntegrationTest.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisCircuitBreakerFeignIntegrationTest.java
@ -0,0 +1,125 @@
|
|||||||
|
/*
|
||||||
|
* 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.feign;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||||
|
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||||
|
import com.tencent.cloud.common.util.ReflectionUtils;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||||
|
import feign.Target;
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.MockedStatic;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||||
|
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
public class PolarisCircuitBreakerNameResolverTest {
|
||||||
|
|
||||||
|
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void beforeAll() {
|
||||||
|
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||||
|
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||||
|
.thenReturn("unit-test");
|
||||||
|
ApplicationContext applicationContext = mock(ApplicationContext.class);
|
||||||
|
RpcEnhancementReporterProperties reporterProperties = mock(RpcEnhancementReporterProperties.class);
|
||||||
|
doReturn(reporterProperties)
|
||||||
|
.when(applicationContext).getBean(RpcEnhancementReporterProperties.class);
|
||||||
|
mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext)
|
||||||
|
.thenReturn(applicationContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
static void afterAll() {
|
||||||
|
mockedApplicationContextAwareUtils.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
MetadataContext.LOCAL_NAMESPACE = NAMESPACE_TEST;
|
||||||
|
MetadataContext.LOCAL_SERVICE = SERVICE_PROVIDER;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
Target target = mock(Target.class);
|
||||||
|
doReturn("test-svc").when(target).name();
|
||||||
|
Method method = ReflectionUtils.findMethod(PolarisCircuitBreakerNameResolverTest.class, "mockRequestMapping");
|
||||||
|
PolarisCircuitBreakerNameResolver resolver = new PolarisCircuitBreakerNameResolver();
|
||||||
|
String polarisCircuitBreakerName = resolver.resolveCircuitBreakerName("test", target, method);
|
||||||
|
assertThat(polarisCircuitBreakerName).isEqualTo("Test#test-svc");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test2() {
|
||||||
|
Target target = mock(Target.class);
|
||||||
|
doReturn("test-svc").when(target).name();
|
||||||
|
Method method = ReflectionUtils.findMethod(PolarisCircuitBreakerNameResolverTest.class, "mockRequestMapping2");
|
||||||
|
PolarisCircuitBreakerNameResolver resolver = new PolarisCircuitBreakerNameResolver();
|
||||||
|
String polarisCircuitBreakerName = resolver.resolveCircuitBreakerName("test", target, method);
|
||||||
|
assertThat(polarisCircuitBreakerName).isEqualTo("Test#test-svc#/");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test3() {
|
||||||
|
Target target = mock(Target.class);
|
||||||
|
doReturn("test-svc").when(target).name();
|
||||||
|
Method method = ReflectionUtils.findMethod(PolarisCircuitBreakerNameResolverTest.class, "mockRequestMapping3");
|
||||||
|
PolarisCircuitBreakerNameResolver resolver = new PolarisCircuitBreakerNameResolver();
|
||||||
|
String polarisCircuitBreakerName = resolver.resolveCircuitBreakerName("test", target, method);
|
||||||
|
assertThat(polarisCircuitBreakerName).isEqualTo("Test#test-svc#/");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@RequestMapping
|
||||||
|
public void mockRequestMapping() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(path = "/")
|
||||||
|
public void mockRequestMapping2() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/")
|
||||||
|
public void mockRequestMapping3() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,89 @@
|
|||||||
|
/*
|
||||||
|
* 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.feign;
|
||||||
|
|
||||||
|
import feign.Feign;
|
||||||
|
import feign.RequestLine;
|
||||||
|
import feign.Target;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClientFactoryBean;
|
||||||
|
import org.springframework.cloud.openfeign.FeignContext;
|
||||||
|
import org.springframework.cloud.openfeign.PolarisFeignCircuitBreakerTargeter;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PolarisFeignCircuitBreakerTargeterTest.
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
public class PolarisFeignCircuitBreakerTargeterTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
CircuitBreakerFactory circuitBreakerFactory;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
PolarisCircuitBreakerNameResolver circuitBreakerNameResolver;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTarget() {
|
||||||
|
PolarisFeignCircuitBreakerTargeter targeter = new PolarisFeignCircuitBreakerTargeter(circuitBreakerFactory, circuitBreakerNameResolver);
|
||||||
|
targeter.target(new FeignClientFactoryBean(), new Feign.Builder(), new FeignContext(), new Target.HardCodedTarget<>(TestApi.class, "/test"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTarget2() {
|
||||||
|
PolarisFeignCircuitBreakerTargeter targeter = new PolarisFeignCircuitBreakerTargeter(circuitBreakerFactory, circuitBreakerNameResolver);
|
||||||
|
FeignClientFactoryBean feignClientFactoryBean = mock(FeignClientFactoryBean.class);
|
||||||
|
doReturn(TestApi.class).when(feignClientFactoryBean).getFallback();
|
||||||
|
doReturn("test").when(feignClientFactoryBean).getName();
|
||||||
|
FeignContext feignClientFactory = mock(FeignContext.class);
|
||||||
|
doReturn(null).when(feignClientFactory).getInstance("test", TestApi.class);
|
||||||
|
assertThatThrownBy(() -> {
|
||||||
|
targeter.target(feignClientFactoryBean, new PolarisFeignCircuitBreaker.Builder(), feignClientFactory, new Target.HardCodedTarget<>(TestApi.class, "/test"));
|
||||||
|
}).isInstanceOf(IllegalStateException.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTarget3() {
|
||||||
|
PolarisFeignCircuitBreakerTargeter targeter = new PolarisFeignCircuitBreakerTargeter(circuitBreakerFactory, circuitBreakerNameResolver);
|
||||||
|
FeignClientFactoryBean feignClientFactoryBean = mock(FeignClientFactoryBean.class);
|
||||||
|
doReturn(void.class).when(feignClientFactoryBean).getFallback();
|
||||||
|
doReturn(TestApi.class).when(feignClientFactoryBean).getFallbackFactory();
|
||||||
|
doReturn("test").when(feignClientFactoryBean).getName();
|
||||||
|
FeignContext feignClientFactory = mock(FeignContext.class);
|
||||||
|
doReturn(Object.class).when(feignClientFactory).getInstance("test", TestApi.class);
|
||||||
|
assertThatThrownBy(() -> {
|
||||||
|
targeter.target(feignClientFactoryBean, new PolarisFeignCircuitBreaker.Builder(), feignClientFactory, new Target.HardCodedTarget<>(TestApi.class, "/test"));
|
||||||
|
}).isInstanceOf(IllegalStateException.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TestApi {
|
||||||
|
@RequestLine("GET /test")
|
||||||
|
void test();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
107
spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerGatewayIntegrationTest.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/gateway/PolarisCircuitBreakerGatewayIntegrationTest.java
107
spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerGatewayIntegrationTest.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/gateway/PolarisCircuitBreakerGatewayIntegrationTest.java
120
spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerIntegrationTest.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerRestTemplateIntegrationTest.java
120
spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerIntegrationTest.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerRestTemplateIntegrationTest.java
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2023 www.tencent.com.
|
||||||
|
* All Rights Reserved.
|
||||||
|
* This program is the confidential and proprietary information of
|
||||||
|
* www.tencent.com ("Confidential Information"). You shall not disclose such
|
||||||
|
* Confidential Information and shall use it only in accordance with
|
||||||
|
* the terms of the license agreement you entered into with www.tencent.com.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.tencent.cloud.polaris.config.adapter;
|
||||||
|
|
||||||
|
import com.tencent.polaris.configuration.api.core.ConfigFileService;
|
||||||
|
|
||||||
|
import org.springframework.core.env.CompositePropertySource;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Date Jul 23, 2023 2:57:49 PM
|
||||||
|
* @author juanyinyang
|
||||||
|
*/
|
||||||
|
public interface PolarisConfigCustomExtensionLayer {
|
||||||
|
void execute(String namespace, Environment environment, CompositePropertySource compositePropertySource, PolarisPropertySourceManager polarisPropertySourceManager, ConfigFileService configFileService);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,94 @@
|
|||||||
|
/*
|
||||||
|
* 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.adapter;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.core.PriorityOrdered;
|
||||||
|
import org.springframework.lang.NonNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mainly used to detect whether the annotation class {@link org.springframework.cloud.context.config.annotation.RefreshScope}
|
||||||
|
* exists, and whether the user has configured beans using this annotation in their business system.
|
||||||
|
* If the annotation {@code @RefreshScope} exists and is used, the auto-optimization will be triggered
|
||||||
|
* in listener {@link com.tencent.cloud.polaris.config.listener.PolarisConfigRefreshOptimizationListener}.
|
||||||
|
*
|
||||||
|
* <p>This bean will only be created and initialized when the config refresh type is {@code RefreshType.REFLECT}.
|
||||||
|
*
|
||||||
|
* @author jarvisxiong
|
||||||
|
*/
|
||||||
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||||
|
public class PolarisConfigRefreshScopeAnnotationDetector implements BeanPostProcessor, InitializingBean, PriorityOrdered {
|
||||||
|
|
||||||
|
private final AtomicBoolean isRefreshScopeAnnotationUsed = new AtomicBoolean(false);
|
||||||
|
|
||||||
|
private Class refreshScopeAnnotationClass;
|
||||||
|
|
||||||
|
private String annotatedRefreshScopeBeanName;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object postProcessBeforeInitialization(@NonNull Object bean, @NonNull String beanName)
|
||||||
|
throws BeansException {
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object postProcessAfterInitialization(@NonNull Object bean, @NonNull String beanName)
|
||||||
|
throws BeansException {
|
||||||
|
if (isRefreshScopeAnnotationUsed() || refreshScopeAnnotationClass == null) {
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
Annotation[] refreshScopeAnnotations = bean.getClass().getAnnotationsByType(refreshScopeAnnotationClass);
|
||||||
|
if (refreshScopeAnnotations.length > 0) {
|
||||||
|
if (isRefreshScopeAnnotationUsed.compareAndSet(false, true)) {
|
||||||
|
annotatedRefreshScopeBeanName = beanName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterPropertiesSet() {
|
||||||
|
try {
|
||||||
|
refreshScopeAnnotationClass = Class.forName(
|
||||||
|
"org.springframework.cloud.context.config.annotation.RefreshScope",
|
||||||
|
false,
|
||||||
|
getClass().getClassLoader());
|
||||||
|
}
|
||||||
|
catch (ClassNotFoundException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return Ordered.LOWEST_PRECEDENCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isRefreshScopeAnnotationUsed() {
|
||||||
|
return isRefreshScopeAnnotationUsed.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAnnotatedRefreshScopeBeanName() {
|
||||||
|
return annotatedRefreshScopeBeanName;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,132 @@
|
|||||||
|
/*
|
||||||
|
* 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.listener;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.config.adapter.PolarisConfigRefreshScopeAnnotationDetector;
|
||||||
|
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
|
||||||
|
import com.tencent.cloud.polaris.config.adapter.PolarisRefreshEntireContextRefresher;
|
||||||
|
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
|
||||||
|
import com.tencent.cloud.polaris.config.enums.RefreshType;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.config.ConstructorArgumentValues;
|
||||||
|
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||||
|
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||||
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||||
|
import org.springframework.cloud.context.refresh.ContextRefresher;
|
||||||
|
import org.springframework.context.ApplicationListener;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
import org.springframework.context.event.ContextRefreshedEvent;
|
||||||
|
import org.springframework.core.env.MapPropertySource;
|
||||||
|
import org.springframework.core.env.MutablePropertySources;
|
||||||
|
import org.springframework.lang.NonNull;
|
||||||
|
|
||||||
|
import static com.tencent.cloud.polaris.config.condition.ReflectRefreshTypeCondition.POLARIS_CONFIG_REFRESH_TYPE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When {@link com.tencent.cloud.polaris.config.adapter.PolarisConfigRefreshScopeAnnotationDetector} detects that
|
||||||
|
* the annotation {@code @RefreshScope} exists and is used, but the config refresh type
|
||||||
|
* {@code spring.cloud.polaris.config.refresh-type} is still {@code RefreshType.REFLECT}, then the framework will
|
||||||
|
* automatically switch the config refresh type to {@code RefreshType.REFRESH_CONTEXT}.
|
||||||
|
*
|
||||||
|
* <p>The purpose of this optimization is to omit additional configuration, and facilitate for users to use the
|
||||||
|
* dynamic configuration refresh strategy of Spring Cloud Context.</p>
|
||||||
|
*
|
||||||
|
* @author jarvisxiong
|
||||||
|
*/
|
||||||
|
public class PolarisConfigRefreshOptimizationListener implements ApplicationListener<ContextRefreshedEvent> {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(PolarisConfigRefreshOptimizationListener.class);
|
||||||
|
|
||||||
|
private static final String CONFIG_REFRESH_TYPE_PROPERTY = "configRefreshTypeProperty";
|
||||||
|
|
||||||
|
private static final String REFLECT_REBINDER_BEAN_NAME = "affectedConfigurationPropertiesRebinder";
|
||||||
|
|
||||||
|
private static final String REFLECT_REFRESHER_BEAN_NAME = "polarisReflectPropertySourceAutoRefresher";
|
||||||
|
|
||||||
|
private static final String REFRESH_CONTEXT_REFRESHER_BEAN_NAME = "polarisRefreshContextPropertySourceAutoRefresher";
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onApplicationEvent(@NonNull ContextRefreshedEvent event) {
|
||||||
|
ConfigurableApplicationContext applicationContext = (ConfigurableApplicationContext) event.getApplicationContext();
|
||||||
|
PolarisConfigRefreshScopeAnnotationDetector detector = applicationContext.getBean(PolarisConfigRefreshScopeAnnotationDetector.class);
|
||||||
|
boolean isRefreshScopeAnnotationUsed = detector.isRefreshScopeAnnotationUsed();
|
||||||
|
String annotatedRefreshScopeBeanName = detector.getAnnotatedRefreshScopeBeanName();
|
||||||
|
// using System.setProperty to set spring.cloud.polaris.config.refresh-type
|
||||||
|
String value = System.getProperty("spring.cloud.polaris.config.refresh-type");
|
||||||
|
boolean isSystemSetRefreshType = RefreshType.REFRESH_CONTEXT.toString().equalsIgnoreCase(value);
|
||||||
|
// a bean is using @RefreshScope, but the config refresh type is still [reflect], switch automatically
|
||||||
|
if (isRefreshScopeAnnotationUsed || isSystemSetRefreshType) {
|
||||||
|
if (isRefreshScopeAnnotationUsed) {
|
||||||
|
LOGGER.warn("Detected that the bean [{}] is using @RefreshScope annotation, but the config refresh type is still [reflect]. " + "[SCT] will automatically switch to [refresh_context].", annotatedRefreshScopeBeanName);
|
||||||
|
}
|
||||||
|
if (isSystemSetRefreshType) {
|
||||||
|
LOGGER.warn("Detected that using System.setProperty to set spring.cloud.polaris.config.refresh-type = refresh_context, but the config refresh type is still [reflect]. " + "[SCT] will automatically switch to [refresh_context].");
|
||||||
|
}
|
||||||
|
switchConfigRefreshTypeProperty(applicationContext);
|
||||||
|
modifyPolarisConfigPropertiesBean(applicationContext);
|
||||||
|
// remove related bean of type [reflect]
|
||||||
|
removeRelatedBeansOfReflect(applicationContext);
|
||||||
|
// register a new refresher bean of type [refresh_context]
|
||||||
|
registerRefresherBeanOfRefreshContext(applicationContext);
|
||||||
|
// add the new refresher to context as a listener
|
||||||
|
addRefresherBeanAsListener(applicationContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void switchConfigRefreshTypeProperty(ConfigurableApplicationContext applicationContext) {
|
||||||
|
MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources();
|
||||||
|
propertySources.addFirst(new MapPropertySource(CONFIG_REFRESH_TYPE_PROPERTY, Collections.singletonMap(POLARIS_CONFIG_REFRESH_TYPE, RefreshType.REFRESH_CONTEXT)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void modifyPolarisConfigPropertiesBean(ConfigurableApplicationContext applicationContext) {
|
||||||
|
PolarisConfigProperties polarisConfigProperties = applicationContext.getBean(PolarisConfigProperties.class);
|
||||||
|
polarisConfigProperties.setRefreshType(RefreshType.REFRESH_CONTEXT);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void removeRelatedBeansOfReflect(ConfigurableApplicationContext applicationContext) {
|
||||||
|
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext.getBeanFactory();
|
||||||
|
beanFactory.removeBeanDefinition(REFLECT_REFRESHER_BEAN_NAME);
|
||||||
|
beanFactory.removeBeanDefinition(REFLECT_REBINDER_BEAN_NAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void registerRefresherBeanOfRefreshContext(ConfigurableApplicationContext applicationContext) {
|
||||||
|
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext.getBeanFactory();
|
||||||
|
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
|
||||||
|
beanDefinition.setBeanClass(PolarisRefreshEntireContextRefresher.class);
|
||||||
|
PolarisConfigProperties polarisConfigProperties = beanFactory.getBean(PolarisConfigProperties.class);
|
||||||
|
PolarisPropertySourceManager polarisPropertySourceManager = beanFactory.getBean(PolarisPropertySourceManager.class);
|
||||||
|
ContextRefresher contextRefresher = beanFactory.getBean(ContextRefresher.class);
|
||||||
|
ConstructorArgumentValues constructorArgumentValues = beanDefinition.getConstructorArgumentValues();
|
||||||
|
constructorArgumentValues.addIndexedArgumentValue(0, polarisConfigProperties);
|
||||||
|
constructorArgumentValues.addIndexedArgumentValue(1, polarisPropertySourceManager);
|
||||||
|
constructorArgumentValues.addIndexedArgumentValue(2, contextRefresher);
|
||||||
|
beanFactory.registerBeanDefinition(REFRESH_CONTEXT_REFRESHER_BEAN_NAME, beanDefinition);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void addRefresherBeanAsListener(ConfigurableApplicationContext applicationContext) {
|
||||||
|
PolarisRefreshEntireContextRefresher refresher = (PolarisRefreshEntireContextRefresher) applicationContext.getBean(REFRESH_CONTEXT_REFRESHER_BEAN_NAME);
|
||||||
|
applicationContext.addApplicationListener(refresher);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.tencent.cloud.polaris.config.logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author juanyinyang
|
||||||
|
*/
|
||||||
|
public enum Level {
|
||||||
|
|
||||||
|
/** 日志级别. */
|
||||||
|
TRACE("TRACE"), DEBUG("DEBUG"), INFO("INFO"), WARN("WARN"), ERROR("ERROR"), FATAL("FATAL"), OFF("OFF");
|
||||||
|
|
||||||
|
private String level;
|
||||||
|
|
||||||
|
Level(String level) {
|
||||||
|
this.level = level;
|
||||||
|
}
|
||||||
|
public String getLevel() {
|
||||||
|
return level;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Level levelOf(String level) {
|
||||||
|
for (Level l : Level.values()) {
|
||||||
|
if (l.level.equalsIgnoreCase(level)) {
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.tencent.cloud.polaris.config.logger;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
||||||
|
import org.springframework.boot.logging.LoggingSystem;
|
||||||
|
import org.springframework.context.ApplicationEvent;
|
||||||
|
import org.springframework.context.ApplicationListener;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author juanyinyang
|
||||||
|
*/
|
||||||
|
public class PolarisConfigLoggerApplicationListener implements ApplicationListener<ApplicationEvent> {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(PolarisConfigLoggerApplicationListener.class);
|
||||||
|
/**
|
||||||
|
* @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onApplicationEvent(ApplicationEvent event) {
|
||||||
|
try {
|
||||||
|
// Initialize application loggingSystem.
|
||||||
|
if (event instanceof ApplicationStartedEvent) {
|
||||||
|
ApplicationStartedEvent startedEvent = (ApplicationStartedEvent) event;
|
||||||
|
ClassLoader classLoader = startedEvent.getSpringApplication().getClassLoader();
|
||||||
|
LoggingSystem loggingSystem = LoggingSystem.get(classLoader);
|
||||||
|
LOGGER.info("PolarisConfigLoggerApplicationListener onApplicationEvent init loggingSystem:{}", loggingSystem);
|
||||||
|
PolarisConfigLoggerContext.setLogSystem(loggingSystem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
LOGGER.error("PolarisConfigLoggerApplicationListener onApplicationEvent exception:", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,97 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018 www.tencent.com.
|
||||||
|
* All Rights Reserved.
|
||||||
|
* This program is the confidential and proprietary information of
|
||||||
|
* www.tencent.com ("Confidential Information"). You shall not disclose such
|
||||||
|
* Confidential Information and shall use it only in accordance with
|
||||||
|
* the terms of the license agreement you entered into with www.tencent.com.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.tencent.cloud.polaris.config.logger;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.boot.logging.LogLevel;
|
||||||
|
import org.springframework.boot.logging.LoggingSystem;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
import static org.springframework.boot.logging.LoggingSystem.ROOT_LOGGER_NAME;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author juanyinyang
|
||||||
|
*/
|
||||||
|
public final class PolarisConfigLoggerContext {
|
||||||
|
|
||||||
|
private static LoggingSystem loggingSystem;
|
||||||
|
|
||||||
|
private PolarisConfigLoggerContext() {
|
||||||
|
|
||||||
|
}
|
||||||
|
protected static void setLogSystem(LoggingSystem logSystem) {
|
||||||
|
Assert.notNull(logSystem, "Logging System should not be null");
|
||||||
|
PolarisConfigLoggerContext.loggingSystem = logSystem;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setLevel(String loggerName, String level) {
|
||||||
|
if (loggingSystem == null) {
|
||||||
|
printLog("[SCT Config] PolarisConfigLoggerContext logger: [" + loggerName + "] change to target level fail. caused by internal exception:" + level, Level.WARN);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Level loggerLevel = Level.levelOf(level);
|
||||||
|
if (loggerLevel == null) {
|
||||||
|
printLog("[SCT Config] PolarisConfigLoggerContext logger: [" + loggerName + "] change to target level fail. caused by level is not support, level:" + level, Level.WARN);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LogLevel logLevel = null;
|
||||||
|
switch (loggerLevel) {
|
||||||
|
case TRACE:
|
||||||
|
logLevel = LogLevel.TRACE;
|
||||||
|
break;
|
||||||
|
case DEBUG:
|
||||||
|
logLevel = LogLevel.DEBUG;
|
||||||
|
break;
|
||||||
|
case OFF:
|
||||||
|
logLevel = LogLevel.OFF;
|
||||||
|
break;
|
||||||
|
case INFO:
|
||||||
|
logLevel = LogLevel.INFO;
|
||||||
|
break;
|
||||||
|
case WARN:
|
||||||
|
logLevel = LogLevel.WARN;
|
||||||
|
break;
|
||||||
|
case ERROR:
|
||||||
|
logLevel = LogLevel.ERROR;
|
||||||
|
break;
|
||||||
|
case FATAL:
|
||||||
|
logLevel = LogLevel.FATAL;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
printLog("[SCT Config] PolarisConfigLoggerContext logger: [" + loggerName + "] setLevel fail. caused by level is not support, level: " + level, Level.WARN);
|
||||||
|
}
|
||||||
|
loggingSystem.setLogLevel(loggerName, logLevel);
|
||||||
|
printLog("[SCT Config] PolarisConfigLoggerContext logger: [" + loggerName + "] changed to level:" + level, Level.INFO);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 打印日志.
|
||||||
|
*/
|
||||||
|
private static void printLog(String message, Level level) {
|
||||||
|
Logger logger = LoggerFactory.getLogger(ROOT_LOGGER_NAME);
|
||||||
|
if (level.ordinal() <= Level.INFO.ordinal()) {
|
||||||
|
if (logger != null) {
|
||||||
|
logger.info(message);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
StdLog.info(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (logger != null) {
|
||||||
|
logger.warn(message);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
StdLog.warn(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,79 @@
|
|||||||
|
package com.tencent.cloud.polaris.config.logger;
|
||||||
|
|
||||||
|
import java.io.PrintStream;
|
||||||
|
import java.util.Calendar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* internal output logger.
|
||||||
|
* @author juanyinyang
|
||||||
|
*/
|
||||||
|
public final class StdLog {
|
||||||
|
|
||||||
|
private static final String CLASS_INFO = StdLog.class.getName();
|
||||||
|
|
||||||
|
|
||||||
|
protected static boolean debugEnabled = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* *enable info level log.
|
||||||
|
*/
|
||||||
|
protected static boolean infoEnabled = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* quite mode will out put nothing.
|
||||||
|
*/
|
||||||
|
protected static boolean quietMode = false;
|
||||||
|
|
||||||
|
private static final String DEBUG_FIX = "StdLog:DEBUG: ";
|
||||||
|
|
||||||
|
private static final String INFO_FIX = "StdLog:INFO: ";
|
||||||
|
|
||||||
|
private static final String WARN_FIX = "StdLog:WARN: ";
|
||||||
|
|
||||||
|
private StdLog() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setQuietMode(boolean quietMode) {
|
||||||
|
StdLog.quietMode = quietMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setInfoEnabled(boolean infoEnabled) {
|
||||||
|
StdLog.infoEnabled = infoEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setDebugEnabled(boolean debugEnabled) {
|
||||||
|
StdLog.debugEnabled = debugEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void debug(String msg) {
|
||||||
|
if (debugEnabled && !quietMode) {
|
||||||
|
println(System.out, DEBUG_FIX + msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void info(String msg) {
|
||||||
|
if (infoEnabled && !quietMode) {
|
||||||
|
println(System.out, INFO_FIX + msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void warn(String msg) {
|
||||||
|
if (infoEnabled && !quietMode) {
|
||||||
|
println(System.err, WARN_FIX + msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void warn(String msg, Throwable t) {
|
||||||
|
if (quietMode) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
println(System.err, WARN_FIX + msg);
|
||||||
|
if (t != null) {
|
||||||
|
t.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void println(PrintStream out, String msg) {
|
||||||
|
out.println(Calendar.getInstance().getTime().toString() + " " + CLASS_INFO + " " + msg);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,101 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed
|
||||||
|
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||||
|
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.tencent.cloud.polaris.config.adapter;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration;
|
||||||
|
import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration;
|
||||||
|
import org.assertj.core.api.InstanceOfAssertFactories;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
|
import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration;
|
||||||
|
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
|
||||||
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.as;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test for {@link PolarisConfigRefreshScopeAnnotationDetector}.
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
|
public class PolarisConfigRefreshScopeAnnotationDetectorTest {
|
||||||
|
|
||||||
|
private static Class refreshScopeAnnotationClass = null;
|
||||||
|
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
refreshScopeAnnotationClass = Class.forName(
|
||||||
|
"org.springframework.cloud.context.config.annotation.RefreshScope",
|
||||||
|
false,
|
||||||
|
PolarisConfigRefreshScopeAnnotationDetectorTest.class.getClassLoader());
|
||||||
|
}
|
||||||
|
catch (ClassNotFoundException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUseRefreshScope() {
|
||||||
|
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||||
|
.withConfiguration(AutoConfigurations.of(PolarisConfigBootstrapAutoConfiguration.class))
|
||||||
|
.withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class))
|
||||||
|
.withConfiguration(AutoConfigurations.of(RefreshAutoConfiguration.class))
|
||||||
|
.withConfiguration(AutoConfigurations.of(ConfigurationPropertiesRebinderAutoConfiguration.class))
|
||||||
|
.withBean("testBeanWithRefreshScope", TestBeanWithRefreshScope.class)
|
||||||
|
.withPropertyValues("spring.application.name=" + "polarisConfigRefreshScopeAnnotationDetectorTest")
|
||||||
|
.withPropertyValues("server.port=" + 8080)
|
||||||
|
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081")
|
||||||
|
.withPropertyValues("spring.cloud.polaris.config.connect-remote-server=false");
|
||||||
|
contextRunner.run(context -> {
|
||||||
|
assertThat(context).hasSingleBean(PolarisConfigRefreshScopeAnnotationDetector.class);
|
||||||
|
PolarisConfigRefreshScopeAnnotationDetector detector = context.getBean(PolarisConfigRefreshScopeAnnotationDetector.class);
|
||||||
|
assertThat(detector.isRefreshScopeAnnotationUsed()).isTrue();
|
||||||
|
assertThat(detector.getAnnotatedRefreshScopeBeanName()).isEqualTo("scopedTarget.testBeanWithRefreshScope");
|
||||||
|
assertThat(detector).extracting("refreshScopeAnnotationClass", as(InstanceOfAssertFactories.type(Class.class)))
|
||||||
|
.isEqualTo(refreshScopeAnnotationClass);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNotUseRefreshScope() {
|
||||||
|
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||||
|
.withConfiguration(AutoConfigurations.of(PolarisConfigBootstrapAutoConfiguration.class))
|
||||||
|
.withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class))
|
||||||
|
.withConfiguration(AutoConfigurations.of(RefreshAutoConfiguration.class))
|
||||||
|
.withConfiguration(AutoConfigurations.of(ConfigurationPropertiesRebinderAutoConfiguration.class))
|
||||||
|
.withPropertyValues("spring.application.name=" + "polarisConfigRefreshScopeAnnotationDetectorTest")
|
||||||
|
.withPropertyValues("server.port=" + 8080)
|
||||||
|
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081")
|
||||||
|
.withPropertyValues("spring.cloud.polaris.config.connect-remote-server=false");
|
||||||
|
contextRunner.run(context -> {
|
||||||
|
assertThat(context).hasSingleBean(PolarisConfigRefreshScopeAnnotationDetector.class);
|
||||||
|
PolarisConfigRefreshScopeAnnotationDetector detector = context.getBean(PolarisConfigRefreshScopeAnnotationDetector.class);
|
||||||
|
assertThat(detector.isRefreshScopeAnnotationUsed()).isFalse();
|
||||||
|
assertThat(detector.getAnnotatedRefreshScopeBeanName()).isNull();
|
||||||
|
assertThat(detector).extracting("refreshScopeAnnotationClass", as(InstanceOfAssertFactories.type(Class.class)))
|
||||||
|
.isEqualTo(refreshScopeAnnotationClass);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@RefreshScope
|
||||||
|
protected static class TestBeanWithRefreshScope {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,154 @@
|
|||||||
|
/*
|
||||||
|
* 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.listener;
|
||||||
|
|
||||||
|
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.adapter.PolarisRefreshAffectedContextRefresher;
|
||||||
|
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
|
||||||
|
import com.tencent.cloud.polaris.config.enums.RefreshType;
|
||||||
|
import com.tencent.polaris.configuration.api.core.ChangeType;
|
||||||
|
import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent;
|
||||||
|
import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.cloud.context.refresh.ContextRefresher;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
import org.springframework.context.support.AbstractApplicationContext;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
|
||||||
|
import static com.tencent.cloud.polaris.config.condition.ReflectRefreshTypeCondition.POLARIS_CONFIG_REFRESH_TYPE;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test for {@link PolarisConfigRefreshOptimizationListener}.
|
||||||
|
*/
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
@SpringBootTest(webEnvironment = DEFINED_PORT, classes = PolarisConfigRefreshOptimizationListenerNotTriggeredTest.TestApplication.class,
|
||||||
|
properties = {
|
||||||
|
"server.port=8081",
|
||||||
|
"spring.cloud.polaris.address=grpc://127.0.0.1:10081",
|
||||||
|
"spring.cloud.polaris.config.connect-remote-server=false",
|
||||||
|
"spring.cloud.polaris.config.refresh-type=reflect",
|
||||||
|
"spring.config.location = classpath:application-test.yml"
|
||||||
|
})
|
||||||
|
public class PolarisConfigRefreshOptimizationListenerNotTriggeredTest {
|
||||||
|
|
||||||
|
private static final String REFLECT_REFRESHER_BEAN_NAME = "polarisReflectPropertySourceAutoRefresher";
|
||||||
|
|
||||||
|
private static final String TEST_NAMESPACE = "testNamespace";
|
||||||
|
|
||||||
|
private static final String TEST_SERVICE_NAME = "testServiceName";
|
||||||
|
|
||||||
|
private static final String TEST_FILE_NAME = "application.properties";
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ConfigurableApplicationContext context;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNotSwitchConfigRefreshType() {
|
||||||
|
RefreshType actualRefreshType = context.getEnvironment()
|
||||||
|
.getProperty(POLARIS_CONFIG_REFRESH_TYPE, RefreshType.class);
|
||||||
|
assertThat(actualRefreshType).isEqualTo(RefreshType.REFLECT);
|
||||||
|
PolarisConfigProperties polarisConfigProperties = context.getBean(PolarisConfigProperties.class);
|
||||||
|
assertThat(polarisConfigProperties.getRefreshType()).isEqualTo(RefreshType.REFLECT);
|
||||||
|
assertThat(context.containsBean(REFLECT_REFRESHER_BEAN_NAME)).isTrue();
|
||||||
|
PolarisRefreshAffectedContextRefresher refresher = context
|
||||||
|
.getBean(REFLECT_REFRESHER_BEAN_NAME, PolarisRefreshAffectedContextRefresher.class);
|
||||||
|
assertThat(((AbstractApplicationContext) context).getApplicationListeners().contains(refresher)).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConfigFileChanged() {
|
||||||
|
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(TEST_NAMESPACE, TEST_SERVICE_NAME, TEST_FILE_NAME,
|
||||||
|
file, content);
|
||||||
|
PolarisPropertySourceManager manager = context.getBean(PolarisPropertySourceManager.class);
|
||||||
|
when(manager.getAllPropertySources()).thenReturn(Lists.newArrayList(polarisPropertySource));
|
||||||
|
|
||||||
|
PolarisRefreshAffectedContextRefresher refresher = context.getBean(PolarisRefreshAffectedContextRefresher.class);
|
||||||
|
PolarisRefreshAffectedContextRefresher spyRefresher = Mockito.spy(refresher);
|
||||||
|
|
||||||
|
spyRefresher.onApplicationEvent(null);
|
||||||
|
|
||||||
|
ConfigPropertyChangeInfo changeInfo = new ConfigPropertyChangeInfo("k1", "v1", "v11", ChangeType.MODIFIED);
|
||||||
|
ConfigPropertyChangeInfo changeInfo2 = new ConfigPropertyChangeInfo("k4", null, "v4", ChangeType.ADDED);
|
||||||
|
ConfigPropertyChangeInfo changeInfo3 = new ConfigPropertyChangeInfo("k2", "v2", null, ChangeType.DELETED);
|
||||||
|
Map<String, ConfigPropertyChangeInfo> changeInfos = new HashMap<>();
|
||||||
|
changeInfos.put("k1", changeInfo);
|
||||||
|
changeInfos.put("k2", changeInfo3);
|
||||||
|
changeInfos.put("k4", changeInfo2);
|
||||||
|
ConfigKVFileChangeEvent event = new ConfigKVFileChangeEvent(changeInfos);
|
||||||
|
file.fireChangeListener(event);
|
||||||
|
|
||||||
|
ContextRefresher mockContextRefresher = context.getBean(ContextRefresher.class);
|
||||||
|
when(mockContextRefresher.refresh()).thenReturn(event.changedKeys());
|
||||||
|
|
||||||
|
Mockito.verify(spyRefresher, Mockito.times(1))
|
||||||
|
.refreshSpringValue("k1");
|
||||||
|
Mockito.verify(spyRefresher, Mockito.times(1))
|
||||||
|
.refreshSpringValue("k2");
|
||||||
|
Mockito.verify(spyRefresher, Mockito.times(1))
|
||||||
|
.refreshSpringValue("k4");
|
||||||
|
Mockito.verify(spyRefresher, Mockito.times(1))
|
||||||
|
.refreshConfigurationProperties(event.changedKeys());
|
||||||
|
}
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
protected static class TestApplication {
|
||||||
|
|
||||||
|
@Primary
|
||||||
|
@Bean
|
||||||
|
public PolarisPropertySourceManager polarisPropertySourceManager() {
|
||||||
|
return mock(PolarisPropertySourceManager.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Primary
|
||||||
|
@Bean
|
||||||
|
public ContextRefresher contextRefresher() {
|
||||||
|
return mock(ContextRefresher.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Component
|
||||||
|
protected static class TestBeanWithoutRefreshScope {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,156 @@
|
|||||||
|
/*
|
||||||
|
* 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.listener;
|
||||||
|
|
||||||
|
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.adapter.PolarisRefreshEntireContextRefresher;
|
||||||
|
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
|
||||||
|
import com.tencent.cloud.polaris.config.enums.RefreshType;
|
||||||
|
import com.tencent.polaris.configuration.api.core.ChangeType;
|
||||||
|
import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent;
|
||||||
|
import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||||
|
import org.springframework.cloud.context.refresh.ContextRefresher;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
import org.springframework.context.support.AbstractApplicationContext;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
|
||||||
|
import static com.tencent.cloud.polaris.config.condition.ReflectRefreshTypeCondition.POLARIS_CONFIG_REFRESH_TYPE;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test for {@link PolarisConfigRefreshOptimizationListener}.
|
||||||
|
*/
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
@SpringBootTest(webEnvironment = DEFINED_PORT, classes = PolarisConfigRefreshOptimizationListenerTriggeredTest.TestApplication.class,
|
||||||
|
properties = {
|
||||||
|
"server.port=8081",
|
||||||
|
"spring.cloud.polaris.address=grpc://127.0.0.1:10081",
|
||||||
|
"spring.cloud.polaris.config.connect-remote-server=false",
|
||||||
|
"spring.cloud.polaris.config.refresh-type=reflect",
|
||||||
|
"spring.config.location = classpath:application-test.yml"
|
||||||
|
})
|
||||||
|
public class PolarisConfigRefreshOptimizationListenerTriggeredTest {
|
||||||
|
|
||||||
|
private static final String REFRESH_CONTEXT_REFRESHER_BEAN_NAME = "polarisRefreshContextPropertySourceAutoRefresher";
|
||||||
|
|
||||||
|
private static final String TEST_NAMESPACE = "testNamespace";
|
||||||
|
|
||||||
|
private static final String TEST_SERVICE_NAME = "testServiceName";
|
||||||
|
|
||||||
|
private static final String TEST_FILE_NAME = "application.properties";
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ConfigurableApplicationContext context;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSwitchConfigRefreshType() {
|
||||||
|
RefreshType actualRefreshType = context.getEnvironment()
|
||||||
|
.getProperty(POLARIS_CONFIG_REFRESH_TYPE, RefreshType.class);
|
||||||
|
assertThat(actualRefreshType).isEqualTo(RefreshType.REFRESH_CONTEXT);
|
||||||
|
PolarisConfigProperties polarisConfigProperties = context.getBean(PolarisConfigProperties.class);
|
||||||
|
assertThat(polarisConfigProperties.getRefreshType()).isEqualTo(RefreshType.REFRESH_CONTEXT);
|
||||||
|
assertThat(context.containsBean(REFRESH_CONTEXT_REFRESHER_BEAN_NAME)).isTrue();
|
||||||
|
PolarisRefreshEntireContextRefresher refresher = context
|
||||||
|
.getBean(REFRESH_CONTEXT_REFRESHER_BEAN_NAME, PolarisRefreshEntireContextRefresher.class);
|
||||||
|
assertThat(((AbstractApplicationContext) context).getApplicationListeners().contains(refresher)).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConfigFileChanged() {
|
||||||
|
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(TEST_NAMESPACE, TEST_SERVICE_NAME, TEST_FILE_NAME,
|
||||||
|
file, content);
|
||||||
|
PolarisPropertySourceManager manager = context.getBean(PolarisPropertySourceManager.class);
|
||||||
|
when(manager.getAllPropertySources()).thenReturn(Lists.newArrayList(polarisPropertySource));
|
||||||
|
|
||||||
|
PolarisRefreshEntireContextRefresher refresher = context.getBean(PolarisRefreshEntireContextRefresher.class);
|
||||||
|
PolarisRefreshEntireContextRefresher spyRefresher = Mockito.spy(refresher);
|
||||||
|
|
||||||
|
spyRefresher.onApplicationEvent(null);
|
||||||
|
|
||||||
|
ConfigPropertyChangeInfo changeInfo = new ConfigPropertyChangeInfo("k1", "v1", "v11", ChangeType.MODIFIED);
|
||||||
|
ConfigPropertyChangeInfo changeInfo2 = new ConfigPropertyChangeInfo("k4", null, "v4", ChangeType.ADDED);
|
||||||
|
ConfigPropertyChangeInfo changeInfo3 = new ConfigPropertyChangeInfo("k2", "v2", null, ChangeType.DELETED);
|
||||||
|
Map<String, ConfigPropertyChangeInfo> changeInfos = new HashMap<>();
|
||||||
|
changeInfos.put("k1", changeInfo);
|
||||||
|
changeInfos.put("k2", changeInfo3);
|
||||||
|
changeInfos.put("k4", changeInfo2);
|
||||||
|
ConfigKVFileChangeEvent event = new ConfigKVFileChangeEvent(changeInfos);
|
||||||
|
file.fireChangeListener(event);
|
||||||
|
|
||||||
|
ContextRefresher mockContextRefresher = context.getBean(ContextRefresher.class);
|
||||||
|
when(mockContextRefresher.refresh()).thenReturn(event.changedKeys());
|
||||||
|
|
||||||
|
Mockito.verify(spyRefresher, Mockito.times(1))
|
||||||
|
.refreshSpringValue("k1");
|
||||||
|
Mockito.verify(spyRefresher, Mockito.times(1))
|
||||||
|
.refreshSpringValue("k2");
|
||||||
|
Mockito.verify(spyRefresher, Mockito.times(1))
|
||||||
|
.refreshSpringValue("k4");
|
||||||
|
Mockito.verify(spyRefresher, Mockito.times(1))
|
||||||
|
.refreshConfigurationProperties(event.changedKeys());
|
||||||
|
}
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
protected static class TestApplication {
|
||||||
|
|
||||||
|
@Primary
|
||||||
|
@Bean
|
||||||
|
public PolarisPropertySourceManager polarisPropertySourceManager() {
|
||||||
|
return mock(PolarisPropertySourceManager.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Primary
|
||||||
|
@Bean
|
||||||
|
public ContextRefresher contextRefresher() {
|
||||||
|
return mock(ContextRefresher.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@RefreshScope
|
||||||
|
protected static class TestBeanWithRefreshScope {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* 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.registry;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Customizer for {@link PolarisRegistration}.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public interface PolarisRegistrationCustomizer {
|
||||||
|
void customize(PolarisRegistration registration);
|
||||||
|
}
|
@ -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.discovery;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
|
||||||
|
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
|
||||||
|
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||||
|
import com.tencent.polaris.api.exception.PolarisException;
|
||||||
|
import com.tencent.polaris.api.rpc.ServicesResponse;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link PolarisDiscoveryHandler}.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
public class PolarisDiscoveryHandlerTest {
|
||||||
|
|
||||||
|
private PolarisDiscoveryHandler polarisDiscoveryHandler;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
PolarisDiscoveryProperties polarisDiscoveryProperties = mock(PolarisDiscoveryProperties.class);
|
||||||
|
doReturn(NAMESPACE_TEST).when(polarisDiscoveryProperties).getNamespace();
|
||||||
|
|
||||||
|
ConsumerAPI consumerAPI = mock(ConsumerAPI.class);
|
||||||
|
ServicesResponse servicesResponse = mock(ServicesResponse.class);
|
||||||
|
doReturn(servicesResponse).when(consumerAPI).getServices(any());
|
||||||
|
|
||||||
|
PolarisSDKContextManager polarisSDKContextManager = mock(PolarisSDKContextManager.class);
|
||||||
|
doReturn(consumerAPI).when(polarisSDKContextManager).getConsumerAPI();
|
||||||
|
polarisDiscoveryHandler = new PolarisDiscoveryHandler(polarisDiscoveryProperties, polarisSDKContextManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetServices() throws PolarisException {
|
||||||
|
ServicesResponse servicesResponse = polarisDiscoveryHandler.getServices();
|
||||||
|
assertThat(servicesResponse).isNotNull();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,76 @@
|
|||||||
|
/*
|
||||||
|
* 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.registry;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
|
||||||
|
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
|
||||||
|
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import static com.tencent.polaris.test.common.Consts.PORT;
|
||||||
|
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link PolarisRegistrationCustomizer}.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public class PolarisRegistrationCustomizerTest {
|
||||||
|
|
||||||
|
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
|
||||||
|
.withConfiguration(AutoConfigurations.of(
|
||||||
|
PolarisContextAutoConfiguration.class,
|
||||||
|
PolarisServiceRegistryAutoConfiguration.class,
|
||||||
|
PolarisDiscoveryClientConfiguration.class))
|
||||||
|
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
|
||||||
|
.withPropertyValues("server.port=" + PORT)
|
||||||
|
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081");
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() {
|
||||||
|
PolarisSDKContextManager.innerDestroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCustomize() {
|
||||||
|
this.contextRunner.run(context -> {
|
||||||
|
PolarisRegistrationCustomizer customizer = context.getBean(PolarisRegistrationCustomizer.class);
|
||||||
|
verify(customizer, times(1)).customize(any(PolarisRegistration.class));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
static class PolarisServiceRegistryAutoConfiguration {
|
||||||
|
@Bean
|
||||||
|
public PolarisRegistrationCustomizer polarisRegistrationCustomizer() {
|
||||||
|
return mock(PolarisRegistrationCustomizer.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>multiple-discovery-nacos-example</artifactId>
|
||||||
|
<groupId>com.tencent.cloud</groupId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>nacos-callee-service-a</artifactId>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-source-plugin</artifactId>
|
||||||
|
<version>3.2.0</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>attach-sources</id>
|
||||||
|
<goals>
|
||||||
|
<goal>jar</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* 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.multiple.discovery.nacos;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Circuit breaker example callee application.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
public class NacosCalleeServiceA {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(NacosCalleeServiceA.class, args);
|
||||||
|
}
|
||||||
|
}
|
39
spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/BlockingLoadBalancerClientAspect.java → spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-a/src/main/java/com/tencent/cloud/multiple/discovery/nacos/NacosCalleeServiceAController.java
39
spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/BlockingLoadBalancerClientAspect.java → spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-a/src/main/java/com/tencent/cloud/multiple/discovery/nacos/NacosCalleeServiceAController.java
@ -0,0 +1,16 @@
|
|||||||
|
server:
|
||||||
|
port: 48082
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: NacosCalleeService
|
||||||
|
cloud:
|
||||||
|
polaris:
|
||||||
|
address: grpc://119.91.66.223:8091
|
||||||
|
namespace: default
|
||||||
|
enabled: true
|
||||||
|
stat:
|
||||||
|
enabled: true
|
||||||
|
port: 28083
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
server-addr: 127.0.0.1:8848
|
@ -0,0 +1,51 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>multiple-discovery-nacos-example</artifactId>
|
||||||
|
<groupId>com.tencent.cloud</groupId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>nacos-callee-service-b</artifactId>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-source-plugin</artifactId>
|
||||||
|
<version>3.2.0</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>attach-sources</id>
|
||||||
|
<goals>
|
||||||
|
<goal>jar</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* 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.multiple.discovery.nacos;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Circuit breaker example callee application.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
public class NacosCalleeServiceB {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(NacosCalleeServiceB.class, args);
|
||||||
|
}
|
||||||
|
}
|
@ -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.multiple.discovery.nacos;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service B Controller.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/example/service/b")
|
||||||
|
public class NacosCalleeServiceBController {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get service information.
|
||||||
|
*
|
||||||
|
* @return service information
|
||||||
|
*/
|
||||||
|
@GetMapping("/info")
|
||||||
|
@ResponseStatus(code = HttpStatus.BAD_GATEWAY)
|
||||||
|
public String info() {
|
||||||
|
return "BAD_GATEWAY ! from service B2";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/health")
|
||||||
|
@ResponseStatus(value = HttpStatus.BAD_GATEWAY, reason = "failed for call my service")
|
||||||
|
public String health() {
|
||||||
|
System.out.println("health check: 502 instance");
|
||||||
|
return "hello world ! I'm a service B1";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
server:
|
||||||
|
port: 48081
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: NacosCalleeService
|
||||||
|
cloud:
|
||||||
|
polaris:
|
||||||
|
address: grpc://119.91.66.223:8091
|
||||||
|
namespace: default
|
||||||
|
enabled: true
|
||||||
|
stat:
|
||||||
|
enabled: true
|
||||||
|
port: 28083
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
server-addr: 127.0.0.1:8848
|
||||||
|
|
@ -0,0 +1,71 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>multiple-discovery-nacos-example</artifactId>
|
||||||
|
<groupId>com.tencent.cloud</groupId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>nacos-discovery-caller</artifactId>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.tencent.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-tencent-polaris-router</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.tencent.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-tencent-discovery-adapter-plugin</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.tencent.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-tencent-polaris-circuitbreaker</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-source-plugin</artifactId>
|
||||||
|
<version>3.2.0</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>attach-sources</id>
|
||||||
|
<goals>
|
||||||
|
<goal>jar</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed
|
||||||
|
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||||
|
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.tencent.cloud.multiple.discovery.nacos;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Circuit breaker example callee application.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableFeignClients
|
||||||
|
public class NacosCallerService {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(NacosCallerService.class, args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* 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.multiple.discovery.nacos;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NacosCallerServiceController.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/example/service/a")
|
||||||
|
public class NacosCallerServiceController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ProviderB polarisServiceB;
|
||||||
|
|
||||||
|
@GetMapping("/getBServiceInfo")
|
||||||
|
public String getBServiceInfoFallbackFromCode() {
|
||||||
|
return polarisServiceB.info();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* 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.multiple.discovery.nacos;
|
||||||
|
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Circuit breaker example callee provider.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
@FeignClient(name = "NacosCalleeService")
|
||||||
|
public interface ProviderB {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get info of service B.
|
||||||
|
*
|
||||||
|
* @return info of service B
|
||||||
|
*/
|
||||||
|
@GetMapping("/example/service/b/info")
|
||||||
|
String info();
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
server:
|
||||||
|
port: 48080
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: NacosCallerService
|
||||||
|
cloud:
|
||||||
|
polaris:
|
||||||
|
address: grpc://119.91.66.223:8091
|
||||||
|
namespace: default
|
||||||
|
enabled: true
|
||||||
|
loadbalancer:
|
||||||
|
enabled: true
|
||||||
|
discoveryType: Nacos
|
||||||
|
circuitbreaker:
|
||||||
|
enabled: true
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
server-addr: 127.0.0.1:8848
|
||||||
|
|
||||||
|
feign:
|
||||||
|
hystrix:
|
||||||
|
enabled: true
|
||||||
|
|
||||||
|
logging:
|
||||||
|
level:
|
||||||
|
root: info
|
||||||
|
com.tencent.cloud: debug
|
@ -0,0 +1,29 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>multiple-discovery-example</artifactId>
|
||||||
|
<groupId>com.tencent.cloud</groupId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>multiple-discovery-nacos-example</artifactId>
|
||||||
|
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
<modules>
|
||||||
|
<module>nacos-callee-service-a</module>
|
||||||
|
<module>nacos-callee-service-b</module>
|
||||||
|
<module>nacos-discovery-caller</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
<version>2.2.9.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>spring-cloud-tencent-examples</artifactId>
|
||||||
|
<groupId>com.tencent.cloud</groupId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>multiple-discovery-example</artifactId>
|
||||||
|
<name>Spring Cloud Starter Tencent Multiple Discovery Example</name>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
|
<modules>
|
||||||
|
<module>multiple-discovery-nacos-example</module>
|
||||||
|
</modules>
|
||||||
|
</project>
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue