commit
b00879c640
@ -0,0 +1,80 @@
|
|||||||
|
/*
|
||||||
|
* 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.core;
|
||||||
|
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* resolve custom transitive metadata from request.
|
||||||
|
*@author lepdou 2022-05-20
|
||||||
|
*/
|
||||||
|
public class CustomTransitiveMetadataResolver {
|
||||||
|
|
||||||
|
private static final String TRANSITIVE_HEADER_PREFIX = "X-SCT-Metadata-Transitive-";
|
||||||
|
private static final int TRANSITIVE_HEADER_PREFIX_LENGTH = TRANSITIVE_HEADER_PREFIX.length();
|
||||||
|
|
||||||
|
public static Map<String, String> resolve(ServerWebExchange exchange) {
|
||||||
|
Map<String, String> result = new HashMap<>();
|
||||||
|
|
||||||
|
HttpHeaders headers = exchange.getRequest().getHeaders();
|
||||||
|
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
|
||||||
|
String key = entry.getKey();
|
||||||
|
|
||||||
|
if (StringUtils.isNotBlank(key) &&
|
||||||
|
StringUtils.startsWithIgnoreCase(key, TRANSITIVE_HEADER_PREFIX)
|
||||||
|
&& !CollectionUtils.isEmpty(entry.getValue())) {
|
||||||
|
|
||||||
|
String sourceKey = StringUtils.substring(key, TRANSITIVE_HEADER_PREFIX_LENGTH);
|
||||||
|
result.put(sourceKey, entry.getValue().get(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, String> resolve(HttpServletRequest request) {
|
||||||
|
Map<String, String> result = new HashMap<>();
|
||||||
|
|
||||||
|
Enumeration<String> headers = request.getHeaderNames();
|
||||||
|
while (headers.hasMoreElements()) {
|
||||||
|
String key = headers.nextElement();
|
||||||
|
|
||||||
|
if (StringUtils.isNotBlank(key) &&
|
||||||
|
StringUtils.startsWithIgnoreCase(key, TRANSITIVE_HEADER_PREFIX)
|
||||||
|
&& StringUtils.isNotBlank(request.getHeader(key))) {
|
||||||
|
|
||||||
|
String sourceKey = StringUtils.substring(key, TRANSITIVE_HEADER_PREFIX_LENGTH);
|
||||||
|
result.put(sourceKey, request.getHeader(key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,145 @@
|
|||||||
|
/*
|
||||||
|
* 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.common.metadata;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* manage metadata from env/config file.
|
||||||
|
*
|
||||||
|
*@author lepdou 2022-05-20
|
||||||
|
*/
|
||||||
|
public class StaticMetadataManager {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(StaticMetadataManager.class);
|
||||||
|
|
||||||
|
private static final String ENV_METADATA_PREFIX = "SCT_METADATA_CONTENT_";
|
||||||
|
private static final int ENV_METADATA_PREFIX_LENGTH = ENV_METADATA_PREFIX.length();
|
||||||
|
private static final String ENV_METADATA_CONTENT_TRANSITIVE = "SCT_METADATA_CONTENT_TRANSITIVE";
|
||||||
|
|
||||||
|
private Map<String, String> envMetadata;
|
||||||
|
private Map<String, String> envTransitiveMetadata;
|
||||||
|
private Map<String, String> configMetadata;
|
||||||
|
private Map<String, String> configTransitiveMetadata;
|
||||||
|
private Map<String, String> mergedStaticMetadata;
|
||||||
|
private Map<String, String> mergedStaticTransitiveMetadata;
|
||||||
|
|
||||||
|
public StaticMetadataManager(MetadataLocalProperties metadataLocalProperties) {
|
||||||
|
parseConfigMetadata(metadataLocalProperties);
|
||||||
|
parseEnvMetadata();
|
||||||
|
merge();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void parseEnvMetadata() {
|
||||||
|
Map<String, String> allEnvs = System.getenv();
|
||||||
|
|
||||||
|
envMetadata = new HashMap<>();
|
||||||
|
// parse all metadata
|
||||||
|
for (Map.Entry<String, String> entry : allEnvs.entrySet()) {
|
||||||
|
String key = entry.getKey();
|
||||||
|
String value = entry.getValue();
|
||||||
|
if (StringUtils.isNotBlank(key) && key.startsWith(ENV_METADATA_PREFIX)
|
||||||
|
&& !key.equals(ENV_METADATA_CONTENT_TRANSITIVE)) {
|
||||||
|
String sourceKey = StringUtils.substring(key, ENV_METADATA_PREFIX_LENGTH);
|
||||||
|
envMetadata.put(sourceKey, value);
|
||||||
|
|
||||||
|
LOGGER.info("[SCT] resolve metadata from env. key = {}, value = {}", sourceKey, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
envMetadata = Collections.unmodifiableMap(envMetadata);
|
||||||
|
|
||||||
|
envTransitiveMetadata = new HashMap<>();
|
||||||
|
// parse transitive metadata
|
||||||
|
String transitiveKeys = allEnvs.get(ENV_METADATA_CONTENT_TRANSITIVE);
|
||||||
|
if (StringUtils.isNotBlank(transitiveKeys)) {
|
||||||
|
String[] keyArr = StringUtils.split(transitiveKeys, ",");
|
||||||
|
if (keyArr != null && keyArr.length > 0) {
|
||||||
|
for (String key : keyArr) {
|
||||||
|
String value = envMetadata.get(key);
|
||||||
|
if (StringUtils.isNotBlank(value)) {
|
||||||
|
envTransitiveMetadata.put(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
envTransitiveMetadata = Collections.unmodifiableMap(envTransitiveMetadata);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void parseConfigMetadata(MetadataLocalProperties metadataLocalProperties) {
|
||||||
|
Map<String, String> allMetadata = metadataLocalProperties.getContent();
|
||||||
|
List<String> transitiveKeys = metadataLocalProperties.getTransitive();
|
||||||
|
|
||||||
|
Map<String, String> result = new HashMap<>();
|
||||||
|
for (String key : transitiveKeys) {
|
||||||
|
if (allMetadata.containsKey(key)) {
|
||||||
|
result.put(key, allMetadata.get(key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
configTransitiveMetadata = Collections.unmodifiableMap(result);
|
||||||
|
configMetadata = Collections.unmodifiableMap(allMetadata);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void merge() {
|
||||||
|
// env priority is bigger than config
|
||||||
|
Map<String, String> mergedMetadataResult = new HashMap<>();
|
||||||
|
|
||||||
|
mergedMetadataResult.putAll(configMetadata);
|
||||||
|
mergedMetadataResult.putAll(envMetadata);
|
||||||
|
|
||||||
|
this.mergedStaticMetadata = Collections.unmodifiableMap(mergedMetadataResult);
|
||||||
|
|
||||||
|
Map<String, String> mergedTransitiveMetadataResult = new HashMap<>();
|
||||||
|
mergedTransitiveMetadataResult.putAll(configTransitiveMetadata);
|
||||||
|
mergedTransitiveMetadataResult.putAll(envTransitiveMetadata);
|
||||||
|
|
||||||
|
this.mergedStaticTransitiveMetadata = Collections.unmodifiableMap(mergedTransitiveMetadataResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getAllEnvMetadata() {
|
||||||
|
return envMetadata;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getEnvTransitiveMetadata() {
|
||||||
|
return envTransitiveMetadata;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getAllConfigMetadata() {
|
||||||
|
return configMetadata;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getConfigTransitiveMetadata() {
|
||||||
|
return configTransitiveMetadata;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getMergedStaticMetadata() {
|
||||||
|
return mergedStaticMetadata;
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, String> getMergedStaticTransitiveMetadata() {
|
||||||
|
return mergedStaticTransitiveMetadata;
|
||||||
|
}
|
||||||
|
}
|
@ -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.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||||
|
|
||||||
|
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.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||||
|
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.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||||
|
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