From edb9236e6070cc77df1c765c559d7b97a5fbc851 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Wed, 7 Sep 2022 20:10:20 +0800 Subject: [PATCH] feat:add spring-cloud-starter-tencent-all and quickstart examples. (#567) --- CHANGELOG.md | 1 + pom.xml | 1 + spring-cloud-starter-tencent-all/pom.xml | 54 ++++++ .../annotation/SpringValueProcessor.java | 2 +- .../ConditionalOnReflectRefreshTypeTest.java | 2 +- .../annotation/SpringValueProcessorTest.java | 5 +- .../filter/QuotaCheckReactiveFilter.java | 11 +- .../filter/QuotaCheckServletFilter.java | 8 +- .../metadata/StaticMetadataManager.java | 24 +-- spring-cloud-tencent-dependencies/pom.xml | 16 +- .../src/main/resources/bootstrap.yml | 3 + .../src/main/resources/bootstrap.yml | 4 + .../src/main/resources/bootstrap.yml | 2 +- spring-cloud-tencent-examples/pom.xml | 43 ++--- .../quickstart-example/pom.xml | 23 +++ .../quickstart-callee-service-a/pom.xml | 62 +++++++ .../callee/CustomConfigChangeListener.java | 47 +++++ .../quickstart/callee/CustomMetadata.java | 46 +++++ .../callee/QuickstartCalleeController.java | 78 ++++++++ .../callee/QuickstartCalleeServiceA.java | 34 ++++ .../src/main/resources/bootstrap.yml | 27 +++ .../quickstart-callee-service-b/pom.xml | 62 +++++++ .../callee/CustomConfigChangeListener.java | 48 +++++ .../quickstart/callee/CustomMetadata.java | 46 +++++ .../callee/QuickstartCalleeController.java | 81 +++++++++ .../callee/QuickstartCalleeServiceB.java | 34 ++++ .../src/main/resources/bootstrap.yml | 27 +++ .../quickstart-caller-service/pom.xml | 67 +++++++ .../caller/QuickstartCalleeService.java | 49 +++++ .../QuickstartCalleeServiceFallback.java | 39 ++++ .../caller/QuickstartCallerApplication.java | 45 +++++ .../caller/QuickstartCallerController.java | 170 ++++++++++++++++++ .../src/main/resources/bootstrap.yml | 41 +++++ .../src/main/resources/polaris.yml | 14 ++ .../quickstart-gateway-service/pom.xml | 67 +++++++ .../gateway/QuickstartGatewayApplication.java | 35 ++++ .../src/main/resources/bootstrap.yml | 28 +++ .../RpcEnhancementAutoConfiguration.java | 6 +- .../reporter/ExceptionPolarisReporter.java | 7 +- .../reporter/SuccessPolarisReporter.java | 3 + .../EnhancedRestTemplateReporter.java | 3 + .../EnhancedRestTemplateReporterTest.java | 4 +- 42 files changed, 1308 insertions(+), 61 deletions(-) create mode 100644 spring-cloud-starter-tencent-all/pom.xml create mode 100644 spring-cloud-tencent-examples/quickstart-example/pom.xml create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/pom.xml create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/CustomConfigChangeListener.java create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/CustomMetadata.java create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeServiceA.java create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/bootstrap.yml create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/pom.xml create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/CustomConfigChangeListener.java create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/CustomMetadata.java create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeServiceB.java create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/resources/bootstrap.yml create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/pom.xml create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCalleeService.java create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCalleeServiceFallback.java create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCallerApplication.java create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCallerController.java create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/bootstrap.yml create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/polaris.yml create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-gateway-service/pom.xml create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-gateway-service/src/main/java/com/tencent/cloud/quickstart/gateway/QuickstartGatewayApplication.java create mode 100644 spring-cloud-tencent-examples/quickstart-example/quickstart-gateway-service/src/main/resources/bootstrap.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index bf1d7c65..b47bd88c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,3 +26,4 @@ - [Optimize:shutdown server when connect to config server failed.](https://github.com/Tencent/spring-cloud-tencent/pull/552) - [fix:fix heartbeat interval different configuration from polaris-java SDK.](https://github.com/Tencent/spring-cloud-tencent/pull/560) - [Optimize: optimize report call result for restTemplate.](https://github.com/Tencent/spring-cloud-tencent/pull/562) +- [feat:add spring-cloud-starter-tencent-all and quickstart examples.](https://github.com/Tencent/spring-cloud-tencent/pull/567) diff --git a/pom.xml b/pom.xml index 9cbaba44..f8c2ab33 100644 --- a/pom.xml +++ b/pom.xml @@ -50,6 +50,7 @@ spring-cloud-starter-tencent-polaris-router spring-cloud-tencent-plugin-starters spring-cloud-tencent-dependencies + spring-cloud-starter-tencent-all spring-cloud-tencent-examples spring-cloud-tencent-coverage diff --git a/spring-cloud-starter-tencent-all/pom.xml b/spring-cloud-starter-tencent-all/pom.xml new file mode 100644 index 00000000..64d26254 --- /dev/null +++ b/spring-cloud-starter-tencent-all/pom.xml @@ -0,0 +1,54 @@ + + + + spring-cloud-tencent + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + spring-cloud-starter-tencent-all + Spring Cloud Starter Tencent ALL In One + pom + + + + + com.tencent.cloud + spring-cloud-starter-tencent-polaris-discovery + + + + com.tencent.cloud + spring-cloud-starter-tencent-polaris-config + + + + com.tencent.cloud + spring-cloud-starter-tencent-polaris-ratelimit + + + + com.tencent.cloud + spring-cloud-starter-tencent-polaris-circuitbreaker + + + + com.tencent.cloud + spring-cloud-starter-tencent-polaris-router + + + + com.tencent.cloud + spring-cloud-starter-tencent-metadata-transfer + + + + com.tencent.cloud + spring-cloud-tencent-pushgateway-plugin + + + diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessor.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessor.java index 24279e07..059b65a1 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessor.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessor.java @@ -150,7 +150,7 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanFactor return; } springValueRegistry.register(beanFactory, key, springValue); - LOGGER.info("Monitoring {}", springValue); + LOGGER.debug("Monitoring {}", springValue); } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnReflectRefreshTypeTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnReflectRefreshTypeTest.java index 80b8dbbc..182883ab 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnReflectRefreshTypeTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnReflectRefreshTypeTest.java @@ -60,7 +60,7 @@ public class ConditionalOnReflectRefreshTypeTest { serverSocket.accept(); } catch (IOException e) { - //ignore + e.printStackTrace(); } }).start(); } diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessorTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessorTest.java index 6ad500ae..99f1b9b1 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessorTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessorTest.java @@ -62,7 +62,7 @@ public class SpringValueProcessorTest { serverSocket.accept(); } catch (IOException e) { - //ignore + e.printStackTrace(); } }).start(); } @@ -156,11 +156,10 @@ public class SpringValueProcessorTest { @Component private static class ValueTest { + private static String name; @Value("${timeout:1000}") private int timeout; - private static String name; - public int getTimeout() { return timeout; } 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 8a1162ee..19323538 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 @@ -98,7 +98,7 @@ public class QuotaCheckReactiveFilter implements WebFilter, Ordered { String localService = MetadataContext.LOCAL_SERVICE; Map labels = getRequestLabels(exchange, localNamespace, localService); - + long waitMs = -1; try { String path = exchange.getRequest().getURI().getPath(); QuotaResponse quotaResponse = QuotaCheckUtils.getQuota(limitAPI, @@ -115,7 +115,7 @@ public class QuotaCheckReactiveFilter implements WebFilter, Ordered { // Unirate if (quotaResponse.getCode() == QuotaResultCode.QuotaResultOk && quotaResponse.getWaitMs() > 0) { LOGGER.debug("The request of [{}] will waiting for {}ms.", path, quotaResponse.getWaitMs()); - return Mono.delay(Duration.ofMillis(quotaResponse.getWaitMs())).flatMap(e -> chain.filter(exchange)); + waitMs = quotaResponse.getWaitMs(); } } catch (Throwable t) { @@ -124,7 +124,12 @@ public class QuotaCheckReactiveFilter implements WebFilter, Ordered { LOGGER.error("fail to invoke getQuota, service is " + localService, t); } - return chain.filter(exchange); + if (waitMs > 0) { + return Mono.delay(Duration.ofMillis(waitMs)).flatMap(e -> chain.filter(exchange)); + } + else { + return chain.filter(exchange); + } } private Map getRequestLabels(ServerWebExchange exchange, String localNamespace, String localService) { 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 57c6a50b..f10a36c5 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 @@ -59,13 +59,11 @@ import static com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant.LAB @Order(RateLimitConstant.FILTER_ORDER) public class QuotaCheckServletFilter extends OncePerRequestFilter { - private static final Logger LOG = LoggerFactory.getLogger(QuotaCheckServletFilter.class); - /** * Default Filter Registration Bean Name Defined . */ public static final String QUOTA_FILTER_BEAN_NAME = "quotaFilterRegistrationBean"; - + private static final Logger LOG = LoggerFactory.getLogger(QuotaCheckServletFilter.class); private final LimitAPI limitAPI; private final PolarisRateLimiterLabelServletResolver labelResolver; @@ -116,14 +114,14 @@ public class QuotaCheckServletFilter extends OncePerRequestFilter { Thread.sleep(quotaResponse.getWaitMs()); } - filterChain.doFilter(request, response); } catch (Throwable t) { // An exception occurs in the rate limiting API call, // which should not affect the call of the business process. LOG.error("fail to invoke getQuota, service is " + localService, t); - filterChain.doFilter(request, response); } + + filterChain.doFilter(request, response); } private Map getRequestLabels(HttpServletRequest request, String localNamespace, String localService) { diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/StaticMetadataManager.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/StaticMetadataManager.java index cb284e93..7a5b041b 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/StaticMetadataManager.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/StaticMetadataManager.java @@ -71,8 +71,8 @@ public class StaticMetadataManager { private Map mergedStaticMetadata; private Map mergedStaticTransitiveMetadata; private Map mergedStaticDisposableMetadata; - private String zone; private String region; + private String zone; private String campus; public StaticMetadataManager(MetadataLocalProperties metadataLocalProperties, @@ -232,17 +232,6 @@ public class StaticMetadataManager { private void parseLocationMetadata(MetadataLocalProperties metadataLocalProperties, InstanceMetadataProvider instanceMetadataProvider) { - // resolve zone info - if (instanceMetadataProvider != null) { - zone = instanceMetadataProvider.getZone(); - } - if (StringUtils.isBlank(zone)) { - zone = System.getenv(ENV_METADATA_ZONE); - } - if (StringUtils.isBlank(zone)) { - zone = metadataLocalProperties.getContent().get(LOCATION_KEY_ZONE); - } - // resolve region info if (instanceMetadataProvider != null) { region = instanceMetadataProvider.getRegion(); @@ -254,6 +243,17 @@ public class StaticMetadataManager { region = metadataLocalProperties.getContent().get(LOCATION_KEY_REGION); } + // resolve zone info + if (instanceMetadataProvider != null) { + zone = instanceMetadataProvider.getZone(); + } + if (StringUtils.isBlank(zone)) { + zone = System.getenv(ENV_METADATA_ZONE); + } + if (StringUtils.isBlank(zone)) { + zone = metadataLocalProperties.getContent().get(LOCATION_KEY_ZONE); + } + // resolve campus info if (instanceMetadataProvider != null) { campus = instanceMetadataProvider.getCampus(); diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index aa284446..e6314c22 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -71,7 +71,7 @@ 1.7.1-Hoxton.SR12-SNAPSHOT - 1.7.3 + 1.7.4-SNAPSHOT 1.2.11 4.5.1 1.12.10 @@ -79,7 +79,6 @@ 3.16.1 1.69 31.0.1-jre - 0.11.0 3.2.0 @@ -158,6 +157,13 @@ ${revision} + + com.tencent.cloud + spring-cloud-starter-tencent-all + pom + ${revision} + + com.tencent.cloud @@ -259,12 +265,6 @@ byte-buddy ${byte-buddy.version} - - - io.prometheus - simpleclient_pushgateway - ${prometheus.version} - diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml index bc5a699e..42dbeeb6 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml @@ -11,6 +11,9 @@ spring: discovery: enabled: true register: true + rpc-enhancement: + reporter: + enabled: true stat: enabled: true port: 28082 diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml index faf40f44..c1fed673 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml @@ -18,6 +18,10 @@ spring: stat: enabled: true port: 28081 + tencent: + rpc-enhancement: + reporter: + enabled: true # pushgateway: # enabled: true # address: 127.0.0.1:9091 diff --git a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/bootstrap.yml index 6741bde1..a001fe02 100644 --- a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/bootstrap.yml @@ -18,4 +18,4 @@ management: web: exposure: include: - - polaris-ratelimit \ No newline at end of file + - polaris-ratelimit diff --git a/spring-cloud-tencent-examples/pom.xml b/spring-cloud-tencent-examples/pom.xml index 6362e8a2..d2219c19 100644 --- a/spring-cloud-tencent-examples/pom.xml +++ b/spring-cloud-tencent-examples/pom.xml @@ -1,35 +1,36 @@ - - spring-cloud-tencent - com.tencent.cloud - ${revision} - ../pom.xml - - 4.0.0 + 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"> + + spring-cloud-tencent + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 - spring-cloud-tencent-examples - pom - Spring Cloud Tencent Examples - Examples of Spring Cloud Tencent + spring-cloud-tencent-examples + pom + Spring Cloud Tencent Examples + Examples of Spring Cloud Tencent - - polaris-discovery-example - polaris-ratelimit-example - polaris-circuitbreaker-example - polaris-gateway-example + + polaris-discovery-example + polaris-ratelimit-example + polaris-circuitbreaker-example + polaris-gateway-example polaris-config-example polaris-router-example metadata-transfer-example polaris-router-grayrelease-example polaris-router-featureenv-example + quickstart-example - - true - + + true + diff --git a/spring-cloud-tencent-examples/quickstart-example/pom.xml b/spring-cloud-tencent-examples/quickstart-example/pom.xml new file mode 100644 index 00000000..07bd50ff --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/pom.xml @@ -0,0 +1,23 @@ + + + + spring-cloud-tencent-examples + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + quickstart-example + pom + Spring Cloud Starter Tencent Quickstart Example + + + quickstart-gateway-service + quickstart-caller-service + quickstart-callee-service-a + quickstart-callee-service-b + + \ No newline at end of file diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/pom.xml b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/pom.xml new file mode 100644 index 00000000..2ada04f8 --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/pom.xml @@ -0,0 +1,62 @@ + + + + quickstart-example + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + quickstart-callee-service-a + Quickstart Callee Service A + + + + com.tencent.cloud + spring-cloud-starter-tencent-all + pom + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-actuator + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.2.0 + + + attach-sources + + jar + + + + + + + diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/CustomConfigChangeListener.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/CustomConfigChangeListener.java new file mode 100644 index 00000000..cf2e9201 --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/CustomConfigChangeListener.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.quickstart.callee; + +import java.util.Set; + +import com.tencent.cloud.polaris.config.annotation.PolarisConfigKVFileChangeListener; +import com.tencent.cloud.polaris.config.listener.ConfigChangeEvent; + +import org.springframework.stereotype.Component; + +/** + * Custom Config Listener Example . + * + * @author Haotian Zhang + */ +@Component +public final class CustomConfigChangeListener { + + /** + * PolarisConfigKVFileChangeListener Example . + * @param event instance of {@link ConfigChangeEvent} + */ + @PolarisConfigKVFileChangeListener(interestedKeyPrefixes = "appName") + public void onChange(ConfigChangeEvent event) { + Set changedKeys = event.changedKeys(); + + for (String changedKey : changedKeys) { + System.out.printf("%s = %s \n", changedKey, event.getChange(changedKey)); + } + } +} diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/CustomMetadata.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/CustomMetadata.java new file mode 100644 index 00000000..bdd60f8e --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/CustomMetadata.java @@ -0,0 +1,46 @@ +/* + * 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.quickstart.callee; + +import java.util.HashMap; +import java.util.Map; + +import com.tencent.cloud.common.spi.InstanceMetadataProvider; + +import org.springframework.stereotype.Component; + +/** + * Custom metadata for instance. + * + * @author Haotian Zhang + */ +@Component +public class CustomMetadata implements InstanceMetadataProvider { + + @Override + public Map getMetadata() { + Map metadata = new HashMap<>(); + metadata.put("k1", "v1"); + return metadata; + } + + @Override + public String getZone() { + return "shenzhen-zone-1"; + } +} diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java new file mode 100644 index 00000000..02920c29 --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java @@ -0,0 +1,78 @@ +/* + * 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.quickstart.callee; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +/** + * Quickstart callee controller. + * + * @author Haotian Zhang + */ +@RestController +@RequestMapping("/quickstart/callee") +public class QuickstartCalleeController { + + private static final Logger LOG = LoggerFactory.getLogger(QuickstartCalleeController.class); + + @Value("${server.port:0}") + private int port; + + @Value("${appName:Callee}") + private String appName; + + /** + * Get sum of two value. + * @param value1 value 1 + * @param value2 value 2 + * @return sum + */ + @GetMapping("/sum") + public int sum(@RequestParam int value1, @RequestParam int value2) { + LOG.info("Quickstart Callee Service is called and sum is {}.", value1 + value2); + return value1 + value2; + } + + /** + * Get information of callee. + * @return information of callee + */ + @GetMapping("/info") + public String info() { + LOG.info("Quickstart [{}] Service [{}] is called.", appName, port); + return String.format("Quickstart [%s] Service [%s] is called.", appName, port); + } + + /** + * Check circuit break. + * + * @return circuit break info + */ + @GetMapping("/circuitBreak") + public String circuitBreak() { + LOG.info("Quickstart Callee Service is called right."); + return "Quickstart Callee Service is called right."; + } +} diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeServiceA.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeServiceA.java new file mode 100644 index 00000000..fb00d56e --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeServiceA.java @@ -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.quickstart.callee; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Quickstart callee application. + * + * @author Haotian Zhang + */ +@SpringBootApplication +public class QuickstartCalleeServiceA { + + public static void main(String[] args) { + SpringApplication.run(QuickstartCalleeServiceA.class, args); + } +} diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/bootstrap.yml new file mode 100644 index 00000000..65ba0b54 --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/bootstrap.yml @@ -0,0 +1,27 @@ +server: + port: 48083 +spring: + application: + name: QuickstartCalleeService + cloud: + polaris: + address: grpc://183.47.111.80:8091 + namespace: default + enabled: true + discovery: + enabled: true + register: true + config: + address: grpc://183.47.111.80:8093 + auto-refresh: true + groups: + - name: ${spring.application.name} + files: [ "config/callee.properties" ] + refresh-type: reflect + ratelimit: + enabled: true + maxQueuingTime: 500 + tencent: + metadata: + content: + region: huanan diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/pom.xml b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/pom.xml new file mode 100644 index 00000000..c34bb9f7 --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/pom.xml @@ -0,0 +1,62 @@ + + + + quickstart-example + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + quickstart-callee-service-b + Quickstart Callee Service B + + + + com.tencent.cloud + spring-cloud-starter-tencent-all + pom + + + + org.springframework.boot + spring-boot-starter-webflux + + + + org.springframework.boot + spring-boot-starter-actuator + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.2.0 + + + attach-sources + + jar + + + + + + + diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/CustomConfigChangeListener.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/CustomConfigChangeListener.java new file mode 100644 index 00000000..0bcfb554 --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/CustomConfigChangeListener.java @@ -0,0 +1,48 @@ +/* + * 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.quickstart.callee; + +import java.util.Set; + +import com.tencent.cloud.polaris.config.annotation.PolarisConfigKVFileChangeListener; +import com.tencent.cloud.polaris.config.listener.ConfigChangeEvent; + +import org.springframework.stereotype.Component; + +/** + * Custom Config Listener Example . + * + * @author Haotian Zhang + */ +@Component +public final class CustomConfigChangeListener { + + /** + * PolarisConfigKVFileChangeListener Example . + * @param event instance of {@link ConfigChangeEvent} + */ + @PolarisConfigKVFileChangeListener(interestedKeyPrefixes = "appName") + public void onChange(ConfigChangeEvent event) { + Set changedKeys = event.changedKeys(); + + for (String changedKey : changedKeys) { + System.out.printf("%s = %s \n", changedKey, event.getChange(changedKey)); + } + } + +} diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/CustomMetadata.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/CustomMetadata.java new file mode 100644 index 00000000..8257f2cd --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/CustomMetadata.java @@ -0,0 +1,46 @@ +/* + * 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.quickstart.callee; + +import java.util.HashMap; +import java.util.Map; + +import com.tencent.cloud.common.spi.InstanceMetadataProvider; + +import org.springframework.stereotype.Component; + +/** + * Custom metadata for instance. + * + * @author Haotian Zhang + */ +@Component +public class CustomMetadata implements InstanceMetadataProvider { + + @Override + public Map getMetadata() { + Map metadata = new HashMap<>(); + metadata.put("k1", "v2"); + return metadata; + } + + @Override + public String getZone() { + return "shenzhen-zone-2"; + } +} diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java new file mode 100644 index 00000000..a690a23c --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java @@ -0,0 +1,81 @@ +/* + * 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.quickstart.callee; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.beans.factory.annotation.Value; +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.RequestParam; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +/** + * Quickstart callee controller. + * + * @author Haotian Zhang + */ +@RestController +@RequestMapping("/quickstart/callee") +public class QuickstartCalleeController { + + private static final Logger LOG = LoggerFactory.getLogger(QuickstartCalleeController.class); + + @Value("${server.port:0}") + private int port; + + @Value("${appName:Callee}") + private String appName; + + /** + * Get sum of two value. + * @param value1 value 1 + * @param value2 value 2 + * @return sum + */ + @GetMapping("/sum") + public int sum(@RequestParam int value1, @RequestParam int value2) { + LOG.info("Quickstart Callee Service is called and sum is {}.", value1 + value2); + return value1 + value2; + } + + /** + * Get information of callee. + * @return information of callee + */ + @GetMapping("/info") + public String info() { + LOG.info("Quickstart [{}] Service [{}] is called.", appName, port); + return String.format("Quickstart [%s] Service [%s] is called.", appName, port); + } + + /** + * Check circuit break. + * + * @return circuit break info + */ + @GetMapping("/circuitBreak") + @ResponseStatus(value = HttpStatus.BAD_GATEWAY, reason = "failed for call quickstart callee service") + public String circuitBreak() { + LOG.info("Quickstart Callee Service [{}] is called wrong.", port); + return String.format("Quickstart Callee Service [%s] is called wrong.", port); + } +} diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeServiceB.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeServiceB.java new file mode 100644 index 00000000..c2740e40 --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeServiceB.java @@ -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.quickstart.callee; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Quickstart callee application. + * + * @author Haotian Zhang + */ +@SpringBootApplication +public class QuickstartCalleeServiceB { + + public static void main(String[] args) { + SpringApplication.run(QuickstartCalleeServiceB.class, args); + } +} diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/resources/bootstrap.yml new file mode 100644 index 00000000..f2e845e7 --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/resources/bootstrap.yml @@ -0,0 +1,27 @@ +server: + port: 48084 +spring: + application: + name: QuickstartCalleeService + cloud: + polaris: + address: grpc://183.47.111.80:8091 + namespace: default + enabled: true + discovery: + enabled: true + register: true + config: + address: grpc://183.47.111.80:8093 + auto-refresh: true + groups: + - name: ${spring.application.name} + files: [ "config/callee.properties" ] + refresh-type: reflect + ratelimit: + enabled: true + maxQueuingTime: 500 + tencent: + metadata: + content: + region: huanan diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/pom.xml b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/pom.xml new file mode 100644 index 00000000..c89b4318 --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/pom.xml @@ -0,0 +1,67 @@ + + + + quickstart-example + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + quickstart-caller-service + Quickstart Caller Service + + + + com.tencent.cloud + spring-cloud-starter-tencent-all + pom + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-actuator + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.2.0 + + + attach-sources + + jar + + + + + + + diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCalleeService.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCalleeService.java new file mode 100644 index 00000000..16486dfc --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCalleeService.java @@ -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.quickstart.caller; + +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * Quickstart callee feign client. + * + * @author Haotian Zhang + */ +@FeignClient(value = "QuickstartCalleeService", fallback = QuickstartCalleeServiceFallback.class) +public interface QuickstartCalleeService { + + /** + * Get sum of two value. + * + * @param value1 value 1 + * @param value2 value 2 + * @return sum + */ + @GetMapping("/quickstart/callee/sum") + int sum(@RequestParam("value1") int value1, @RequestParam("value2") int value2); + + /** + * Check circuit break. + * + * @return circuit break info + */ + @GetMapping("/quickstart/callee/circuitBreak") + String circuitBreak(); +} diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCalleeServiceFallback.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCalleeServiceFallback.java new file mode 100644 index 00000000..4e7a0003 --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCalleeServiceFallback.java @@ -0,0 +1,39 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.quickstart.caller; + +import org.springframework.stereotype.Component; + +/** + * Quickstart callee feign client fallback. + * + * @author Haotian Zhang + */ +@Component +public class QuickstartCalleeServiceFallback implements QuickstartCalleeService { + + @Override + public int sum(int value1, int value2) { + return 0; + } + + @Override + public String circuitBreak() { + return "CircuitBreak is triggered."; + } +} diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCallerApplication.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCallerApplication.java new file mode 100644 index 00000000..904a2a50 --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCallerApplication.java @@ -0,0 +1,45 @@ +/* + * 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.quickstart.caller; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestTemplate; + +/** + * Quickstart caller application. + * + * @author Haotian Zhang + */ +@SpringBootApplication +@EnableFeignClients +public class QuickstartCallerApplication { + + public static void main(String[] args) { + SpringApplication.run(QuickstartCallerApplication.class, args); + } + + @Bean + @LoadBalanced + public RestTemplate restTemplate() { + return new RestTemplate(); + } +} diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCallerController.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCallerController.java new file mode 100644 index 00000000..40b1233a --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/QuickstartCallerController.java @@ -0,0 +1,170 @@ +/* + * 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.quickstart.caller; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicInteger; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; + +/** + * Quickstart caller controller. + * + * @author Haotian Zhang + */ +@RestController +@RequestMapping("/quickstart/caller") +public class QuickstartCallerController { + + private static final Logger LOG = LoggerFactory.getLogger(QuickstartCallerController.class); + @Autowired + private RestTemplate restTemplate; + @Autowired + private QuickstartCalleeService quickstartCalleeService; + + /** + * Get sum of two value. + * @param value1 value 1 + * @param value2 value 2 + * @return sum + */ + @GetMapping("/feign") + public int feign(@RequestParam int value1, @RequestParam int value2) { + return quickstartCalleeService.sum(value1, value2); + } + + /** + * Get information of callee. + * @return information of callee + */ + @GetMapping("/rest") + public String rest() { + return restTemplate.getForObject("http://QuickstartCalleeService/quickstart/callee/info", String.class); + } + + /** + * Get information of callee. + * @return information of callee + */ + @GetMapping("/circuitBreak") + public String circuitBreak() { + return quickstartCalleeService.circuitBreak(); + } + + /** + * Get information 30 times per 1 second. + * + * @return result of 30 calls. + * @throws InterruptedException exception + */ + @GetMapping("/ratelimit") + public String invokeInfo() throws InterruptedException { + StringBuffer builder = new StringBuffer(); + CountDownLatch count = new CountDownLatch(30); + AtomicInteger index = new AtomicInteger(0); + for (int i = 0; i < 30; i++) { + new Thread(() -> { + try { + ResponseEntity entity = restTemplate.getForEntity( + "http://QuickstartCalleeService/quickstart/callee/info", String.class); + builder.append(entity.getBody() + "\n"); + } + catch (RestClientException e) { + if (e instanceof HttpClientErrorException.TooManyRequests) { + builder.append("TooManyRequests " + index.incrementAndGet() + "\n"); + } + else { + throw e; + } + } + count.countDown(); + }).start(); + } + count.await(); + return builder.toString(); + } + + /** + * Get information with unirate. + * + * @return information + */ + @GetMapping("/unirate") + public String unirate() throws InterruptedException { + StringBuffer builder = new StringBuffer(); + CountDownLatch count = new CountDownLatch(30); + AtomicInteger index = new AtomicInteger(0); + long currentTimestamp = System.currentTimeMillis(); + for (int i = 0; i < 30; i++) { + new Thread(() -> { + try { + long startTimestamp = System.currentTimeMillis(); + ResponseEntity entity = restTemplate.getForEntity( + "http://QuickstartCalleeService/quickstart/callee/info", String.class); + long endTimestamp = System.currentTimeMillis(); + builder.append("Start timestamp:" + startTimestamp + ". End timestamp: " + endTimestamp + + ". diff interval:" + (endTimestamp - startTimestamp) + "\n"); + } + catch (RestClientException e) { + if (e instanceof HttpClientErrorException.TooManyRequests) { + builder.append("TooManyRequests " + index.incrementAndGet() + "\n"); + } + else { + throw e; + } + } + count.countDown(); + }).start(); + } + count.await(); + long lastTimestamp = System.currentTimeMillis(); + builder.append("Unirate request from " + currentTimestamp + " to " + lastTimestamp + " with interval " + (lastTimestamp - currentTimestamp) + "ms."); + + return builder.toString(); + } + + /** + * Get information of callee. + * @return information of callee + */ + @GetMapping("/info") + public String info() { + LOG.info("Quickstart Callee Service is called."); + return "Quickstart Callee Service is called."; + } + + /** + * health check. + * @return health check info + */ + @GetMapping("/healthCheck") + public String healthCheck() { + return "ok"; + } +} diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/bootstrap.yml new file mode 100644 index 00000000..33425c08 --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/bootstrap.yml @@ -0,0 +1,41 @@ +server: + port: 48082 +spring: + application: + name: QuickstartCallerService + cloud: + polaris: + address: grpc://183.47.111.80:8091 + namespace: default + enabled: true + discovery: + enabled: true + register: true + heartbeat: + enabled: true + health-check-url: /quickstart/caller/healthCheck + circuitbreaker: + enabled: true + stat: + enabled: true + port: 28081 + # pushgateway: + # enabled: true + # address: 127.0.0.1:9091 + tencent: + rpc-enhancement: + enabled: true + reporter: + enabled: true + ignore-internal-server-error: true + series: server_error + statuses: gateway_timeout, bad_gateway, service_unavailable +feign: + hystrix: + enabled: true +management: + endpoints: + web: + exposure: + include: + - polaris-discovery diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/polaris.yml b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/polaris.yml new file mode 100644 index 00000000..fbe20fc8 --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/polaris.yml @@ -0,0 +1,14 @@ +consumer: + circuitBreaker: + checkPeriod: 100ms + chain: + - errorCount + - errorRate + plugin: + errorCount: + continuousErrorThreshold: 1 + metricNumBuckets: 1 + errorRate: + errorRateThreshold: 100 + metricStatTimeWindow: 1s + requestVolumeThreshold: 1 diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-gateway-service/pom.xml b/spring-cloud-tencent-examples/quickstart-example/quickstart-gateway-service/pom.xml new file mode 100644 index 00000000..48591243 --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-gateway-service/pom.xml @@ -0,0 +1,67 @@ + + + + quickstart-example + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + quickstart-gateway-service + Quickstart Gateway Service + + + + com.tencent.cloud + spring-cloud-starter-tencent-all + pom + + + + com.tencent.cloud + spring-cloud-tencent-gateway-plugin + + + + org.springframework.cloud + spring-cloud-starter-gateway + + + + org.springframework.boot + spring-boot-starter-actuator + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.2.0 + + + attach-sources + + jar + + + + + + + diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-gateway-service/src/main/java/com/tencent/cloud/quickstart/gateway/QuickstartGatewayApplication.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-gateway-service/src/main/java/com/tencent/cloud/quickstart/gateway/QuickstartGatewayApplication.java new file mode 100644 index 00000000..5db23a74 --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-gateway-service/src/main/java/com/tencent/cloud/quickstart/gateway/QuickstartGatewayApplication.java @@ -0,0 +1,35 @@ +/* + * 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.quickstart.gateway; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Quickstart SCG application. + * + * @author Haotian Zhang + */ +@SpringBootApplication +public class QuickstartGatewayApplication { + + public static void main(String[] args) { + SpringApplication.run(QuickstartGatewayApplication.class, args); + } + +} diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-gateway-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/quickstart-example/quickstart-gateway-service/src/main/resources/bootstrap.yml new file mode 100644 index 00000000..fa49aa66 --- /dev/null +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-gateway-service/src/main/resources/bootstrap.yml @@ -0,0 +1,28 @@ +server: + port: 48081 +spring: + application: + name: QuickStartGatewayService + cloud: + tencent: + plugin: + scg: + staining: + enabled: true + rule-staining: + enabled: true + router: + feature-env: + enabled: true + polaris: + address: grpc://183.47.111.80:8091 + namespace: default + enabled: true + gateway: + routes: + - id: QuickstartCallerService + uri: lb://QuickstartCallerService + predicates: + - Path=/QuickstartCallerService/** + filters: + - StripPrefix=1 diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/config/RpcEnhancementAutoConfiguration.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/config/RpcEnhancementAutoConfiguration.java index 8e22239c..14529f34 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/config/RpcEnhancementAutoConfiguration.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/config/RpcEnhancementAutoConfiguration.java @@ -34,6 +34,7 @@ import com.tencent.polaris.api.core.ConsumerAPI; import org.springframework.beans.factory.SmartInitializingSingleton; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -43,6 +44,8 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.context.annotation.Role; import org.springframework.web.client.RestTemplate; /** @@ -66,6 +69,7 @@ public class RpcEnhancementAutoConfiguration { @Configuration(proxyBeanMethods = false) @ConditionalOnClass(name = "org.springframework.cloud.openfeign.FeignAutoConfiguration") @AutoConfigureBefore(name = "org.springframework.cloud.openfeign.FeignAutoConfiguration") + @Role(RootBeanDefinition.ROLE_INFRASTRUCTURE) protected static class PolarisFeignClientAutoConfiguration { @Bean @@ -75,7 +79,7 @@ public class RpcEnhancementAutoConfiguration { } @Bean - public EnhancedFeignBeanPostProcessor polarisFeignBeanPostProcessor(EnhancedFeignPluginRunner pluginRunner) { + public EnhancedFeignBeanPostProcessor polarisFeignBeanPostProcessor(@Lazy EnhancedFeignPluginRunner pluginRunner) { return new EnhancedFeignBeanPostProcessor(pluginRunner); } diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/plugin/reporter/ExceptionPolarisReporter.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/plugin/reporter/ExceptionPolarisReporter.java index b870f329..1f1c4366 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/plugin/reporter/ExceptionPolarisReporter.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/plugin/reporter/ExceptionPolarisReporter.java @@ -42,12 +42,10 @@ import org.springframework.core.Ordered; public class ExceptionPolarisReporter implements EnhancedFeignPlugin { private static final Logger LOG = LoggerFactory.getLogger(ExceptionPolarisReporter.class); - + private final RpcEnhancementReporterProperties reporterProperties; @Autowired(required = false) private ConsumerAPI consumerAPI; - private RpcEnhancementReporterProperties reporterProperties; - public ExceptionPolarisReporter(RpcEnhancementReporterProperties reporterProperties) { this.reporterProperties = reporterProperties; } @@ -79,6 +77,9 @@ public class ExceptionPolarisReporter implements EnhancedFeignPlugin { LOG.debug("Will report result of {}. Request=[{}]. Response=[{}].", retStatus.name(), request, response); ServiceCallResult resultRequest = ReporterUtils.createServiceCallResult(request, retStatus); consumerAPI.updateServiceCallResult(resultRequest); + // update result without method for service circuit break. + resultRequest.setMethod(""); + consumerAPI.updateServiceCallResult(resultRequest); } } diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/plugin/reporter/SuccessPolarisReporter.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/plugin/reporter/SuccessPolarisReporter.java index eadaeaaf..f7d59b51 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/plugin/reporter/SuccessPolarisReporter.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/plugin/reporter/SuccessPolarisReporter.java @@ -75,6 +75,9 @@ public class SuccessPolarisReporter extends AbstractPolarisReporterAdapter imple LOG.debug("Will report result of {}. Request=[{}]. Response=[{}].", retStatus.name(), request, response); ServiceCallResult resultRequest = ReporterUtils.createServiceCallResult(request, retStatus); consumerAPI.updateServiceCallResult(resultRequest); + // update result without method for service circuit break. + resultRequest.setMethod(""); + consumerAPI.updateServiceCallResult(resultRequest); } } diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporter.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporter.java index 2a56c39b..06379a8c 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporter.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporter.java @@ -133,6 +133,9 @@ public class EnhancedRestTemplateReporter extends AbstractPolarisReporterAdapter LOGGER.debug("Will report result of {}. URL=[{}]. Response=[{}].", resultRequest.getRetStatus().name(), url, response); consumerAPI.updateServiceCallResult(resultRequest); + // update result without method for service circuit break. + resultRequest.setMethod(""); + consumerAPI.updateServiceCallResult(resultRequest); } catch (Exception e) { LOGGER.error("RestTemplate response reporter execute failed of {} url {}", response, url, e); diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporterTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporterTest.java index 173cb727..099f266d 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporterTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporterTest.java @@ -121,7 +121,7 @@ public class EnhancedRestTemplateReporterTest { URI uri = mock(URI.class); enhancedRestTemplateReporter.handleError(uri, HttpMethod.GET, response); - verify(consumerAPI).updateServiceCallResult(any()); + verify(consumerAPI, times(2)).updateServiceCallResult(any()); verify(delegate).handleError(uri, HttpMethod.GET, response); } @@ -136,7 +136,7 @@ public class EnhancedRestTemplateReporterTest { URI uri = mock(URI.class); enhancedRestTemplateReporter.handleError(uri, HttpMethod.GET, response); - verify(consumerAPI).updateServiceCallResult(any()); + verify(consumerAPI, times(2)).updateServiceCallResult(any()); verify(delegate, times(0)).handleError(uri, HttpMethod.GET, response); }