From 407911918a19ef86e93237981210017d98e99cb7 Mon Sep 17 00:00:00 2001 From: lepdou Date: Wed, 6 Apr 2022 18:53:38 +0800 Subject: [PATCH] 1. fix router bug 2. add router example --- CHANGELOG.md | 17 +---- changes/changes-1.2.0.md | 20 ++++++ .../polaris/ribbon/PolarisServerList.java | 6 +- .../rule/PolarisWeightedRandomRule.java | 49 +++++++++---- .../polaris-router-example/pom.xml | 39 ++++++++++ .../router-callee-service1/pom.xml | 50 +++++++++++++ .../example/RouterCalleeApplication1.java | 34 +++++++++ .../example/RouterCalleeController.java | 52 ++++++++++++++ .../src/main/resources/bootstrap.yml | 13 ++++ .../router-callee-service2/pom.xml | 50 +++++++++++++ .../example/RouterCalleeApplication2.java | 34 +++++++++ .../example/RouterCalleeController.java | 53 ++++++++++++++ .../src/main/resources/bootstrap.yml | 14 ++++ .../router-caller-service/pom.xml | 56 +++++++++++++++ .../router/example/RouterCalleeService.java | 35 +++++++++ .../example/RouterCallerApplication.java | 48 +++++++++++++ .../example/RouterCallerController.java | 71 +++++++++++++++++++ .../src/main/resources/bootstrap.yml | 9 +++ spring-cloud-tencent-examples/pom.xml | 1 + 19 files changed, 620 insertions(+), 31 deletions(-) create mode 100644 changes/changes-1.2.0.md create mode 100644 spring-cloud-tencent-examples/polaris-router-example/pom.xml create mode 100644 spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/pom.xml create mode 100644 spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeApplication1.java create mode 100644 spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeController.java create mode 100644 spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/resources/bootstrap.yml create mode 100644 spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/pom.xml create mode 100644 spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeApplication2.java create mode 100644 spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeController.java create mode 100644 spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/resources/bootstrap.yml create mode 100644 spring-cloud-tencent-examples/polaris-router-example/router-caller-service/pom.xml create mode 100644 spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeService.java create mode 100644 spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/java/com/tencent/cloud/polaris/router/example/RouterCallerApplication.java create mode 100644 spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/java/com/tencent/cloud/polaris/router/example/RouterCallerController.java create mode 100644 spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/resources/bootstrap.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index 536654eac..b0b57b739 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,20 +1,5 @@ # Change Log --- -- [the server address only support one ip](https://github.com/Tencent/spring-cloud-tencent/pull/36) -- [feature: get service list by given namespace](https://github.com/Tencent/spring-cloud-tencent/pull/38) -- [feat:Support multi-discovery server.](https://github.com/Tencent/spring-cloud-tencent/pull/42) -- [fix:fix [Deserialization of Untrusted Data in logback]](https://github.com/Tencent/spring-cloud-tencent/pull/44/files) -- [feat:support customize registered ip address.](https://github.com/Tencent/spring-cloud-tencent/pull/47) -- [fix:fix import default log implement error](https://github.com/Tencent/spring-cloud-tencent/pull/53) -- [send heartbeat if healthcheck url not configured](https://github.com/Tencent/spring-cloud-tencent/pull/54) -- [feat:optimize project structure and checkstyle](https://github.com/Tencent/spring-cloud-tencent/pull/56) -- [feat:divide storage and transfer of metadata.](https://github.com/Tencent/spring-cloud-tencent/pull/63) -- [feat:support polaris config center.](https://github.com/Tencent/spring-cloud-tencent/pull/69) -- [feat:optimize metadata module.](https://github.com/Tencent/spring-cloud-tencent/pull/70) -- [feat: optimize pom dependency and demo](https://github.com/Tencent/spring-cloud-tencent/pull/71) -- [feat:upgrade polaris version to 1.3.0.](https://github.com/Tencent/spring-cloud-tencent/pull/72) -- [feat:rollback init polaris-context to application phase](https://github.com/Tencent/spring-cloud-tencent/pull/73) -- [feat:tencent-commons add spring-cloud-starter dependency](https://github.com/Tencent/spring-cloud-tencent/pull/76) -- [fix:fix fetching wrong PEER_SERVICE in SCG filter.](https://github.com/Tencent/spring-cloud-tencent/pull/79) +- [Bugfix: fix router bug and add router example](https://github.com/Tencent/spring-cloud-tencent/pull/89) diff --git a/changes/changes-1.2.0.md b/changes/changes-1.2.0.md new file mode 100644 index 000000000..536654eac --- /dev/null +++ b/changes/changes-1.2.0.md @@ -0,0 +1,20 @@ +# Change Log +--- + +- [the server address only support one ip](https://github.com/Tencent/spring-cloud-tencent/pull/36) +- [feature: get service list by given namespace](https://github.com/Tencent/spring-cloud-tencent/pull/38) +- [feat:Support multi-discovery server.](https://github.com/Tencent/spring-cloud-tencent/pull/42) +- [fix:fix [Deserialization of Untrusted Data in logback]](https://github.com/Tencent/spring-cloud-tencent/pull/44/files) +- [feat:support customize registered ip address.](https://github.com/Tencent/spring-cloud-tencent/pull/47) +- [fix:fix import default log implement error](https://github.com/Tencent/spring-cloud-tencent/pull/53) +- [send heartbeat if healthcheck url not configured](https://github.com/Tencent/spring-cloud-tencent/pull/54) +- [feat:optimize project structure and checkstyle](https://github.com/Tencent/spring-cloud-tencent/pull/56) +- [feat:divide storage and transfer of metadata.](https://github.com/Tencent/spring-cloud-tencent/pull/63) +- [feat:support polaris config center.](https://github.com/Tencent/spring-cloud-tencent/pull/69) +- [feat:optimize metadata module.](https://github.com/Tencent/spring-cloud-tencent/pull/70) +- [feat: optimize pom dependency and demo](https://github.com/Tencent/spring-cloud-tencent/pull/71) +- [feat:upgrade polaris version to 1.3.0.](https://github.com/Tencent/spring-cloud-tencent/pull/72) +- [feat:rollback init polaris-context to application phase](https://github.com/Tencent/spring-cloud-tencent/pull/73) +- [feat:tencent-commons add spring-cloud-starter dependency](https://github.com/Tencent/spring-cloud-tencent/pull/76) +- [fix:fix fetching wrong PEER_SERVICE in SCG filter.](https://github.com/Tencent/spring-cloud-tencent/pull/79) + diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisServerList.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisServerList.java index 77698eea1..4ab2ae02a 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisServerList.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisServerList.java @@ -13,6 +13,7 @@ * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. + * */ package com.tencent.cloud.polaris.ribbon; @@ -55,9 +56,8 @@ public class PolarisServerList extends AbstractServerList { } private List getServers() { - InstancesResponse filteredInstances = polarisDiscoveryHandler - .getFilteredInstances(serviceId); - ServiceInstances serviceInstances = filteredInstances.toServiceInstances(); + InstancesResponse allInstances = polarisDiscoveryHandler.getInstances(serviceId); + ServiceInstances serviceInstances = allInstances.toServiceInstances(); List polarisServers = new ArrayList<>(); for (Instance instance : serviceInstances.getInstances()) { polarisServers.add(new PolarisServer(serviceInstances, instance)); diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/rule/PolarisWeightedRandomRule.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/rule/PolarisWeightedRandomRule.java index 4da574cf4..fb805d9ad 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/rule/PolarisWeightedRandomRule.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/rule/PolarisWeightedRandomRule.java @@ -13,18 +13,24 @@ * 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.rule; +import java.util.ArrayList; import java.util.List; import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.AbstractLoadBalancerRule; import com.netflix.loadbalancer.Server; +import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.pojo.PolarisServer; import com.tencent.polaris.api.config.consumer.LoadBalanceConfig; +import com.tencent.polaris.api.pojo.DefaultServiceInstances; import com.tencent.polaris.api.pojo.Instance; +import com.tencent.polaris.api.pojo.ServiceInstances; +import com.tencent.polaris.api.pojo.ServiceKey; import com.tencent.polaris.router.api.core.RouterAPI; import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest; import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse; @@ -51,23 +57,42 @@ public class PolarisWeightedRandomRule extends AbstractLoadBalancerRule { @Override public Server choose(Object key) { - List allServers = getLoadBalancer().getReachableServers(); - if (CollectionUtils.isEmpty(allServers)) { + //1. filter by router + List serversAfterRouter = getLoadBalancer().getReachableServers(); + if (CollectionUtils.isEmpty(serversAfterRouter)) { return null; } - Server server = allServers.get(0); - if (!(server instanceof PolarisServer)) { - throw new IllegalStateException( - "PolarisDiscoveryRule only support PolarisServer instances"); - } - PolarisServer polarisServer = (PolarisServer) server; + + ServiceInstances serviceInstances = transferServersToServiceInstances(serversAfterRouter); + + //2. filter by load balance ProcessLoadBalanceRequest request = new ProcessLoadBalanceRequest(); - request.setDstInstances(polarisServer.getServiceInstances()); + request.setDstInstances(serviceInstances); request.setLbPolicy(POLICY); - ProcessLoadBalanceResponse processLoadBalanceResponse = polarisRouter - .processLoadBalance(request); + ProcessLoadBalanceResponse processLoadBalanceResponse = polarisRouter.processLoadBalance(request); Instance targetInstance = processLoadBalanceResponse.getTargetInstance(); - return new PolarisServer(polarisServer.getServiceInstances(), targetInstance); + + return new PolarisServer(serviceInstances, targetInstance); + } + + ServiceInstances transferServersToServiceInstances(List servers) { + List instances = new ArrayList<>(servers.size()); + String serviceName = null; + + for (Server server : servers) { + if (server instanceof PolarisServer) { + Instance instance = ((PolarisServer) server).getInstance(); + instances.add(instance); + + if (serviceName == null) { + serviceName = instance.getService(); + } + } + } + + ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, serviceName); + + return new DefaultServiceInstances(serviceKey, instances); } } diff --git a/spring-cloud-tencent-examples/polaris-router-example/pom.xml b/spring-cloud-tencent-examples/polaris-router-example/pom.xml new file mode 100644 index 000000000..37e8aee34 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-example/pom.xml @@ -0,0 +1,39 @@ + + + + spring-cloud-tencent-examples + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + polaris-router-example + pom + + + router-callee-service1 + router-callee-service2 + router-caller-service + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + diff --git a/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/pom.xml b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/pom.xml new file mode 100644 index 000000000..253d3739a --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/pom.xml @@ -0,0 +1,50 @@ + + + + polaris-router-example + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + router-callee-service1 + + + + com.tencent.cloud + spring-cloud-starter-tencent-polaris-discovery + + + + + + + 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/polaris-router-example/router-callee-service1/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeApplication1.java b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeApplication1.java new file mode 100644 index 000000000..4692d6847 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeApplication1.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.polaris.router.example; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Router callee application. + *@author lepdou 2022-04-06 + */ +@SpringBootApplication +public class RouterCalleeApplication1 { + + public static void main(String[] args) { + SpringApplication.run(RouterCalleeApplication1.class, args); + } +} diff --git a/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeController.java b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeController.java new file mode 100644 index 000000000..4a9f19394 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeController.java @@ -0,0 +1,52 @@ +/* + * 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.example; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * Discovery callee controller. + * + * @author lepdou 2022-04-06 + */ +@RestController +@RequestMapping("/router/service/callee") +public class RouterCalleeController { + + private static Logger LOG = LoggerFactory.getLogger(RouterCalleeController.class); + + @Value("${server.port:0}") + private int port; + + /** + * Get information of callee. + * @return information of callee + */ + @GetMapping("/info") + public String info() { + LOG.info("Discovery Service Callee [{}] is called.", port); + return String.format("Discovery Service Callee [%s] is called.", port); + } +} diff --git a/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/resources/bootstrap.yml new file mode 100644 index 000000000..1d352ec85 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service1/src/main/resources/bootstrap.yml @@ -0,0 +1,13 @@ +server: + port: 48081 +spring: + application: + name: RouterCalleeService + cloud: + tencent: + metadata: + content: + label1: value1 + polaris: + address: grpc://127.0.0.1:8091 + namespace: default diff --git a/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/pom.xml b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/pom.xml new file mode 100644 index 000000000..0dd00a788 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/pom.xml @@ -0,0 +1,50 @@ + + + + polaris-router-example + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + router-callee-service2 + + + + com.tencent.cloud + spring-cloud-starter-tencent-polaris-discovery + + + + + + + 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/polaris-router-example/router-callee-service2/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeApplication2.java b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeApplication2.java new file mode 100644 index 000000000..5f0bc6300 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeApplication2.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.polaris.router.example; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Router callee application. + * @author lepdou 2022-04-06 + */ +@SpringBootApplication +public class RouterCalleeApplication2 { + + public static void main(String[] args) { + SpringApplication.run(RouterCalleeApplication2.class, args); + } +} diff --git a/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeController.java b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeController.java new file mode 100644 index 000000000..f02f1887d --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeController.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.polaris.router.example; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * Discovery callee controller. + * + * @author lepdou 2022-04-06 + */ +@RestController +@RequestMapping("/router/service/callee") +public class RouterCalleeController { + + private static Logger LOG = LoggerFactory.getLogger(RouterCalleeController.class); + + @Value("${server.port:0}") + private int port; + + /** + * Get information of callee. + * @return information of callee + */ + @GetMapping("/info") + public String info() { + LOG.info("Discovery Service Callee [{}] is called.", port); + return String.format("Discovery Service Callee [%s] is called.", port); + } + +} diff --git a/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/resources/bootstrap.yml new file mode 100644 index 000000000..ebf6fe5c8 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-example/router-callee-service2/src/main/resources/bootstrap.yml @@ -0,0 +1,14 @@ +server: + port: 48082 +spring: + application: + name: RouterCalleeService + cloud: + tencent: + metadata: + content: + version: v2 + xx: zz + polaris: + address: grpc://127.0.0.1:8091 + namespace: default diff --git a/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/pom.xml b/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/pom.xml new file mode 100644 index 000000000..8a1af3407 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/pom.xml @@ -0,0 +1,56 @@ + + + + polaris-router-example + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + router-caller-service + + + + com.tencent.cloud + spring-cloud-starter-tencent-polaris-discovery + + + + com.tencent.cloud + spring-cloud-starter-tencent-polaris-router + + + + + + + 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/polaris-router-example/router-caller-service/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeService.java b/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeService.java new file mode 100644 index 000000000..52439e3e5 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/java/com/tencent/cloud/polaris/router/example/RouterCalleeService.java @@ -0,0 +1,35 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.router.example; + +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; + +/** + * Router callee feign client. + * + * @author lepdou 2022-04-06 + */ +@FeignClient("RouterCalleeService") +public interface RouterCalleeService { + + @GetMapping("/router/service/callee/info") + String info(); + +} diff --git a/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/java/com/tencent/cloud/polaris/router/example/RouterCallerApplication.java b/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/java/com/tencent/cloud/polaris/router/example/RouterCallerApplication.java new file mode 100644 index 000000000..f7b114a41 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/java/com/tencent/cloud/polaris/router/example/RouterCallerApplication.java @@ -0,0 +1,48 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.router.example; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +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; + +/** + * Router caller application. + * + *@author lepdou 2022-04-06 + */ +@SpringBootApplication +@EnableDiscoveryClient +@EnableFeignClients +public class RouterCallerApplication { + + public static void main(String[] args) { + SpringApplication.run(RouterCallerApplication.class, args); + } + + @Bean + @LoadBalanced + public RestTemplate restTemplate() { + return new RestTemplate(); + } +} diff --git a/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/java/com/tencent/cloud/polaris/router/example/RouterCallerController.java b/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/java/com/tencent/cloud/polaris/router/example/RouterCallerController.java new file mode 100644 index 000000000..13b4ed4b7 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/java/com/tencent/cloud/polaris/router/example/RouterCallerController.java @@ -0,0 +1,71 @@ +/* + * 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.example; + +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; +import org.springframework.web.client.RestTemplate; + +/** + * Discovery caller controller. + * + * @author lepdou 2022-04-06 + */ +@RestController +@RequestMapping("/router/service/caller") +public class RouterCallerController { + + @Autowired + private RestTemplate restTemplate; + + @Autowired + private RouterCalleeService routerCalleeService; + + /** + * Get info of two value. + * @return info + */ + @GetMapping("/feign") + public String feign() { + return routerCalleeService.info(); + } + + /** + * Get information of callee. + * @return information of callee + */ + @GetMapping("/rest") + public String rest() { + return restTemplate.getForObject( + "http://DiscoveryCalleeService/discovery/service/callee/info", + String.class); + } + + /** + * health check. + * @return health check info + */ + @GetMapping("/healthCheck") + public String healthCheck() { + return "pk ok"; + } + +} diff --git a/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/resources/bootstrap.yml new file mode 100644 index 000000000..0c90a3d48 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/resources/bootstrap.yml @@ -0,0 +1,9 @@ +server: + port: 48083 +spring: + application: + name: RouterCallerService + cloud: + polaris: + address: grpc://127.0.0.1:8091 + namespace: default diff --git a/spring-cloud-tencent-examples/pom.xml b/spring-cloud-tencent-examples/pom.xml index 61106d501..2e86e4d8d 100644 --- a/spring-cloud-tencent-examples/pom.xml +++ b/spring-cloud-tencent-examples/pom.xml @@ -21,6 +21,7 @@ polaris-circuitbreaker-example polaris-gateway-example polaris-config-example + polaris-router-example