From e8e4daf29ec6dcbd31342ff2c33be93e734cdb81 Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Thu, 9 May 2024 15:21:35 +0800 Subject: [PATCH 1/6] feat: add lane router and lossless features from 2023 --- spring-cloud-tencent-dependencies/pom.xml | 2 +- .../lossless-callee-service/pom.xml | 63 ++++++++++ .../callee/LosslessCalleeController.java | 95 +++++++++++++++ .../callee/LosslessCalleeService.java | 29 +++++ .../src/main/resources/bootstrap.yml | 36 ++++++ .../lossless-nacos-callee-service/pom.xml | 69 +++++++++++ .../callee/LosslessNacosCalleeController.java | 95 +++++++++++++++ .../callee/LosslessNacosCalleeService.java | 23 ++-- .../src/main/resources/bootstrap.yml | 20 +++ .../lossless-example/pom.xml | 18 +++ .../pom.xml | 24 ++++ .../pom.xml | 70 +++++++++++ .../cloud/lane/callee/CustomMetadata.java | 47 ++++++++ .../callee/LaneRouterCalleeApplication.java | 29 +++++ .../callee/LaneRouterCalleeController.java | 91 ++++++++++++++ .../src/main/resources/application.yml | 36 ++++++ .../pom.xml | 75 ++++++++++++ .../cloud/lane/caller/CustomMetadata.java | 47 ++++++++ .../lane/caller/LaneRouterCalleeService.java | 41 +++++++ .../caller/LaneRouterCallerApplication.java | 57 +++++++++ .../caller/LaneRouterCallerController.java | 114 ++++++++++++++++++ .../src/main/resources/application.yml | 42 +++++++ .../router-grayrelease-lane-gateway/pom.xml | 48 ++++++++ .../gateway/LaneRouterGatewayApplication.java | 29 +++++ .../src/main/resources/application.yml | 39 ++++++ spring-cloud-tencent-examples/pom.xml | 2 + ...acosDiscoveryAdapterAutoConfiguration.java | 13 +- .../transformer/NacosInstanceTransformer.java | 27 +++-- .../NacosRegistrationTransformer.java | 49 ++++++++ ...DiscoveryAdapterAutoConfigurationTest.java | 2 + .../config/NacosInstanceTransformerTest.java | 21 ++-- .../pom.xml | 7 -- .../lossless/LosslessRegistryAspect.java | 20 +-- .../SpringCloudLosslessActionProvider.java | 19 +-- .../config/LosslessAutoConfiguration.java | 46 ++----- .../NacosDiscoveryNamespaceGetter.java | 47 -------- .../lossless/LosslessRegistryAspectTest.java | 53 -------- .../RpcEnhancementAutoConfiguration.java | 18 +++ .../transformer/InstanceTransformer.java | 53 ++++++++ .../PolarisInstanceTransformer.java | 44 +++++++ .../PolarisRegistrationTransformer.java | 15 ++- .../transformer/RegistrationTransformer.java | 58 +++++++++ 42 files changed, 1519 insertions(+), 214 deletions(-) create mode 100644 spring-cloud-tencent-examples/lossless-example/lossless-callee-service/pom.xml create mode 100644 spring-cloud-tencent-examples/lossless-example/lossless-callee-service/src/main/java/com/tencent/cloud/lossless/callee/LosslessCalleeController.java create mode 100644 spring-cloud-tencent-examples/lossless-example/lossless-callee-service/src/main/java/com/tencent/cloud/lossless/callee/LosslessCalleeService.java create mode 100644 spring-cloud-tencent-examples/lossless-example/lossless-callee-service/src/main/resources/bootstrap.yml create mode 100644 spring-cloud-tencent-examples/lossless-example/lossless-nacos-callee-service/pom.xml create mode 100644 spring-cloud-tencent-examples/lossless-example/lossless-nacos-callee-service/src/main/java/com/tencent/cloud/lossless/nacos/callee/LosslessNacosCalleeController.java rename spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/transfomer/PolarisDiscoveryNamespaceGetter.java => spring-cloud-tencent-examples/lossless-example/lossless-nacos-callee-service/src/main/java/com/tencent/cloud/lossless/nacos/callee/LosslessNacosCalleeService.java (59%) create mode 100644 spring-cloud-tencent-examples/lossless-example/lossless-nacos-callee-service/src/main/resources/bootstrap.yml create mode 100644 spring-cloud-tencent-examples/lossless-example/pom.xml create mode 100644 spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/pom.xml create mode 100644 spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/pom.xml create mode 100644 spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/src/main/java/com/tencent/cloud/lane/callee/CustomMetadata.java create mode 100644 spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/src/main/java/com/tencent/cloud/lane/callee/LaneRouterCalleeApplication.java create mode 100644 spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/src/main/java/com/tencent/cloud/lane/callee/LaneRouterCalleeController.java create mode 100644 spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/src/main/resources/application.yml create mode 100644 spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/pom.xml create mode 100644 spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/java/com/tencent/cloud/lane/caller/CustomMetadata.java create mode 100644 spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/java/com/tencent/cloud/lane/caller/LaneRouterCalleeService.java create mode 100644 spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/java/com/tencent/cloud/lane/caller/LaneRouterCallerApplication.java create mode 100644 spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/java/com/tencent/cloud/lane/caller/LaneRouterCallerController.java create mode 100644 spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/resources/application.yml create mode 100644 spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-gateway/pom.xml create mode 100644 spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-gateway/src/main/java/com/tencent/cloud/polaris/router/lane/gateway/LaneRouterGatewayApplication.java create mode 100644 spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-gateway/src/main/resources/application.yml create mode 100644 spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/main/java/com/tencent/cloud/plugin/discovery/adapter/transformer/NacosRegistrationTransformer.java delete mode 100644 spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/transfomer/NacosDiscoveryNamespaceGetter.java create mode 100644 spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/transformer/InstanceTransformer.java create mode 100644 spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/transformer/PolarisInstanceTransformer.java rename spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/transfomer/DiscoveryNamespaceGetter.java => spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/transformer/PolarisRegistrationTransformer.java (78%) create mode 100644 spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/transformer/RegistrationTransformer.java diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 1e5b3d6192..a13e2f395c 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -74,7 +74,7 @@ 1.14.0-Hoxton.SR12-SNAPSHOT - 1.15.3 + 1.15.5-SNAPSHOT 32.0.1-jre diff --git a/spring-cloud-tencent-examples/lossless-example/lossless-callee-service/pom.xml b/spring-cloud-tencent-examples/lossless-example/lossless-callee-service/pom.xml new file mode 100644 index 0000000000..3672717020 --- /dev/null +++ b/spring-cloud-tencent-examples/lossless-example/lossless-callee-service/pom.xml @@ -0,0 +1,63 @@ + + 4.0.0 + + lossless-example + com.tencent.cloud + ${revision} + ../pom.xml + + lossless-callee-service + Spring Cloud Starter Tencent Lossless Callee Service Example + + + + com.tencent.cloud + spring-cloud-starter-tencent-all + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-actuator + + + + com.tencent.cloud + spring-cloud-tencent-lossless-plugin + + + + + + + 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/lossless-example/lossless-callee-service/src/main/java/com/tencent/cloud/lossless/callee/LosslessCalleeController.java b/spring-cloud-tencent-examples/lossless-example/lossless-callee-service/src/main/java/com/tencent/cloud/lossless/callee/LosslessCalleeController.java new file mode 100644 index 0000000000..ec7347c91e --- /dev/null +++ b/spring-cloud-tencent-examples/lossless-example/lossless-callee-service/src/main/java/com/tencent/cloud/lossless/callee/LosslessCalleeController.java @@ -0,0 +1,95 @@ +/* + * 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.lossless.callee; + +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + +import com.tencent.cloud.common.constant.MetadataConstant; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import static com.tencent.cloud.common.constant.ContextConstant.UTF_8; + +@RestController +@RequestMapping("/lossless/callee") +public class LosslessCalleeController { + + private static final Logger LOG = LoggerFactory.getLogger(LosslessCalleeController.class); + + @Value("${lossless.healthy.delay.second:0}") + private int healthyDelay; + + private final AtomicBoolean calledHealthyEndpoint = new AtomicBoolean(false); + + private final AtomicInteger healthy = new AtomicInteger(0); + + @GetMapping("/health") + public ResponseEntity health() { + if (healthy.get() == 1) { + return new ResponseEntity<>("OK", HttpStatus.OK); + } + else { + if (calledHealthyEndpoint.compareAndSet(false, true)) { + Thread thread = new Thread(new Runnable() { + @Override + public void run() { + if (healthyDelay > 0) { + try { + Thread.sleep(healthyDelay * 1000L); + } + catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + healthy.set(1); + } + }); + thread.start(); + } + return new ResponseEntity<>("NOK", HttpStatus.SERVICE_UNAVAILABLE); + } + } + + /** + * Get metadata in HTTP query. + * + * @param metadataStr metadata string + * @return metadata in HTTP header + * @throws UnsupportedEncodingException encoding exception + */ + @RequestMapping("/echo") + public String echoHeader(@RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String metadataStr) + throws UnsupportedEncodingException { + LOG.info(URLDecoder.decode(metadataStr, UTF_8)); + metadataStr = URLDecoder.decode(metadataStr, UTF_8); + return metadataStr; + } + +} diff --git a/spring-cloud-tencent-examples/lossless-example/lossless-callee-service/src/main/java/com/tencent/cloud/lossless/callee/LosslessCalleeService.java b/spring-cloud-tencent-examples/lossless-example/lossless-callee-service/src/main/java/com/tencent/cloud/lossless/callee/LosslessCalleeService.java new file mode 100644 index 0000000000..542653ba04 --- /dev/null +++ b/spring-cloud-tencent-examples/lossless-example/lossless-callee-service/src/main/java/com/tencent/cloud/lossless/callee/LosslessCalleeService.java @@ -0,0 +1,29 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.lossless.callee; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class LosslessCalleeService { + public static void main(String[] args) { + SpringApplication.run(LosslessCalleeService.class, args); + } +} diff --git a/spring-cloud-tencent-examples/lossless-example/lossless-callee-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/lossless-example/lossless-callee-service/src/main/resources/bootstrap.yml new file mode 100644 index 0000000000..4e8c2a8ce4 --- /dev/null +++ b/spring-cloud-tencent-examples/lossless-example/lossless-callee-service/src/main/resources/bootstrap.yml @@ -0,0 +1,36 @@ +server: + port: 48090 +spring: + application: + name: LosslessCalleeService + cloud: + polaris: + address: grpc://119.91.66.223:8091 + namespace: default + enabled: true + discovery: + enabled: true + register: true + contract: + exposure: true + report: + enabled: true + stat: + enabled: true + port: 28084 + lossless: + enabled: true + port: 28084 + #healthCheckPath: /actuator/health + #healthCheckInterval: 5000 +lossless: + healthy: + delay: + second: 20 +management: + endpoints: + web: + exposure: + include: + - polaris-discovery + - health \ No newline at end of file diff --git a/spring-cloud-tencent-examples/lossless-example/lossless-nacos-callee-service/pom.xml b/spring-cloud-tencent-examples/lossless-example/lossless-nacos-callee-service/pom.xml new file mode 100644 index 0000000000..72f7ed345d --- /dev/null +++ b/spring-cloud-tencent-examples/lossless-example/lossless-nacos-callee-service/pom.xml @@ -0,0 +1,69 @@ + + 4.0.0 + + lossless-example + com.tencent.cloud + ${revision} + ../pom.xml + + com.tencent.polaris + lossless-nacos-callee-service + Spring Cloud Starter Tencent Lossless Nacos Callee Service Example + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + 2.2.10-RC2 + + + + com.tencent.cloud + spring-cloud-starter-tencent-discovery-adapter-plugin + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-actuator + + + + com.tencent.cloud + spring-cloud-tencent-lossless-plugin + + + + + + + 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/lossless-example/lossless-nacos-callee-service/src/main/java/com/tencent/cloud/lossless/nacos/callee/LosslessNacosCalleeController.java b/spring-cloud-tencent-examples/lossless-example/lossless-nacos-callee-service/src/main/java/com/tencent/cloud/lossless/nacos/callee/LosslessNacosCalleeController.java new file mode 100644 index 0000000000..ea96f64b3c --- /dev/null +++ b/spring-cloud-tencent-examples/lossless-example/lossless-nacos-callee-service/src/main/java/com/tencent/cloud/lossless/nacos/callee/LosslessNacosCalleeController.java @@ -0,0 +1,95 @@ +/* + * 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.lossless.nacos.callee; + +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + +import com.tencent.cloud.common.constant.MetadataConstant; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import static com.tencent.cloud.common.constant.ContextConstant.UTF_8; + +@RestController +@RequestMapping("/lossless/nacos/callee") +public class LosslessNacosCalleeController { + + private static final Logger LOG = LoggerFactory.getLogger(LosslessNacosCalleeController.class); + + @Value("${lossless.healthy.delay.second:0}") + private int healthyDelay; + + private final AtomicBoolean calledHealthyEndpoint = new AtomicBoolean(false); + + private final AtomicInteger healthy = new AtomicInteger(0); + + @GetMapping("/health") + public ResponseEntity health() { + if (healthy.get() == 1) { + return new ResponseEntity<>("OK", HttpStatus.OK); + } + else { + if (calledHealthyEndpoint.compareAndSet(false, true)) { + Thread thread = new Thread(new Runnable() { + @Override + public void run() { + if (healthyDelay > 0) { + try { + Thread.sleep(healthyDelay * 1000L); + } + catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + healthy.set(1); + } + }); + thread.start(); + } + return new ResponseEntity<>("NOK", HttpStatus.SERVICE_UNAVAILABLE); + } + } + + /** + * Get metadata in HTTP query. + * + * @param metadataStr metadata string + * @return metadata in HTTP header + * @throws UnsupportedEncodingException encoding exception + */ + @RequestMapping("/echo") + public String echoHeader(@RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String metadataStr) + throws UnsupportedEncodingException { + LOG.info(URLDecoder.decode(metadataStr, UTF_8)); + metadataStr = URLDecoder.decode(metadataStr, UTF_8); + return metadataStr; + } + +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/transfomer/PolarisDiscoveryNamespaceGetter.java b/spring-cloud-tencent-examples/lossless-example/lossless-nacos-callee-service/src/main/java/com/tencent/cloud/lossless/nacos/callee/LosslessNacosCalleeService.java similarity index 59% rename from spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/transfomer/PolarisDiscoveryNamespaceGetter.java rename to spring-cloud-tencent-examples/lossless-example/lossless-nacos-callee-service/src/main/java/com/tencent/cloud/lossless/nacos/callee/LosslessNacosCalleeService.java index 51126897ba..a2d29436ab 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/transfomer/PolarisDiscoveryNamespaceGetter.java +++ b/spring-cloud-tencent-examples/lossless-example/lossless-nacos-callee-service/src/main/java/com/tencent/cloud/lossless/nacos/callee/LosslessNacosCalleeService.java @@ -13,26 +13,19 @@ * 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.plugin.lossless.transfomer; +package com.tencent.cloud.lossless.nacos.callee; -import com.tencent.cloud.polaris.PolarisDiscoveryProperties; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; -/** - * Discovery namespace getter for Polaris. - * - * @author Shedfree Wu - */ -public class PolarisDiscoveryNamespaceGetter implements DiscoveryNamespaceGetter { +@SpringBootApplication +public class LosslessNacosCalleeService { - private String namespace; - - public PolarisDiscoveryNamespaceGetter(PolarisDiscoveryProperties polarisDiscoveryProperties) { - this.namespace = polarisDiscoveryProperties.getNamespace(); + public static void main(String[] args) { + SpringApplication.run(LosslessNacosCalleeService.class, args); } - public String getNamespace() { - return namespace; - } } diff --git a/spring-cloud-tencent-examples/lossless-example/lossless-nacos-callee-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/lossless-example/lossless-nacos-callee-service/src/main/resources/bootstrap.yml new file mode 100644 index 0000000000..a621c8c493 --- /dev/null +++ b/spring-cloud-tencent-examples/lossless-example/lossless-nacos-callee-service/src/main/resources/bootstrap.yml @@ -0,0 +1,20 @@ +server: + port: 48091 +spring: + application: + name: LosslessNacosCalleeService + cloud: + nacos: + discovery: + server-addr: 127.0.0.1:8848 + enabled: true + polaris: + lossless: + enabled: true + port: 28085 + healthCheckPath: /actuator/health + healthCheckInterval: 5000 +lossless: + healthy: + delay: + second: 20 \ No newline at end of file diff --git a/spring-cloud-tencent-examples/lossless-example/pom.xml b/spring-cloud-tencent-examples/lossless-example/pom.xml new file mode 100644 index 0000000000..02d46dabd2 --- /dev/null +++ b/spring-cloud-tencent-examples/lossless-example/pom.xml @@ -0,0 +1,18 @@ + + + spring-cloud-tencent-examples + com.tencent.cloud + ${revision} + ../pom.xml + + + lossless-callee-service + lossless-nacos-callee-service + + 4.0.0 + + lossless-example + pom + Spring Cloud Starter Tencent Lossless Example + \ No newline at end of file diff --git a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/pom.xml b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/pom.xml new file mode 100644 index 0000000000..6f602207d4 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/pom.xml @@ -0,0 +1,24 @@ + + + + spring-cloud-tencent-examples + com.tencent.cloud + ${revision} + ../pom.xml + + + router-grayrelease-lane-gateway + router-grayrelease-lane-caller-service + router-grayrelease-lane-callee-service + + 4.0.0 + + com.tencent.polaris + polaris-router-grayrelease-lane-example + + pom + Spring Cloud Tencent Polaris Router Lane Example + Example of Spring Cloud Tencent Polaris Router Lane + \ No newline at end of file diff --git a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/pom.xml b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/pom.xml new file mode 100644 index 0000000000..3431f03d08 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/pom.xml @@ -0,0 +1,70 @@ + + + 4.0.0 + + com.tencent.polaris + polaris-router-grayrelease-lane-example + ${revision} + ../pom.xml + + + route-grayrelease-lane-callee-service + + + + spring-cloud-starter-tencent-polaris-discovery + com.tencent.cloud + + + + com.tencent.cloud + spring-cloud-starter-tencent-polaris-router + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.2.0 + + + attach-sources + + jar + + + + + + + + \ No newline at end of file diff --git a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/src/main/java/com/tencent/cloud/lane/callee/CustomMetadata.java b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/src/main/java/com/tencent/cloud/lane/callee/CustomMetadata.java new file mode 100644 index 0000000000..ba623274e0 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/src/main/java/com/tencent/cloud/lane/callee/CustomMetadata.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.lane.callee; + +import java.util.HashMap; +import java.util.Map; + +import com.tencent.cloud.common.spi.InstanceMetadataProvider; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +/** + * Custom metadata for instance. + * + * @author Haotian Zhang + */ +@Component +public class CustomMetadata implements InstanceMetadataProvider { + + @Value("${service.lane:base}") + private String lane; + + @Override + public Map getMetadata() { + Map metadata = new HashMap<>(); + if (!"base".equals(lane)) { + metadata.put("lane", lane); + } + return metadata; + } +} diff --git a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/src/main/java/com/tencent/cloud/lane/callee/LaneRouterCalleeApplication.java b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/src/main/java/com/tencent/cloud/lane/callee/LaneRouterCalleeApplication.java new file mode 100644 index 0000000000..5ffabdd2cf --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/src/main/java/com/tencent/cloud/lane/callee/LaneRouterCalleeApplication.java @@ -0,0 +1,29 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.lane.callee; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class LaneRouterCalleeApplication { + + public static void main(String[] args) { + SpringApplication.run(LaneRouterCalleeApplication.class, args); + } +} diff --git a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/src/main/java/com/tencent/cloud/lane/callee/LaneRouterCalleeController.java b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/src/main/java/com/tencent/cloud/lane/callee/LaneRouterCalleeController.java new file mode 100644 index 0000000000..d10859db7f --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/src/main/java/com/tencent/cloud/lane/callee/LaneRouterCalleeController.java @@ -0,0 +1,91 @@ +/* + * 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.lane.callee; + +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; + +import com.tencent.cloud.common.constant.MetadataConstant; +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.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import static com.tencent.cloud.common.constant.ContextConstant.UTF_8; + +@RestController +@RequestMapping("/lane/callee") +public class LaneRouterCalleeController { + + private static final Logger LOG = LoggerFactory.getLogger(LaneRouterCalleeController.class); + + @Value("${server.port:0}") + private int port; + + @Value("${spring.cloud.client.ip-address:127.0.0.1}") + private String ip; + + @Value("${appName:${spring.application.name}}") + private String appName; + + @Value("${service.lane:base}") + private String lane; + + /** + * Get sum of two value. + * @param value1 value 1 + * @param value2 value 2 + * @return sum + */ + @GetMapping("/sum") + public String sum(@RequestParam int value1, @RequestParam int value2) { + LOG.info("Lane [{}] Callee Service [{} - {}:{}] is called and sum is [{}].", lane, appName, ip, port, value1 + value2); + return String.format("Lane [%s] Callee Service [%s - %s:%s] is called and sum is [%s].", lane, appName, ip, port, value1 + value2); + } + + /** + * Get information of callee. + * @return information of callee + */ + @GetMapping("/info") + public String info() { + LOG.info("Lane [{}] Service [{} - {}:{}] is called.", lane, appName, ip, port); + return String.format("Lane [%s] Service [%s - %s:%s] is called.", lane, appName, ip, port); + } + + /** + * Get metadata in HTTP header. + * + * @param metadataStr metadata string + * @return metadata in HTTP header + * @throws UnsupportedEncodingException encoding exception + */ + @RequestMapping("/echo") + public String echoHeader(@RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String metadataStr) + throws UnsupportedEncodingException { + LOG.info(URLDecoder.decode(metadataStr, UTF_8)); + metadataStr = URLDecoder.decode(metadataStr, UTF_8); + return String.format("Lane [%s]: %s", lane, metadataStr); + } + +} diff --git a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/src/main/resources/application.yml b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/src/main/resources/application.yml new file mode 100644 index 0000000000..29d9d9c961 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/src/main/resources/application.yml @@ -0,0 +1,36 @@ +server: + port: 48093 +spring: + application: + name: LaneCalleeService + cloud: + polaris: + address: grpc://119.91.66.223:8091 + namespace: default + enabled: true + discovery: + enabled: true + register: true + contract: + exposure: true + report: + enabled: true + stat: + enabled: true + port: 28083 + # pushgateway: + # enabled: true + # address: 127.0.0.1:9091 + config: + address: grpc://9.134.5.52:8093 + auto-refresh: true + groups: + - name: ${spring.application.name} + files: [ "config/callee.properties" ] +management: + endpoints: + web: + exposure: + include: + - polaris-discovery + - polaris-config \ No newline at end of file diff --git a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/pom.xml b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/pom.xml new file mode 100644 index 0000000000..f1a48d81bd --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/pom.xml @@ -0,0 +1,75 @@ + + + 4.0.0 + + com.tencent.polaris + polaris-router-grayrelease-lane-example + ${revision} + ../pom.xml + + + route-grayrelease-lane-caller-service + + + + spring-cloud-starter-tencent-polaris-discovery + com.tencent.cloud + + + + com.tencent.cloud + spring-cloud-starter-tencent-polaris-router + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-webflux + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.2.0 + + + attach-sources + + jar + + + + + + + + \ No newline at end of file diff --git a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/java/com/tencent/cloud/lane/caller/CustomMetadata.java b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/java/com/tencent/cloud/lane/caller/CustomMetadata.java new file mode 100644 index 0000000000..51939912ec --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/java/com/tencent/cloud/lane/caller/CustomMetadata.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.lane.caller; + +import java.util.HashMap; +import java.util.Map; + +import com.tencent.cloud.common.spi.InstanceMetadataProvider; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +/** + * Custom metadata for instance. + * + * @author Haotian Zhang + */ +@Component +public class CustomMetadata implements InstanceMetadataProvider { + + @Value("${service.lane:base}") + private String lane; + + @Override + public Map getMetadata() { + Map metadata = new HashMap<>(); + if (!"base".equals(lane)) { + metadata.put("lane", lane); + } + return metadata; + } +} diff --git a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/java/com/tencent/cloud/lane/caller/LaneRouterCalleeService.java b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/java/com/tencent/cloud/lane/caller/LaneRouterCalleeService.java new file mode 100644 index 0000000000..b057f4ffe2 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/java/com/tencent/cloud/lane/caller/LaneRouterCalleeService.java @@ -0,0 +1,41 @@ +/* + * 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.lane.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("LaneCalleeService") +public interface LaneRouterCalleeService { + + /** + * Get sum of two value. + * + * @param value1 value 1 + * @param value2 value 2 + * @return sum + */ + @GetMapping("/lane/callee/sum") + String sum(@RequestParam("value1") int value1, @RequestParam("value2") int value2); +} diff --git a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/java/com/tencent/cloud/lane/caller/LaneRouterCallerApplication.java b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/java/com/tencent/cloud/lane/caller/LaneRouterCallerApplication.java new file mode 100644 index 0000000000..6b387c7b17 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/java/com/tencent/cloud/lane/caller/LaneRouterCallerApplication.java @@ -0,0 +1,57 @@ +/* + * 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.lane.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; +import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.util.DefaultUriBuilderFactory; + +@SpringBootApplication +@EnableFeignClients +public class LaneRouterCallerApplication { + + public static void main(String[] args) { + SpringApplication.run(LaneRouterCallerApplication.class, args); + } + + @Bean + @LoadBalanced + public RestTemplate restTemplate() { + return new RestTemplate(); + } + + @Bean + @LoadBalanced + public RestTemplate defaultRestTemplate() { + DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory("http://QuickstartCalleeService"); + RestTemplate restTemplate = new RestTemplate(); + restTemplate.setUriTemplateHandler(uriBuilderFactory); + return restTemplate; + } + + @LoadBalanced + @Bean + WebClient.Builder webClientBuilder() { + return WebClient.builder().baseUrl("http://QuickstartCalleeService"); + } +} diff --git a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/java/com/tencent/cloud/lane/caller/LaneRouterCallerController.java b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/java/com/tencent/cloud/lane/caller/LaneRouterCallerController.java new file mode 100644 index 0000000000..90f990dbc2 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/java/com/tencent/cloud/lane/caller/LaneRouterCallerController.java @@ -0,0 +1,114 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.lane.caller; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import reactor.core.publisher.Mono; + +import org.springframework.beans.factory.annotation.Autowired; +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; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.reactive.function.client.WebClient; + +@RestController +@RequestMapping("/lane/caller") +public class LaneRouterCallerController { + + private static final Logger LOG = LoggerFactory.getLogger(LaneRouterCallerController.class); + + @Value("${server.port:0}") + private int port; + + @Value("${spring.cloud.client.ip-address:127.0.0.1}") + private String ip; + + @Autowired + private RestTemplate restTemplate; + + @Autowired + private LaneRouterCalleeService quickstartCalleeService; + + @Autowired + private WebClient.Builder webClientBuilder; + + @Value("${service.lane:base}") + private String lane; + + @Value("${appName:${spring.application.name}}") + private String appName; + + /** + * Get sum of two value. + * @param value1 value 1 + * @param value2 value 2 + * @return sum + */ + @GetMapping("/feign") + public String feign(@RequestParam int value1, @RequestParam int value2) { + String value = quickstartCalleeService.sum(value1, value2); + return String.format("Lane [%s] Caller Service [%s - %s:%s] -> %s", lane, appName, ip, port, value); + } + + /** + * Get information of callee. + * @return information of callee + */ + @GetMapping("/rest") + public String rest() { + String value = restTemplate.getForObject("http://LaneCalleeService/lane/callee/info", String.class); + return String.format("Lane [%s] Caller Service [%s - %s:%s] -> %s", lane, appName, ip, port, value); + } + + /** + * Get information of callee. + * @return information of callee + */ + @GetMapping("/webclient") + public Mono webclient() { + return webClientBuilder + .build() + .get() + .uri("/quickstart/callee/echo") + .retrieve() + .bodyToMono(String.class); + } + + /** + * Get information of caller. + * @return information of caller + */ + @GetMapping("/info") + public String info() { + LOG.info("Lane {} Service [{} - {}:{}] is called.", lane, appName, ip, port); + return String.format("Lane [%s] Service [%s - %s:%s] is called.", lane, appName, ip, port); + } + + /** + * health check. + * @return health check info + */ + @GetMapping("/healthCheck") + public String healthCheck() { + return "ok"; + } +} diff --git a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/resources/application.yml b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/resources/application.yml new file mode 100644 index 0000000000..259358d554 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/resources/application.yml @@ -0,0 +1,42 @@ +server: + port: 48092 +spring: + application: + name: LaneCallerService + cloud: + polaris: + address: grpc://119.91.66.223:8091 + namespace: default + enabled: true + discovery: + enabled: true + register: true + heartbeat: + enabled: true + health-check-url: /lane/caller/healthCheck + contract: + exposure: true + report: + enabled: true + circuitbreaker: + enabled: true + stat: + enabled: true + port: 28082 + # 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 +management: + endpoints: + web: + exposure: + include: + - polaris-discovery \ No newline at end of file diff --git a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-gateway/pom.xml b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-gateway/pom.xml new file mode 100644 index 0000000000..efd0edff1b --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-gateway/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + + com.tencent.polaris + polaris-router-grayrelease-lane-example + ${revision} + ../pom.xml + + + router-grayrelease-lane-gateway + + + + spring-cloud-starter-tencent-polaris-discovery + com.tencent.cloud + + + + com.tencent.cloud + spring-cloud-starter-tencent-polaris-router + + + + org.springframework.cloud + spring-cloud-starter-gateway + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + + + \ No newline at end of file diff --git a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-gateway/src/main/java/com/tencent/cloud/polaris/router/lane/gateway/LaneRouterGatewayApplication.java b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-gateway/src/main/java/com/tencent/cloud/polaris/router/lane/gateway/LaneRouterGatewayApplication.java new file mode 100644 index 0000000000..338dd7b016 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-gateway/src/main/java/com/tencent/cloud/polaris/router/lane/gateway/LaneRouterGatewayApplication.java @@ -0,0 +1,29 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.polaris.router.lane.gateway; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class LaneRouterGatewayApplication { + + public static void main(String[] args) { + SpringApplication.run(LaneRouterGatewayApplication.class, args); + } +} diff --git a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-gateway/src/main/resources/application.yml b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-gateway/src/main/resources/application.yml new file mode 100644 index 0000000000..1c02136d0d --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-gateway/src/main/resources/application.yml @@ -0,0 +1,39 @@ +server: + port: 48090 +spring: + application: + name: LaneRouterGatewayService + config: + import: optional:polaris + cloud: + polaris: + address: grpc://119.91.66.223:8091 + namespace: default + enabled: true + contract: + exposure: true + report: + enabled: true + stat: + enabled: true + port: 28081 + gateway: + discovery: + locator: + enabled: true + 'predicates[0]': + name: Path + args: + patterns: '''/'' + serviceId + ''/**''' + 'filters[0]': + name: RewritePath + args: + regexp: '''/'' + serviceId + ''/(?.*)''' + replacement: '''/$\{remaining}''' + routes: + - id: LaneRouterCallerService + uri: lb://LaneCallerService + predicates: + - Path=/LaneCallerService/** + filters: + - StripPrefix=1 diff --git a/spring-cloud-tencent-examples/pom.xml b/spring-cloud-tencent-examples/pom.xml index c7e0c5a944..df75f878b0 100644 --- a/spring-cloud-tencent-examples/pom.xml +++ b/spring-cloud-tencent-examples/pom.xml @@ -21,6 +21,8 @@ polaris-router-grayrelease-example polaris-router-featureenv-example quickstart-example + lossless-example + polaris-router-grayrelease-lane-example diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/main/java/com/tencent/cloud/plugin/discovery/adapter/config/NacosDiscoveryAdapterAutoConfiguration.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/main/java/com/tencent/cloud/plugin/discovery/adapter/config/NacosDiscoveryAdapterAutoConfiguration.java index d1332926b7..4f82638ad7 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/main/java/com/tencent/cloud/plugin/discovery/adapter/config/NacosDiscoveryAdapterAutoConfiguration.java +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/main/java/com/tencent/cloud/plugin/discovery/adapter/config/NacosDiscoveryAdapterAutoConfiguration.java @@ -19,8 +19,10 @@ package com.tencent.cloud.plugin.discovery.adapter.config; import com.tencent.cloud.plugin.discovery.adapter.transformer.NacosInstanceTransformer; +import com.tencent.cloud.plugin.discovery.adapter.transformer.NacosRegistrationTransformer; import com.tencent.cloud.polaris.loadbalancer.config.PolarisLoadBalancerAutoConfiguration; -import com.tencent.cloud.polaris.loadbalancer.transformer.InstanceTransformer; +import com.tencent.cloud.rpc.enhancement.transformer.InstanceTransformer; +import com.tencent.cloud.rpc.enhancement.transformer.RegistrationTransformer; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -41,9 +43,16 @@ public class NacosDiscoveryAdapterAutoConfiguration { @Bean @ConditionalOnMissingBean - @ConditionalOnClass(name = "com.alibaba.cloud.nacos.ribbon.NacosServer") + @ConditionalOnClass(name = "com.alibaba.cloud.nacos.NacosServiceInstance") public InstanceTransformer instanceTransformer() { return new NacosInstanceTransformer(); } + @Bean + @ConditionalOnMissingBean + @ConditionalOnClass(name = "com.alibaba.cloud.nacos.registry.NacosRegistration") + public RegistrationTransformer registrationTransformer() { + return new NacosRegistrationTransformer(); + } + } diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/main/java/com/tencent/cloud/plugin/discovery/adapter/transformer/NacosInstanceTransformer.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/main/java/com/tencent/cloud/plugin/discovery/adapter/transformer/NacosInstanceTransformer.java index db10472962..dbcc1c8bca 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/main/java/com/tencent/cloud/plugin/discovery/adapter/transformer/NacosInstanceTransformer.java +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/main/java/com/tencent/cloud/plugin/discovery/adapter/transformer/NacosInstanceTransformer.java @@ -18,10 +18,11 @@ package com.tencent.cloud.plugin.discovery.adapter.transformer; -import com.alibaba.cloud.nacos.ribbon.NacosServer; -import com.netflix.loadbalancer.Server; -import com.tencent.cloud.polaris.loadbalancer.transformer.InstanceTransformer; +import com.tencent.cloud.rpc.enhancement.transformer.InstanceTransformer; import com.tencent.polaris.api.pojo.DefaultInstance; +import org.apache.commons.lang.StringUtils; + +import org.springframework.cloud.client.ServiceInstance; /** * NacosInstanceTransformer. @@ -31,12 +32,20 @@ public class NacosInstanceTransformer implements InstanceTransformer { @Override - public void transformCustom(DefaultInstance instance, Server server) { - if ("com.alibaba.cloud.nacos.ribbon.NacosServer".equals(server.getClass().getName())) { - NacosServer nacosServer = (NacosServer) server; - instance.setWeight((int) (nacosServer.getInstance().getWeight() * 100)); - instance.setHealthy(nacosServer.getInstance().isHealthy()); - instance.setMetadata(nacosServer.getMetadata()); + public void transformCustom(DefaultInstance instance, ServiceInstance serviceInstance) { + if ("com.alibaba.cloud.nacos.NacosServiceInstance".equals(serviceInstance.getClass().getName())) { + String nacosWeight = serviceInstance.getMetadata().get("nacos.weight"); + instance.setWeight( + StringUtils.isBlank(nacosWeight) ? 100 : (int) Double.parseDouble(nacosWeight) * 100 + ); + String nacosHealthy = serviceInstance.getMetadata().get("nacos.healthy"); + instance.setHealthy( + !StringUtils.isBlank(nacosHealthy) && Boolean.parseBoolean(nacosHealthy) + ); + String nacosInstanceId = serviceInstance.getMetadata().get("nacos.instanceId"); + instance.setId(nacosInstanceId); + } } + } diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/main/java/com/tencent/cloud/plugin/discovery/adapter/transformer/NacosRegistrationTransformer.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/main/java/com/tencent/cloud/plugin/discovery/adapter/transformer/NacosRegistrationTransformer.java new file mode 100644 index 0000000000..d82558d272 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/main/java/com/tencent/cloud/plugin/discovery/adapter/transformer/NacosRegistrationTransformer.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.plugin.discovery.adapter.transformer; + +import com.alibaba.cloud.nacos.NacosDiscoveryProperties; +import com.alibaba.cloud.nacos.registry.NacosRegistration; +import com.tencent.cloud.rpc.enhancement.transformer.RegistrationTransformer; +import com.tencent.polaris.api.pojo.DefaultInstance; +import com.tencent.polaris.api.utils.StringUtils; + +import org.springframework.cloud.client.serviceregistry.Registration; + +public class NacosRegistrationTransformer implements RegistrationTransformer { + + @Override + public String getRegistry() { + return "nacos"; + } + + @Override + public void transformCustom(DefaultInstance instance, Registration registration) { + if (registration instanceof NacosRegistration) { + NacosRegistration nacosRegistration = (NacosRegistration) registration; + NacosDiscoveryProperties nacosDiscoveryProperties = nacosRegistration.getNacosDiscoveryProperties(); + String namespace = nacosDiscoveryProperties.getNamespace(); + if (StringUtils.isBlank(namespace)) { + namespace = "default"; + } + instance.setNamespace(namespace); + } + } + +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/test/java/com/tencent/cloud/plugin/discovery/adapter/config/NacosDiscoveryAdapterAutoConfigurationTest.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/test/java/com/tencent/cloud/plugin/discovery/adapter/config/NacosDiscoveryAdapterAutoConfigurationTest.java index 8689cb2f0c..b2338053eb 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/test/java/com/tencent/cloud/plugin/discovery/adapter/config/NacosDiscoveryAdapterAutoConfigurationTest.java +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/test/java/com/tencent/cloud/plugin/discovery/adapter/config/NacosDiscoveryAdapterAutoConfigurationTest.java @@ -19,6 +19,7 @@ package com.tencent.cloud.plugin.discovery.adapter.config; import com.tencent.cloud.plugin.discovery.adapter.transformer.NacosInstanceTransformer; +import com.tencent.cloud.rpc.enhancement.transformer.PolarisInstanceTransformer; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -44,6 +45,7 @@ public class NacosDiscoveryAdapterAutoConfigurationTest { public void testDefaultInitialization() { this.contextRunner.run(context -> { assertThat(context).hasSingleBean(NacosInstanceTransformer.class); + assertThat(context).doesNotHaveBean(PolarisInstanceTransformer.class); }); } } diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/test/java/com/tencent/cloud/plugin/discovery/adapter/config/NacosInstanceTransformerTest.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/test/java/com/tencent/cloud/plugin/discovery/adapter/config/NacosInstanceTransformerTest.java index 00dd8be77f..8320d6d902 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/test/java/com/tencent/cloud/plugin/discovery/adapter/config/NacosInstanceTransformerTest.java +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/test/java/com/tencent/cloud/plugin/discovery/adapter/config/NacosInstanceTransformerTest.java @@ -18,10 +18,12 @@ package com.tencent.cloud.plugin.discovery.adapter.config; -import com.alibaba.cloud.nacos.ribbon.NacosServer; +import java.util.HashMap; + +import com.alibaba.cloud.nacos.NacosServiceInstance; import com.tencent.cloud.common.util.ApplicationContextAwareUtils; import com.tencent.cloud.plugin.discovery.adapter.transformer.NacosInstanceTransformer; -import com.tencent.polaris.api.pojo.DefaultInstance; +import com.tencent.polaris.api.pojo.Instance; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.mockito.MockedStatic; @@ -51,14 +53,13 @@ public static void beforeAll() { @Test public void test() { NacosInstanceTransformer nacosInstanceTransformer = new NacosInstanceTransformer(); - - com.alibaba.nacos.api.naming.pojo.Instance nacosInstance = new com.alibaba.nacos.api.naming.pojo.Instance(); - nacosInstance.setHealthy(true); - NacosServer nacosServer = new NacosServer(nacosInstance); - - - DefaultInstance instance = new DefaultInstance(); - nacosInstanceTransformer.transformCustom(instance, nacosServer); + NacosServiceInstance nacosServiceInstance = new NacosServiceInstance(); + nacosServiceInstance.setMetadata(new HashMap() {{ + put("nacos.weight", "0.01"); + put("nacos.healthy", "true"); + put("nacos.instanceId", "xxx"); + }}); + Instance instance = nacosInstanceTransformer.transform(nacosServiceInstance); assertThat(instance.isHealthy()).isEqualTo(true); } diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/pom.xml b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/pom.xml index c1a4712a57..59a313be94 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/pom.xml +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/pom.xml @@ -58,13 +58,6 @@ true - - com.alibaba.cloud - spring-cloud-starter-alibaba-nacos-discovery - 2.2.9.RELEASE - true - - com.tencent.polaris polaris-test-mock-discovery diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/LosslessRegistryAspect.java b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/LosslessRegistryAspect.java index e0126d4587..c9af945765 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/LosslessRegistryAspect.java +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/LosslessRegistryAspect.java @@ -18,8 +18,9 @@ package com.tencent.cloud.plugin.lossless; import com.tencent.cloud.plugin.lossless.config.LosslessProperties; -import com.tencent.cloud.plugin.lossless.transfomer.DiscoveryNamespaceGetter; import com.tencent.cloud.polaris.context.PolarisSDKContextManager; +import com.tencent.cloud.polaris.context.config.PolarisContextProperties; +import com.tencent.cloud.rpc.enhancement.transformer.RegistrationTransformer; import com.tencent.polaris.api.pojo.BaseInstance; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; @@ -37,7 +38,7 @@ @Aspect public class LosslessRegistryAspect { - private ServiceRegistry serviceRegistry; + private ServiceRegistry serviceRegistry; private Registration registration; @@ -45,16 +46,19 @@ public class LosslessRegistryAspect { private PolarisSDKContextManager polarisSDKContextManager; - private DiscoveryNamespaceGetter discoveryNamespaceGetter; + private RegistrationTransformer registrationTransformer; - public LosslessRegistryAspect(ServiceRegistry serviceRegistry, Registration registration, - LosslessProperties losslessProperties, PolarisSDKContextManager polarisSDKContextManager, - DiscoveryNamespaceGetter discoveryNamespaceGetter) { + private PolarisContextProperties properties; + + public LosslessRegistryAspect(ServiceRegistry serviceRegistry, Registration registration, + PolarisContextProperties properties, LosslessProperties losslessProperties, + PolarisSDKContextManager polarisSDKContextManager, RegistrationTransformer registrationTransformer) { this.serviceRegistry = serviceRegistry; this.registration = registration; this.losslessProperties = losslessProperties; this.polarisSDKContextManager = polarisSDKContextManager; - this.discoveryNamespaceGetter = discoveryNamespaceGetter; + this.registrationTransformer = registrationTransformer; + this.properties = properties; } @Pointcut("execution(public * org.springframework.cloud.client.serviceregistry.ServiceRegistry.register(..))") @@ -71,7 +75,7 @@ public void deregisterPointcut() { public Object invokeRegister(ProceedingJoinPoint joinPoint) throws Throwable { // web started, get port from registration - BaseInstance instance = SpringCloudLosslessActionProvider.getBaseInstance(registration, discoveryNamespaceGetter); + BaseInstance instance = SpringCloudLosslessActionProvider.getBaseInstance(registration, registrationTransformer); Runnable registerAction = () -> executeJoinPoint(joinPoint); diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/SpringCloudLosslessActionProvider.java b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/SpringCloudLosslessActionProvider.java index 511721a5f4..a97df5960b 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/SpringCloudLosslessActionProvider.java +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/SpringCloudLosslessActionProvider.java @@ -22,11 +22,10 @@ import com.tencent.cloud.common.util.OkHttpUtil; import com.tencent.cloud.plugin.lossless.config.LosslessProperties; -import com.tencent.cloud.plugin.lossless.transfomer.DiscoveryNamespaceGetter; +import com.tencent.cloud.rpc.enhancement.transformer.RegistrationTransformer; import com.tencent.polaris.api.plugin.lossless.InstanceProperties; import com.tencent.polaris.api.plugin.lossless.LosslessActionProvider; import com.tencent.polaris.api.pojo.BaseInstance; -import com.tencent.polaris.api.pojo.DefaultBaseInstance; import com.tencent.polaris.api.utils.StringUtils; import org.springframework.cloud.client.serviceregistry.Registration; @@ -89,20 +88,8 @@ public boolean doHealthCheck() { losslessProperties.getHealthCheckPath(), headers); } - public static BaseInstance getBaseInstance(Registration registration, DiscoveryNamespaceGetter namespaceGetter) { - return getBaseInstance(registration, registration.getPort(), namespaceGetter); + public static BaseInstance getBaseInstance(Registration registration, RegistrationTransformer registrationTransformer) { + return registrationTransformer.transform(registration); } - public static BaseInstance getBaseInstance(Registration registration, Integer port, - DiscoveryNamespaceGetter namespaceGetter) { - DefaultBaseInstance baseInstance = new DefaultBaseInstance(); - if (namespaceGetter != null) { - baseInstance.setNamespace(namespaceGetter.getNamespace()); - } - baseInstance.setService(registration.getServiceId()); - // before web start, port in registration not init - baseInstance.setPort(port); - baseInstance.setHost(registration.getHost()); - return baseInstance; - } } diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/config/LosslessAutoConfiguration.java b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/config/LosslessAutoConfiguration.java index 5bab83f9f1..8b2cffe49f 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/config/LosslessAutoConfiguration.java +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/config/LosslessAutoConfiguration.java @@ -18,20 +18,12 @@ package com.tencent.cloud.plugin.lossless.config; -import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled; -import com.alibaba.cloud.nacos.NacosDiscoveryProperties; import com.tencent.cloud.plugin.lossless.LosslessRegistryAspect; -import com.tencent.cloud.plugin.lossless.transfomer.DiscoveryNamespaceGetter; -import com.tencent.cloud.plugin.lossless.transfomer.NacosDiscoveryNamespaceGetter; -import com.tencent.cloud.plugin.lossless.transfomer.PolarisDiscoveryNamespaceGetter; -import com.tencent.cloud.polaris.PolarisDiscoveryProperties; import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; import com.tencent.cloud.polaris.context.PolarisSDKContextManager; -import com.tencent.cloud.polaris.discovery.ConditionalOnPolarisDiscoveryEnabled; +import com.tencent.cloud.polaris.context.config.PolarisContextProperties; +import com.tencent.cloud.rpc.enhancement.transformer.RegistrationTransformer; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; @@ -51,34 +43,12 @@ public class LosslessAutoConfiguration { @Bean @ConditionalOnMissingBean - public LosslessRegistryAspect losslessRegistryAspect(ServiceRegistry serviceRegistry, Registration registration, - LosslessProperties losslessProperties, PolarisSDKContextManager polarisSDKContextManager, - @Autowired(required = false) DiscoveryNamespaceGetter discoveryNamespaceGetter) { - return new LosslessRegistryAspect(serviceRegistry, registration, losslessProperties, polarisSDKContextManager, discoveryNamespaceGetter); + public LosslessRegistryAspect losslessRegistryAspect( + ServiceRegistry serviceRegistry, Registration registration, PolarisContextProperties properties, + LosslessProperties losslessProperties, PolarisSDKContextManager polarisSDKContextManager, + RegistrationTransformer registrationTransformer) { + return new LosslessRegistryAspect(serviceRegistry, registration, properties, losslessProperties, + polarisSDKContextManager, registrationTransformer); } - @ConditionalOnClass(name = "com.alibaba.cloud.nacos.NacosDiscoveryProperties") - static class Nacos { - - @Bean - @ConditionalOnMissingBean - @ConditionalOnNacosDiscoveryEnabled - @ConditionalOnBean(NacosDiscoveryProperties.class) - public NacosDiscoveryNamespaceGetter nacosDiscoveryNamespaceGetter( - NacosDiscoveryProperties nacosDiscoveryProperties) { - return new NacosDiscoveryNamespaceGetter(nacosDiscoveryProperties); - } - } - - @ConditionalOnClass(name = "com.tencent.cloud.polaris.PolarisDiscoveryProperties") - static class Polaris { - @Bean - @ConditionalOnMissingBean - @ConditionalOnPolarisDiscoveryEnabled - @ConditionalOnBean(PolarisDiscoveryProperties.class) - public PolarisDiscoveryNamespaceGetter polarisDiscoveryNamespaceGetter( - PolarisDiscoveryProperties polarisDiscoveryProperties) { - return new PolarisDiscoveryNamespaceGetter(polarisDiscoveryProperties); - } - } } diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/transfomer/NacosDiscoveryNamespaceGetter.java b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/transfomer/NacosDiscoveryNamespaceGetter.java deleted file mode 100644 index 6d0b605c2c..0000000000 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/transfomer/NacosDiscoveryNamespaceGetter.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -package com.tencent.cloud.plugin.lossless.transfomer; - -import java.lang.reflect.Method; -import java.util.Properties; - -import com.alibaba.cloud.nacos.NacosDiscoveryProperties; -import com.alibaba.nacos.client.naming.utils.InitUtils; - -import org.springframework.util.ReflectionUtils; - -/** - * Discovery namespace getter for Nacos. - * - * @author Shedfree Wu - */ -public class NacosDiscoveryNamespaceGetter implements DiscoveryNamespaceGetter { - private String namespace; - - public NacosDiscoveryNamespaceGetter(NacosDiscoveryProperties nacosDiscoveryProperties) { - // getNacosProperties is private in low version of spring-cloud-starter-alibaba-nacos-discovery - Method method = ReflectionUtils.findMethod(NacosDiscoveryProperties.class, "getNacosProperties"); - method.setAccessible(true); - this.namespace = InitUtils.initNamespaceForNaming( - (Properties) ReflectionUtils.invokeMethod(method, nacosDiscoveryProperties)); - } - - public String getNamespace() { - return namespace; - } -} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/test/java/com/tencent/cloud/plugin/lossless/LosslessRegistryAspectTest.java b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/test/java/com/tencent/cloud/plugin/lossless/LosslessRegistryAspectTest.java index db43f84a76..45f656529b 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/test/java/com/tencent/cloud/plugin/lossless/LosslessRegistryAspectTest.java +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/test/java/com/tencent/cloud/plugin/lossless/LosslessRegistryAspectTest.java @@ -19,22 +19,15 @@ import java.util.Collections; -import com.alibaba.cloud.nacos.NacosServiceAutoConfiguration; -import com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration; -import com.alibaba.cloud.nacos.registry.NacosRegistration; -import com.alibaba.cloud.nacos.registry.NacosServiceRegistryAutoConfiguration; -import com.alibaba.cloud.nacos.util.UtilIPv6AutoConfiguration; import com.tencent.cloud.common.util.OkHttpUtil; import com.tencent.cloud.plugin.lossless.config.LosslessAutoConfiguration; import com.tencent.cloud.plugin.lossless.config.LosslessPropertiesBootstrapConfiguration; -import com.tencent.cloud.plugin.lossless.transfomer.DiscoveryNamespaceGetter; import com.tencent.cloud.polaris.context.PolarisSDKContextManager; import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration; import com.tencent.cloud.polaris.registry.PolarisRegistration; import com.tencent.cloud.polaris.registry.PolarisServiceRegistry; -import com.tencent.polaris.api.pojo.BaseInstance; import com.tencent.polaris.api.pojo.ServiceKey; import com.tencent.polaris.test.mock.discovery.NamingServer; import org.junit.jupiter.api.AfterAll; @@ -46,10 +39,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration; -import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationUtils; -import org.springframework.cloud.client.serviceregistry.Registration; -import org.springframework.cloud.commons.util.UtilAutoConfiguration; import org.springframework.context.annotation.Configuration; import static org.assertj.core.api.Assertions.assertThat; @@ -72,8 +62,6 @@ public class LosslessRegistryAspectTest { private static int LOSSLESS_PORT_1 = 28083; - private static int NACOS_LOSSLESS_PORT = 28093; - private static NamingServer namingServer; private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() @@ -116,31 +104,6 @@ public class LosslessRegistryAspectTest { .withPropertyValues("spring.cloud.polaris.discovery.namespace=" + NAMESPACE_TEST) .withPropertyValues("spring.cloud.polaris.discovery.token=xxxxxx"); - private final WebApplicationContextRunner nacosContextRunner = new WebApplicationContextRunner() - .withConfiguration(AutoConfigurations.of( - AutoServiceRegistrationProperties.class, - UtilAutoConfiguration.class, - UtilIPv6AutoConfiguration.class, - NacosDiscoveryAutoConfiguration.class, - NacosServiceAutoConfiguration.class, - NacosServiceRegistryAutoConfiguration.class, - LosslessAutoConfiguration.class, - LosslessPropertiesBootstrapConfiguration.class, - PolarisContextAutoConfiguration.class) - ) - .withPropertyValues("spring.cloud.nacos.discovery.enabled=true") - .withPropertyValues("spring.cloud.nacos.discovery.namespace=" + NAMESPACE_TEST) - .withPropertyValues("spring.cloud.polaris.discovery.enabled=false") - .withPropertyValues("spring.cloud.polaris.lossless.delayRegisterInterval=5000") - .withPropertyValues("spring.cloud.polaris.lossless.healthCheckPath=") - .withPropertyValues("spring.cloud.polaris.lossless.port=" + NACOS_LOSSLESS_PORT) - .withPropertyValues("spring.application.name=" + SERVICE_PROVIDER) - .withPropertyValues("server.port=" + APPLICATION_PORT) - .withPropertyValues("spring.cloud.polaris.localIpAddress=" + HOST) - .withPropertyValues("spring.cloud.polaris.localPort=" + APPLICATION_PORT) - .withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081") - .withPropertyValues("spring.cloud.polaris.discovery.token=xxxxxx"); - @BeforeAll static void beforeAll() throws Exception { namingServer = NamingServer.startNamingServer(10081); @@ -220,22 +183,6 @@ public void testRegister2() { }); } - @Test - public void testNaocsRegister() { - this.nacosContextRunner.run(context -> { - - DiscoveryNamespaceGetter discoveryNamespaceGetter = context.getBean(DiscoveryNamespaceGetter.class); - Registration registration = context.getBean(Registration.class); - - assertThat(registration instanceof NacosRegistration).isTrue(); - assertThat(discoveryNamespaceGetter.getNamespace()).isEqualTo(NAMESPACE_TEST); - - BaseInstance baseInstance = SpringCloudLosslessActionProvider.getBaseInstance(registration, discoveryNamespaceGetter); - assertThat(baseInstance.getNamespace()).isEqualTo(NAMESPACE_TEST); - }); - } - - @Configuration @EnableAutoConfiguration static class PolarisPropertiesConfiguration { 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 102f931c6b..ea0825a444 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 @@ -35,6 +35,10 @@ import com.tencent.cloud.rpc.enhancement.resttemplate.EnhancedRestTemplateInterceptor; import com.tencent.cloud.rpc.enhancement.resttemplate.PolarisLoadBalancerRequestTransformer; import com.tencent.cloud.rpc.enhancement.scg.EnhancedGatewayGlobalFilter; +import com.tencent.cloud.rpc.enhancement.transformer.InstanceTransformer; +import com.tencent.cloud.rpc.enhancement.transformer.PolarisInstanceTransformer; +import com.tencent.cloud.rpc.enhancement.transformer.PolarisRegistrationTransformer; +import com.tencent.cloud.rpc.enhancement.transformer.RegistrationTransformer; import com.tencent.cloud.rpc.enhancement.webclient.EnhancedWebClientExchangeFilterFunction; import com.tencent.cloud.rpc.enhancement.webclient.PolarisLoadBalancerClientRequestTransformer; import com.tencent.cloud.rpc.enhancement.webclient.RibbonLoadBalancerClientAspect; @@ -84,6 +88,20 @@ @AutoConfigureAfter(PolarisContextAutoConfiguration.class) public class RpcEnhancementAutoConfiguration { + @Bean + @ConditionalOnMissingBean + @ConditionalOnClass(name = "com.tencent.cloud.common.pojo.PolarisServiceInstance") + public InstanceTransformer instanceTransformer() { + return new PolarisInstanceTransformer(); + } + + @Bean + @ConditionalOnMissingBean + @ConditionalOnClass(name = "com.tencent.cloud.polaris.registry.PolarisRegistration") + public RegistrationTransformer registrationTransformer() { + return new PolarisRegistrationTransformer(); + } + @Bean @Lazy public EnhancedPluginRunner enhancedFeignPluginRunner( diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/transformer/InstanceTransformer.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/transformer/InstanceTransformer.java new file mode 100644 index 0000000000..13c19dfb04 --- /dev/null +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/transformer/InstanceTransformer.java @@ -0,0 +1,53 @@ +/* + * 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.rpc.enhancement.transformer; + +import com.tencent.cloud.common.metadata.MetadataContext; +import com.tencent.polaris.api.pojo.DefaultInstance; +import com.tencent.polaris.api.pojo.Instance; + +import org.springframework.cloud.client.ServiceInstance; + +/** + * InstanceTransformer. + * + * @author sean yu + */ +public interface InstanceTransformer { + + default Instance transform(ServiceInstance serviceInstance) { + DefaultInstance instance = new DefaultInstance(); + transformDefault(instance, serviceInstance); + transformCustom(instance, serviceInstance); + return instance; + } + + default void transformDefault(DefaultInstance instance, ServiceInstance serviceInstance) { + instance.setNamespace(MetadataContext.LOCAL_NAMESPACE); + instance.setService(serviceInstance.getServiceId()); + instance.setProtocol(serviceInstance.getScheme()); + instance.setId(serviceInstance.getInstanceId()); + instance.setHost(serviceInstance.getHost()); + instance.setPort(serviceInstance.getPort()); + instance.setMetadata(serviceInstance.getMetadata()); + } + + void transformCustom(DefaultInstance instance, ServiceInstance serviceInstance); + +} diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/transformer/PolarisInstanceTransformer.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/transformer/PolarisInstanceTransformer.java new file mode 100644 index 0000000000..a44ef3a56f --- /dev/null +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/transformer/PolarisInstanceTransformer.java @@ -0,0 +1,44 @@ +/* + * 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.rpc.enhancement.transformer; + +import com.tencent.cloud.common.pojo.PolarisServiceInstance; +import com.tencent.polaris.api.pojo.DefaultInstance; + +import org.springframework.cloud.client.ServiceInstance; + +/** + * PolarisInstanceTransformer. + * + * @author sean yu + */ +public class PolarisInstanceTransformer implements InstanceTransformer { + + @Override + public void transformCustom(DefaultInstance instance, ServiceInstance serviceInstance) { + if (serviceInstance instanceof PolarisServiceInstance) { + PolarisServiceInstance polarisServiceInstance = (PolarisServiceInstance) serviceInstance; + instance.setRegion(polarisServiceInstance.getPolarisInstance().getRegion()); + instance.setZone(polarisServiceInstance.getPolarisInstance().getZone()); + instance.setCampus(polarisServiceInstance.getPolarisInstance().getCampus()); + instance.setWeight(polarisServiceInstance.getPolarisInstance().getWeight()); + } + } + +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/transfomer/DiscoveryNamespaceGetter.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/transformer/PolarisRegistrationTransformer.java similarity index 78% rename from spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/transfomer/DiscoveryNamespaceGetter.java rename to spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/transformer/PolarisRegistrationTransformer.java index d23a67d0bf..20df4f1830 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-lossless-plugin/src/main/java/com/tencent/cloud/plugin/lossless/transfomer/DiscoveryNamespaceGetter.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/transformer/PolarisRegistrationTransformer.java @@ -13,16 +13,15 @@ * 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.plugin.lossless.transfomer; +package com.tencent.cloud.rpc.enhancement.transformer; -/** - * Interface for discovery namespace getter. - * - * @author Shedfree Wu - */ -public interface DiscoveryNamespaceGetter { +public class PolarisRegistrationTransformer implements RegistrationTransformer { - String getNamespace(); + @Override + public String getRegistry() { + return "polaris"; + } } diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/transformer/RegistrationTransformer.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/transformer/RegistrationTransformer.java new file mode 100644 index 0000000000..7996ebd78b --- /dev/null +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/transformer/RegistrationTransformer.java @@ -0,0 +1,58 @@ +/* + * 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.rpc.enhancement.transformer; + +import com.tencent.cloud.common.metadata.MetadataContext; +import com.tencent.polaris.api.pojo.DefaultInstance; +import com.tencent.polaris.api.pojo.Instance; + +import org.springframework.cloud.client.serviceregistry.Registration; + + +/** + * RegistrationTransformer extensions to adapt 3rd registration to polaris instance. + * + * @author andrew shan + */ +public interface RegistrationTransformer { + + String getRegistry(); + + default Instance transform(Registration registration) { + DefaultInstance instance = new DefaultInstance(); + transformDefault(instance, registration); + transformCustom(instance, registration); + return instance; + } + + default void transformDefault(DefaultInstance instance, Registration registration) { + instance.setRegistry(getRegistry()); + instance.setNamespace(MetadataContext.LOCAL_NAMESPACE); + instance.setService(registration.getServiceId()); + instance.setProtocol(registration.getScheme()); + instance.setId(registration.getInstanceId()); + instance.setHost(registration.getHost()); + instance.setPort(registration.getPort()); + instance.setMetadata(registration.getMetadata()); + } + + default void transformCustom(DefaultInstance instance, Registration registration) { + + } +} From 4144d81f74355291a43a4051c82767091ff191d0 Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Thu, 9 May 2024 15:24:01 +0800 Subject: [PATCH 2/6] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0f43caaa0..f925d3b3f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,3 +9,4 @@ - [feat:add polaris ThreadLocal plugin.](https://github.com/Tencent/spring-cloud-tencent/pull/1255) - [feat:upgrade jackson version.](https://github.com/Tencent/spring-cloud-tencent/pull/1257) - [fix:fix wrong report when using Zuul with instance not found exception.](https://github.com/Tencent/spring-cloud-tencent/pull/1283) +- [feat: merge lane router and lossless features from 2023](https://github.com/Tencent/spring-cloud-tencent/pull/1288) \ No newline at end of file From e4577ea9eafb00e207c345df63b2ea94fe17a091 Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Thu, 9 May 2024 16:37:48 +0800 Subject: [PATCH 3/6] fix: change application.yml to bootstrap.yml --- .../src/main/resources/{application.yml => bootstrap.yml} | 0 .../src/main/resources/{application.yml => bootstrap.yml} | 0 .../src/main/resources/{application.yml => bootstrap.yml} | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/src/main/resources/{application.yml => bootstrap.yml} (100%) rename spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/resources/{application.yml => bootstrap.yml} (100%) rename spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-gateway/src/main/resources/{application.yml => bootstrap.yml} (100%) diff --git a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/src/main/resources/application.yml b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/src/main/resources/bootstrap.yml similarity index 100% rename from spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/src/main/resources/application.yml rename to spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-callee-service/src/main/resources/bootstrap.yml diff --git a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/resources/application.yml b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/resources/bootstrap.yml similarity index 100% rename from spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/resources/application.yml rename to spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-caller-service/src/main/resources/bootstrap.yml diff --git a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-gateway/src/main/resources/application.yml b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-gateway/src/main/resources/bootstrap.yml similarity index 100% rename from spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-gateway/src/main/resources/application.yml rename to spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-gateway/src/main/resources/bootstrap.yml From 3bc8051674386b1c156b6099d743846d83199893 Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Fri, 10 May 2024 12:16:25 +0800 Subject: [PATCH 4/6] fix: fix npe when add circuitbreak module without feign.hystrix.enable=true --- ...sFeignCircuitBreakerInvocationHandler.java | 59 ++++++++++--------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignCircuitBreakerInvocationHandler.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignCircuitBreakerInvocationHandler.java index b8419a29cb..0379c9977b 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignCircuitBreakerInvocationHandler.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignCircuitBreakerInvocationHandler.java @@ -113,36 +113,41 @@ else if ("hashCode".equals(method.getName())) { else if ("toString".equals(method.getName())) { return toString(); } - - String circuitName = circuitBreakerNameResolver.resolveCircuitBreakerName(feignClientName, target, method); - CircuitBreaker circuitBreaker = factory.create(circuitName); Supplier supplier = asSupplier(method, args); - Function fallbackFunction; - if (this.nullableFallbackFactory != null) { - fallbackFunction = throwable -> { - Object fallback = this.nullableFallbackFactory.create(throwable); - try { - return this.fallbackMethodMap.get(method).invoke(fallback, args); - } - catch (Exception exception) { - unwrapAndRethrow(exception); - } - return null; - }; + if (circuitBreakerNameResolver != null) { + String circuitName = circuitBreakerNameResolver.resolveCircuitBreakerName(feignClientName, target, method); + CircuitBreaker circuitBreaker = factory.create(circuitName); + + Function fallbackFunction; + if (this.nullableFallbackFactory != null) { + fallbackFunction = throwable -> { + Object fallback = this.nullableFallbackFactory.create(throwable); + try { + return this.fallbackMethodMap.get(method).invoke(fallback, args); + } + catch (Exception exception) { + unwrapAndRethrow(exception); + } + return null; + }; + } + else { + fallbackFunction = throwable -> { + PolarisCircuitBreakerFallbackFactory.DefaultFallback fallback = + (PolarisCircuitBreakerFallbackFactory.DefaultFallback) new PolarisCircuitBreakerFallbackFactory(this.decoder).create(throwable); + return fallback.fallback(method); + }; + } + try { + return circuitBreaker.run(supplier, fallbackFunction); + } + catch (FallbackWrapperException e) { + // unwrap And Rethrow + throw e.getCause(); + } } else { - fallbackFunction = throwable -> { - PolarisCircuitBreakerFallbackFactory.DefaultFallback fallback = - (PolarisCircuitBreakerFallbackFactory.DefaultFallback) new PolarisCircuitBreakerFallbackFactory(this.decoder).create(throwable); - return fallback.fallback(method); - }; - } - try { - return circuitBreaker.run(supplier, fallbackFunction); - } - catch (FallbackWrapperException e) { - // unwrap And Rethrow - throw e.getCause(); + return supplier.get(); } } From 0292af05a2a1c2b394483db0f3f6909d7593f275 Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Fri, 10 May 2024 12:24:27 +0800 Subject: [PATCH 5/6] Update CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f925d3b3f7..48d92ae6d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,4 +9,5 @@ - [feat:add polaris ThreadLocal plugin.](https://github.com/Tencent/spring-cloud-tencent/pull/1255) - [feat:upgrade jackson version.](https://github.com/Tencent/spring-cloud-tencent/pull/1257) - [fix:fix wrong report when using Zuul with instance not found exception.](https://github.com/Tencent/spring-cloud-tencent/pull/1283) -- [feat: merge lane router and lossless features from 2023](https://github.com/Tencent/spring-cloud-tencent/pull/1288) \ No newline at end of file +- [feat: merge lane router and lossless features from 2023](https://github.com/Tencent/spring-cloud-tencent/pull/1288) +- [fix: fix npe when add circuitbreak module without feign.hystrix.enable=true](https://github.com/Tencent/spring-cloud-tencent/pull/1292) \ No newline at end of file From db6d89865c71e0ad0ca1d7d819df135b4dd6d9a3 Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Wed, 15 May 2024 14:34:38 +0800 Subject: [PATCH 6/6] feat: support spring.cloud.polaris.router.nearby-router.matchLevel to config nearyby level --- .../polaris/router/RouterConfigModifier.java | 60 +++++++++++++++++ .../RouterBootstrapAutoConfiguration.java | 36 ++++++++++ ...RouterConfigModifierAutoConfiguration.java | 47 +++++++++++++ .../PolarisNearByRouterProperties.java | 11 +++ ...itional-spring-configuration-metadata.json | 6 ++ .../main/resources/META-INF/spring.factories | 2 + .../RouterBootstrapAutoConfigurationTest.java | 67 +++++++++++++++++++ .../PolarisNearByRouterPropertiesTest.java | 2 +- 8 files changed, 230 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/RouterConfigModifier.java create mode 100644 spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterBootstrapAutoConfiguration.java create mode 100644 spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterConfigModifierAutoConfiguration.java create mode 100644 spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/RouterBootstrapAutoConfigurationTest.java diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/RouterConfigModifier.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/RouterConfigModifier.java new file mode 100644 index 0000000000..4d30dab48c --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/RouterConfigModifier.java @@ -0,0 +1,60 @@ +/* + * 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.router; + +import com.tencent.cloud.common.constant.OrderConstant; +import com.tencent.cloud.polaris.context.PolarisConfigModifier; +import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties; +import com.tencent.polaris.api.config.consumer.ServiceRouterConfig; +import com.tencent.polaris.api.plugin.route.LocationLevel; +import com.tencent.polaris.factory.config.ConfigurationImpl; +import com.tencent.polaris.plugins.router.nearby.NearbyRouterConfig; +import org.apache.commons.lang.StringUtils; + +/** + * RouterConfigModifier. + * + * @author sean yu + */ +public class RouterConfigModifier implements PolarisConfigModifier { + + private final PolarisNearByRouterProperties polarisNearByRouterProperties; + + public RouterConfigModifier(PolarisNearByRouterProperties polarisNearByRouterProperties) { + this.polarisNearByRouterProperties = polarisNearByRouterProperties; + } + + @Override + public void modify(ConfigurationImpl configuration) { + if (StringUtils.isNotBlank(polarisNearByRouterProperties.getMatchLevel())) { + LocationLevel locationLevel = LocationLevel.valueOf(polarisNearByRouterProperties.getMatchLevel()); + NearbyRouterConfig nearbyRouterConfig = configuration.getConsumer().getServiceRouter().getPluginConfig( + ServiceRouterConfig.DEFAULT_ROUTER_NEARBY, NearbyRouterConfig.class); + nearbyRouterConfig.setMatchLevel(locationLevel); + configuration.getConsumer().getServiceRouter() + .setPluginConfig(ServiceRouterConfig.DEFAULT_ROUTER_NEARBY, nearbyRouterConfig); + } + + } + + @Override + public int getOrder() { + return OrderConstant.Modifier.ROUTER_ORDER; + } +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterBootstrapAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterBootstrapAutoConfiguration.java new file mode 100644 index 0000000000..2d02b2a282 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterBootstrapAutoConfiguration.java @@ -0,0 +1,36 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.router.config; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +/** + * RouterBootstrapAutoConfiguration. + * + * @author sean yu + */ +@Configuration(proxyBeanMethods = false) +@ConditionalOnProperty("spring.cloud.polaris.enabled") +@Import(RouterConfigModifierAutoConfiguration.class) +public class RouterBootstrapAutoConfiguration { + + +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterConfigModifierAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterConfigModifierAutoConfiguration.java new file mode 100644 index 0000000000..e1de4a37f7 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterConfigModifierAutoConfiguration.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.polaris.router.config; + +import com.tencent.cloud.polaris.router.RouterConfigModifier; +import com.tencent.cloud.polaris.router.config.properties.PolarisMetadataRouterProperties; +import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties; +import com.tencent.cloud.polaris.router.config.properties.PolarisRuleBasedRouterProperties; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +/** + * RouterConfigModifierAutoConfiguration. + * + * @author sean yu + */ +@Configuration(proxyBeanMethods = false) +@ConditionalOnPolarisRouterEnabled +@Import({PolarisNearByRouterProperties.class, PolarisMetadataRouterProperties.class, PolarisRuleBasedRouterProperties.class}) +public class RouterConfigModifierAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public RouterConfigModifier routerConfigModifier(PolarisNearByRouterProperties polarisNearByRouterProperties) { + return new RouterConfigModifier(polarisNearByRouterProperties); + } + +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterProperties.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterProperties.java index 3cd69ca12d..1c6a76a60e 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterProperties.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterProperties.java @@ -30,6 +30,8 @@ public class PolarisNearByRouterProperties { private boolean enabled = true; + private String matchLevel; + public boolean isEnabled() { return enabled; } @@ -38,10 +40,19 @@ public void setEnabled(boolean enabled) { this.enabled = enabled; } + public String getMatchLevel() { + return matchLevel; + } + + public void setMatchLevel(String matchLevel) { + this.matchLevel = matchLevel; + } + @Override public String toString() { return "PolarisNearByRouterProperties{" + "enabled=" + enabled + + ", matchLevel='" + matchLevel + '\'' + '}'; } } diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 0c935af6e4..631bd8e42e 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -12,6 +12,12 @@ "defaultValue": true, "description": "the switch for near by router." }, + { + "name": "spring.cloud.polaris.router.nearby-router.matchLevel", + "type": "java.lang.String", + "defaultValue": "zone", + "description": "the match level for nearby router, options can be region/zone/campus." + }, { "name": "spring.cloud.polaris.router.rule-router.enabled", "type": "java.lang.Boolean", diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/spring.factories b/spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/spring.factories index 75f7939ca6..db8b9bec4d 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/spring.factories @@ -2,3 +2,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.tencent.cloud.polaris.router.config.RouterAutoConfiguration,\ com.tencent.cloud.polaris.router.config.FeignAutoConfiguration,\ com.tencent.cloud.polaris.router.endpoint.PolarisRouterEndpointAutoConfiguration +org.springframework.cloud.bootstrap.BootstrapConfiguration=\ + com.tencent.cloud.polaris.router.config.RouterBootstrapAutoConfiguration \ No newline at end of file diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/RouterBootstrapAutoConfigurationTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/RouterBootstrapAutoConfigurationTest.java new file mode 100644 index 0000000000..f10d9ada45 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/RouterBootstrapAutoConfigurationTest.java @@ -0,0 +1,67 @@ +/* + * 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.router.config; + +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; +import com.tencent.cloud.polaris.router.RouterConfigModifier; +import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties; +import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementAutoConfiguration; +import com.tencent.polaris.api.config.Configuration; +import com.tencent.polaris.api.config.consumer.ServiceRouterConfig; +import com.tencent.polaris.factory.ConfigAPIFactory; +import com.tencent.polaris.factory.config.ConfigurationImpl; +import com.tencent.polaris.plugins.router.nearby.NearbyRouterConfig; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * RouterBootstrapAutoConfigurationTest. + * + * @author sean yu + */ +public class RouterBootstrapAutoConfigurationTest { + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of( + PolarisContextAutoConfiguration.class, + PolarisNearByRouterProperties.class, + RpcEnhancementAutoConfiguration.class, + RouterBootstrapAutoConfiguration.class)) + .withPropertyValues("spring.cloud.polaris.enabled=true") + .withPropertyValues("spring.cloud.polaris.router.nearby-router.matchLevel=campus") + .withPropertyValues("spring.cloud.polaris.circuitbreaker.enabled=true"); + + @Test + public void testDefaultInitialization() { + this.contextRunner.run(context -> { + assertThat(context).hasSingleBean(RouterConfigModifier.class); + RouterConfigModifier routerConfigModifier = (RouterConfigModifier) context.getBean("routerConfigModifier"); + Configuration configuration = ConfigAPIFactory.defaultConfig(); + routerConfigModifier.modify((ConfigurationImpl) configuration); + NearbyRouterConfig nearbyRouterConfig = configuration.getConsumer().getServiceRouter().getPluginConfig( + ServiceRouterConfig.DEFAULT_ROUTER_NEARBY, NearbyRouterConfig.class); + Assertions.assertEquals("campus", nearbyRouterConfig.getMatchLevel().name()); + }); + } +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterPropertiesTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterPropertiesTest.java index 572b6f92b8..379fbf7e97 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterPropertiesTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterPropertiesTest.java @@ -49,6 +49,6 @@ public void setEnabled() { @Test public void testToString() { assertThat(properties.toString()) - .isEqualTo("PolarisNearByRouterProperties{enabled=true}"); + .isEqualTo("PolarisNearByRouterProperties{enabled=true, matchLevel='null'}"); } }