Add disposable samples .

pull/430/head
misselvexu 3 years ago
parent ce8c84638a
commit dae4726f00

@ -18,6 +18,7 @@
package com.tencent.cloud.common.metadata; package com.tencent.cloud.common.metadata;
import java.net.URLEncoder;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
@ -34,7 +35,9 @@ import org.springframework.util.StringUtils;
import static com.tencent.cloud.common.constant.ContextConstant.UTF_8; import static com.tencent.cloud.common.constant.ContextConstant.UTF_8;
import static com.tencent.cloud.common.constant.MetadataConstant.INTERNAL_METADATA_DISPOSABLE; import static com.tencent.cloud.common.constant.MetadataConstant.INTERNAL_METADATA_DISPOSABLE;
import static com.tencent.cloud.common.util.JacksonUtils.deserialize2Map; import static com.tencent.cloud.common.util.JacksonUtils.deserialize2Map;
import static com.tencent.cloud.common.util.JacksonUtils.serialize2Json;
import static java.net.URLDecoder.decode; import static java.net.URLDecoder.decode;
import static java.net.URLEncoder.encode;
/** /**
* Metadata Context Holder. * Metadata Context Holder.
@ -121,11 +124,14 @@ public final class MetadataContextHolder {
it.remove(); it.remove();
} }
} }
// reset
dynamicTransitiveMetadata.put(INTERNAL_METADATA_DISPOSABLE, encode(serialize2Json(keyStatus), UTF_8));
} }
} }
catch (Exception e) { catch (Exception e) {
LOGGER.error("Runtime system does not support utf-8 coding.", e); LOGGER.error("Runtime system does not support utf-8 coding.", e);
} }
} }
Map<String, String> staticTransitiveMetadata = Map<String, String> staticTransitiveMetadata =

@ -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.callee;
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 = "MetadataCalleeService2",
fallback = MetadataCallee2ServiceFallback.class)
public interface MetadataCallee2Service {
/**
* Get information of callee.
* @return information of callee
*/
@GetMapping("/metadata/service/callee2/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.callee;
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 MetadataCallee2ServiceFallback implements MetadataCallee2Service {
@Override
public Map<String, String> info() {
return Maps.newHashMap();
}
}

@ -28,6 +28,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/** /**
* Metadata callee controller. * Metadata callee controller.
@ -43,6 +44,15 @@ public class MetadataCalleeController {
@Value("${server.port:0}") @Value("${server.port:0}")
private int port; private int port;
private final MetadataCallee2Service metadataCallee2Service;
private final RestTemplate restTemplate;
public MetadataCalleeController(MetadataCallee2Service metadataCallee2Service, RestTemplate restTemplate) {
this.metadataCallee2Service = metadataCallee2Service;
this.restTemplate = restTemplate;
}
/** /**
* Get information of callee. * Get information of callee.
* @return information of callee * @return information of callee
@ -51,6 +61,14 @@ public class MetadataCalleeController {
public Map<String, String> info() { public Map<String, String> info() {
LOG.info("Metadata Service Callee [{}] is called.", port); LOG.info("Metadata Service Callee [{}] is called.", port);
// Call remote service with RestTemplate
Map<String, String> calleeMetadata = restTemplate.getForObject(
"http://MetadataCalleeService2/metadata/service/callee2/info",
Map.class);
calleeMetadata.forEach((key, value) -> {
LOG.info("Callee2 Metadata (Key-Value): {} : {}", key, value);
});
// Get Custom Metadata From Context // Get Custom Metadata From Context
MetadataContext context = MetadataContextHolder.get(); MetadataContext context = MetadataContextHolder.get();
Map<String, String> customMetadataMap = context.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); Map<String, String> customMetadataMap = context.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);

@ -19,6 +19,10 @@ package com.tencent.cloud.metadata.service.callee;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; 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. * Metadata callee application.
@ -26,10 +30,17 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* @author Palmer Xu * @author Palmer Xu
*/ */
@SpringBootApplication @SpringBootApplication
@EnableFeignClients
public class MetadataCalleeService { public class MetadataCalleeService {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(MetadataCalleeService.class, args); SpringApplication.run(MetadataCalleeService.class, args);
} }
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
} }

@ -5,9 +5,27 @@ spring:
name: MetadataCalleeService name: MetadataCalleeService
cloud: cloud:
polaris: polaris:
address: grpc://183.47.111.80:8091 address: grpc://127.0.0.1:8091
namespace: default namespace: default
enabled: true enabled: true
discovery: discovery:
enabled: true enabled: true
register: 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
# Assigned which metadata key-value will be passed along the link
transitive:
- CUSTOM-METADATA-KEY-TRANSITIVE-2
management:
endpoints:
web:
exposure:
include:
- polaris-metadata

@ -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-service2</artifactId>
<name>Spring Cloud Tencent Metadata Transfer Callee Service 2</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/callee2")
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-2 [{}] 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 MetadataCalleeService2 {
public static void main(String[] args) {
SpringApplication.run(MetadataCalleeService2.class, args);
}
}

@ -0,0 +1,20 @@
server:
port: 48088
spring:
application:
name: MetadataCalleeService2
cloud:
polaris:
address: grpc://127.0.0.1:8091
namespace: default
enabled: true
discovery:
enabled: true
register: true
management:
endpoints:
web:
exposure:
include:
- polaris-metadata

@ -5,7 +5,7 @@ spring:
name: MetadataCallerService name: MetadataCallerService
cloud: cloud:
polaris: polaris:
address: grpc://183.47.111.80:8091 address: grpc://127.0.0.1:8091
namespace: default namespace: default
enabled: true enabled: true
discovery: discovery:
@ -19,11 +19,16 @@ spring:
content: content:
# Example: intransitive # Example: intransitive
CUSTOM-METADATA-KEY-LOCAL: CUSTOM-VALUE-LOCAL CUSTOM-METADATA-KEY-LOCAL: CUSTOM-VALUE-LOCAL
#
CUSTOM-METADATA-KEY-DISPOSABLE: CUSTOM-VALUE-DISPOSABLE
# Example: transitive # Example: transitive
CUSTOM-METADATA-KEY-TRANSITIVE: CUSTOM-VALUE-TRANSITIVE CUSTOM-METADATA-KEY-TRANSITIVE: CUSTOM-VALUE-TRANSITIVE
# Assigned which metadata key-value will be passed along the link # Assigned which metadata key-value will be passed along the link
transitive: transitive:
- CUSTOM-METADATA-KEY-TRANSITIVE - CUSTOM-METADATA-KEY-TRANSITIVE
- CUSTOM-METADATA-KEY-DISPOSABLE
disposable:
- CUSTOM-METADATA-KEY-DISPOSABLE
management: management:
endpoints: endpoints:
web: web:

@ -16,6 +16,7 @@
<modules> <modules>
<module>metadata-callee-service</module> <module>metadata-callee-service</module>
<module>metadata-callee-service2</module>
<module>metadata-caller-service</module> <module>metadata-caller-service</module>
</modules> </modules>

Loading…
Cancel
Save