Add disposable metadata transfer support . (#459)
parent
2155b3bd9d
commit
0f32fb47e6
37
spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-service/src/main/java/com/tencent/cloud/metadata/service/callee/MetadataCalleeController.java → spring-cloud-tencent-examples/metadata-transfer-example/metadata-backend/src/main/java/com/tencent/cloud/metadata/service/backend/MetadataBackendController.java
37
spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-service/src/main/java/com/tencent/cloud/metadata/service/callee/MetadataCalleeController.java → spring-cloud-tencent-examples/metadata-transfer-example/metadata-backend/src/main/java/com/tencent/cloud/metadata/service/backend/MetadataBackendController.java
7
spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-service/src/main/java/com/tencent/cloud/metadata/service/callee/MetadataCalleeService.java → spring-cloud-tencent-examples/metadata-transfer-example/metadata-backend/src/main/java/com/tencent/cloud/metadata/service/backend/MetadataBackendService.java
7
spring-cloud-tencent-examples/metadata-transfer-example/metadata-callee-service/src/main/java/com/tencent/cloud/metadata/service/callee/MetadataCalleeService.java → spring-cloud-tencent-examples/metadata-transfer-example/metadata-backend/src/main/java/com/tencent/cloud/metadata/service/backend/MetadataBackendService.java
@ -1,109 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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,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-frontend</artifactId>
|
||||||
|
<name>Spring Cloud Tencent Metadata Transfer Frontent 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,155 @@
|
|||||||
|
/*
|
||||||
|
* 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.frontend;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
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.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/frontend")
|
||||||
|
public class MetadataFrontendController {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(MetadataFrontendController.class);
|
||||||
|
|
||||||
|
private final RestTemplate restTemplate;
|
||||||
|
|
||||||
|
private final MetadataMiddleService metadataMiddleService;
|
||||||
|
|
||||||
|
public MetadataFrontendController(RestTemplate restTemplate,
|
||||||
|
MetadataMiddleService metadataMiddleService) {
|
||||||
|
this.restTemplate = restTemplate;
|
||||||
|
this.metadataMiddleService = metadataMiddleService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 = new HashMap<>();
|
||||||
|
|
||||||
|
// Call remote service with feign client
|
||||||
|
Map<String, Map<String, String>> middleResult = metadataMiddleService.info();
|
||||||
|
|
||||||
|
if (middleResult != null) {
|
||||||
|
ret.putAll(middleResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get Custom Metadata From Context
|
||||||
|
MetadataContext context = MetadataContextHolder.get();
|
||||||
|
Map<String, String> customMetadataMap = context.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||||
|
|
||||||
|
customMetadataMap.forEach((key, value) -> {
|
||||||
|
LOG.info("Metadata Middle Custom Metadata (Key-Value): {} : {}", key, value);
|
||||||
|
});
|
||||||
|
|
||||||
|
ret.put("frontend-transitive-metadata", customMetadataMap);
|
||||||
|
|
||||||
|
// Get All Disposable metadata from upstream service
|
||||||
|
Map<String, String> upstreamDisposableMetadatas = MetadataContextHolder.getAllDisposableMetadata(true);
|
||||||
|
upstreamDisposableMetadatas.forEach((key, value) -> {
|
||||||
|
LOG.info("Upstream Custom Disposable Metadata (Key-Value): {} : {}", key, value);
|
||||||
|
});
|
||||||
|
|
||||||
|
ret.put("frontend-upstream-disposable-metadata", upstreamDisposableMetadatas);
|
||||||
|
|
||||||
|
// Get All Disposable metadata from upstream service
|
||||||
|
Map<String, String> localDisposableMetadatas = MetadataContextHolder.getAllDisposableMetadata(false);
|
||||||
|
localDisposableMetadatas.forEach((key, value) -> {
|
||||||
|
LOG.info("Local Custom Disposable Metadata (Key-Value): {} : {}", key, value);
|
||||||
|
});
|
||||||
|
|
||||||
|
ret.put("frontend-local-disposable-metadata", localDisposableMetadatas);
|
||||||
|
|
||||||
|
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 = new HashMap<>();
|
||||||
|
|
||||||
|
// Call remote service with RestTemplate
|
||||||
|
Map<String, Map<String, String>> middleResult = restTemplate.getForObject(
|
||||||
|
"http://MetadataMiddleService/metadata/service/middle/info", Map.class);
|
||||||
|
|
||||||
|
if (middleResult != null) {
|
||||||
|
ret.putAll(middleResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get Custom Metadata From Context
|
||||||
|
MetadataContext context = MetadataContextHolder.get();
|
||||||
|
Map<String, String> customMetadataMap = context.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||||
|
|
||||||
|
customMetadataMap.forEach((key, value) -> {
|
||||||
|
LOG.info("Metadata Middle Custom Metadata (Key-Value): {} : {}", key, value);
|
||||||
|
});
|
||||||
|
|
||||||
|
ret.put("frontend-transitive-metadata", customMetadataMap);
|
||||||
|
|
||||||
|
// Get All Disposable metadata from upstream service
|
||||||
|
Map<String, String> upstreamDisposableMetadatas = MetadataContextHolder.getAllDisposableMetadata(true);
|
||||||
|
upstreamDisposableMetadatas.forEach((key, value) -> {
|
||||||
|
LOG.info("Upstream Custom Disposable Metadata (Key-Value): {} : {}", key, value);
|
||||||
|
});
|
||||||
|
|
||||||
|
ret.put("frontend-upstream-disposable-metadata", upstreamDisposableMetadatas);
|
||||||
|
|
||||||
|
// Get All Disposable metadata from upstream service
|
||||||
|
Map<String, String> localDisposableMetadatas = MetadataContextHolder.getAllDisposableMetadata(false);
|
||||||
|
localDisposableMetadatas.forEach((key, value) -> {
|
||||||
|
LOG.info("Local Custom Disposable Metadata (Key-Value): {} : {}", key, value);
|
||||||
|
});
|
||||||
|
|
||||||
|
ret.put("frontend-local-disposable-metadata", localDisposableMetadatas);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* health check.
|
||||||
|
*
|
||||||
|
* @return health check info
|
||||||
|
*/
|
||||||
|
@GetMapping("/healthCheck")
|
||||||
|
public String healthCheck() {
|
||||||
|
return "pk ok";
|
||||||
|
}
|
||||||
|
}
|
9
spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCallerService.java → spring-cloud-tencent-examples/metadata-transfer-example/metadata-frontend/src/main/java/com/tencent/cloud/metadata/service/frontend/MetadataFrontendService.java
9
spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCallerService.java → spring-cloud-tencent-examples/metadata-transfer-example/metadata-frontend/src/main/java/com/tencent/cloud/metadata/service/frontend/MetadataFrontendService.java
13
spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCalleeService.java → spring-cloud-tencent-examples/metadata-transfer-example/metadata-frontend/src/main/java/com/tencent/cloud/metadata/service/frontend/MetadataMiddleService.java
13
spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCalleeService.java → spring-cloud-tencent-examples/metadata-transfer-example/metadata-frontend/src/main/java/com/tencent/cloud/metadata/service/frontend/MetadataMiddleService.java
7
spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCalleeServiceFallback.java → spring-cloud-tencent-examples/metadata-transfer-example/metadata-frontend/src/main/java/com/tencent/cloud/metadata/service/frontend/MetadataMiddleServiceFallback.java
7
spring-cloud-tencent-examples/metadata-transfer-example/metadata-caller-service/src/main/java/com/tencent/cloud/metadata/service/caller/MetadataCalleeServiceFallback.java → spring-cloud-tencent-examples/metadata-transfer-example/metadata-frontend/src/main/java/com/tencent/cloud/metadata/service/frontend/MetadataMiddleServiceFallback.java
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* 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.middle;
|
||||||
|
|
||||||
|
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 = "MetadataBackendService",
|
||||||
|
fallback = MetadataBackendServiceFallback.class)
|
||||||
|
public interface MetadataBackendService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get information of callee.
|
||||||
|
* @return information of callee
|
||||||
|
*/
|
||||||
|
@GetMapping("/metadata/service/backend/info")
|
||||||
|
Map<String, Map<String, String>> info();
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* 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.middle;
|
||||||
|
|
||||||
|
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 MetadataBackendServiceFallback implements MetadataBackendService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Map<String, String>> info() {
|
||||||
|
return Maps.newHashMap();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,122 @@
|
|||||||
|
/*
|
||||||
|
* 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.middle;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
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;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import static com.tencent.cloud.common.util.JacksonUtils.serialize2Json;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metadata callee controller.
|
||||||
|
*
|
||||||
|
* @author Palmer Xu
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/metadata/service/middle")
|
||||||
|
public class MetadataMiddleController {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(MetadataMiddleController.class);
|
||||||
|
|
||||||
|
@Value("${server.port:0}")
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
private final MetadataBackendService metadataBackendService;
|
||||||
|
|
||||||
|
private final RestTemplate restTemplate;
|
||||||
|
|
||||||
|
public MetadataMiddleController(MetadataBackendService metadataBackendService, RestTemplate restTemplate) {
|
||||||
|
this.metadataBackendService = metadataBackendService;
|
||||||
|
this.restTemplate = restTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get information of callee.
|
||||||
|
*
|
||||||
|
* @return information of callee
|
||||||
|
*/
|
||||||
|
@GetMapping("/info")
|
||||||
|
public Map<String, Map<String, String>> info() {
|
||||||
|
|
||||||
|
// Build result
|
||||||
|
Map<String, Map<String, String>> ret = new HashMap<>();
|
||||||
|
|
||||||
|
LOG.info("Metadata Middle Service [{}] is called.", port);
|
||||||
|
|
||||||
|
// Call remote service with RestTemplate
|
||||||
|
Map<String, Map<String, String>> backendResult = restTemplate.getForObject(
|
||||||
|
"http://MetadataBackendService/metadata/service/backend/info", Map.class);
|
||||||
|
|
||||||
|
if (backendResult != null) {
|
||||||
|
LOG.info("RestTemplate Backend Metadata");
|
||||||
|
LOG.info("\r{}", serialize2Json(backendResult, true));
|
||||||
|
backendResult.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call remote service with Feign
|
||||||
|
backendResult = metadataBackendService.info();
|
||||||
|
if (backendResult != null) {
|
||||||
|
LOG.info("Feign Backend Metadata");
|
||||||
|
LOG.info("\r{}", serialize2Json(backendResult, true));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (backendResult != null) {
|
||||||
|
ret.putAll(backendResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get Custom Metadata From Context
|
||||||
|
MetadataContext context = MetadataContextHolder.get();
|
||||||
|
Map<String, String> customMetadataMap = context.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||||
|
|
||||||
|
customMetadataMap.forEach((key, value) -> {
|
||||||
|
LOG.info("Metadata Middle Custom Metadata (Key-Value): {} : {}", key, value);
|
||||||
|
});
|
||||||
|
|
||||||
|
ret.put("middle-transitive-metadata", customMetadataMap);
|
||||||
|
|
||||||
|
// Get All Disposable metadata from upstream service
|
||||||
|
Map<String, String> upstreamDisposableMetadatas = MetadataContextHolder.getAllDisposableMetadata(true);
|
||||||
|
upstreamDisposableMetadatas.forEach((key, value) -> {
|
||||||
|
LOG.info("Upstream Custom Disposable Metadata (Key-Value): {} : {}", key, value);
|
||||||
|
});
|
||||||
|
|
||||||
|
ret.put("middle-upstream-disposable-metadata", upstreamDisposableMetadatas);
|
||||||
|
|
||||||
|
// Get All Disposable metadata from upstream service
|
||||||
|
Map<String, String> localDisposableMetadatas = MetadataContextHolder.getAllDisposableMetadata(false);
|
||||||
|
localDisposableMetadatas.forEach((key, value) -> {
|
||||||
|
LOG.info("Local Custom Disposable Metadata (Key-Value): {} : {}", key, value);
|
||||||
|
});
|
||||||
|
|
||||||
|
ret.put("middle-local-disposable-metadata", localDisposableMetadatas);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* 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.middle;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
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 callee application.
|
||||||
|
*
|
||||||
|
* @author Palmer Xu
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableFeignClients
|
||||||
|
public class MetadataMiddleService {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(MetadataMiddleService.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@LoadBalanced
|
||||||
|
public RestTemplate restTemplate() {
|
||||||
|
return new RestTemplate();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
server:
|
||||||
|
port: 48084
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: MetadataMiddleService
|
||||||
|
cloud:
|
||||||
|
polaris:
|
||||||
|
address: grpc://183.47.111.80:8091
|
||||||
|
namespace: default
|
||||||
|
enabled: true
|
||||||
|
discovery:
|
||||||
|
enabled: true
|
||||||
|
register: true
|
||||||
|
tencent:
|
||||||
|
metadata:
|
||||||
|
# Defined your metadata keys & values
|
||||||
|
content:
|
||||||
|
# Example: intransitive
|
||||||
|
CUSTOM-METADATA-KEY-LOCAL-2: CUSTOM-VALUE-LOCAL-2
|
||||||
|
# Example: transitive
|
||||||
|
CUSTOM-METADATA-KEY-TRANSITIVE-2: CUSTOM-VALUE-TRANSITIVE-2
|
||||||
|
# Example: disposable
|
||||||
|
CUSTOM-METADATA-KEY-DISPOSABLE: CUSTOM-VALUE-DISPOSABLE-MIDDLE
|
||||||
|
# Assigned which metadata key-value will be passed along the link
|
||||||
|
transitive:
|
||||||
|
- CUSTOM-METADATA-KEY-TRANSITIVE-2
|
||||||
|
disposable:
|
||||||
|
- CUSTOM-METADATA-KEY-DISPOSABLE
|
||||||
|
|
||||||
|
management:
|
||||||
|
endpoints:
|
||||||
|
web:
|
||||||
|
exposure:
|
||||||
|
include:
|
||||||
|
- polaris-metadata
|
Loading…
Reference in new issue