Merge branch '2021.0' of https://github.com/Tencent/spring-cloud-tencent into config_data
commit
ec18f4d0d5
@ -1,117 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2007-present the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
import java.net.*;
|
|
||||||
import java.io.*;
|
|
||||||
import java.nio.channels.*;
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
public class MavenWrapperDownloader {
|
|
||||||
|
|
||||||
private static final String WRAPPER_VERSION = "0.5.6";
|
|
||||||
/**
|
|
||||||
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
|
|
||||||
*/
|
|
||||||
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
|
|
||||||
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
|
|
||||||
* use instead of the default one.
|
|
||||||
*/
|
|
||||||
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
|
|
||||||
".mvn/wrapper/maven-wrapper.properties";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Path where the maven-wrapper.jar will be saved to.
|
|
||||||
*/
|
|
||||||
private static final String MAVEN_WRAPPER_JAR_PATH =
|
|
||||||
".mvn/wrapper/maven-wrapper.jar";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Name of the property which should be used to override the default download url for the wrapper.
|
|
||||||
*/
|
|
||||||
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
|
|
||||||
|
|
||||||
public static void main(String args[]) {
|
|
||||||
System.out.println("- Downloader started");
|
|
||||||
File baseDirectory = new File(args[0]);
|
|
||||||
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
|
|
||||||
|
|
||||||
// If the maven-wrapper.properties exists, read it and check if it contains a custom
|
|
||||||
// wrapperUrl parameter.
|
|
||||||
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
|
|
||||||
String url = DEFAULT_DOWNLOAD_URL;
|
|
||||||
if(mavenWrapperPropertyFile.exists()) {
|
|
||||||
FileInputStream mavenWrapperPropertyFileInputStream = null;
|
|
||||||
try {
|
|
||||||
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
|
|
||||||
Properties mavenWrapperProperties = new Properties();
|
|
||||||
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
|
|
||||||
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
|
|
||||||
} catch (IOException e) {
|
|
||||||
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
if(mavenWrapperPropertyFileInputStream != null) {
|
|
||||||
mavenWrapperPropertyFileInputStream.close();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
// Ignore ...
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
System.out.println("- Downloading from: " + url);
|
|
||||||
|
|
||||||
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
|
|
||||||
if(!outputFile.getParentFile().exists()) {
|
|
||||||
if(!outputFile.getParentFile().mkdirs()) {
|
|
||||||
System.out.println(
|
|
||||||
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
|
|
||||||
try {
|
|
||||||
downloadFileFromURL(url, outputFile);
|
|
||||||
System.out.println("Done");
|
|
||||||
System.exit(0);
|
|
||||||
} catch (Throwable e) {
|
|
||||||
System.out.println("- Error downloading");
|
|
||||||
e.printStackTrace();
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
|
|
||||||
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
|
|
||||||
String username = System.getenv("MVNW_USERNAME");
|
|
||||||
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
|
|
||||||
Authenticator.setDefault(new Authenticator() {
|
|
||||||
@Override
|
|
||||||
protected PasswordAuthentication getPasswordAuthentication() {
|
|
||||||
return new PasswordAuthentication(username, password);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
URL website = new URL(urlString);
|
|
||||||
ReadableByteChannel rbc;
|
|
||||||
rbc = Channels.newChannel(website.openStream());
|
|
||||||
FileOutputStream fos = new FileOutputStream(destination);
|
|
||||||
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
|
|
||||||
fos.close();
|
|
||||||
rbc.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Binary file not shown.
@ -1,2 +1,2 @@
|
|||||||
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip
|
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.6/apache-maven-3.8.6-bin.zip
|
||||||
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar
|
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar
|
||||||
|
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
|
@ -0,0 +1,128 @@
|
|||||||
|
/*
|
||||||
|
* 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.polaris.loadbalancer;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.pojo.PolarisServiceInstance;
|
||||||
|
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||||
|
import com.tencent.cloud.polaris.loadbalancer.config.PolarisLoadBalancerProperties;
|
||||||
|
import com.tencent.polaris.api.pojo.Instance;
|
||||||
|
import com.tencent.polaris.router.api.core.RouterAPI;
|
||||||
|
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse;
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockedStatic;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.Request;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.Response;
|
||||||
|
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
|
||||||
|
|
||||||
|
import static com.tencent.cloud.common.metadata.MetadataContext.LOCAL_NAMESPACE;
|
||||||
|
import static com.tencent.cloud.common.metadata.MetadataContext.LOCAL_SERVICE;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link PolarisLoadBalancer}.
|
||||||
|
*
|
||||||
|
* @author rod.xu
|
||||||
|
*/
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class PolarisLoadBalancerTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private RouterAPI routerAPI;
|
||||||
|
@Mock
|
||||||
|
private ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider;
|
||||||
|
@Mock
|
||||||
|
private PolarisLoadBalancerProperties loadBalancerProperties;
|
||||||
|
|
||||||
|
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||||
|
private static Instance testInstance;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void beforeClass() {
|
||||||
|
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||||
|
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||||
|
.thenReturn("unit-test");
|
||||||
|
|
||||||
|
testInstance = Instance.createDefaultInstance("instance-id", LOCAL_NAMESPACE,
|
||||||
|
LOCAL_SERVICE, "host", 8090);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void afterClass() {
|
||||||
|
mockedApplicationContextAwareUtils.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void chooseNormalLogicTest_thenReturnAvailablePolarisInstance() {
|
||||||
|
|
||||||
|
Request request = Mockito.mock(Request.class);
|
||||||
|
List<ServiceInstance> mockInstanceList = new ArrayList<>();
|
||||||
|
mockInstanceList.add(new PolarisServiceInstance(testInstance));
|
||||||
|
|
||||||
|
ServiceInstanceListSupplier serviceInstanceListSupplier = Mockito.mock(ServiceInstanceListSupplier.class);
|
||||||
|
when(serviceInstanceListSupplier.get(request)).thenReturn(Flux.just(mockInstanceList));
|
||||||
|
|
||||||
|
when(supplierObjectProvider.getIfAvailable(any())).thenReturn(serviceInstanceListSupplier);
|
||||||
|
when(loadBalancerProperties.getEnabled()).thenReturn(true);
|
||||||
|
|
||||||
|
ProcessLoadBalanceResponse mockLbRes = new ProcessLoadBalanceResponse(testInstance);
|
||||||
|
when(routerAPI.processLoadBalance(any())).thenReturn(mockLbRes);
|
||||||
|
|
||||||
|
// request construct and execute invoke
|
||||||
|
PolarisLoadBalancer polarisLoadBalancer = new PolarisLoadBalancer(LOCAL_SERVICE, supplierObjectProvider,
|
||||||
|
loadBalancerProperties, routerAPI);
|
||||||
|
Mono<Response<ServiceInstance>> responseMono = polarisLoadBalancer.choose(request);
|
||||||
|
ServiceInstance serviceInstance = responseMono.block().getServer();
|
||||||
|
|
||||||
|
// verify method has invoked
|
||||||
|
verify(loadBalancerProperties).getEnabled();
|
||||||
|
verify(supplierObjectProvider).getIfAvailable(any());
|
||||||
|
|
||||||
|
//result assert
|
||||||
|
Assertions.assertThat(serviceInstance).isNotNull();
|
||||||
|
Assertions.assertThat(serviceInstance instanceof PolarisServiceInstance).isTrue();
|
||||||
|
|
||||||
|
PolarisServiceInstance polarisServiceInstance = (PolarisServiceInstance) serviceInstance;
|
||||||
|
|
||||||
|
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getId()).isEqualTo("instance-id");
|
||||||
|
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getNamespace()).isEqualTo(LOCAL_NAMESPACE);
|
||||||
|
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getService()).isEqualTo(LOCAL_SERVICE);
|
||||||
|
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getHost()).isEqualTo("host");
|
||||||
|
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getPort()).isEqualTo(8090);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,94 @@
|
|||||||
|
/*
|
||||||
|
* 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.polaris.loadbalancer;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.pojo.PolarisServiceInstance;
|
||||||
|
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockedStatic;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
|
||||||
|
|
||||||
|
import static com.tencent.cloud.common.metadata.MetadataContext.LOCAL_NAMESPACE;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link PolarisServiceInstanceListSupplier}.
|
||||||
|
*
|
||||||
|
* @author rod.xu
|
||||||
|
*/
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class PolarisServiceInstanceListSupplierTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private ServiceInstanceListSupplier serviceInstanceListSupplier;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void chooseInstancesTest() {
|
||||||
|
try (MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils = Mockito
|
||||||
|
.mockStatic(ApplicationContextAwareUtils.class)) {
|
||||||
|
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||||
|
.thenReturn("test-unit");
|
||||||
|
|
||||||
|
PolarisServiceInstanceListSupplier instanceListSupplier =
|
||||||
|
new PolarisServiceInstanceListSupplier(serviceInstanceListSupplier);
|
||||||
|
|
||||||
|
List<ServiceInstance> allServers = new ArrayList<>();
|
||||||
|
ServiceInstance instance1 = new DefaultServiceInstance("unit-test-instanceId-01",
|
||||||
|
"unit-test-serviceId", "unit-test-host-01", 8090, false);
|
||||||
|
ServiceInstance instance2 = new DefaultServiceInstance("unit-test-instanceId-02",
|
||||||
|
"unit-test-serviceId", "unit-test-host-02", 8090, false);
|
||||||
|
|
||||||
|
allServers.add(instance1);
|
||||||
|
allServers.add(instance2);
|
||||||
|
|
||||||
|
List<ServiceInstance> polarisInstanceList = instanceListSupplier.chooseInstances(allServers);
|
||||||
|
|
||||||
|
Assertions.assertThat(polarisInstanceList).isNotNull();
|
||||||
|
Assertions.assertThat(polarisInstanceList.size()).isEqualTo(allServers.size());
|
||||||
|
|
||||||
|
for (ServiceInstance serviceInstance : polarisInstanceList) {
|
||||||
|
Assertions.assertThat(serviceInstance instanceof PolarisServiceInstance).isTrue();
|
||||||
|
|
||||||
|
PolarisServiceInstance polarisServiceInstance = (PolarisServiceInstance) serviceInstance;
|
||||||
|
|
||||||
|
Assertions.assertThat(polarisServiceInstance.isSecure()).isFalse();
|
||||||
|
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getService())
|
||||||
|
.isEqualTo("unit-test-serviceId");
|
||||||
|
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getNamespace())
|
||||||
|
.isEqualTo(LOCAL_NAMESPACE);
|
||||||
|
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getPort()).isEqualTo(8090);
|
||||||
|
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getId()
|
||||||
|
.startsWith("unit-test-instanceId")).isTrue();
|
||||||
|
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getHost()
|
||||||
|
.startsWith("unit-test-host")).isTrue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
* 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.rpc.enhancement.config;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.springframework.http.HttpStatus.MOVED_PERMANENTLY;
|
||||||
|
import static org.springframework.http.HttpStatus.MULTIPLE_CHOICES;
|
||||||
|
import static org.springframework.http.HttpStatus.Series.CLIENT_ERROR;
|
||||||
|
import static org.springframework.http.HttpStatus.Series.SERVER_ERROR;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test For {@link RpcEnhancementReporterProperties}.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = RpcEnhancementReporterPropertiesTest.TestApplication.class)
|
||||||
|
@ActiveProfiles("test")
|
||||||
|
public class RpcEnhancementReporterPropertiesTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RpcEnhancementReporterProperties rpcEnhancementReporterProperties;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDefaultInitialization() {
|
||||||
|
assertThat(rpcEnhancementReporterProperties).isNotNull();
|
||||||
|
assertThat(rpcEnhancementReporterProperties.isIgnoreInternalServerError()).isFalse();
|
||||||
|
assertThat(rpcEnhancementReporterProperties.getSeries()).isNotEmpty();
|
||||||
|
assertThat(rpcEnhancementReporterProperties.getSeries().get(0)).isEqualTo(CLIENT_ERROR);
|
||||||
|
assertThat(rpcEnhancementReporterProperties.getSeries().get(1)).isEqualTo(SERVER_ERROR);
|
||||||
|
assertThat(rpcEnhancementReporterProperties.getStatuses()).isNotEmpty();
|
||||||
|
assertThat(rpcEnhancementReporterProperties.getStatuses().get(0)).isEqualTo(MULTIPLE_CHOICES);
|
||||||
|
assertThat(rpcEnhancementReporterProperties.getStatuses().get(1)).isEqualTo(MOVED_PERMANENTLY);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
protected static class TestApplication {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,8 @@
|
|||||||
spring.cloud.polaris.stat.enabled=true
|
spring.cloud.polaris.stat.enabled=true
|
||||||
spring.cloud.polaris.stat.port=20000
|
spring.cloud.polaris.stat.port=20000
|
||||||
spring.cloud.polaris.stat.path=/xxx
|
spring.cloud.polaris.stat.path=/xxx
|
||||||
|
spring.cloud.tencent.rpc-enhancement.reporter.ignore-internal-server-error=false
|
||||||
|
spring.cloud.tencent.rpc-enhancement.reporter.series[0]=CLIENT_ERROR
|
||||||
|
spring.cloud.tencent.rpc-enhancement.reporter.series[1]=SERVER_ERROR
|
||||||
|
spring.cloud.tencent.rpc-enhancement.reporter.statuses[0]=MULTIPLE_CHOICES
|
||||||
|
spring.cloud.tencent.rpc-enhancement.reporter.statuses[1]=MOVED_PERMANENTLY
|
||||||
|
Loading…
Reference in new issue