Skip to content

Commit

Permalink
Merge branch '2021.0' into feature/nacos-namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
wenxuan70 authored Sep 28, 2023
2 parents 5613b58 + abad0c3 commit a553cc0
Show file tree
Hide file tree
Showing 82 changed files with 2,166 additions and 1,950 deletions.
1 change: 1 addition & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ fixes #

- [ ] Will pull request to branch of 2020.0.
- [ ] Will pull request to branch of 2022.0.
- [ ] Will pull request to branch of hoxton.
2 changes: 1 addition & 1 deletion .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ header:
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
paths:
- "**/java/**"
- "**/tencent/**"
language:
Java:
extensions:
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@
- [fix: dynamic routing using cookies.](https://github.com/Tencent/spring-cloud-tencent/pull/1097)
- [Refactoring:remove invalid @AutoConfigureAfter and @AutoConfigureBefore from discovery client automatic configuration.](https://github.com/Tencent/spring-cloud-tencent/pull/1115)
- [feat: support nacos namespace mapping](https://github.com/Tencent/spring-cloud-tencent/pull/1122)
- [feat: support log path configuration parameters.](https://github.com/Tencent/spring-cloud-tencent/pull/1128)
- [refactor:optimize the order and condition matching of service registration automatic configuration.](https://github.com/Tencent/spring-cloud-tencent/pull/1129)
- [feat: add circuit breaker actuator.](https://github.com/Tencent/spring-cloud-tencent/pull/1136)
- [feat: add metadata transfer for http header via spring.cloud.tencent.metadata.headers.](https://github.com/Tencent/spring-cloud-tencent/pull/1137)
- [feat:support service contract reporting.](https://github.com/Tencent/spring-cloud-tencent/pull/1139)
- [feat:add swagger exposure filters.](https://github.com/Tencent/spring-cloud-tencent/pull/1146)
- [feat:add swagger report switch.](https://github.com/Tencent/spring-cloud-tencent/pull/1148)
- [fix:fix retry loadbalancer not working bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1154)
2 changes: 1 addition & 1 deletion README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Spring Cloud Tencent 所有组件都已上传到 Maven 中央仓库,只需要
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-dependencies</artifactId>
<!--version number-->
<version>1.12.0-2021.0.8</version>
<version>1.12.1-2021.0.8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ For example:
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-dependencies</artifactId>
<!--version number-->
<version>1.12.0-2021.0.8</version>
<version>1.12.1-2021.0.8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<module>spring-cloud-starter-tencent-polaris-ratelimit</module>
<module>spring-cloud-starter-tencent-polaris-circuitbreaker</module>
<module>spring-cloud-starter-tencent-polaris-router</module>
<module>spring-cloud-starter-tencent-polaris-contract</module>
<module>spring-cloud-tencent-plugin-starters</module>
<module>spring-cloud-tencent-dependencies</module>
<module>spring-cloud-starter-tencent-all</module>
Expand Down Expand Up @@ -91,7 +92,7 @@
<revision>1.13.0-2021.0.8-SNAPSHOT</revision>

<!-- Spring Framework -->
<spring.framework.version>5.3.29</spring.framework.version>
<spring.framework.version>5.3.25</spring.framework.version>

<!-- Spring Boot -->
<spring.boot.version>2.6.15</spring.boot.version>
Expand Down
22 changes: 17 additions & 5 deletions spring-cloud-starter-tencent-all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
<artifactId>spring-cloud-starter-tencent-metadata-transfer</artifactId>
</dependency>

<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-contract</artifactId>
</dependency>

<!-- Spring Cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
Expand Down Expand Up @@ -88,12 +93,19 @@
<configuration>
<createSourcesJar>true</createSourcesJar>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring/org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration.imports</resource>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>
META-INF/spring/org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration.imports
</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports</resource>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
</resource>
</transformer>
</transformers>
</configuration>
Expand Down
12 changes: 12 additions & 0 deletions spring-cloud-starter-tencent-polaris-circuitbreaker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@
</dependency>
<!-- Polaris dependencies end -->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator-autoconfigure</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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.endpoint;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.JsonFormat;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.util.JacksonUtils;
import com.tencent.cloud.polaris.context.ServiceRuleManager;
import com.tencent.polaris.specification.api.v1.fault.tolerance.CircuitBreakerProto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;

/**
* Endpoint of polaris circuit breaker, include circuit breaker rules.
*
* @author wenxuan70
*/
@Endpoint(id = "polaris-circuit-breaker")
public class PolarisCircuitBreakerEndpoint {

private static final Logger LOG = LoggerFactory.getLogger(PolarisCircuitBreakerEndpoint.class);

private final ServiceRuleManager serviceRuleManager;

public PolarisCircuitBreakerEndpoint(ServiceRuleManager serviceRuleManager) {
this.serviceRuleManager = serviceRuleManager;
}

@ReadOperation
public Map<String, Object> circuitBreaker() {
CircuitBreakerProto.CircuitBreaker circuitBreaker = serviceRuleManager.getServiceCircuitBreakerRule(
MetadataContext.LOCAL_NAMESPACE,
MetadataContext.LOCAL_SERVICE
);

Map<String, Object> polarisCircuitBreakerInfo = new HashMap<>();

polarisCircuitBreakerInfo.put("namespace", MetadataContext.LOCAL_NAMESPACE);
polarisCircuitBreakerInfo.put("service", MetadataContext.LOCAL_SERVICE);
polarisCircuitBreakerInfo.put("circuitBreakerRules", parseCircuitBreakerRule(circuitBreaker));

return polarisCircuitBreakerInfo;
}

private List<Object> parseCircuitBreakerRule(CircuitBreakerProto.CircuitBreaker circuitBreaker) {
List<Object> circuitBreakerRuleList = new ArrayList<>();

for (CircuitBreakerProto.CircuitBreakerRule circuitBreakerRule : circuitBreaker.getRulesList()) {
String ruleJson;
try {
ruleJson = JsonFormat.printer().print(circuitBreakerRule);
}
catch (InvalidProtocolBufferException e) {
LOG.error("rule to Json failed. check rule {}.", circuitBreakerRule, e);
throw new RuntimeException("Json failed.", e);
}
circuitBreakerRuleList.add(JacksonUtils.deserialize2Map(ruleJson));
}

return circuitBreakerRuleList;
}
}
Original file line number Diff line number Diff line change
@@ -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.circuitbreaker.endpoint;

import com.tencent.cloud.polaris.circuitbreaker.config.ConditionalOnPolarisCircuitBreakerEnabled;
import com.tencent.cloud.polaris.context.ServiceRuleManager;

import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* The AutoConfiguration for Polaris CircuitBreaker's Endpoint.
*
* @author wenxuan70
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(Endpoint.class)
@ConditionalOnPolarisCircuitBreakerEnabled
public class PolarisCircuitBreakerEndpointAutoConfiguration {

@Bean
@ConditionalOnBean(ServiceRuleManager.class)
@ConditionalOnMissingBean
@ConditionalOnAvailableEndpoint
public PolarisCircuitBreakerEndpoint polarisCircuitBreakerEndpoint(ServiceRuleManager serviceRuleManager) {
return new PolarisCircuitBreakerEndpoint(serviceRuleManager);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tencent.cloud.polaris.circuitbreaker.config.PolarisCircuitBreakerAutoConfiguration,\
com.tencent.cloud.polaris.circuitbreaker.config.ReactivePolarisCircuitBreakerAutoConfiguration,\
com.tencent.cloud.polaris.circuitbreaker.config.PolarisCircuitBreakerFeignClientAutoConfiguration,\
com.tencent.cloud.polaris.circuitbreaker.config.GatewayPolarisCircuitBreakerAutoConfiguration
com.tencent.cloud.polaris.circuitbreaker.config.GatewayPolarisCircuitBreakerAutoConfiguration,\
com.tencent.cloud.polaris.circuitbreaker.endpoint.PolarisCircuitBreakerEndpointAutoConfiguration
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.tencent.cloud.polaris.circuitbreaker.config.PolarisCircuitBreakerBootstrapConfiguration
Original file line number Diff line number Diff line change
@@ -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.polaris.circuitbreaker.endpoint;

import java.util.Map;

import com.google.protobuf.StringValue;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
import com.tencent.cloud.polaris.context.ServiceRuleManager;
import com.tencent.polaris.specification.api.v1.fault.tolerance.CircuitBreakerProto;
import com.tencent.polaris.specification.api.v1.model.ModelProto;
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 org.springframework.boot.test.context.runner.ApplicationContextRunner;

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.mock;
import static org.mockito.Mockito.when;

/**
* Test for {@link PolarisCircuitBreakerEndpoint}.
*
* @author wenxuan70
*/
@ExtendWith(MockitoExtension.class)
public class PolarisCircuitBreakerEndpointTest {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withBean(ApplicationContextAwareUtils.class)
.withPropertyValues("spring.cloud.polaris.namespace=" + NAMESPACE_TEST)
.withPropertyValues("spring.cloud.polaris.service=" + SERVICE_PROVIDER);

private ServiceRuleManager serviceRuleManager;

@BeforeEach
void setUp() {
serviceRuleManager = mock(ServiceRuleManager.class);
when(serviceRuleManager.getServiceCircuitBreakerRule(anyString(), anyString())).thenAnswer(invocation -> {
CircuitBreakerProto.CircuitBreakerRule.Builder ruleBuilder = CircuitBreakerProto.CircuitBreakerRule.newBuilder();
ruleBuilder.setName("test_for_circuit_breaker");
ruleBuilder.setEnable(true);
ruleBuilder.setLevel(CircuitBreakerProto.Level.METHOD);
CircuitBreakerProto.RuleMatcher.Builder rmBuilder = CircuitBreakerProto.RuleMatcher.newBuilder();
rmBuilder.setDestination(CircuitBreakerProto.RuleMatcher.DestinationService.newBuilder().setNamespace("default").setService("svc2").setMethod(
ModelProto.MatchString.newBuilder().setValue(StringValue.newBuilder().setValue("*").build()).build()).build());
rmBuilder.setSource(CircuitBreakerProto.RuleMatcher.SourceService.newBuilder().setNamespace("*").setService("*").build());
ruleBuilder.setRuleMatcher(rmBuilder.build());
return CircuitBreakerProto.CircuitBreaker.newBuilder().addRules(ruleBuilder.build()).build();
});
}

@Test
public void testPolarisCircuitBreaker() {
contextRunner.run(context -> {
PolarisCircuitBreakerEndpoint endpoint = new PolarisCircuitBreakerEndpoint(serviceRuleManager);
Map<String, Object> circuitBreakerInfo = endpoint.circuitBreaker();
assertThat(circuitBreakerInfo).isNotNull();
assertThat(circuitBreakerInfo.get("namespace")).isNotNull();
assertThat(circuitBreakerInfo.get("service")).isNotNull();
assertThat(circuitBreakerInfo.get("circuitBreakerRules")).asList().isNotEmpty();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@
import com.tencent.cloud.polaris.config.annotation.PolarisConfigAnnotationProcessor;
import com.tencent.cloud.polaris.config.condition.ConditionalOnReflectRefreshType;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.listener.PolarisConfigApplicationEventListener;
import com.tencent.cloud.polaris.config.listener.PolarisConfigChangeEventListener;
import com.tencent.cloud.polaris.config.listener.PolarisConfigRefreshOptimizationListener;
import com.tencent.cloud.polaris.config.logger.PolarisConfigLoggerApplicationListener;
import com.tencent.cloud.polaris.config.spring.annotation.SpringValueProcessor;
import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper;
import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;

import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand Down Expand Up @@ -122,10 +120,5 @@ public PolarisConfigRefreshScopeAnnotationDetector polarisConfigRefreshScopeAnno
public PolarisConfigRefreshOptimizationListener polarisConfigRefreshOptimizationListener() {
return new PolarisConfigRefreshOptimizationListener();
}

@Bean
public PolarisConfigApplicationEventListener polarisContextApplicationEventListener(PolarisSDKContextManager polarisSDKContextManager) {
return new PolarisConfigApplicationEventListener(polarisSDKContextManager);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ public PropertySource<?> locate(Environment environment) {

private void initCustomPolarisConfigExtensionFiles(CompositePropertySource compositePropertySource) {
if (polarisConfigCustomExtensionLayer == null) {
LOGGER.debug("[SCT Config] PolarisAdaptorTsfConfigExtensionLayer is not init, ignore the following execution steps");
LOGGER.debug("[SCT Config] PolarisConfigCustomExtensionLayer is not init, ignore the following execution steps");
return;
}
polarisConfigCustomExtensionLayer.initConfigFiles(environment, compositePropertySource, polarisPropertySourceManager, configFileService);
}

private void afterLocatePolarisConfigExtension(CompositePropertySource compositePropertySource) {
if (polarisConfigCustomExtensionLayer == null) {
LOGGER.debug("[SCT Config] PolarisAdaptorTsfConfigExtensionLayer is not init, ignore the following execution steps");
LOGGER.debug("[SCT Config] PolarisConfigCustomExtensionLayer is not init, ignore the following execution steps");
return;
}
polarisConfigCustomExtensionLayer.executeAfterLocateConfigReturning(compositePropertySource);
Expand Down
Loading

0 comments on commit a553cc0

Please sign in to comment.