diff --git a/CHANGELOG.md b/CHANGELOG.md index a87d2084..090f66c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,3 +3,4 @@ - feature: support reactive discovery client health indicator. - feature: Enhance default configuration to support `application*.yaml` and `bootstrap*.yaml`. +- feat:adapt for nacos instance. diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/endpoint/PolarisDiscoveryEndpointTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/endpoint/PolarisDiscoveryEndpointTest.java index 14152df6..5a513cf4 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/endpoint/PolarisDiscoveryEndpointTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/endpoint/PolarisDiscoveryEndpointTest.java @@ -16,6 +16,7 @@ */ package com.tencent.cloud.polaris.endpoint; +import java.util.Collections; import java.util.Map; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; @@ -38,6 +39,8 @@ import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST; import static com.tencent.polaris.test.common.Consts.PORT; import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; /** * Test for polaris discovery endpoint. @@ -48,7 +51,7 @@ public class PolarisDiscoveryEndpointTest { private static NamingServer namingServer; - private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() + private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() .withConfiguration(AutoConfigurations.of( PolarisPropertiesConfiguration.class, PolarisDiscoveryClientConfiguration.class, @@ -79,12 +82,17 @@ public class PolarisDiscoveryEndpointTest { .getBean(PolarisDiscoveryProperties.class); DiscoveryClient discoveryClient = context .getBean(PolarisDiscoveryClient.class); + PolarisDiscoveryHandler polarisDiscoveryHandler = context.getBean(PolarisDiscoveryHandler.class); PolarisDiscoveryEndpoint polarisDiscoveryEndpoint = new PolarisDiscoveryEndpoint(polarisDiscoveryProperties, discoveryClient, polarisDiscoveryHandler); - Map mapInfo = polarisDiscoveryEndpoint.polarisDiscovery("java_provider_test"); - assertThat(polarisDiscoveryProperties).isEqualTo(mapInfo.get("PolarisDiscoveryProperties")); + + DiscoveryClient discoveryClient1 = mock(DiscoveryClient.class); + doReturn(Collections.singletonList("xx")).when(discoveryClient1).getServices(); + PolarisDiscoveryEndpoint polarisDiscoveryEndpoint1 = new PolarisDiscoveryEndpoint(polarisDiscoveryProperties, discoveryClient1, polarisDiscoveryHandler); + Map mapInfo2 = polarisDiscoveryEndpoint1.polarisDiscovery(null); + assertThat(polarisDiscoveryProperties).isEqualTo(mapInfo2.get("PolarisDiscoveryProperties")); }); } diff --git a/spring-cloud-tencent-coverage/pom.xml b/spring-cloud-tencent-coverage/pom.xml index bc873aae..ee44f02f 100644 --- a/spring-cloud-tencent-coverage/pom.xml +++ b/spring-cloud-tencent-coverage/pom.xml @@ -78,6 +78,11 @@ com.tencent.cloud spring-cloud-tencent-gateway-plugin + + + com.tencent.cloud + spring-cloud-starter-tencent-discovery-adapter-plugin + diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 898bb163..a506ecad 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -180,6 +180,12 @@ ${revision} + + com.tencent.cloud + spring-cloud-starter-tencent-discovery-adapter-plugin + ${revision} + + com.google.guava diff --git a/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-a/pom.xml b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-a/pom.xml new file mode 100644 index 00000000..d15b8dca --- /dev/null +++ b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-a/pom.xml @@ -0,0 +1,50 @@ + + + + multiple-discovery-nacos-example + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + nacos-callee-service-a + + + + org.springframework.boot + spring-boot-starter-web + + + + + + + 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/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-a/src/main/java/com/tencent/cloud/multiple/discovery/nacos/NacosCalleeServiceA.java b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-a/src/main/java/com/tencent/cloud/multiple/discovery/nacos/NacosCalleeServiceA.java new file mode 100644 index 00000000..3e2152dd --- /dev/null +++ b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-a/src/main/java/com/tencent/cloud/multiple/discovery/nacos/NacosCalleeServiceA.java @@ -0,0 +1,34 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.multiple.discovery.nacos; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Circuit breaker example callee application. + * + * @author Haotian Zhang + */ +@SpringBootApplication +public class NacosCalleeServiceA { + + public static void main(String[] args) { + SpringApplication.run(NacosCalleeServiceA.class, args); + } +} diff --git a/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-a/src/main/java/com/tencent/cloud/multiple/discovery/nacos/NacosCalleeServiceAController.java b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-a/src/main/java/com/tencent/cloud/multiple/discovery/nacos/NacosCalleeServiceAController.java new file mode 100644 index 00000000..f325e14e --- /dev/null +++ b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-a/src/main/java/com/tencent/cloud/multiple/discovery/nacos/NacosCalleeServiceAController.java @@ -0,0 +1,50 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.multiple.discovery.nacos; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * Service B Controller. + * + * @author Haotian Zhang + */ +@RestController +@RequestMapping("/example/service/b") +public class NacosCalleeServiceAController { + + /** + * Get service information. + * + * @return service information + */ + @GetMapping("/info") + public String info() { + return "hello world ! I'm a service B1"; + } + + @GetMapping("/health") + public String health() { + System.out.println("health check: 200 instance"); + return "hello world ! I'm a service B1"; + } + +} diff --git a/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-a/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-a/src/main/resources/bootstrap.yml new file mode 100644 index 00000000..23d44a7d --- /dev/null +++ b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-a/src/main/resources/bootstrap.yml @@ -0,0 +1,16 @@ +server: + port: 48082 +spring: + application: + name: NacosCalleeService + cloud: + polaris: + address: grpc://119.91.66.223:8091 + namespace: default + enabled: true + stat: + enabled: true + port: 28083 + nacos: + discovery: + server-addr: 127.0.0.1:8848 diff --git a/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-b/pom.xml b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-b/pom.xml new file mode 100644 index 00000000..ce0cd96c --- /dev/null +++ b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-b/pom.xml @@ -0,0 +1,51 @@ + + + + multiple-discovery-nacos-example + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + nacos-callee-service-b + + + + org.springframework.boot + spring-boot-starter-web + + + + + + + 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/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-b/src/main/java/com/tencent/cloud/multiple/discovery/nacos/NacosCalleeServiceB.java b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-b/src/main/java/com/tencent/cloud/multiple/discovery/nacos/NacosCalleeServiceB.java new file mode 100644 index 00000000..8e4aa86d --- /dev/null +++ b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-b/src/main/java/com/tencent/cloud/multiple/discovery/nacos/NacosCalleeServiceB.java @@ -0,0 +1,34 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.multiple.discovery.nacos; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Circuit breaker example callee application. + * + * @author Haotian Zhang + */ +@SpringBootApplication +public class NacosCalleeServiceB { + + public static void main(String[] args) { + SpringApplication.run(NacosCalleeServiceB.class, args); + } +} diff --git a/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-b/src/main/java/com/tencent/cloud/multiple/discovery/nacos/NacosCalleeServiceBController.java b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-b/src/main/java/com/tencent/cloud/multiple/discovery/nacos/NacosCalleeServiceBController.java new file mode 100644 index 00000000..cae686fb --- /dev/null +++ b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-b/src/main/java/com/tencent/cloud/multiple/discovery/nacos/NacosCalleeServiceBController.java @@ -0,0 +1,54 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.multiple.discovery.nacos; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +/** + * Service B Controller. + * + * @author Haotian Zhang + */ +@RestController +@RequestMapping("/example/service/b") +public class NacosCalleeServiceBController { + + /** + * Get service information. + * + * @return service information + */ + @GetMapping("/info") + @ResponseStatus(code = HttpStatus.BAD_GATEWAY) + public String info() { + return "BAD_GATEWAY ! from service B2"; + } + + @GetMapping("/health") + @ResponseStatus(value = HttpStatus.BAD_GATEWAY, reason = "failed for call my service") + public String health() { + System.out.println("health check: 502 instance"); + return "hello world ! I'm a service B1"; + } + +} diff --git a/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-b/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-b/src/main/resources/bootstrap.yml new file mode 100644 index 00000000..e176c5b9 --- /dev/null +++ b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-callee-service-b/src/main/resources/bootstrap.yml @@ -0,0 +1,17 @@ +server: + port: 48081 +spring: + application: + name: NacosCalleeService + cloud: + polaris: + address: grpc://119.91.66.223:8091 + namespace: default + enabled: true + stat: + enabled: true + port: 28083 + nacos: + discovery: + server-addr: 127.0.0.1:8848 + diff --git a/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-discovery-caller/pom.xml b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-discovery-caller/pom.xml new file mode 100644 index 00000000..8b70623c --- /dev/null +++ b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-discovery-caller/pom.xml @@ -0,0 +1,65 @@ + + + + multiple-discovery-nacos-example + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + nacos-discovery-caller + + + + org.springframework.boot + spring-boot-starter-web + + + + + + + + + com.tencent.cloud + spring-cloud-starter-tencent-discovery-adapter-plugin + + + + 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/multiple-discovery-example/multiple-discovery-nacos-example/nacos-discovery-caller/src/main/java/com/tencent/cloud/multiple/discovery/nacos/NacosCallerService.java b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-discovery-caller/src/main/java/com/tencent/cloud/multiple/discovery/nacos/NacosCallerService.java new file mode 100644 index 00000000..dabb5ac9 --- /dev/null +++ b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-discovery-caller/src/main/java/com/tencent/cloud/multiple/discovery/nacos/NacosCallerService.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.multiple.discovery.nacos; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.openfeign.EnableFeignClients; + +/** + * Circuit breaker example callee application. + * + * @author Haotian Zhang + */ +@SpringBootApplication +@EnableFeignClients +public class NacosCallerService { + + public static void main(String[] args) { + SpringApplication.run(NacosCallerService.class, args); + } +} diff --git a/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-discovery-caller/src/main/java/com/tencent/cloud/multiple/discovery/nacos/NacosCallerServiceController.java b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-discovery-caller/src/main/java/com/tencent/cloud/multiple/discovery/nacos/NacosCallerServiceController.java new file mode 100644 index 00000000..f2d6bc9a --- /dev/null +++ b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-discovery-caller/src/main/java/com/tencent/cloud/multiple/discovery/nacos/NacosCallerServiceController.java @@ -0,0 +1,42 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.multiple.discovery.nacos; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * NacosCallerServiceController. + * + * @author Haotian Zhang + */ +@RestController +@RequestMapping("/example/service/a") +public class NacosCallerServiceController { + + @Autowired + private ProviderB polarisServiceB; + + @GetMapping("/getBServiceInfo") + public String getBServiceInfoFallbackFromCode() { + return polarisServiceB.info(); + } + +} diff --git a/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-discovery-caller/src/main/java/com/tencent/cloud/multiple/discovery/nacos/ProviderB.java b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-discovery-caller/src/main/java/com/tencent/cloud/multiple/discovery/nacos/ProviderB.java new file mode 100644 index 00000000..6b83a715 --- /dev/null +++ b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-discovery-caller/src/main/java/com/tencent/cloud/multiple/discovery/nacos/ProviderB.java @@ -0,0 +1,38 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.multiple.discovery.nacos; + +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; + +/** + * Circuit breaker example callee provider. + * + * @author Haotian Zhang + */ +@FeignClient(name = "NacosCalleeService") +public interface ProviderB { + + /** + * Get info of service B. + * + * @return info of service B + */ + @GetMapping("/example/service/b/info") + String info(); +} diff --git a/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-discovery-caller/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-discovery-caller/src/main/resources/bootstrap.yml new file mode 100644 index 00000000..ddc71f85 --- /dev/null +++ b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/nacos-discovery-caller/src/main/resources/bootstrap.yml @@ -0,0 +1,27 @@ +server: + port: 48080 +spring: + application: + name: NacosCallerService + cloud: + polaris: + address: grpc://119.91.66.223:8091 + namespace: default + enabled: true + loadbalancer: + enabled: true + discoveryType: Nacos + circuitbreaker: + enabled: true + nacos: + discovery: + server-addr: 127.0.0.1:8848 + +feign: + hystrix: + enabled: true + +logging: + level: + root: info + com.tencent.cloud: debug \ No newline at end of file diff --git a/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/pom.xml b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/pom.xml new file mode 100644 index 00000000..5a4ab613 --- /dev/null +++ b/spring-cloud-tencent-examples/multiple-discovery-example/multiple-discovery-nacos-example/pom.xml @@ -0,0 +1,29 @@ + + + + multiple-discovery-example + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + multiple-discovery-nacos-example + + pom + + nacos-callee-service-a + nacos-callee-service-b + nacos-discovery-caller + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + 2.2.9.RELEASE + + + \ No newline at end of file diff --git a/spring-cloud-tencent-examples/multiple-discovery-example/pom.xml b/spring-cloud-tencent-examples/multiple-discovery-example/pom.xml new file mode 100644 index 00000000..7586bb6d --- /dev/null +++ b/spring-cloud-tencent-examples/multiple-discovery-example/pom.xml @@ -0,0 +1,20 @@ + + + + spring-cloud-tencent-examples + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + multiple-discovery-example + Spring Cloud Starter Tencent Multiple Discovery Example + pom + + + multiple-discovery-nacos-example + + \ No newline at end of file diff --git a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-feign-example/src/main/resources/application.yml b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-feign-example/src/main/resources/bootstrap.yml similarity index 100% rename from spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-feign-example/src/main/resources/application.yml rename to spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-feign-example/src/main/resources/bootstrap.yml diff --git a/spring-cloud-tencent-examples/pom.xml b/spring-cloud-tencent-examples/pom.xml index d2219c19..175b40bf 100644 --- a/spring-cloud-tencent-examples/pom.xml +++ b/spring-cloud-tencent-examples/pom.xml @@ -26,6 +26,7 @@ polaris-router-grayrelease-example polaris-router-featureenv-example quickstart-example + multiple-discovery-example diff --git a/spring-cloud-tencent-plugin-starters/pom.xml b/spring-cloud-tencent-plugin-starters/pom.xml index 6487f9a8..bc2f153b 100644 --- a/spring-cloud-tencent-plugin-starters/pom.xml +++ b/spring-cloud-tencent-plugin-starters/pom.xml @@ -17,6 +17,7 @@ spring-cloud-tencent-featureenv-plugin spring-cloud-tencent-gateway-plugin + spring-cloud-starter-tencent-discovery-adapter-plugin diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/pom.xml b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/pom.xml new file mode 100644 index 00000000..5638897e --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/pom.xml @@ -0,0 +1,42 @@ + + + + spring-cloud-tencent-plugin-starters + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + spring-cloud-starter-tencent-discovery-adapter-plugin + Spring Cloud Starter Tencent Discovery Adapter Plugin + + + + + com.tencent.cloud + spring-cloud-starter-tencent-polaris-router + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + 2.2.9.RELEASE + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.mockito + mockito-inline + test + + + 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 new file mode 100644 index 00000000..d1332926 --- /dev/null +++ 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 @@ -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.config; + +import com.tencent.cloud.plugin.discovery.adapter.transformer.NacosInstanceTransformer; +import com.tencent.cloud.polaris.loadbalancer.config.PolarisLoadBalancerAutoConfiguration; +import com.tencent.cloud.polaris.loadbalancer.transformer.InstanceTransformer; + +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * NacosDiscoveryAdapterAutoConfiguration. + * + * @author Haotian Zhang + */ +@Configuration(proxyBeanMethods = false) +@ConditionalOnDiscoveryEnabled +@AutoConfigureBefore(PolarisLoadBalancerAutoConfiguration.class) +public class NacosDiscoveryAdapterAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + @ConditionalOnClass(name = "com.alibaba.cloud.nacos.ribbon.NacosServer") + public InstanceTransformer instanceTransformer() { + return new NacosInstanceTransformer(); + } + +} 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 new file mode 100644 index 00000000..db104729 --- /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/NacosInstanceTransformer.java @@ -0,0 +1,42 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.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.polaris.api.pojo.DefaultInstance; + +/** + * NacosInstanceTransformer. + * + * @author Haotian Zhang + */ +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()); + } + } +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/main/resources/META-INF/spring.factories b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/main/resources/META-INF/spring.factories new file mode 100644 index 00000000..5bce5c86 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-discovery-adapter-plugin/src/main/resources/META-INF/spring.factories @@ -0,0 +1,4 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + com.tencent.cloud.plugin.discovery.adapter.config.NacosDiscoveryAdapterAutoConfiguration +org.springframework.cloud.bootstrap.BootstrapConfiguration=\ + com.tencent.cloud.plugin.discovery.adapter.config.NacosDiscoveryAdapterAutoConfiguration 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 new file mode 100644 index 00000000..8689cb2f --- /dev/null +++ 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 @@ -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.config; + +import com.tencent.cloud.plugin.discovery.adapter.transformer.NacosInstanceTransformer; +import org.junit.jupiter.api.Test; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * NacosDiscoveryAdapterAutoConfigurationTest. + * + * @author Haotian Zhang + */ +public class NacosDiscoveryAdapterAutoConfigurationTest { + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of( + NacosDiscoveryAdapterAutoConfiguration.class, + LoadBalancerAutoConfiguration.class + )); + + @Test + public void testDefaultInitialization() { + this.contextRunner.run(context -> { + assertThat(context).hasSingleBean(NacosInstanceTransformer.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 new file mode 100644 index 00000000..00dd8be7 --- /dev/null +++ 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 @@ -0,0 +1,65 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.plugin.discovery.adapter.config; + +import com.alibaba.cloud.nacos.ribbon.NacosServer; +import com.tencent.cloud.common.util.ApplicationContextAwareUtils; +import com.tencent.cloud.plugin.discovery.adapter.transformer.NacosInstanceTransformer; +import com.tencent.polaris.api.pojo.DefaultInstance; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * NacosInstanceTransformerTest. + * + * @author Haotian Zhang + */ +public class NacosInstanceTransformerTest { + + private static MockedStatic mockedApplicationContextAwareUtils; + + @BeforeAll + public static void beforeAll() { + mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class); + mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties("spring.cloud.polaris.namespace")) + .thenReturn("default"); + mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties("spring.cloud.polaris.service")) + .thenReturn("test"); + } + + + @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); + assertThat(instance.isHealthy()).isEqualTo(true); + } + +} diff --git a/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancer.java b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancer.java index 58c4dc7d..07335adf 100644 --- a/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancer.java +++ b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancer.java @@ -34,6 +34,7 @@ import com.tencent.cloud.common.constant.ContextConstant; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.pojo.PolarisServer; import com.tencent.cloud.polaris.loadbalancer.config.PolarisLoadBalancerProperties; +import com.tencent.cloud.polaris.loadbalancer.transformer.InstanceTransformer; import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.api.pojo.DefaultInstance; import com.tencent.polaris.api.pojo.DefaultServiceInstances; @@ -58,11 +59,14 @@ public class PolarisLoadBalancer extends DynamicServerListLoadBalancer { private final PolarisLoadBalancerProperties polarisLoadBalancerProperties; + private final InstanceTransformer instanceTransformer; + public PolarisLoadBalancer(IClientConfig config, IRule rule, IPing ping, ServerList serverList, - ConsumerAPI consumerAPI, PolarisLoadBalancerProperties properties) { + ConsumerAPI consumerAPI, PolarisLoadBalancerProperties properties, InstanceTransformer instanceTransformer) { super(config, rule, ping, serverList, null, new PollingServerListUpdater()); this.consumerAPI = consumerAPI; this.polarisLoadBalancerProperties = properties; + this.instanceTransformer = instanceTransformer; } @Override @@ -114,6 +118,7 @@ public class PolarisLoadBalancer extends DynamicServerListLoadBalancer { throw new IllegalStateException( "PolarisLoadBalancer only Server with AppName or ServiceIdForDiscovery attribute"); } + ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, name); List instances = new ArrayList<>(8); for (Server server : allServers) { @@ -127,6 +132,9 @@ public class PolarisLoadBalancer extends DynamicServerListLoadBalancer { instance.setPort(server.getPort()); instance.setZone(server.getZone()); instance.setWeight(100); + if (instanceTransformer != null) { + instanceTransformer.transformCustom(instance, server); + } instances.add(instance); } serviceInstances = new DefaultServiceInstances(serviceKey, instances); diff --git a/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisRibbonClientConfiguration.java b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisRibbonClientConfiguration.java index c41d9a4a..6a6a6bd9 100644 --- a/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisRibbonClientConfiguration.java +++ b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisRibbonClientConfiguration.java @@ -29,9 +29,11 @@ import com.netflix.loadbalancer.ServerList; import com.tencent.cloud.polaris.loadbalancer.PolarisLoadBalancer; import com.tencent.cloud.polaris.loadbalancer.PolarisRingHashRule; import com.tencent.cloud.polaris.loadbalancer.PolarisWeightedRandomRule; +import com.tencent.cloud.polaris.loadbalancer.transformer.InstanceTransformer; import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.router.api.core.RouterAPI; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; @@ -46,9 +48,10 @@ public class PolarisRibbonClientConfiguration { @Bean public ILoadBalancer polarisLoadBalancer(IClientConfig iClientConfig, IRule iRule, IPing iPing, ServerList serverList, - ConsumerAPI consumerAPI, PolarisLoadBalancerProperties polarisLoadBalancerProperties) { + ConsumerAPI consumerAPI, PolarisLoadBalancerProperties polarisLoadBalancerProperties, + @Autowired(required = false) InstanceTransformer instanceTransformer) { return new PolarisLoadBalancer(iClientConfig, iRule, iPing, serverList, - consumerAPI, polarisLoadBalancerProperties); + consumerAPI, polarisLoadBalancerProperties, instanceTransformer); } @Bean diff --git a/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/transformer/InstanceTransformer.java b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/transformer/InstanceTransformer.java new file mode 100644 index 00000000..7b12c31e --- /dev/null +++ b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/transformer/InstanceTransformer.java @@ -0,0 +1,32 @@ +/* + * 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.loadbalancer.transformer; + +import com.netflix.loadbalancer.Server; +import com.tencent.polaris.api.pojo.DefaultInstance; + +/** + * InstanceTransformer. + * + * @author Haotian Zhang + */ +public interface InstanceTransformer { + + void transformCustom(DefaultInstance instance, Server server); + +} diff --git a/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancerTest.java b/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancerTest.java index e633f553..4bbf896a 100644 --- a/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancerTest.java +++ b/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancerTest.java @@ -98,7 +98,7 @@ public class PolarisLoadBalancerTest { ServerList emptyServerList = new StaticServerList<>(); PolarisLoadBalancer balancer = new PolarisLoadBalancer(config, rule, new DummyPing(), emptyServerList, - consumerAPI, properties); + consumerAPI, properties, null); String host = balancer.choose(null); @@ -132,7 +132,7 @@ public class PolarisLoadBalancerTest { ServerList staticServerList = assembleServerList(); PolarisLoadBalancer balancer = new PolarisLoadBalancer(config, rule, new DummyPing(), staticServerList, - consumerAPI, properties); + consumerAPI, properties, null); String host = balancer.choose(null); Assertions.assertThat(host).isEqualTo("127.0.0.1:8080");