feat:enhance Feign and RestTemplate and support Polaris monitor. (#435)
Co-authored-by: VOPEN.XYZ <x_vivi@yeah.net>pull/441/merge
parent
3e1d79d033
commit
3c4bddd6d1
@ -1,5 +1,4 @@
|
||||
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
|
||||
com.tencent.cloud.polaris.circuitbreaker.config.PolarisCircuitBreakerBootstrapConfiguration
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
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
8
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
8
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
|
@ -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,74 @@
|
||||
<?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-loadbalancer</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<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.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-ribbon</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,109 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.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.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.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)
|
||||
@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() {
|
||||
return new SuccessPolarisReporter();
|
||||
}
|
||||
|
||||
@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(
|
||||
ConsumerAPI consumerAPI) {
|
||||
return new EnhancedRestTemplateReporter(consumerAPI);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public EnhancedRestTemplateModifier polarisRestTemplateBeanPostProcessor(
|
||||
EnhancedRestTemplateReporter restTemplateResponseErrorHandler) {
|
||||
return new EnhancedRestTemplateModifier(restTemplateResponseErrorHandler);
|
||||
}
|
||||
}
|
||||
}
|
@ -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,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,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.feign.plugin.reporter;
|
||||
|
||||
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 SuccessPolarisReporter implements EnhancedFeignPlugin {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(SuccessPolarisReporter.class);
|
||||
|
||||
@Autowired(required = false)
|
||||
private ConsumerAPI consumerAPI;
|
||||
|
||||
@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 (response.status() > 500) {
|
||||
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;
|
||||
}
|
||||
}
|
25
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
25
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,27 @@
|
||||
{
|
||||
"properties": [
|
||||
{
|
||||
"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
|
8
spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignBlockingLoadBalancerClientTest.java → spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignBlockingLoadBalancerClientTest.java
8
spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignBlockingLoadBalancerClientTest.java → spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignBlockingLoadBalancerClientTest.java
@ -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;
|
||||
|
||||
import feign.Request;
|
||||
import feign.Response;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test for {@link EnhancedFeignContext}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class EnhancedFeignContextTest {
|
||||
|
||||
@Test
|
||||
public void testGetAndSet() {
|
||||
EnhancedFeignContext enhancedFeignContext = new EnhancedFeignContext();
|
||||
enhancedFeignContext.setRequest(mock(Request.class));
|
||||
enhancedFeignContext.setOptions(mock(Request.Options.class));
|
||||
enhancedFeignContext.setResponse(mock(Response.class));
|
||||
enhancedFeignContext.setException(mock(Exception.class));
|
||||
assertThat(enhancedFeignContext.getRequest()).isNotNull();
|
||||
assertThat(enhancedFeignContext.getOptions()).isNotNull();
|
||||
assertThat(enhancedFeignContext.getResponse()).isNotNull();
|
||||
assertThat(enhancedFeignContext.getException()).isNotNull();
|
||||
}
|
||||
}
|
@ -0,0 +1,101 @@
|
||||
/*
|
||||
* 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.feign.plugin.EnhancedFeignContext;
|
||||
import com.tencent.cloud.rpc.enhancement.feign.plugin.EnhancedFeignPluginType;
|
||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||
import com.tencent.polaris.api.pojo.RetStatus;
|
||||
import feign.Request;
|
||||
import feign.Response;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test for {@link ExceptionPolarisReporter}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ExceptionPolarisReporterTest {
|
||||
|
||||
private static MockedStatic<ReporterUtils> mockedReporterUtils;
|
||||
@Mock
|
||||
private ConsumerAPI consumerAPI;
|
||||
@InjectMocks
|
||||
private ExceptionPolarisReporter exceptionPolarisReporter;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() {
|
||||
mockedReporterUtils = Mockito.mockStatic(ReporterUtils.class);
|
||||
mockedReporterUtils.when(() -> ReporterUtils.createServiceCallResult(any(Request.class), any(RetStatus.class)))
|
||||
.thenReturn(null);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() {
|
||||
mockedReporterUtils.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetName() {
|
||||
assertThat(exceptionPolarisReporter.getName()).isEqualTo(ExceptionPolarisReporter.class.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testType() {
|
||||
assertThat(exceptionPolarisReporter.getType()).isEqualTo(EnhancedFeignPluginType.EXCEPTION);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRun() {
|
||||
// mock request
|
||||
Request request = mock(Request.class);
|
||||
// mock response
|
||||
Response response = mock(Response.class);
|
||||
|
||||
EnhancedFeignContext context = new EnhancedFeignContext();
|
||||
context.setRequest(request);
|
||||
context.setResponse(response);
|
||||
exceptionPolarisReporter.run(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandlerThrowable() {
|
||||
// mock request
|
||||
Request request = mock(Request.class);
|
||||
// mock response
|
||||
Response response = mock(Response.class);
|
||||
|
||||
EnhancedFeignContext context = new EnhancedFeignContext();
|
||||
context.setRequest(request);
|
||||
context.setResponse(response);
|
||||
exceptionPolarisReporter.handlerThrowable(context, new RuntimeException("Mock exception."));
|
||||
}
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* 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.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||
import com.tencent.polaris.api.pojo.RetStatus;
|
||||
import com.tencent.polaris.api.rpc.ServiceCallResult;
|
||||
import feign.Request;
|
||||
import feign.RequestTemplate;
|
||||
import feign.Target;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test for {@link ReporterUtils}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ReporterUtilsTest {
|
||||
|
||||
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() {
|
||||
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||
.thenReturn("unit-test");
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() {
|
||||
mockedApplicationContextAwareUtils.close();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MetadataContext.LOCAL_NAMESPACE = NAMESPACE_TEST;
|
||||
MetadataContext.LOCAL_SERVICE = SERVICE_PROVIDER;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateServiceCallResult() {
|
||||
// mock target
|
||||
Target<?> target = mock(Target.class);
|
||||
doReturn(SERVICE_PROVIDER).when(target).name();
|
||||
|
||||
// mock RequestTemplate.class
|
||||
RequestTemplate requestTemplate = new RequestTemplate();
|
||||
requestTemplate.feignTarget(target);
|
||||
|
||||
// mock request
|
||||
Request request = mock(Request.class);
|
||||
doReturn(requestTemplate).when(request).requestTemplate();
|
||||
doReturn("http://1.1.1.1:2345/path").when(request).url();
|
||||
|
||||
ServiceCallResult serviceCallResult = ReporterUtils.createServiceCallResult(request, RetStatus.RetSuccess);
|
||||
assertThat(serviceCallResult.getNamespace()).isEqualTo(NAMESPACE_TEST);
|
||||
assertThat(serviceCallResult.getService()).isEqualTo(SERVICE_PROVIDER);
|
||||
assertThat(serviceCallResult.getHost()).isEqualTo("1.1.1.1");
|
||||
assertThat(serviceCallResult.getPort()).isEqualTo(2345);
|
||||
assertThat(serviceCallResult.getRetStatus()).isEqualTo(RetStatus.RetSuccess);
|
||||
assertThat(serviceCallResult.getMethod()).isEqualTo("/path");
|
||||
assertThat(serviceCallResult.getCallerService().getNamespace()).isEqualTo(NAMESPACE_TEST);
|
||||
assertThat(serviceCallResult.getCallerService().getService()).isEqualTo(SERVICE_PROVIDER);
|
||||
}
|
||||
}
|
@ -0,0 +1,103 @@
|
||||
/*
|
||||
* 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.feign.plugin.EnhancedFeignContext;
|
||||
import com.tencent.cloud.rpc.enhancement.feign.plugin.EnhancedFeignPluginType;
|
||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||
import com.tencent.polaris.api.pojo.RetStatus;
|
||||
import feign.Request;
|
||||
import feign.Response;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test for {@link SuccessPolarisReporter}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class SuccessPolarisReporterTest {
|
||||
|
||||
private static MockedStatic<ReporterUtils> mockedReporterUtils;
|
||||
@Mock
|
||||
private ConsumerAPI consumerAPI;
|
||||
@InjectMocks
|
||||
private SuccessPolarisReporter successPolarisReporter;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() {
|
||||
mockedReporterUtils = Mockito.mockStatic(ReporterUtils.class);
|
||||
mockedReporterUtils.when(() -> ReporterUtils.createServiceCallResult(any(Request.class), any(RetStatus.class)))
|
||||
.thenReturn(null);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() {
|
||||
mockedReporterUtils.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetName() {
|
||||
assertThat(successPolarisReporter.getName()).isEqualTo(SuccessPolarisReporter.class.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testType() {
|
||||
assertThat(successPolarisReporter.getType()).isEqualTo(EnhancedFeignPluginType.POST);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRun() {
|
||||
// mock request
|
||||
Request request = mock(Request.class);
|
||||
// mock response
|
||||
Response response = mock(Response.class);
|
||||
doReturn(502).when(response).status();
|
||||
|
||||
EnhancedFeignContext context = new EnhancedFeignContext();
|
||||
context.setRequest(request);
|
||||
context.setResponse(response);
|
||||
successPolarisReporter.run(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandlerThrowable() {
|
||||
// mock request
|
||||
Request request = mock(Request.class);
|
||||
// mock response
|
||||
Response response = mock(Response.class);
|
||||
|
||||
EnhancedFeignContext context = new EnhancedFeignContext();
|
||||
context.setRequest(request);
|
||||
context.setResponse(response);
|
||||
successPolarisReporter.handlerThrowable(context, new RuntimeException("Mock exception."));
|
||||
}
|
||||
}
|
12
spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisRestTemplateResponseErrorHandlerTest.java → spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporterTest.java
12
spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisRestTemplateResponseErrorHandlerTest.java → spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporterTest.java
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisStatPropertiesAutoConfiguration}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisStatPropertiesAutoConfigurationTest {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(PolarisStatPropertiesAutoConfiguration.class));
|
||||
|
||||
@Test
|
||||
public void testDefaultInitialization() {
|
||||
this.contextRunner.run(context -> {
|
||||
assertThat(context).hasSingleBean(PolarisStatPropertiesAutoConfiguration.class);
|
||||
assertThat(context).hasSingleBean(PolarisStatProperties.class);
|
||||
assertThat(context).hasSingleBean(StatConfigModifier.class);
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisStatPropertiesBootstrapConfiguration}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisStatPropertiesBootstrapConfigurationTest {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(PolarisStatPropertiesBootstrapConfiguration.class))
|
||||
.withPropertyValues("spring.cloud.polaris.enabled=true");
|
||||
|
||||
@Test
|
||||
public void testDefaultInitialization() {
|
||||
this.contextRunner.run(context -> {
|
||||
assertThat(context).hasSingleBean(PolarisStatPropertiesBootstrapConfiguration.class);
|
||||
assertThat(context).hasSingleBean(PolarisStatPropertiesAutoConfiguration.class);
|
||||
assertThat(context).hasSingleBean(PolarisStatProperties.class);
|
||||
assertThat(context).hasSingleBean(StatConfigModifier.class);
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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.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;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisStatProperties}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = PolarisStatPropertiesTest.TestApplication.class)
|
||||
@ActiveProfiles("test")
|
||||
public class PolarisStatPropertiesTest {
|
||||
|
||||
@Autowired
|
||||
private PolarisStatProperties polarisStatProperties;
|
||||
|
||||
@Test
|
||||
public void testDefaultInitialization() {
|
||||
assertThat(polarisStatProperties).isNotNull();
|
||||
assertThat(polarisStatProperties.isEnabled()).isTrue();
|
||||
assertThat(polarisStatProperties.getHost()).isNotBlank();
|
||||
assertThat(polarisStatProperties.getPort()).isEqualTo(20000);
|
||||
assertThat(polarisStatProperties.getPath()).isEqualTo("/xxx");
|
||||
}
|
||||
|
||||
@SpringBootApplication
|
||||
protected static class TestApplication {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
spring.cloud.polaris.stat.enabled=true
|
||||
spring.cloud.polaris.stat.port=20000
|
||||
spring.cloud.polaris.stat.path=/xxx
|
Loading…
Reference in new issue