From 7ba0fe8e9527c8555dc134681ebaff1a7a775167 Mon Sep 17 00:00:00 2001 From: "VOPEN.XYZ" Date: Mon, 30 May 2022 10:29:05 +0800 Subject: [PATCH] Add metedata transfer example . (#209) Co-authored-by: Haotian Zhang <928016560@qq.com> --- CHANGELOG.md | 1 + .../metadata-transfer-example/README-zh.md | 118 ++++++++++++++++++ .../metadata-transfer-example/README.md | 117 +++++++++++++++++ .../metadata-callee-service/pom.xml | 51 ++++++++ .../callee/MetadataCalleeController.java | 65 ++++++++++ .../service/callee/MetadataCalleeService.java | 35 ++++++ .../src/main/resources/bootstrap.yml | 13 ++ .../metadata-caller-service/pom.xml | 51 ++++++++ .../service/caller/MetadataCalleeService.java | 41 ++++++ .../caller/MetadataCalleeServiceFallback.java | 39 ++++++ .../caller/MetadataCallerController.java | 109 ++++++++++++++++ .../service/caller/MetadataCallerService.java | 48 +++++++ .../src/main/resources/bootstrap.yml | 26 ++++ .../metadata-transfer-example/pom.xml | 43 +++++++ spring-cloud-tencent-examples/pom.xml | 1 + 15 files changed, 758 insertions(+) create mode 100644 spring-cloud-tencent-examples/metadata-transfer-example/README-zh.md create mode 100644 spring-cloud-tencent-examples/metadata-transfer-example/README.md create mode 100644 spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-service/pom.xml create mode 100644 spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-service/src/main/java/com/tencent/cloud/metadata/service/callee/MetadataCalleeController.java create mode 100644 spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-service/src/main/java/com/tencent/cloud/metadata/service/callee/MetadataCalleeService.java create mode 100644 spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-service/src/main/resources/bootstrap.yml create mode 100644 spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/pom.xml create mode 100644 spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCalleeService.java create mode 100644 spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCalleeServiceFallback.java create mode 100644 spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCallerController.java create mode 100644 spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCallerService.java create mode 100644 spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/resources/bootstrap.yml create mode 100644 spring-cloud-tencent-examples/metadata-transfer-example/pom.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index 06c1f8175..d08c4aaae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ # Change Log --- +- [Add metadata transfer example.](https://github.com/Tencent/spring-cloud-tencent/pull/209) diff --git a/spring-cloud-tencent-examples/metadata-transfer-example/README-zh.md b/spring-cloud-tencent-examples/metadata-transfer-example/README-zh.md new file mode 100644 index 000000000..9af9aff6d --- /dev/null +++ b/spring-cloud-tencent-examples/metadata-transfer-example/README-zh.md @@ -0,0 +1,118 @@ +# Spring Cloud Tencent Metadata Transfer example + +## 样例简介 + +本样例将介绍如何在Spring Cloud项目中使用```spring-cloud-starter-tencent-metadata-transfer```以使用其各项功能。 + +本样例包括```metadata-callee-service```、```metadata-caller-service```。 + +## 使用说明 + +### 修改配置 + +配置如下所示。其中,${ip}和${port}为Polaris后端服务的IP地址与端口号。 + +```yaml +spring: + application: + name: ${application.name} + cloud: + polaris: + address: ${ip}:${port} +``` + +### Maven依赖 + +```xml + + com.tencent.cloud + spring-cloud-starter-tencent-metadata-transfer + +``` + +### 启动样例 + +#### 启动Polaris后端服务 + +参考[Polaris Getting Started](https://github.com/PolarisMesh/polaris#getting-started)。 + +#### 启动应用 + +##### IDEA启动 + +分别启动 +- ```spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-service```的```MetadataCalleeService``` +- ```spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service```的```MetadataCallerService``` + + +##### Maven打包启动 + +在```spring-cloud-tencent-examples/metadata-transfer-example```下执行 + +```sh +mvn clean package +``` + +然后在```metadata-callee-service```、```metadata-caller-service```下找到生成的jar包,运行 + +``` +java -jar ${app.jar} +``` + +启动应用,其中${app.jar}替换为对应的jar包名。 + +### 元数据配置 + +在```spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service```项目的```bootstrap.yml```配置文件中 + +```yaml +spring: + cloud: + tencent: + metadata: + # 定义元数据的键值对 + content: + # 示例:本地元数据,默认不在链路中传递 + CUSTOM-METADATA-KEY-LOCAL: CUSTOM-VALUE-LOCAL + # 示例:可传递元数据 + CUSTOM-METADATA-KEY-TRANSITIVE: CUSTOM-VALUE-TRANSITIVE + # 指定哪个元数据的键值将沿着链接传递 + transitive: + - CUSTOM-METADATA-KEY-TRANSITIVE + +``` + +### 验证 + +#### 请求调用 + +```shell +curl -L -X GET 'http://127.0.0.1:48080/metadata/service/caller/feign/info' +``` + +预期返回值 + +``` +{ + "caller-metadata-contents": { + "CUSTOM-METADATA-KEY-TRANSITIVE": "CUSTOM-VALUE-TRANSITIVE", + "CUSTOM-METADATA-KEY-LOCAL": "CUSTOM-VALUE-LOCAL" + }, + "callee-transitive-metadata": { + "CUSTOM-METADATA-KEY-TRANSITIVE": "CUSTOM-VALUE-TRANSITIVE" + }, + "caller-transitive-metadata": { + "CUSTOM-METADATA-KEY-TRANSITIVE": "CUSTOM-VALUE-TRANSITIVE" + } +} +``` + +返回值解析 + +- Key `caller-metadata-contents` 表示 `metadata-caller-service` 项目中默认配置的所有的元数据。 +- Key `caller-transitive-metadata` 表示 `metadata-caller-service` 项目中指定的可以在链路中传递的元数据列表。 +- Key `callee-transitive-metadata` 表示 `metadata-callee-service` 项目被 `metadata-caller-service` 调用时传递过来的上游的元数据列表。 + +### Wiki参考 + +查看 [Spring Cloud Tencent Metadata Transfer 使用指南](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Metadata-Transfer-%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97) . \ No newline at end of file diff --git a/spring-cloud-tencent-examples/metadata-transfer-example/README.md b/spring-cloud-tencent-examples/metadata-transfer-example/README.md new file mode 100644 index 000000000..b0d912ce9 --- /dev/null +++ b/spring-cloud-tencent-examples/metadata-transfer-example/README.md @@ -0,0 +1,117 @@ +# Spring Cloud Tencent Metadata Transfer example + +## Example Introduction + +This example shows how to use ```spring-cloud-starter-tencent-metadata-transfer``` in Spring Cloud project for its features. + +This example contains ```metadata-callee-service```、```metadata-caller-service```. + +## Instruction + +### Configuration + +The configuration is as the following shows. ${ip} and ${port} are Polaris backend IP address and port number. + +```yaml +spring: + application: + name: ${application.name} + cloud: + polaris: + address: ${ip}:${port} +``` + +### Maven Dependency + +```xml + + com.tencent.cloud + spring-cloud-starter-tencent-metadata-transfer + +``` + +### Launching Example + +#### Launching Polaris Backend Service + +Reference to [Polaris Getting Started](https://github.com/PolarisMesh/polaris#getting-started) + +#### Launching Application + +- IDEA Launching + +- ```spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-service```‘s```MetadataCalleeService``` +- ```spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service```'s```MetadataCallerService``` + +- Maven Package Launching + +Execute under ```spring-cloud-tencent-examples/metadata-transfer-example``` + +```sh +mvn clean package +``` + +Then find the jars under ```metadata-callee-service```、```metadata-caller-service```, and run it: + +``` +java -jar ${app.jar} +``` + +Launch application, change ${app.jar} to jar's package name. + + +### Metadata Configuration + +In the ```bootstrap.yml``` configuration file of the ```spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service``` project + +```yaml +spring: + cloud: + tencent: + metadata: + # Defined your metadata keys & values + content: + # Example: intransitive + CUSTOM-METADATA-KEY-LOCAL: CUSTOM-VALUE-LOCAL + # Example: transitive + CUSTOM-METADATA-KEY-TRANSITIVE: CUSTOM-VALUE-TRANSITIVE + # Assigned which metadata key-value will be passed along the link + transitive: + - CUSTOM-METADATA-KEY-TRANSITIVE + +``` + +### Verify + +#### Request Invoke + +```shell +curl -L -X GET 'http://127.0.0.1:48080/metadata/service/caller/feign/info' +``` + +Expected return rate + +``` +{ + "caller-metadata-contents": { + "CUSTOM-METADATA-KEY-TRANSITIVE": "CUSTOM-VALUE-TRANSITIVE", + "CUSTOM-METADATA-KEY-LOCAL": "CUSTOM-VALUE-LOCAL" + }, + "callee-transitive-metadata": { + "CUSTOM-METADATA-KEY-TRANSITIVE": "CUSTOM-VALUE-TRANSITIVE" + }, + "caller-transitive-metadata": { + "CUSTOM-METADATA-KEY-TRANSITIVE": "CUSTOM-VALUE-TRANSITIVE" + } +} +``` + +Response value description + +- Key `caller-metadata-contents` represents all metadata configured by default in the `metadata-caller-service` project. +- Key `caller-transitive-metadata` represents the list of metadata that can be passed in the link specified in the `metadata-caller-service` item. +- Key `callee-transitive-metadata` represents the list of upstream metadata passed when the `metadata-callee-service` project is called by `metadata-caller-service`. + +### Wiki Reference + +See [Spring Cloud Tencent Metadata Transfer Usage Document](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-Metadata-Transfer-Usage-Document) for more reference . diff --git a/spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-service/pom.xml b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-service/pom.xml new file mode 100644 index 000000000..237c9877f --- /dev/null +++ b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-service/pom.xml @@ -0,0 +1,51 @@ + + + + metadata-transfer-example + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + metadata-callee-service + Spring Cloud Tencent Metadata Transfer Callee Service + + + + 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/metadata-transfer-example/metadata-callee-service/src/main/java/com/tencent/cloud/metadata/service/callee/MetadataCalleeController.java b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-service/src/main/java/com/tencent/cloud/metadata/service/callee/MetadataCalleeController.java new file mode 100644 index 000000000..1e515fdf6 --- /dev/null +++ b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-service/src/main/java/com/tencent/cloud/metadata/service/callee/MetadataCalleeController.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.metadata.service.callee; + +import java.util.Map; + +import com.tencent.cloud.common.metadata.MetadataContext; +import com.tencent.cloud.common.metadata.MetadataContextHolder; +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; + +/** + * Metadata callee controller. + * + * @author Palmer Xu + */ +@RestController +@RequestMapping("/metadata/service/callee") +public class MetadataCalleeController { + + private static final Logger LOG = LoggerFactory.getLogger(MetadataCalleeController.class); + + @Value("${server.port:0}") + private int port; + + /** + * Get information of callee. + * @return information of callee + */ + @GetMapping("/info") + public Map info() { + LOG.info("Metadata Service Callee [{}] is called.", port); + + // Get Custom Metadata From Context + MetadataContext context = MetadataContextHolder.get(); + Map customMetadataMap = context.getAllTransitiveCustomMetadata(); + + customMetadataMap.forEach((key, value) -> { + LOG.info("Custom Metadata (Key-Value): {} : {}", key, value); + }); + + return customMetadataMap; + } + +} diff --git a/spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-service/src/main/java/com/tencent/cloud/metadata/service/callee/MetadataCalleeService.java b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-service/src/main/java/com/tencent/cloud/metadata/service/callee/MetadataCalleeService.java new file mode 100644 index 000000000..06771a5e8 --- /dev/null +++ b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-service/src/main/java/com/tencent/cloud/metadata/service/callee/MetadataCalleeService.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.metadata.service.callee; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Metadata callee application. + * + * @author Palmer Xu + */ +@SpringBootApplication +public class MetadataCalleeService { + + public static void main(String[] args) { + SpringApplication.run(MetadataCalleeService.class, args); + } + +} diff --git a/spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-service/src/main/resources/bootstrap.yml new file mode 100644 index 000000000..c1ded3f47 --- /dev/null +++ b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-service/src/main/resources/bootstrap.yml @@ -0,0 +1,13 @@ +server: + port: 48084 +spring: + application: + name: MetadataCalleeService + cloud: + polaris: + address: grpc://127.0.0.1:8091 + namespace: default + enabled: true + discovery: + enabled: true + register: true diff --git a/spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/pom.xml b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/pom.xml new file mode 100644 index 000000000..1c1306805 --- /dev/null +++ b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/pom.xml @@ -0,0 +1,51 @@ + + + + metadata-transfer-example + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + metadata-caller-service + Spring Cloud Tencent Metadata Transfer Caller Service + + + + 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/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCalleeService.java b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCalleeService.java new file mode 100644 index 000000000..65bea6707 --- /dev/null +++ b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCalleeService.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.metadata.service.caller; + +import java.util.Map; + +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; + +/** + * Metadata callee feign client. + * + * @author Palmer Xu + */ +@FeignClient(value = "MetadataCalleeService", + fallback = MetadataCalleeServiceFallback.class) +public interface MetadataCalleeService { + + /** + * Get information of callee. + * @return information of callee + */ + @GetMapping("/metadata/service/callee/info") + Map info(); + +} diff --git a/spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCalleeServiceFallback.java b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCalleeServiceFallback.java new file mode 100644 index 000000000..ba508831a --- /dev/null +++ b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCalleeServiceFallback.java @@ -0,0 +1,39 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.metadata.service.caller; + +import java.util.Map; + +import com.google.common.collect.Maps; + +import org.springframework.stereotype.Component; + +/** + * Metadata callee feign client fallback. + * + * @author Palmer Xu + */ +@Component +public class MetadataCalleeServiceFallback implements MetadataCalleeService { + + @Override + public Map info() { + return Maps.newHashMap(); + } + +} diff --git a/spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCallerController.java b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCallerController.java new file mode 100644 index 000000000..116504190 --- /dev/null +++ b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCallerController.java @@ -0,0 +1,109 @@ +/* + * 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.metadata.service.caller; + +import java.util.Map; + +import com.google.common.collect.Maps; +import com.tencent.cloud.common.metadata.MetadataContext; +import com.tencent.cloud.common.metadata.MetadataContextHolder; +import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; + +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; + +/** + * Metadata caller controller. + * + * @author Palmer Xu + */ +@RestController +@RequestMapping("/metadata/service/caller") +public class MetadataCallerController { + + private final RestTemplate restTemplate; + + private final MetadataCalleeService metadataCalleeService; + + private final MetadataLocalProperties metadataLocalProperties; + + public MetadataCallerController(RestTemplate restTemplate, + MetadataCalleeService metadataCalleeService, + MetadataLocalProperties metadataLocalProperties) { + this.restTemplate = restTemplate; + this.metadataCalleeService = metadataCalleeService; + this.metadataLocalProperties = metadataLocalProperties; + } + + /** + * Get metadata info from remote service. + * @return metadata map + */ + @GetMapping("/feign/info") + public Map> feign() { + Map> ret = Maps.newHashMap(); + + // Call remote service with feign client + Map calleeMetadata = metadataCalleeService.info(); + ret.put("callee-transitive-metadata", calleeMetadata); + + // Get Custom Metadata From Context + MetadataContext context = MetadataContextHolder.get(); + Map callerTransitiveMetadata = context.getAllTransitiveCustomMetadata(); + ret.put("caller-transitive-metadata", callerTransitiveMetadata); + ret.put("caller-metadata-contents", metadataLocalProperties.getContent()); + + return ret; + } + + /** + * Get metadata information of callee. + * @return information of callee + */ + @SuppressWarnings("unchecked") + @GetMapping("/rest/info") + public Map> rest() { + Map> ret = Maps.newHashMap(); + + // Call remote service with RestTemplate + Map calleeMetadata = restTemplate.getForObject( + "http://MetadataCalleeService/metadata/service/callee/info", + Map.class); + ret.put("callee-transitive-metadata", calleeMetadata); + + // Get Custom Metadata From Context + MetadataContext context = MetadataContextHolder.get(); + Map callerTransitiveMetadata = context.getAllTransitiveCustomMetadata(); + ret.put("caller-transitive-metadata", callerTransitiveMetadata); + ret.put("caller-metadata-contents", metadataLocalProperties.getContent()); + + return ret; + } + + /** + * health check. + * @return health check info + */ + @GetMapping("/healthCheck") + public String healthCheck() { + return "pk ok"; + } + +} diff --git a/spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCallerService.java b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCallerService.java new file mode 100644 index 000000000..a586c7e72 --- /dev/null +++ b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCallerService.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.metadata.service.caller; + +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; + +/** + * Metadata caller application. + * + * @author Palmer Xu + */ +@SpringBootApplication +@EnableDiscoveryClient +@EnableFeignClients +public class MetadataCallerService { + + public static void main(String[] args) { + SpringApplication.run(MetadataCallerService.class, args); + } + + @Bean + @LoadBalanced + public RestTemplate restTemplate() { + return new RestTemplate(); + } + +} diff --git a/spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/resources/bootstrap.yml new file mode 100644 index 000000000..90781f1e9 --- /dev/null +++ b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/resources/bootstrap.yml @@ -0,0 +1,26 @@ +server: + port: 48080 +spring: + application: + name: MetadataCallerService + cloud: + polaris: + address: grpc://127.0.0.1:8091 + namespace: default + enabled: true + discovery: + enabled: true + register: true + heartbeat-enabled: true + health-check-url: /metadata/service/caller/healthCheck + tencent: + metadata: + # Defined your metadata keys & values + content: + # Example: intransitive + CUSTOM-METADATA-KEY-LOCAL: CUSTOM-VALUE-LOCAL + # Example: transitive + CUSTOM-METADATA-KEY-TRANSITIVE: CUSTOM-VALUE-TRANSITIVE + # Assigned which metadata key-value will be passed along the link + transitive: + - CUSTOM-METADATA-KEY-TRANSITIVE diff --git a/spring-cloud-tencent-examples/metadata-transfer-example/pom.xml b/spring-cloud-tencent-examples/metadata-transfer-example/pom.xml new file mode 100644 index 000000000..bd2b961ec --- /dev/null +++ b/spring-cloud-tencent-examples/metadata-transfer-example/pom.xml @@ -0,0 +1,43 @@ + + + + spring-cloud-tencent-examples + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + metadata-transfer-example + pom + Spring Cloud Starter Tencent Metadata Transfer Example + + + metadata-callee-service + metadata-caller-service + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + com.tencent.cloud + spring-cloud-starter-tencent-metadata-transfer + + + diff --git a/spring-cloud-tencent-examples/pom.xml b/spring-cloud-tencent-examples/pom.xml index 2e86e4d8d..2897d6211 100644 --- a/spring-cloud-tencent-examples/pom.xml +++ b/spring-cloud-tencent-examples/pom.xml @@ -22,6 +22,7 @@ polaris-gateway-example polaris-config-example polaris-router-example + metadata-transfer-example