From edfc1ddcf719a899f9d96364b0fe5c603e8347df Mon Sep 17 00:00:00 2001 From: lepdou Date: Mon, 11 Apr 2022 20:22:41 +0800 Subject: [PATCH 1/9] fix discovery weight param not set to register request bug --- CHANGELOG.md | 1 + .../tencent/cloud/polaris/PolarisDiscoveryProperties.java | 6 +++--- .../cloud/polaris/registry/PolarisServiceRegistry.java | 1 + .../META-INF/additional-spring-configuration-metadata.json | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0b57b73..a663b3d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,4 +2,5 @@ --- - [Bugfix: fix router bug and add router example](https://github.com/Tencent/spring-cloud-tencent/pull/89) +- [feat:fix discovery weight param not set to register request bug](https://github.com/Tencent/spring-cloud-tencent/pull/102) diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisDiscoveryProperties.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisDiscoveryProperties.java index 35f39721..351ab62f 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisDiscoveryProperties.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisDiscoveryProperties.java @@ -56,7 +56,7 @@ public class PolarisDiscoveryProperties { * Load balance weight. */ @Value("${spring.cloud.polaris.discovery.weight:#{100}}") - private float weight; + private int weight; /** * Version number. @@ -134,11 +134,11 @@ public class PolarisDiscoveryProperties { this.namespace = namespace; } - public float getWeight() { + public int getWeight() { return weight; } - public void setWeight(float weight) { + public void setWeight(int weight) { this.weight = weight; } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java index 7e72a075..b947db59 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java @@ -95,6 +95,7 @@ public class PolarisServiceRegistry implements ServiceRegistry { instanceRegisterRequest.setService(registration.getServiceId()); instanceRegisterRequest.setHost(registration.getHost()); instanceRegisterRequest.setPort(registration.getPort()); + instanceRegisterRequest.setWeight(polarisDiscoveryProperties.getWeight()); instanceRegisterRequest.setToken(polarisDiscoveryProperties.getToken()); if (null != heartbeatExecutor) { instanceRegisterRequest.setTtl(ttl); diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 89021b3d..6608a3b9 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -50,7 +50,7 @@ }, { "name": "spring.cloud.polaris.weight", - "type": "java.lang.String", + "type": "java.lang.Integer", "defaultValue": 100, "description": "the weight of polaris instance , use to load-balance." } From c7f6575567606edb7d01d27320623fbe20bce877 Mon Sep 17 00:00:00 2001 From: lepdou Date: Mon, 11 Apr 2022 20:32:38 +0800 Subject: [PATCH 2/9] add custom label resolver spi for rate limit --- CHANGELOG.md | 2 +- .../config/RateLimitConfiguration.java | 14 ++++-- .../ratelimit/constant/RateLimitConstant.java | 5 ++ .../filter/QuotaCheckReactiveFilter.java | 39 +++++++++++---- .../filter/QuotaCheckServletFilter.java | 32 ++++++++++--- ...larisRateLimiterLabelReactiveResolver.java | 40 ++++++++++++++++ ...olarisRateLimiterLabelServletResolver.java | 40 ++++++++++++++++ .../service/callee/CustomLabelResolver.java | 47 +++++++++++++++++++ 8 files changed, 200 insertions(+), 19 deletions(-) create mode 100644 spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/spi/PolarisRateLimiterLabelReactiveResolver.java create mode 100644 spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/spi/PolarisRateLimiterLabelServletResolver.java create mode 100644 spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/CustomLabelResolver.java diff --git a/CHANGELOG.md b/CHANGELOG.md index b0b57b73..c3ef7243 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,4 +2,4 @@ --- - [Bugfix: fix router bug and add router example](https://github.com/Tencent/spring-cloud-tencent/pull/89) - +- [feat:add custom label resolver spi for rate limit](https://github.com/Tencent/spring-cloud-tencent/pull/105) diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/config/RateLimitConfiguration.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/config/RateLimitConfiguration.java index 27a16189..825e552e 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/config/RateLimitConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/config/RateLimitConfiguration.java @@ -13,6 +13,7 @@ * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. + * */ package com.tencent.cloud.polaris.ratelimit.config; @@ -20,6 +21,8 @@ package com.tencent.cloud.polaris.ratelimit.config; import com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant; import com.tencent.cloud.polaris.ratelimit.filter.QuotaCheckReactiveFilter; import com.tencent.cloud.polaris.ratelimit.filter.QuotaCheckServletFilter; +import com.tencent.cloud.polaris.ratelimit.spi.PolarisRateLimiterLabelReactiveResolver; +import com.tencent.cloud.polaris.ratelimit.spi.PolarisRateLimiterLabelServletResolver; import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.ratelimit.api.core.LimitAPI; import com.tencent.polaris.ratelimit.factory.LimitAPIFactory; @@ -30,6 +33,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.lang.Nullable; import static javax.servlet.DispatcherType.ASYNC; import static javax.servlet.DispatcherType.ERROR; @@ -60,8 +64,9 @@ public class RateLimitConfiguration { @Bean @ConditionalOnMissingBean - public QuotaCheckServletFilter quotaCheckFilter(LimitAPI limitAPI) { - return new QuotaCheckServletFilter(limitAPI); + public QuotaCheckServletFilter quotaCheckFilter(LimitAPI limitAPI, + @Nullable PolarisRateLimiterLabelServletResolver labelResolver) { + return new QuotaCheckServletFilter(limitAPI, labelResolver); } @Bean @@ -85,8 +90,9 @@ public class RateLimitConfiguration { static class MetadataReactiveFilterConfig { @Bean - public QuotaCheckReactiveFilter quotaCheckReactiveFilter(LimitAPI limitAPI) { - return new QuotaCheckReactiveFilter(limitAPI); + public QuotaCheckReactiveFilter quotaCheckReactiveFilter(LimitAPI limitAPI, + @Nullable PolarisRateLimiterLabelReactiveResolver labelResolver) { + return new QuotaCheckReactiveFilter(limitAPI, labelResolver); } } diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/constant/RateLimitConstant.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/constant/RateLimitConstant.java index 5c96b864..81413452 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/constant/RateLimitConstant.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/constant/RateLimitConstant.java @@ -37,6 +37,11 @@ public final class RateLimitConstant { */ public static String QUOTA_LIMITED_INFO = "The request is deny by rate limit because the throttling threshold is reached"; + /** + * The build in label method. + */ + public static String LABEL_METHOD = "method"; + private RateLimitConstant() { } diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java index 1727a018..833f75af 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java @@ -13,6 +13,7 @@ * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. + * */ package com.tencent.cloud.polaris.ratelimit.filter; @@ -23,6 +24,7 @@ import java.util.Map; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant; +import com.tencent.cloud.polaris.ratelimit.spi.PolarisRateLimiterLabelReactiveResolver; import com.tencent.cloud.polaris.ratelimit.utils.QuotaCheckUtils; import com.tencent.polaris.ratelimit.api.core.LimitAPI; import com.tencent.polaris.ratelimit.api.rpc.QuotaResponse; @@ -37,10 +39,13 @@ import org.springframework.core.io.buffer.DataBuffer; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.server.reactive.ServerHttpResponse; +import org.springframework.util.CollectionUtils; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebFilter; import org.springframework.web.server.WebFilterChain; +import static com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant.LABEL_METHOD; + /** * Reactive filter to check quota. * @@ -53,8 +58,12 @@ public class QuotaCheckReactiveFilter implements WebFilter, Ordered { private final LimitAPI limitAPI; - public QuotaCheckReactiveFilter(LimitAPI limitAPI) { + private final PolarisRateLimiterLabelReactiveResolver labelResolver; + + public QuotaCheckReactiveFilter(LimitAPI limitAPI, + PolarisRateLimiterLabelReactiveResolver labelResolver) { this.limitAPI = limitAPI; + this.labelResolver = labelResolver; } @Override @@ -66,23 +75,37 @@ public class QuotaCheckReactiveFilter implements WebFilter, Ordered { public Mono filter(ServerWebExchange exchange, WebFilterChain chain) { String localNamespace = MetadataContext.LOCAL_NAMESPACE; String localService = MetadataContext.LOCAL_SERVICE; - String method = exchange.getRequest().getURI().getPath(); - Map labels = null; - if (StringUtils.isNotBlank(method)) { - labels = new HashMap<>(); - labels.put("method", method); + + Map labels = new HashMap<>(); + + // add build in labels + String path = exchange.getRequest().getURI().getPath(); + if (StringUtils.isNotBlank(path)) { + labels.put(LABEL_METHOD, path); + } + + // add custom labels + if (labelResolver != null) { + try { + Map customLabels = labelResolver.resolve(exchange); + if (!CollectionUtils.isEmpty(customLabels)) { + labels.putAll(customLabels); + } + } catch (Throwable e) { + LOG.error("resolve custom label failed. resolver = {}", labelResolver.getClass().getName(), e); + } } try { QuotaResponse quotaResponse = QuotaCheckUtils.getQuota(limitAPI, localNamespace, localService, 1, labels, null); + if (quotaResponse.getCode() == QuotaResultCode.QuotaResultLimited) { ServerHttpResponse response = exchange.getResponse(); response.setStatusCode(HttpStatus.TOO_MANY_REQUESTS); response.getHeaders().setContentType(MediaType.APPLICATION_JSON); DataBuffer dataBuffer = response.bufferFactory().allocateBuffer().write( - (RateLimitConstant.QUOTA_LIMITED_INFO + quotaResponse.getInfo()) - .getBytes(StandardCharsets.UTF_8)); + RateLimitConstant.QUOTA_LIMITED_INFO.getBytes(StandardCharsets.UTF_8)); return response.writeWith(Mono.just(dataBuffer)); } } diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckServletFilter.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckServletFilter.java index c436272a..7b36652a 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckServletFilter.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckServletFilter.java @@ -29,6 +29,7 @@ import javax.servlet.http.HttpServletResponse; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant; +import com.tencent.cloud.polaris.ratelimit.spi.PolarisRateLimiterLabelServletResolver; import com.tencent.cloud.polaris.ratelimit.utils.QuotaCheckUtils; import com.tencent.polaris.ratelimit.api.core.LimitAPI; import com.tencent.polaris.ratelimit.api.rpc.QuotaResponse; @@ -38,8 +39,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.annotation.Order; +import org.springframework.util.CollectionUtils; import org.springframework.web.filter.OncePerRequestFilter; +import static com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant.LABEL_METHOD; import static org.springframework.http.HttpStatus.TOO_MANY_REQUESTS; /** @@ -54,9 +57,11 @@ public class QuotaCheckServletFilter extends OncePerRequestFilter { .getLogger(QuotaCheckServletFilter.class); private final LimitAPI limitAPI; + private final PolarisRateLimiterLabelServletResolver labelResolver; - public QuotaCheckServletFilter(LimitAPI limitAPI) { + public QuotaCheckServletFilter(LimitAPI limitAPI, PolarisRateLimiterLabelServletResolver labelResolver) { this.limitAPI = limitAPI; + this.labelResolver = labelResolver; } @Override @@ -65,11 +70,26 @@ public class QuotaCheckServletFilter extends OncePerRequestFilter { throws ServletException, IOException { String localNamespace = MetadataContext.LOCAL_NAMESPACE; String localService = MetadataContext.LOCAL_SERVICE; - String method = request.getRequestURI(); - Map labels = null; - if (StringUtils.isNotBlank(method)) { - labels = new HashMap<>(); - labels.put("method", method); + + Map labels = new HashMap<>(); + + // add build in labels + String path = request.getRequestURI(); + + if (StringUtils.isNotBlank(path)) { + labels.put(LABEL_METHOD, path); + } + + // add custom labels + if (labelResolver != null) { + try { + Map customLabels = labelResolver.resolve(request); + if (!CollectionUtils.isEmpty(customLabels)) { + labels.putAll(customLabels); + } + } catch (Throwable e) { + LOG.error("resolve custom label failed. resolver = {}", labelResolver.getClass().getName(), e); + } } try { diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/spi/PolarisRateLimiterLabelReactiveResolver.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/spi/PolarisRateLimiterLabelReactiveResolver.java new file mode 100644 index 00000000..790c7c0d --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/spi/PolarisRateLimiterLabelReactiveResolver.java @@ -0,0 +1,40 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.ratelimit.spi; + +import java.util.Map; + +import org.springframework.web.server.ServerWebExchange; + +/** + * Resolve custom label from request. The label used for rate limit params. + * + * @author lepdou 2022-03-31 + */ +public interface PolarisRateLimiterLabelReactiveResolver { + + /** + * Resolve custom label from request. + * + * @param exchange the http request + * @return resolved labels + */ + Map resolve(ServerWebExchange exchange); + +} diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/spi/PolarisRateLimiterLabelServletResolver.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/spi/PolarisRateLimiterLabelServletResolver.java new file mode 100644 index 00000000..8fe85909 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/spi/PolarisRateLimiterLabelServletResolver.java @@ -0,0 +1,40 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.ratelimit.spi; + +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +/** + * Resolve custom label from request. The label used for rate limit params. + * + * @author lepdou 2022-03-31 + */ +public interface PolarisRateLimiterLabelServletResolver { + + /** + * Resolve custom label from request. + * + * @param request the http request + * @return resolved labels + */ + Map resolve(HttpServletRequest request); + +} diff --git a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/CustomLabelResolver.java b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/CustomLabelResolver.java new file mode 100644 index 00000000..837de09c --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/CustomLabelResolver.java @@ -0,0 +1,47 @@ +/* + * 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.ratelimit.example.service.callee; + +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import com.tencent.cloud.polaris.ratelimit.spi.PolarisRateLimiterLabelServletResolver; + +import org.springframework.stereotype.Component; + +/** + * resolver custom label from request. + * + *@author lepdou 2022-03-31 + */ +@Component +public class CustomLabelResolver implements PolarisRateLimiterLabelServletResolver { + + @Override + public Map resolve(HttpServletRequest request) { + //rate limit by some request params. such as query params, headers .. + + Map labels = new HashMap<>(); + labels.put("user", "zhangsan"); + + return labels; + } +} From 858c6337360b2922b43cbabe2547c47b17beb7c0 Mon Sep 17 00:00:00 2001 From: lepdou Date: Mon, 11 Apr 2022 21:09:11 +0800 Subject: [PATCH 3/9] optimize router example --- .../router-callee-service2/src/main/resources/bootstrap.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/resources/bootstrap.yml index ebf6fe5c..138c119f 100644 --- a/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/resources/bootstrap.yml @@ -7,8 +7,7 @@ spring: tencent: metadata: content: - version: v2 - xx: zz + label1: value2 polaris: address: grpc://127.0.0.1:8091 namespace: default From a4a45c169afc305bbc3d2f66caff79237e168781 Mon Sep 17 00:00:00 2001 From: lepdou Date: Tue, 12 Apr 2022 11:01:13 +0800 Subject: [PATCH 4/9] fix causing cpu 100% when set ScheduledThreadPoolExecutor corePoolSize=0 --- CHANGELOG.md | 1 + .../polaris/registry/PolarisServiceRegistry.java | 12 +++++------- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0b57b73..42b5c968 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,4 +2,5 @@ --- - [Bugfix: fix router bug and add router example](https://github.com/Tencent/spring-cloud-tencent/pull/89) +- [Bugfix: fix causing cpu 100% when set ScheduledThreadPoolExecutor corePoolSize=0](https://github.com/Tencent/spring-cloud-tencent/pull/98) diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java index 7e72a075..59d65d77 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java @@ -18,8 +18,8 @@ package com.tencent.cloud.polaris.registry; +import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; @@ -71,13 +71,11 @@ public class PolarisServiceRegistry implements ServiceRegistry { this.polarisDiscoveryProperties = polarisDiscoveryProperties; this.polarisDiscoveryHandler = polarisDiscoveryHandler; this.metadataLocalProperties = metadataLocalProperties; + if (polarisDiscoveryProperties.isHeartbeatEnabled()) { - ScheduledThreadPoolExecutor heartbeatExecutor = new ScheduledThreadPoolExecutor( - 0, new NamedThreadFactory("spring-cloud-heartbeat")); - heartbeatExecutor.setMaximumPoolSize(1); - this.heartbeatExecutor = heartbeatExecutor; - } - else { + this.heartbeatExecutor = Executors + .newSingleThreadScheduledExecutor(new NamedThreadFactory("spring-cloud-heartbeat")); + } else { this.heartbeatExecutor = null; } } From 72fa0c2e90b9d38e325210ab56095ea2f326d56b Mon Sep 17 00:00:00 2001 From: lepdou Date: Thu, 14 Apr 2022 21:03:05 +0800 Subject: [PATCH 5/9] Update README.md --- README.md | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/README.md b/README.md index 752ba57b..0234907a 100644 --- a/README.md +++ b/README.md @@ -67,16 +67,5 @@ For example, Spring Cloud Tencent's 1.0.1.Hoxton.SR9 corresponds to the Spring C - [Spring Cloud Tencent Router](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Router-Usage-Document) - [Spring Cloud Tencent Metadata Transfer](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Metadata-Transfer-Usage-Document) -### Version Standard - -We use a version policy related to Spring Cloud's major version number. - -Project version includes ```${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION}-${CORRESPONDING_MAJOR_VERSION_OF_SPRING_CLOUD}.${CORRESPONDING_MINOR_VERSION_OF_SPRING_CLOUD}-${RELEASE_TYPE}```. -```${MAJOR_VERSION}```, ```${MINOR_VERSION}```, ```${PATCH_VERSION}``` are in numbers starting from 0. -```${CORRESPONDING_MAJOR_VERSION_OF_SPRING_CLOUD}``` is the same as the major version number of Spring Cloud, like Hoxton, Greenwich. ```${CORRESPONDING_MINOR_VERSION_OF_SPRING_CLOUD}``` is the same as the major version number of Spring Cloud, like RS9. -```${RELEASE_TYPE}``` is like RELEASE or RC currently. Actually, the RELEASE version does not add a release type in the version, and the RS version will add a suffix and start from RC0. - -For example: 1.2.0-Hoxton.SR9-RC0 - ## License The spring-cloud-tencent is licensed under the BSD 3-Clause License. Copyright and license information can be found in the file [LICENSE](LICENSE) From fcf490a4d08084d9926652314699c6dc6619655e Mon Sep 17 00:00:00 2001 From: lepdou Date: Thu, 14 Apr 2022 21:33:16 +0800 Subject: [PATCH 6/9] update readme.md --- README-zh.md | 121 +++++++++++++++++++++++++++------------------------ README.md | 118 +++++++++++++++++++++++++------------------------ 2 files changed, 124 insertions(+), 115 deletions(-) diff --git a/README-zh.md b/README-zh.md index 95030deb..0f787ef9 100644 --- a/README-zh.md +++ b/README-zh.md @@ -1,72 +1,77 @@ # Spring Cloud Tencent +[![Build Status](https://github.com/Tencent/spring-cloud-tencent/actions/workflows/junit_test.yml/badge.svg)](https://github.com/Tencent/spring-cloud-tencent/actions/workflows/junit_test.yml) +[![Maven Central](https://img.shields.io/maven-central/v/com.tencent.cloud/spring-cloud-tencent?label=Maven%20Central)](https://search.maven.org/search?q=g:com.tencent.cloud%20AND%20a:spring-cloud-tencent) + [English](./README.md) | 简体中文 --- ## 介绍 -Spring Cloud Tencent包含了分布式应用微服务开发过程中所需的组件,基于 Spring Cloud 框架的开发者可以使用这些组件快速进行分布式应用的开发。 - -## 主要功能 - -* **服务注册与发现**:基于 Spring Cloud Common的标准进行微服务的注册与发现。 -* **服务路由与负载均衡**:基于 Ribbon 的接口标准,提供场景更丰富的动态路由以及负载均衡的能力。 -* **故障节点熔断**:提供故障节点的熔断剔除以及主/被动探测恢复的能力,保证分布式服务的可靠性。 -* **服务限流**:支持微服务被调接入层和网关主动调用的限流功能,保证后台微服务稳定性,可通过控制台动态配置规则,及查看流量监控数据。 -* **元数据传递**: 支持网关及微服务应用之间的自定义元数据传递。 - -## 如何构建 - -* [2020.0.x](https://github.com/Tencent/spring-cloud-tencent/tree/2020.0.x)分支对应的是 Spring Cloud 2020.0版本,编译环境最低支持JDK 1.8。 -* [main](https://github.com/Tencent/spring-cloud-tencent/tree/main) 分支对应的是 Spring Cloud Hoxton版本,编译环境最低支持JDK 1.8。 -* [greenwich](https://github.com/Tencent/spring-cloud-tencent/tree/greenwich) 分支对应的是 Spring Cloud Greenwich版本,编译环境最低支持JDK 1.8。 - -Spring Cloud Tencent 使用 Maven 来构建,最快的使用方式是将本项目 clone 到本地,然后执行以下命令: -```bash - ./mvnw install -``` -执行完毕后,项目将被安装到本地 Maven 仓库。 - -## 如何使用 - -### 如何引入依赖 - -在 dependencyManagement 中添加如下配置,然后在 dependencies 中添加自己所需使用的依赖即可使用。 +Spring Cloud Tencent 是腾讯开发和维护的一站式微服务解决方案。 + +Spring Cloud Tencent 实现了Spring Cloud 标准微服务 SPI,开发者可以基于 Spring Cloud Tencent 快速开发 Spring Cloud 云原生分布式应用。 + +Spring Cloud Tencent 的核心依托腾讯开源的一站式服务发现与治理平台 [Polaris](https://github.com/polarismesh/polaris),实现各种分布式微服务场景。 + +- [Polaris Github home page](https://github.com/polarismesh/polaris) +- [Polaris official website](https://polarismesh.cn/) + +Spring Cloud 腾讯提供的能力包括但不限于: + +- 服务注册和发现 +- 动态配置管理 +- 服务治理 + - 服务电流限制 + - 服务断路器 + - 服务路由 + - ... +- 标签透传 + +## 使用指南 + +Spring Cloud Tencent 所有组件都已上传到 Maven 中央仓库,只需要引入依赖即可。 + +例如: + +```` + + + + + com.tencent.cloud + spring-cloud-tencent-dependencies + + ${version} + pom + import + + + + + + + + com.tencent.cloud + spring-cloud-starter-tencent-polaris-discovery + + ```` - - - - com.tencent.cloud - spring-cloud-tencent-dependencies - 1.1.4.Hoxton.SR9 - pom - import - - - -```` - -### 使用文档 -- [Spring Cloud Tencent 版本管理](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-%E7%89%88%E6%9C%AC%E7%AE%A1%E7%90%86) -- [服务注册与发现](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Discovery-%E4%BD%BF%E7%94%A8%E6%96%87%E6%A1%A3) -- [配置中心](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Config-%E4%BD%BF%E7%94%A8%E6%96%87%E6%A1%A3) -- [服务限流](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Rate-Limit-%E4%BD%BF%E7%94%A8%E6%96%87%E6%A1%A3) -- [服务熔断](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Circuitbreaker-%E4%BD%BF%E7%94%A8%E6%96%87%E6%A1%A3) -- [服务路由](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Router-%E4%BD%BF%E7%94%A8%E6%96%87%E6%A1%A3) -- [元数据传递](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Metadata-Transfer-%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97) - -## 版本号规范 - -采取与Spring Cloud大版本号相关的版本策略。 - -项目的版本号格式为 ```大版本号.小版本号.补丁版本号-对应Spring Cloud的大版本号.对应Spring Cloud的小版本号-发布类型``` 的形式。 -大版本号、小版本号、补丁版本号的类型为数字,从 0 开始取值。 -对应Spring Cloud的大版本号为Spring Cloud提供的英文版本号,例如Hoxton、Greenwich等。对应Spring Cloud的小版本号为Spring Cloud给出的小版本号,例如 RS9 等。 -发布类型目前包括正式发布和发布候选版(RC)。在实际的版本号中,正式发布版不额外添加发布类型,发布候选版将添加后缀,并从 RC0 开始。 -示例:1.2.0-Hoxton.SR9-RC0 +- 快速开始 + - [Spring Cloud Tencent 版本管理](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Version-Management) + - [Spring Cloud Tencent 服务注册与发现](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Discovery-Usage-Documentation) + - [Spring Cloud Tencent 配置中心](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Config-Usage-Documentation) + - [Spring Cloud Tencent 限流](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Rate-Limit-Usage-Document) + - [Spring Cloud Tencent 熔断](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Circuitbreaker-Usage-Document) + - [Spring Cloud Tencent 服务路由](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Router-Usage-Document) + - [Spring Cloud Tencent 标签传递](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Metadata-Transfer-Usage-Document) + +- 开发文档 + - [项目概览](https://github.com/Tencent/spring-cloud-tencent/wiki/%E9%A1%B9%E7%9B%AE%E6%A6%82%E8%A7%88) + - [参与共建](https://github.com/Tencent/spring-cloud-tencent/wiki/Contributing) ## License The spring-cloud-tencent is licensed under the BSD 3-Clause License. Copyright and license information can be found in the file [LICENSE](LICENSE) diff --git a/README.md b/README.md index 0234907a..730ef4a9 100644 --- a/README.md +++ b/README.md @@ -7,65 +7,69 @@ English | [简体中文](./README-zh.md) ## Introduction -Spring Cloud Tencent contains components distributed micro-service applications need during developing phase, developers that built their key architectures based on Spring Cloud can use these components +Spring Cloud Tencent is a one-stop microservice solution developed and maintained by Tencent. + +Spring Cloud Tencent implements the Spring Cloud standard microservice SPI, so developers can quickly develop Spring Cloud cloud-native distributed applications based on Spring Cloud Tencent. + +The core of Spring Cloud Tencent relies on Tencent's open-source one-stop service discovery and governance platform [Polaris](https://github.com/polarismesh/polaris) to realize various distributed microservice scenarios. + +- [Polaris Github home page](https://github.com/polarismesh/polaris) +- [Polaris official website](https://polarismesh.cn/) + +The capabilities provided by Spring Cloud Tencent include but are not limited to: + +- Service registration and discovery +- Dynamic configuration management +- Service Governance + - Service current limit + - Service circuit breaker + - Service routing + - ... +- Label transparent transmission + +## Use Guide + +All the components of Spring Cloud Tencent have been uploaded to the Maven central repository, just need to introduce dependencies. + +For example: + +```` + + + + + com.tencent.cloud + spring-cloud-tencent-dependencies + + ${version} + pom + import + + + + + + + + com.tencent.cloud + spring-cloud-starter-tencent-polaris-discovery + + -Based on Spring Cloud Tencent, you only need a small configuration to launch Spring Cloud and micro-service's joint solutions. - -## Key Features - -* **Service Registration and Discovery**: Based on Spring Cloud's discovery and registration standard. -* **Service Routing and LoadBalancer**: Based on ribbon's API port, provide dynamic routing and load balancing use cases. -* **CircuitBreaker Node**: Support circuitbreaker auto-reset ability, ensure the reliability of distributed server -* **Rate Limiter**: Support rate limit of microservice and gateway, ensure the stability of backend, one can configure policies and traffic data from the control panel -* **Metadata Delivery**: Support metadata delivery between gateways and microservices. - -## Components - -**[Polaris](https://github.com/PolarisMesh/polaris)**:Polaris Spring Cloud operation centre, provide solutions to registration, dynamic routing, load balancing and circuitbreaker. - -## How to build - -* master's branch matches Spring Cloud Hoxton, support lowest at JDK 1.8. - -Spring Cloud Tencent uses Maven to construct, the fastest way is to clone project to local files, then execute the following orders: - -```bash -./mvnw install -``` - -When all the steps are finished, the project will be installed in local Maven repository. - -## How to Use - -### How to Introduce Dependency - -Add the following configurations in dependencyManagement, then add the dependencies you need. -At the same time, you need to pay attention to the Spring Cloud version corresponding to Spring Cloud Tencent, and then the corresponding Spring Boot version. -For example, Spring Cloud Tencent's 1.0.1.Hoxton.SR9 corresponds to the Spring Cloud Hoxton version and requires Spring Boot 2.3.x. - -```` - - - - com.tencent.cloud - spring-cloud-tencent-dependencies - - ${version} - pom - import - - - ```` - ### Starter Usage Doc -- [Spring Cloud Tencent Version Management](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Version-Management) -- [Spring Cloud Tencent Discovery](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Discovery-Usage-Documentation) -- [Spring Cloud Tencent Config](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Config-Usage-Documentation) -- [Spring Cloud Tencent Rate Limit](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Rate-Limit-Usage-Document) -- [Spring Cloud Tencent CircuitBreaker](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Circuitbreaker-Usage-Document) -- [Spring Cloud Tencent Router](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Router-Usage-Document) -- [Spring Cloud Tencent Metadata Transfer](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Metadata-Transfer-Usage-Document) - + - Quick Start + - [Spring Cloud Tencent Version Management](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Version-Management) + - [Spring Cloud Tencent Discovery](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Discovery-Usage-Documentation) + - [Spring Cloud Tencent Config](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Config-Usage-Documentation) + - [Spring Cloud Tencent Rate Limit](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Rate-Limit-Usage-Document) + - [Spring Cloud Tencent CircuitBreaker](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Circuitbreaker-Usage-Document) + - [Spring Cloud Tencent Router](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Router-Usage-Document) + - [Spring Cloud Tencent Metadata Transfer](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Metadata-Transfer-Usage-Document) + +- Development Documentation + - [Project Structure Overview](https://github.com/Tencent/spring-cloud-tencent/wiki/%E9%A1%B9%E7%9B%AE%E6%A6%82%E8%A7%88) + - [Participate in co-construction](https://github.com/Tencent/spring-cloud-tencent/wiki/Contributing) + ## License The spring-cloud-tencent is licensed under the BSD 3-Clause License. Copyright and license information can be found in the file [LICENSE](LICENSE) From a20c4605a78d686fe7b2cf3947b86e5e621653f3 Mon Sep 17 00:00:00 2001 From: lepdou Date: Thu, 14 Apr 2022 21:38:35 +0800 Subject: [PATCH 7/9] update readme.md --- README-zh.md | 8 ++++++-- README.md | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README-zh.md b/README-zh.md index 0f787ef9..2cb049b8 100644 --- a/README-zh.md +++ b/README-zh.md @@ -29,6 +29,10 @@ Spring Cloud 腾讯提供的能力包括但不限于: - ... - 标签透传 +## 管控台 + +image + ## 使用指南 Spring Cloud Tencent 所有组件都已上传到 Maven 中央仓库,只需要引入依赖即可。 @@ -60,7 +64,7 @@ Spring Cloud Tencent 所有组件都已上传到 Maven 中央仓库,只需要 ```` -- 快速开始 +- ### 快速开始 - [Spring Cloud Tencent 版本管理](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Version-Management) - [Spring Cloud Tencent 服务注册与发现](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Discovery-Usage-Documentation) - [Spring Cloud Tencent 配置中心](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Config-Usage-Documentation) @@ -69,7 +73,7 @@ Spring Cloud Tencent 所有组件都已上传到 Maven 中央仓库,只需要 - [Spring Cloud Tencent 服务路由](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Router-Usage-Document) - [Spring Cloud Tencent 标签传递](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Metadata-Transfer-Usage-Document) -- 开发文档 +- ### 开发文档 - [项目概览](https://github.com/Tencent/spring-cloud-tencent/wiki/%E9%A1%B9%E7%9B%AE%E6%A6%82%E8%A7%88) - [参与共建](https://github.com/Tencent/spring-cloud-tencent/wiki/Contributing) diff --git a/README.md b/README.md index 730ef4a9..fd2b0eeb 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,10 @@ The capabilities provided by Spring Cloud Tencent include but are not limited to - ... - Label transparent transmission +## Screenshots + +image + ## Use Guide All the components of Spring Cloud Tencent have been uploaded to the Maven central repository, just need to introduce dependencies. @@ -58,7 +62,7 @@ For example: ```` - - Quick Start + - ### Quick Start - [Spring Cloud Tencent Version Management](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Version-Management) - [Spring Cloud Tencent Discovery](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Discovery-Usage-Documentation) - [Spring Cloud Tencent Config](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Config-Usage-Documentation) @@ -67,7 +71,7 @@ For example: - [Spring Cloud Tencent Router](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Router-Usage-Document) - [Spring Cloud Tencent Metadata Transfer](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Metadata-Transfer-Usage-Document) -- Development Documentation +- ### Development Documentation - [Project Structure Overview](https://github.com/Tencent/spring-cloud-tencent/wiki/%E9%A1%B9%E7%9B%AE%E6%A6%82%E8%A7%88) - [Participate in co-construction](https://github.com/Tencent/spring-cloud-tencent/wiki/Contributing) From cc1b1f139867e5aa88b0566fe480746c29438ead Mon Sep 17 00:00:00 2001 From: lepdou Date: Thu, 14 Apr 2022 21:42:05 +0800 Subject: [PATCH 8/9] update readme.md --- README-zh.md | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README-zh.md b/README-zh.md index 2cb049b8..540bb9ef 100644 --- a/README-zh.md +++ b/README-zh.md @@ -39,7 +39,7 @@ Spring Cloud Tencent 所有组件都已上传到 Maven 中央仓库,只需要 例如: -```` +```` xml diff --git a/README.md b/README.md index fd2b0eeb..41ccca3f 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ All the components of Spring Cloud Tencent have been uploaded to the Maven centr For example: -```` +```` xml From c3fb0fd336c1d7b2d2de2976ed509fec6b4a8487 Mon Sep 17 00:00:00 2001 From: lepdou Date: Thu, 14 Apr 2022 21:46:02 +0800 Subject: [PATCH 9/9] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 41ccca3f..2a149099 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ The capabilities provided by Spring Cloud Tencent include but are not limited to - Service registration and discovery - Dynamic configuration management - Service Governance - - Service current limit + - Service rate limit - Service circuit breaker - Service routing - ...