You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
spring-cloud-tencent/spring-cloud-tencent-examples/metadata-transfer-example/README-zh.md

3.1 KiB

Spring Cloud Tencent Metadata Transfer example

样例简介

本样例将介绍如何在Spring Cloud项目中使用spring-cloud-starter-tencent-metadata-transfer以使用其各项功能。

本样例包括metadata-callee-servicemetadata-caller-service

使用说明

修改配置

配置如下所示。其中,{ip}和{port}为Polaris后端服务的IP地址与端口号。

spring:
  application:
    name: ${application.name}
  cloud:
    polaris:
      address: ${ip}:${port}

Maven依赖

<dependency>
    <groupId>com.tencent.cloud</groupId>
    <artifactId>spring-cloud-starter-tencent-metadata-transfer</artifactId>
</dependency>

启动样例

启动Polaris后端服务

参考Polaris Getting Started

启动应用

IDEA启动

分别启动

  • spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-serviceMetadataCalleeService
  • spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-serviceMetadataCallerService
Maven打包启动

spring-cloud-tencent-examples/metadata-transfer-example下执行

mvn clean package

然后在metadata-callee-servicemetadata-caller-service下找到生成的jar包运行

java -jar ${app.jar}

启动应用,其中${app.jar}替换为对应的jar包名。

元数据配置

spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service项目的bootstrap.yml配置文件中

spring:
  cloud:
    tencent:
      metadata:
        # 定义元数据的键值对
        content:
          # 示例:本地元数据,默认不在链路中传递
          CUSTOM-METADATA-KEY-LOCAL: CUSTOM-VALUE-LOCAL
          # 示例:可传递元数据
          CUSTOM-METADATA-KEY-TRANSITIVE: CUSTOM-VALUE-TRANSITIVE
        # 指定哪个元数据的键值将沿着链接传递
        transitive:
          - CUSTOM-METADATA-KEY-TRANSITIVE

验证

请求调用

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 使用指南 .