commit
593abd7b46
@ -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
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
|
|
||||||
com.tencent.cloud.polaris.circuitbreaker.config.PolarisCircuitBreakerBootstrapConfiguration
|
|
||||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
com.tencent.cloud.polaris.circuitbreaker.config.PolarisCircuitBreakerAutoConfiguration
|
com.tencent.cloud.polaris.circuitbreaker.config.PolarisCircuitBreakerAutoConfiguration
|
||||||
|
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
|
||||||
|
com.tencent.cloud.polaris.circuitbreaker.config.PolarisCircuitBreakerBootstrapConfiguration
|
||||||
|
18
spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/PolarisFeignClientAutoConfigurationTest.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisCircuitBreakerAutoConfigurationTest.java
18
spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/PolarisFeignClientAutoConfigurationTest.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisCircuitBreakerAutoConfigurationTest.java
10
spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerBootstrapConfigurationTest.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisCircuitBreakerBootstrapConfigurationTest.java
10
spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerBootstrapConfigurationTest.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisCircuitBreakerBootstrapConfigurationTest.java
@ -1,15 +0,0 @@
|
|||||||
spring:
|
|
||||||
cloud:
|
|
||||||
polaris:
|
|
||||||
address: grpc://127.0.0.1:8091
|
|
||||||
|
|
||||||
feign:
|
|
||||||
polaris:
|
|
||||||
enable: true
|
|
||||||
compression:
|
|
||||||
request:
|
|
||||||
enabled: false
|
|
||||||
mime-types: text/xml,application/xml,application/json
|
|
||||||
min-request-size: 2048
|
|
||||||
response:
|
|
||||||
enabled: false
|
|
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,56 @@
|
|||||||
|
/*
|
||||||
|
* 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.context.logging;
|
||||||
|
|
||||||
|
import com.tencent.polaris.logging.PolarisLogging;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
|
||||||
|
import org.springframework.boot.context.event.ApplicationFailedEvent;
|
||||||
|
import org.springframework.boot.context.logging.LoggingApplicationListener;
|
||||||
|
import org.springframework.context.ApplicationEvent;
|
||||||
|
import org.springframework.context.event.GenericApplicationListener;
|
||||||
|
import org.springframework.core.ResolvableType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reload of Polaris logging configuration.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public class PolarisLoggingApplicationListener implements GenericApplicationListener {
|
||||||
|
|
||||||
|
private static final int ORDER = LoggingApplicationListener.DEFAULT_ORDER + 2;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supportsEventType(ResolvableType resolvableType) {
|
||||||
|
Class<?> type = resolvableType.getRawClass();
|
||||||
|
if (type == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return ApplicationEnvironmentPreparedEvent.class.isAssignableFrom(type)
|
||||||
|
|| ApplicationFailedEvent.class.isAssignableFrom(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return ORDER;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onApplicationEvent(ApplicationEvent applicationEvent) {
|
||||||
|
PolarisLogging.getInstance().loadConfiguration();
|
||||||
|
}
|
||||||
|
}
|
@ -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,62 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>spring-cloud-tencent</artifactId>
|
||||||
|
<groupId>com.tencent.cloud</groupId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>spring-cloud-tencent-rpc-enhancement</artifactId>
|
||||||
|
<name>Spring Cloud Starter Tencent RPC Enhancement</name>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- Spring Cloud Tencent dependencies start -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.tencent.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-tencent-polaris-loadbalancer</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Spring Cloud Tencent dependencies end -->
|
||||||
|
|
||||||
|
<!-- Polaris dependencies start -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.tencent.polaris</groupId>
|
||||||
|
<artifactId>stat-prometheus</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Polaris dependencies end -->
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.tencent.polaris</groupId>
|
||||||
|
<artifactId>polaris-test-common</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-inline</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,119 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
|
|
||||||
|
import static org.springframework.http.HttpStatus.BAD_GATEWAY;
|
||||||
|
import static org.springframework.http.HttpStatus.BANDWIDTH_LIMIT_EXCEEDED;
|
||||||
|
import static org.springframework.http.HttpStatus.GATEWAY_TIMEOUT;
|
||||||
|
import static org.springframework.http.HttpStatus.HTTP_VERSION_NOT_SUPPORTED;
|
||||||
|
import static org.springframework.http.HttpStatus.INSUFFICIENT_STORAGE;
|
||||||
|
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
|
||||||
|
import static org.springframework.http.HttpStatus.LOOP_DETECTED;
|
||||||
|
import static org.springframework.http.HttpStatus.NETWORK_AUTHENTICATION_REQUIRED;
|
||||||
|
import static org.springframework.http.HttpStatus.NOT_EXTENDED;
|
||||||
|
import static org.springframework.http.HttpStatus.NOT_IMPLEMENTED;
|
||||||
|
import static org.springframework.http.HttpStatus.SERVICE_UNAVAILABLE;
|
||||||
|
import static org.springframework.http.HttpStatus.VARIANT_ALSO_NEGOTIATES;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract Polaris Reporter Adapter .
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:iskp.me@gmail.com">Elve.Xu</a> 2022-07-11
|
||||||
|
*/
|
||||||
|
public abstract class AbstractPolarisReporterAdapter {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(AbstractPolarisReporterAdapter.class);
|
||||||
|
private static final List<HttpStatus> HTTP_STATUSES = toList(NOT_IMPLEMENTED, BAD_GATEWAY,
|
||||||
|
SERVICE_UNAVAILABLE, GATEWAY_TIMEOUT, HTTP_VERSION_NOT_SUPPORTED, VARIANT_ALSO_NEGOTIATES,
|
||||||
|
INSUFFICIENT_STORAGE, LOOP_DETECTED, BANDWIDTH_LIMIT_EXCEEDED, NOT_EXTENDED, NETWORK_AUTHENTICATION_REQUIRED);
|
||||||
|
protected final RpcEnhancementReporterProperties properties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor With {@link RpcEnhancementReporterProperties} .
|
||||||
|
*
|
||||||
|
* @param properties instance of {@link RpcEnhancementReporterProperties}.
|
||||||
|
*/
|
||||||
|
protected AbstractPolarisReporterAdapter(RpcEnhancementReporterProperties properties) {
|
||||||
|
this.properties = properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert items to List.
|
||||||
|
*
|
||||||
|
* @param items item arrays
|
||||||
|
* @param <T> Object Generics.
|
||||||
|
* @return list
|
||||||
|
*/
|
||||||
|
@SafeVarargs
|
||||||
|
private static <T> List<T> toList(T... items) {
|
||||||
|
return new ArrayList<>(Arrays.asList(items));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback after completion of request processing, Check if business meltdown reporting is required.
|
||||||
|
*
|
||||||
|
* @param httpStatus request http status code
|
||||||
|
* @return true , otherwise return false .
|
||||||
|
*/
|
||||||
|
protected boolean apply(@Nullable HttpStatus httpStatus) {
|
||||||
|
if (Objects.isNull(httpStatus)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// statuses > series
|
||||||
|
List<HttpStatus> status = properties.getStatuses();
|
||||||
|
|
||||||
|
if (status.isEmpty()) {
|
||||||
|
List<HttpStatus.Series> series = properties.getSeries();
|
||||||
|
// Check INTERNAL_SERVER_ERROR (500) status.
|
||||||
|
if (properties.isIgnoreInternalServerError() && Objects.equals(httpStatus, INTERNAL_SERVER_ERROR)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (series.isEmpty()) {
|
||||||
|
return HTTP_STATUSES.contains(httpStatus);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
try {
|
||||||
|
return series.contains(HttpStatus.Series.valueOf(httpStatus));
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
LOG.warn("Decode http status failed.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Use the user-specified fuse status code.
|
||||||
|
return status.contains(httpStatus);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// DEFAULT RETURN FALSE.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,113 @@
|
|||||||
|
/*
|
||||||
|
* 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 java.util.List;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.feign.EnhancedFeignBeanPostProcessor;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.feign.plugin.EnhancedFeignPlugin;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.feign.plugin.reporter.ExceptionPolarisReporter;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.feign.plugin.reporter.SuccessPolarisReporter;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.resttemplate.EnhancedRestTemplateModifier;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.resttemplate.EnhancedRestTemplateReporter;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.resttemplate.PolarisResponseErrorHandler;
|
||||||
|
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auto Configuration for Polaris {@link feign.Feign} OR {@link RestTemplate} which can automatically bring in the call
|
||||||
|
* results for reporting.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:iskp.me@gmail.com">Palmer.Xu</a> 2022-06-29
|
||||||
|
*/
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@ConditionalOnProperty(value = "spring.cloud.tencent.rpc-enhancement.enabled", havingValue = "true", matchIfMissing = true)
|
||||||
|
@EnableConfigurationProperties(RpcEnhancementReporterProperties.class)
|
||||||
|
@AutoConfigureAfter(PolarisContextAutoConfiguration.class)
|
||||||
|
public class RpcEnhancementAutoConfiguration {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration for Polaris {@link feign.Feign} which can automatically bring in the call
|
||||||
|
* results for reporting.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@ConditionalOnClass(name = "org.springframework.cloud.openfeign.FeignAutoConfiguration")
|
||||||
|
@AutoConfigureBefore(name = "org.springframework.cloud.openfeign.FeignAutoConfiguration")
|
||||||
|
protected static class PolarisFeignClientAutoConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Order(HIGHEST_PRECEDENCE)
|
||||||
|
public EnhancedFeignBeanPostProcessor polarisFeignBeanPostProcessor(
|
||||||
|
@Autowired(required = false) List<EnhancedFeignPlugin> enhancedFeignPlugins) {
|
||||||
|
return new EnhancedFeignBeanPostProcessor(enhancedFeignPlugins);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
static class PolarisReporterConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public SuccessPolarisReporter successPolarisReporter(RpcEnhancementReporterProperties properties) {
|
||||||
|
return new SuccessPolarisReporter(properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ExceptionPolarisReporter exceptionPolarisReporter() {
|
||||||
|
return new ExceptionPolarisReporter();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration for Polaris {@link RestTemplate} which can automatically bring in the call
|
||||||
|
* results for reporting.
|
||||||
|
*
|
||||||
|
* @author wh 2022/6/21
|
||||||
|
*/
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@ConditionalOnClass(name = "org.springframework.web.client.RestTemplate")
|
||||||
|
protected static class PolarisRestTemplateAutoConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public EnhancedRestTemplateReporter polarisRestTemplateResponseErrorHandler(
|
||||||
|
RpcEnhancementReporterProperties properties, ConsumerAPI consumerAPI,
|
||||||
|
@Autowired(required = false) PolarisResponseErrorHandler polarisResponseErrorHandler) {
|
||||||
|
return new EnhancedRestTemplateReporter(properties, consumerAPI, polarisResponseErrorHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public EnhancedRestTemplateModifier polarisRestTemplateBeanPostProcessor(
|
||||||
|
EnhancedRestTemplateReporter restTemplateResponseErrorHandler) {
|
||||||
|
return new EnhancedRestTemplateModifier(restTemplateResponseErrorHandler);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,86 @@
|
|||||||
|
/*
|
||||||
|
* 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 java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Properties of Polaris CircuitBreaker .
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:iskp.me@gmail.com">Elve.Xu</a> 2022-07-08
|
||||||
|
*/
|
||||||
|
@ConfigurationProperties("spring.cloud.tencent.rpc-enhancement.reporter")
|
||||||
|
public class RpcEnhancementReporterProperties {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specify the Http status code(s) that needs to be reported as FAILED.
|
||||||
|
*/
|
||||||
|
private List<HttpStatus> statuses = new ArrayList<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specify List of HTTP status series that needs to be reported as FAILED when status list is empty.
|
||||||
|
*/
|
||||||
|
private List<HttpStatus.Series> series = toList(HttpStatus.Series.SERVER_ERROR);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If ignore "Internal Server Error Http Status Code (500)",
|
||||||
|
* Only takes effect if the attribute {@link RpcEnhancementReporterProperties#series} is not empty.
|
||||||
|
*/
|
||||||
|
private boolean ignoreInternalServerError = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert items to List.
|
||||||
|
*
|
||||||
|
* @param items item arrays
|
||||||
|
* @param <T> Object Generics.
|
||||||
|
* @return list
|
||||||
|
*/
|
||||||
|
@SafeVarargs
|
||||||
|
private static <T> List<T> toList(T... items) {
|
||||||
|
return new ArrayList<>(Arrays.asList(items));
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<HttpStatus> getStatuses() {
|
||||||
|
return statuses;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStatuses(List<HttpStatus> statuses) {
|
||||||
|
this.statuses = statuses;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<HttpStatus.Series> getSeries() {
|
||||||
|
return series;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSeries(List<HttpStatus.Series> series) {
|
||||||
|
this.series = series;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isIgnoreInternalServerError() {
|
||||||
|
return ignoreInternalServerError;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIgnoreInternalServerError(boolean ignoreInternalServerError) {
|
||||||
|
this.ignoreInternalServerError = ignoreInternalServerError;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,153 @@
|
|||||||
|
/*
|
||||||
|
* 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.feign;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import com.tencent.cloud.rpc.enhancement.feign.plugin.EnhancedFeignContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.feign.plugin.EnhancedFeignPlugin;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.feign.plugin.EnhancedFeignPluginType;
|
||||||
|
import feign.Client;
|
||||||
|
import feign.Request;
|
||||||
|
import feign.Request.Options;
|
||||||
|
import feign.Response;
|
||||||
|
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
import static feign.Util.checkNotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrap for {@link Client}.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public class EnhancedFeignClient implements Client {
|
||||||
|
|
||||||
|
private final Client delegate;
|
||||||
|
|
||||||
|
private List<EnhancedFeignPlugin> preEnhancedFeignPlugins;
|
||||||
|
|
||||||
|
private List<EnhancedFeignPlugin> postEnhancedFeignPlugins;
|
||||||
|
|
||||||
|
private List<EnhancedFeignPlugin> exceptionEnhancedFeignPlugins;
|
||||||
|
|
||||||
|
private List<EnhancedFeignPlugin> finallyEnhancedFeignPlugins;
|
||||||
|
|
||||||
|
public EnhancedFeignClient(Client target, List<EnhancedFeignPlugin> enhancedFeignPlugins) {
|
||||||
|
this.delegate = checkNotNull(target, "target");
|
||||||
|
|
||||||
|
// Init the EnhancedFeignPlugins list.
|
||||||
|
this.preEnhancedFeignPlugins = new ArrayList<>();
|
||||||
|
this.postEnhancedFeignPlugins = new ArrayList<>();
|
||||||
|
this.exceptionEnhancedFeignPlugins = new ArrayList<>();
|
||||||
|
this.finallyEnhancedFeignPlugins = new ArrayList<>();
|
||||||
|
if (!CollectionUtils.isEmpty(enhancedFeignPlugins)) {
|
||||||
|
for (EnhancedFeignPlugin feignPlugin : enhancedFeignPlugins) {
|
||||||
|
if (feignPlugin.getType().equals(EnhancedFeignPluginType.PRE)) {
|
||||||
|
this.preEnhancedFeignPlugins.add(feignPlugin);
|
||||||
|
}
|
||||||
|
else if (feignPlugin.getType().equals(EnhancedFeignPluginType.POST)) {
|
||||||
|
this.postEnhancedFeignPlugins.add(feignPlugin);
|
||||||
|
}
|
||||||
|
else if (feignPlugin.getType().equals(EnhancedFeignPluginType.EXCEPTION)) {
|
||||||
|
this.exceptionEnhancedFeignPlugins.add(feignPlugin);
|
||||||
|
}
|
||||||
|
else if (feignPlugin.getType().equals(EnhancedFeignPluginType.FINALLY)) {
|
||||||
|
this.finallyEnhancedFeignPlugins.add(feignPlugin);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Set the ordered enhanced feign plugins.
|
||||||
|
this.preEnhancedFeignPlugins = getSortedEnhancedFeignPlugin(this.preEnhancedFeignPlugins);
|
||||||
|
this.postEnhancedFeignPlugins = getSortedEnhancedFeignPlugin(this.postEnhancedFeignPlugins);
|
||||||
|
this.exceptionEnhancedFeignPlugins = getSortedEnhancedFeignPlugin(this.exceptionEnhancedFeignPlugins);
|
||||||
|
this.finallyEnhancedFeignPlugins = getSortedEnhancedFeignPlugin(this.finallyEnhancedFeignPlugins);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Response execute(Request request, Options options) throws IOException {
|
||||||
|
EnhancedFeignContext enhancedFeignContext = new EnhancedFeignContext();
|
||||||
|
enhancedFeignContext.setRequest(request);
|
||||||
|
enhancedFeignContext.setOptions(options);
|
||||||
|
|
||||||
|
// Run pre enhanced feign plugins.
|
||||||
|
for (EnhancedFeignPlugin plugin : preEnhancedFeignPlugins) {
|
||||||
|
try {
|
||||||
|
plugin.run(enhancedFeignContext);
|
||||||
|
}
|
||||||
|
catch (Throwable throwable) {
|
||||||
|
plugin.handlerThrowable(enhancedFeignContext, throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Response response = delegate.execute(request, options);
|
||||||
|
enhancedFeignContext.setResponse(response);
|
||||||
|
|
||||||
|
// Run post enhanced feign plugins.
|
||||||
|
for (EnhancedFeignPlugin plugin : postEnhancedFeignPlugins) {
|
||||||
|
try {
|
||||||
|
plugin.run(enhancedFeignContext);
|
||||||
|
}
|
||||||
|
catch (Throwable throwable) {
|
||||||
|
plugin.handlerThrowable(enhancedFeignContext, throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
catch (IOException origin) {
|
||||||
|
enhancedFeignContext.setException(origin);
|
||||||
|
// Run exception enhanced feign plugins.
|
||||||
|
for (EnhancedFeignPlugin plugin : exceptionEnhancedFeignPlugins) {
|
||||||
|
try {
|
||||||
|
plugin.run(enhancedFeignContext);
|
||||||
|
}
|
||||||
|
catch (Throwable throwable) {
|
||||||
|
plugin.handlerThrowable(enhancedFeignContext, throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw origin;
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
// Run finally enhanced feign plugins.
|
||||||
|
for (EnhancedFeignPlugin plugin : finallyEnhancedFeignPlugins) {
|
||||||
|
try {
|
||||||
|
plugin.run(enhancedFeignContext);
|
||||||
|
}
|
||||||
|
catch (Throwable throwable) {
|
||||||
|
plugin.handlerThrowable(enhancedFeignContext, throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ascending, which means the lower order number, the earlier executing enhanced feign plugin.
|
||||||
|
*
|
||||||
|
* @return sorted feign pre plugin list
|
||||||
|
*/
|
||||||
|
private List<EnhancedFeignPlugin> getSortedEnhancedFeignPlugin(List<EnhancedFeignPlugin> preEnhancedFeignPlugins) {
|
||||||
|
return new ArrayList<>(preEnhancedFeignPlugins)
|
||||||
|
.stream()
|
||||||
|
.sorted(Comparator.comparing(EnhancedFeignPlugin::getOrder))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
* 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.feign.plugin;
|
||||||
|
|
||||||
|
import feign.Request;
|
||||||
|
import feign.Response;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Context used by EnhancedFeignPlugin.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public class EnhancedFeignContext {
|
||||||
|
|
||||||
|
private Request request;
|
||||||
|
|
||||||
|
private Request.Options options;
|
||||||
|
|
||||||
|
private Response response;
|
||||||
|
|
||||||
|
private Exception exception;
|
||||||
|
|
||||||
|
public Request getRequest() {
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRequest(Request request) {
|
||||||
|
this.request = request;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Request.Options getOptions() {
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOptions(Request.Options options) {
|
||||||
|
this.options = options;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Response getResponse() {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResponse(Response response) {
|
||||||
|
this.response = response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Exception getException() {
|
||||||
|
return exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setException(Exception exception) {
|
||||||
|
this.exception = exception;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* 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.feign.plugin;
|
||||||
|
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pre plugin used by EnhancedFeignClient.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public interface EnhancedFeignPlugin extends Ordered {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get name of plugin.
|
||||||
|
*
|
||||||
|
* @return name
|
||||||
|
*/
|
||||||
|
default String getName() {
|
||||||
|
return this.getClass().getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get type of plugin.
|
||||||
|
*
|
||||||
|
* @return {@link EnhancedFeignPluginType}
|
||||||
|
*/
|
||||||
|
EnhancedFeignPluginType getType();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the plugin.
|
||||||
|
*
|
||||||
|
* @param context context in enhanced feign client.
|
||||||
|
* @throws Throwable throwable thrown from run method.
|
||||||
|
*/
|
||||||
|
void run(EnhancedFeignContext context) throws Throwable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler throwable from {@link EnhancedFeignPlugin#run(EnhancedFeignContext)}.
|
||||||
|
*
|
||||||
|
* @param context context in enhanced feign client.
|
||||||
|
* @param throwable throwable thrown from run method.
|
||||||
|
*/
|
||||||
|
default void handlerThrowable(EnhancedFeignContext context, Throwable throwable) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -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.rpc.enhancement.feign.plugin;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type of EnhancedFeignPlugin.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public enum EnhancedFeignPluginType {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pre feign plugin.
|
||||||
|
*/
|
||||||
|
PRE,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Post feign plugin.
|
||||||
|
*/
|
||||||
|
POST,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exception feign plugin.
|
||||||
|
*/
|
||||||
|
EXCEPTION,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finally feign plugin.
|
||||||
|
*/
|
||||||
|
FINALLY
|
||||||
|
}
|
@ -0,0 +1,85 @@
|
|||||||
|
/*
|
||||||
|
* 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.feign.plugin.reporter;
|
||||||
|
|
||||||
|
import java.net.SocketTimeoutException;
|
||||||
|
|
||||||
|
import com.tencent.cloud.rpc.enhancement.feign.plugin.EnhancedFeignContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.feign.plugin.EnhancedFeignPlugin;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.feign.plugin.EnhancedFeignPluginType;
|
||||||
|
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||||
|
import com.tencent.polaris.api.pojo.RetStatus;
|
||||||
|
import com.tencent.polaris.api.rpc.ServiceCallResult;
|
||||||
|
import feign.Request;
|
||||||
|
import feign.Response;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Polaris reporter when feign call is successful.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public class ExceptionPolarisReporter implements EnhancedFeignPlugin {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(ExceptionPolarisReporter.class);
|
||||||
|
|
||||||
|
@Autowired(required = false)
|
||||||
|
private ConsumerAPI consumerAPI;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return ExceptionPolarisReporter.class.getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EnhancedFeignPluginType getType() {
|
||||||
|
return EnhancedFeignPluginType.EXCEPTION;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(EnhancedFeignContext context) {
|
||||||
|
if (consumerAPI != null) {
|
||||||
|
Request request = context.getRequest();
|
||||||
|
Response response = context.getResponse();
|
||||||
|
Exception exception = context.getException();
|
||||||
|
RetStatus retStatus = RetStatus.RetFail;
|
||||||
|
if (exception instanceof SocketTimeoutException) {
|
||||||
|
retStatus = RetStatus.RetTimeout;
|
||||||
|
}
|
||||||
|
LOG.debug("Will report result of {}. Request=[{}]. Response=[{}].", retStatus.name(), request, response);
|
||||||
|
ServiceCallResult resultRequest = ReporterUtils.createServiceCallResult(request, retStatus);
|
||||||
|
consumerAPI.updateServiceCallResult(resultRequest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handlerThrowable(EnhancedFeignContext context, Throwable throwable) {
|
||||||
|
Request request = context.getRequest();
|
||||||
|
Response response = context.getResponse();
|
||||||
|
LOG.error("ExceptionPolarisReporter runs failed. Request=[{}]. Response=[{}].", request, response, throwable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return Ordered.HIGHEST_PRECEDENCE + 1;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,88 @@
|
|||||||
|
/*
|
||||||
|
* 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.feign.plugin.reporter;
|
||||||
|
|
||||||
|
import com.tencent.cloud.rpc.enhancement.AbstractPolarisReporterAdapter;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.feign.plugin.EnhancedFeignContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.feign.plugin.EnhancedFeignPlugin;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.feign.plugin.EnhancedFeignPluginType;
|
||||||
|
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||||
|
import com.tencent.polaris.api.pojo.RetStatus;
|
||||||
|
import com.tencent.polaris.api.rpc.ServiceCallResult;
|
||||||
|
import feign.Request;
|
||||||
|
import feign.Response;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Polaris reporter when feign call is successful.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public class SuccessPolarisReporter extends AbstractPolarisReporterAdapter implements EnhancedFeignPlugin {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(SuccessPolarisReporter.class);
|
||||||
|
@Autowired(required = false)
|
||||||
|
private ConsumerAPI consumerAPI;
|
||||||
|
|
||||||
|
public SuccessPolarisReporter(RpcEnhancementReporterProperties properties) {
|
||||||
|
super(properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return SuccessPolarisReporter.class.getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EnhancedFeignPluginType getType() {
|
||||||
|
return EnhancedFeignPluginType.POST;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(EnhancedFeignContext context) {
|
||||||
|
if (consumerAPI != null) {
|
||||||
|
Request request = context.getRequest();
|
||||||
|
Response response = context.getResponse();
|
||||||
|
RetStatus retStatus = RetStatus.RetSuccess;
|
||||||
|
if (apply(HttpStatus.resolve(response.status()))) {
|
||||||
|
retStatus = RetStatus.RetFail;
|
||||||
|
}
|
||||||
|
LOG.debug("Will report result of {}. Request=[{}]. Response=[{}].", retStatus.name(), request, response);
|
||||||
|
ServiceCallResult resultRequest = ReporterUtils.createServiceCallResult(request, retStatus);
|
||||||
|
consumerAPI.updateServiceCallResult(resultRequest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handlerThrowable(EnhancedFeignContext context, Throwable throwable) {
|
||||||
|
Request request = context.getRequest();
|
||||||
|
Response response = context.getResponse();
|
||||||
|
LOG.error("SuccessPolarisReporter runs failed. Request=[{}]. Response=[{}].", request, response, throwable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return Ordered.HIGHEST_PRECEDENCE + 1;
|
||||||
|
}
|
||||||
|
}
|
18
spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisRestTemplateResponseErrorHandler.java → spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporter.java
18
spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisRestTemplateResponseErrorHandler.java → spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporter.java
@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* 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.stat.config;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The properties for stat reporter.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
@ConfigurationProperties("spring.cloud.polaris.stat")
|
||||||
|
public class PolarisStatProperties {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If state reporter enabled.
|
||||||
|
*/
|
||||||
|
private boolean enabled = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local host for prometheus to pull.
|
||||||
|
*/
|
||||||
|
private String host;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Port for prometheus to pull.
|
||||||
|
*/
|
||||||
|
private int port = 28080;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Path for prometheus to pull.
|
||||||
|
*/
|
||||||
|
private String path = "/metrics";
|
||||||
|
|
||||||
|
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnabled(boolean enabled) {
|
||||||
|
this.enabled = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHost() {
|
||||||
|
return host;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHost(String host) {
|
||||||
|
this.host = host;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPort() {
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPort(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPath() {
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPath(String path) {
|
||||||
|
this.path = path;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* 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.stat.config;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Autoconfiguration of stat reporter.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@ConditionalOnPolarisEnabled
|
||||||
|
@Import({PolarisStatProperties.class})
|
||||||
|
public class PolarisStatPropertiesAutoConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
public StatConfigModifier statReporterConfigModifier(PolarisStatProperties polarisStatProperties, Environment environment) {
|
||||||
|
return new StatConfigModifier(polarisStatProperties, environment);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* 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.stat.config;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Autoconfiguration of stat reporter at bootstrap phase.
|
||||||
|
*
|
||||||
|
* @author lepdou 2022-03-29
|
||||||
|
*/
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@ConditionalOnProperty("spring.cloud.polaris.enabled")
|
||||||
|
@Import(PolarisStatPropertiesAutoConfiguration.class)
|
||||||
|
public class PolarisStatPropertiesBootstrapConfiguration {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* 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.stat.config;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.constant.ContextConstant;
|
||||||
|
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
|
||||||
|
import com.tencent.polaris.factory.config.ConfigurationImpl;
|
||||||
|
import com.tencent.polaris.plugins.stat.prometheus.handler.PrometheusHandlerConfig;
|
||||||
|
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import static com.tencent.polaris.api.config.global.StatReporterConfig.DEFAULT_REPORTER_PROMETHEUS;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Config modifier for stat reporter.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public class StatConfigModifier implements PolarisConfigModifier {
|
||||||
|
|
||||||
|
private final PolarisStatProperties polarisStatProperties;
|
||||||
|
|
||||||
|
private final Environment environment;
|
||||||
|
|
||||||
|
public StatConfigModifier(PolarisStatProperties polarisStatProperties, Environment environment) {
|
||||||
|
this.polarisStatProperties = polarisStatProperties;
|
||||||
|
this.environment = environment;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void modify(ConfigurationImpl configuration) {
|
||||||
|
// Turn on stat reporter configuration.
|
||||||
|
configuration.getGlobal().getStatReporter().setEnable(polarisStatProperties.isEnabled());
|
||||||
|
|
||||||
|
// Set prometheus plugin.
|
||||||
|
if (polarisStatProperties.isEnabled()) {
|
||||||
|
PrometheusHandlerConfig prometheusHandlerConfig = configuration.getGlobal().getStatReporter()
|
||||||
|
.getPluginConfig(DEFAULT_REPORTER_PROMETHEUS, PrometheusHandlerConfig.class);
|
||||||
|
if (!StringUtils.hasText(polarisStatProperties.getHost())) {
|
||||||
|
polarisStatProperties.setHost(environment.getProperty("spring.cloud.client.ip-address"));
|
||||||
|
}
|
||||||
|
prometheusHandlerConfig.setHost(polarisStatProperties.getHost());
|
||||||
|
prometheusHandlerConfig.setPort(polarisStatProperties.getPort());
|
||||||
|
prometheusHandlerConfig.setPath(polarisStatProperties.getPath());
|
||||||
|
configuration.getGlobal().getStatReporter()
|
||||||
|
.setPluginConfig(DEFAULT_REPORTER_PROMETHEUS, prometheusHandlerConfig);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return ContextConstant.ModifierOrder.STAT_REPORTER_ORDER;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
{
|
||||||
|
"properties": [
|
||||||
|
{
|
||||||
|
"name": "spring.cloud.tencent.rpc-enhancement.enabled",
|
||||||
|
"type": "java.lang.Boolean",
|
||||||
|
"defaultValue": true,
|
||||||
|
"description": "If rpc enhancement enabled."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "spring.cloud.tencent.rpc-enhancement.reporter.ignore-internal-server-error",
|
||||||
|
"type": "java.lang.Boolean",
|
||||||
|
"defaultValue": true,
|
||||||
|
"description": "If ignore \"Internal Server Error Http Status Code (500)\"."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "spring.cloud.tencent.rpc-enhancement.reporter.series",
|
||||||
|
"type": "java.util.List<org.springframework.http.HttpStatus.Series>",
|
||||||
|
"defaultValue": "HttpStatus.Series.SERVER_ERROR",
|
||||||
|
"description": "Specify List of HTTP status series that needs to be reported as FAILED when status list is empty."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "spring.cloud.tencent.rpc-enhancement.reporter.statuses",
|
||||||
|
"type": "java.util.List<org.springframework.http.HttpStatus>",
|
||||||
|
"defaultValue": "",
|
||||||
|
"description": "Specify the Http status code(s) that needs to be reported as FAILED."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "spring.cloud.polaris.stat.enabled",
|
||||||
|
"type": "java.lang.Boolean",
|
||||||
|
"defaultValue": false,
|
||||||
|
"description": "Enable polaris stat reporter or not."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "spring.cloud.polaris.stat.host",
|
||||||
|
"type": "java.lang.String",
|
||||||
|
"description": "Local host for prometheus to pull."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "spring.cloud.polaris.stat.port",
|
||||||
|
"type": "java.lang.Integer",
|
||||||
|
"defaultValue": "28080",
|
||||||
|
"description": "Port for prometheus to pull."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "spring.cloud.polaris.stat.path",
|
||||||
|
"type": "java.lang.String",
|
||||||
|
"defaultValue": "/metrics",
|
||||||
|
"description": "Path for prometheus to pull."
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
|
||||||
|
com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatPropertiesBootstrapConfiguration
|
||||||
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
|
com.tencent.cloud.rpc.enhancement.config.RpcEnhancementAutoConfiguration,\
|
||||||
|
com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatPropertiesAutoConfiguration
|
@ -0,0 +1,116 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test For {@link AbstractPolarisReporterAdapter}.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:iskp.me@gmail.com">Elve.Xu</a> 2022/7/11
|
||||||
|
*/
|
||||||
|
public class AbstractPolarisReporterAdapterTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testApplyWithDefaultConfig() {
|
||||||
|
RpcEnhancementReporterProperties properties = new RpcEnhancementReporterProperties();
|
||||||
|
// Mock Condition
|
||||||
|
SimplePolarisReporterAdapter adapter = new SimplePolarisReporterAdapter(properties);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assertions.assertThat(adapter.apply(HttpStatus.OK)).isEqualTo(false);
|
||||||
|
Assertions.assertThat(adapter.apply(HttpStatus.INTERNAL_SERVER_ERROR)).isEqualTo(false);
|
||||||
|
Assertions.assertThat(adapter.apply(HttpStatus.BAD_GATEWAY)).isEqualTo(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testApplyWithoutIgnoreInternalServerError() {
|
||||||
|
RpcEnhancementReporterProperties properties = new RpcEnhancementReporterProperties();
|
||||||
|
// Mock Condition
|
||||||
|
properties.getStatuses().clear();
|
||||||
|
properties.setIgnoreInternalServerError(false);
|
||||||
|
|
||||||
|
SimplePolarisReporterAdapter adapter = new SimplePolarisReporterAdapter(properties);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assertions.assertThat(adapter.apply(HttpStatus.OK)).isEqualTo(false);
|
||||||
|
Assertions.assertThat(adapter.apply(HttpStatus.INTERNAL_SERVER_ERROR)).isEqualTo(true);
|
||||||
|
Assertions.assertThat(adapter.apply(HttpStatus.BAD_GATEWAY)).isEqualTo(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testApplyWithIgnoreInternalServerError() {
|
||||||
|
RpcEnhancementReporterProperties properties = new RpcEnhancementReporterProperties();
|
||||||
|
// Mock Condition
|
||||||
|
properties.getStatuses().clear();
|
||||||
|
properties.setIgnoreInternalServerError(true);
|
||||||
|
|
||||||
|
SimplePolarisReporterAdapter adapter = new SimplePolarisReporterAdapter(properties);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assertions.assertThat(adapter.apply(HttpStatus.OK)).isEqualTo(false);
|
||||||
|
Assertions.assertThat(adapter.apply(HttpStatus.INTERNAL_SERVER_ERROR)).isEqualTo(false);
|
||||||
|
Assertions.assertThat(adapter.apply(HttpStatus.BAD_GATEWAY)).isEqualTo(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testApplyWithoutSeries() {
|
||||||
|
RpcEnhancementReporterProperties properties = new RpcEnhancementReporterProperties();
|
||||||
|
// Mock Condition
|
||||||
|
properties.getStatuses().clear();
|
||||||
|
properties.getSeries().clear();
|
||||||
|
|
||||||
|
SimplePolarisReporterAdapter adapter = new SimplePolarisReporterAdapter(properties);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assertions.assertThat(adapter.apply(HttpStatus.OK)).isEqualTo(false);
|
||||||
|
Assertions.assertThat(adapter.apply(HttpStatus.INTERNAL_SERVER_ERROR)).isEqualTo(false);
|
||||||
|
Assertions.assertThat(adapter.apply(HttpStatus.BAD_GATEWAY)).isEqualTo(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testApplyWithSeries() {
|
||||||
|
RpcEnhancementReporterProperties properties = new RpcEnhancementReporterProperties();
|
||||||
|
// Mock Condition
|
||||||
|
properties.getStatuses().clear();
|
||||||
|
properties.getSeries().clear();
|
||||||
|
properties.getSeries().add(HttpStatus.Series.CLIENT_ERROR);
|
||||||
|
|
||||||
|
SimplePolarisReporterAdapter adapter = new SimplePolarisReporterAdapter(properties);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assertions.assertThat(adapter.apply(HttpStatus.OK)).isEqualTo(false);
|
||||||
|
Assertions.assertThat(adapter.apply(HttpStatus.INTERNAL_SERVER_ERROR)).isEqualTo(false);
|
||||||
|
Assertions.assertThat(adapter.apply(HttpStatus.BAD_GATEWAY)).isEqualTo(false);
|
||||||
|
Assertions.assertThat(adapter.apply(HttpStatus.FORBIDDEN)).isEqualTo(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple Polaris CircuitBreak Adapter Implements .
|
||||||
|
*/
|
||||||
|
public static class SimplePolarisReporterAdapter extends AbstractPolarisReporterAdapter {
|
||||||
|
|
||||||
|
public SimplePolarisReporterAdapter(RpcEnhancementReporterProperties properties) {
|
||||||
|
super(properties);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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 {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue