Add metedata transfer example for (2021.0). (#211)
parent
5acacd1d36
commit
a656d9ca7f
@ -1,3 +1,4 @@
|
||||
# Change Log
|
||||
---
|
||||
|
||||
- [Add metadata transfer example.](https://github.com/Tencent/spring-cloud-tencent/pull/211)
|
||||
|
@ -0,0 +1,51 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>metadata-transfer-example</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>metadata-callee-service</artifactId>
|
||||
<name>Spring Cloud Tencent Metadata Transfer Callee Service</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>3.2.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -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<String, String> info() {
|
||||
LOG.info("Metadata Service Callee [{}] is called.", port);
|
||||
|
||||
// Get Custom Metadata From Context
|
||||
MetadataContext context = MetadataContextHolder.get();
|
||||
Map<String, String> customMetadataMap = context.getAllTransitiveCustomMetadata();
|
||||
|
||||
customMetadataMap.forEach((key, value) -> {
|
||||
LOG.info("Custom Metadata (Key-Value): {} : {}", key, value);
|
||||
});
|
||||
|
||||
return customMetadataMap;
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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
|
@ -0,0 +1,51 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>metadata-transfer-example</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>metadata-caller-service</artifactId>
|
||||
<name>Spring Cloud Tencent Metadata Transfer Caller Service</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>3.2.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -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<String, String> info();
|
||||
|
||||
}
|
@ -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<String, String> info() {
|
||||
return Maps.newHashMap();
|
||||
}
|
||||
|
||||
}
|
@ -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<String, Map<String, String>> feign() {
|
||||
Map<String, Map<String, String>> ret = Maps.newHashMap();
|
||||
|
||||
// Call remote service with feign client
|
||||
Map<String, String> calleeMetadata = metadataCalleeService.info();
|
||||
ret.put("callee-transitive-metadata", calleeMetadata);
|
||||
|
||||
// Get Custom Metadata From Context
|
||||
MetadataContext context = MetadataContextHolder.get();
|
||||
Map<String, String> 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<String, Map<String, String>> rest() {
|
||||
Map<String, Map<String, String>> ret = Maps.newHashMap();
|
||||
|
||||
// Call remote service with RestTemplate
|
||||
Map<String, String> 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<String, String> 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";
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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
|
@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>spring-cloud-tencent-examples</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>metadata-transfer-example</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<name>Spring Cloud Starter Tencent Metadata Transfer Example</name>
|
||||
|
||||
<modules>
|
||||
<module>metadata-callee-service</module>
|
||||
<module>metadata-caller-service</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-tencent-metadata-transfer</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
Loading…
Reference in new issue