parent
ec1b11ff55
commit
02f04d7d0a
@ -0,0 +1,97 @@
|
|||||||
|
/*
|
||||||
|
* 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.circuitbreaker.reporter;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import com.tencent.cloud.rpc.enhancement.AbstractPolarisReporterAdapter;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPlugin;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedRequestContext;
|
||||||
|
import com.tencent.polaris.api.plugin.circuitbreaker.ResourceStat;
|
||||||
|
import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI;
|
||||||
|
import com.tencent.polaris.client.api.SDKContext;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
|
||||||
|
public class ExceptionCircuitBreakerReporter extends AbstractPolarisReporterAdapter implements EnhancedPlugin {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(ExceptionCircuitBreakerReporter.class);
|
||||||
|
|
||||||
|
private final CircuitBreakAPI circuitBreakAPI;
|
||||||
|
|
||||||
|
public ExceptionCircuitBreakerReporter(RpcEnhancementReporterProperties reportProperties,
|
||||||
|
SDKContext context,
|
||||||
|
CircuitBreakAPI circuitBreakAPI) {
|
||||||
|
super(reportProperties, context);
|
||||||
|
this.circuitBreakAPI = circuitBreakAPI;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return ExceptionCircuitBreakerReporter.class.getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EnhancedPluginType getType() {
|
||||||
|
return EnhancedPluginType.EXCEPTION;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(EnhancedPluginContext context) throws Throwable {
|
||||||
|
if (!super.reportProperties.isEnabled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
EnhancedRequestContext request = context.getRequest();
|
||||||
|
ServiceInstance serviceInstance = Optional.ofNullable(context.getServiceInstance()).orElse(new DefaultServiceInstance());
|
||||||
|
|
||||||
|
ResourceStat resourceStat = createInstanceResourceStat(
|
||||||
|
serviceInstance.getServiceId(),
|
||||||
|
serviceInstance.getHost(),
|
||||||
|
serviceInstance.getPort(),
|
||||||
|
request.getUrl(),
|
||||||
|
null,
|
||||||
|
context.getDelay(),
|
||||||
|
context.getThrowable()
|
||||||
|
);
|
||||||
|
|
||||||
|
LOG.debug("Will report CircuitBreaker ResourceStat of {}. Request=[{} {}]. Response=[{}]. Delay=[{}]ms.",
|
||||||
|
resourceStat.getRetStatus().name(), request.getHttpMethod().name(), request.getUrl().getPath(), context.getThrowable().getMessage(), context.getDelay());
|
||||||
|
|
||||||
|
circuitBreakAPI.report(resourceStat);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handlerThrowable(EnhancedPluginContext context, Throwable throwable) {
|
||||||
|
LOG.error("ExceptionCircuitBreakerReporter runs failed. context=[{}].",
|
||||||
|
context, throwable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return Ordered.HIGHEST_PRECEDENCE + 2;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,99 @@
|
|||||||
|
/*
|
||||||
|
* 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.circuitbreaker.reporter;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import com.tencent.cloud.rpc.enhancement.AbstractPolarisReporterAdapter;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPlugin;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedRequestContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedResponseContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.reporter.SuccessPolarisReporter;
|
||||||
|
import com.tencent.polaris.api.plugin.circuitbreaker.ResourceStat;
|
||||||
|
import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI;
|
||||||
|
import com.tencent.polaris.client.api.SDKContext;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
|
||||||
|
|
||||||
|
public class SuccessCircuitBreakerReporter extends AbstractPolarisReporterAdapter implements EnhancedPlugin {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(SuccessPolarisReporter.class);
|
||||||
|
|
||||||
|
private final CircuitBreakAPI circuitBreakAPI;
|
||||||
|
|
||||||
|
public SuccessCircuitBreakerReporter(RpcEnhancementReporterProperties reportProperties,
|
||||||
|
SDKContext context,
|
||||||
|
CircuitBreakAPI circuitBreakAPI) {
|
||||||
|
super(reportProperties, context);
|
||||||
|
this.circuitBreakAPI = circuitBreakAPI;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return SuccessCircuitBreakerReporter.class.getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EnhancedPluginType getType() {
|
||||||
|
return EnhancedPluginType.POST;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(EnhancedPluginContext context) throws Throwable {
|
||||||
|
if (!super.reportProperties.isEnabled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
EnhancedRequestContext request = context.getRequest();
|
||||||
|
EnhancedResponseContext response = context.getResponse();
|
||||||
|
ServiceInstance serviceInstance = Optional.ofNullable(context.getServiceInstance()).orElse(new DefaultServiceInstance());
|
||||||
|
|
||||||
|
ResourceStat resourceStat = createInstanceResourceStat(
|
||||||
|
serviceInstance.getServiceId(),
|
||||||
|
serviceInstance.getHost(),
|
||||||
|
serviceInstance.getPort(),
|
||||||
|
request.getUrl(),
|
||||||
|
response.getHttpStatus(),
|
||||||
|
context.getDelay(),
|
||||||
|
null
|
||||||
|
);
|
||||||
|
|
||||||
|
LOG.debug("Will report CircuitBreaker ResourceStat of {}. Request=[{} {}]. Response=[{}]. Delay=[{}]ms.",
|
||||||
|
resourceStat.getRetStatus().name(), request.getHttpMethod().name(), request.getUrl().getPath(), response.getHttpStatus(), context.getDelay());
|
||||||
|
|
||||||
|
circuitBreakAPI.report(resourceStat);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handlerThrowable(EnhancedPluginContext context, Throwable throwable) {
|
||||||
|
LOG.error("SuccessCircuitBreakerReporter runs failed. context=[{}].",
|
||||||
|
context, throwable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return Ordered.HIGHEST_PRECEDENCE + 2;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,145 @@
|
|||||||
|
/*
|
||||||
|
* 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.circuitbreaker.reporter;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||||
|
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedRequestContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedResponseContext;
|
||||||
|
import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI;
|
||||||
|
import com.tencent.polaris.client.api.SDKContext;
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockedStatic;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
|
||||||
|
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;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ExceptionCircuitBreakerReporterTest.
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
public class ExceptionCircuitBreakerReporterTest {
|
||||||
|
|
||||||
|
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||||
|
@Mock
|
||||||
|
private RpcEnhancementReporterProperties reporterProperties;
|
||||||
|
@Mock
|
||||||
|
private SDKContext sdkContext;
|
||||||
|
@InjectMocks
|
||||||
|
private ExceptionCircuitBreakerReporter exceptionCircuitBreakerReporter;
|
||||||
|
@Mock
|
||||||
|
private CircuitBreakAPI circuitBreakAPI;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void beforeAll() {
|
||||||
|
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||||
|
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||||
|
.thenReturn("unit-test");
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
static void afterAll() {
|
||||||
|
mockedApplicationContextAwareUtils.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
MetadataContext.LOCAL_NAMESPACE = NAMESPACE_TEST;
|
||||||
|
MetadataContext.LOCAL_SERVICE = SERVICE_PROVIDER;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetName() {
|
||||||
|
assertThat(exceptionCircuitBreakerReporter.getName()).isEqualTo(ExceptionCircuitBreakerReporter.class.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testType() {
|
||||||
|
assertThat(exceptionCircuitBreakerReporter.getType()).isEqualTo(EnhancedPluginType.EXCEPTION);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRun() throws Throwable {
|
||||||
|
EnhancedPluginContext context = mock(EnhancedPluginContext.class);
|
||||||
|
// test not report
|
||||||
|
exceptionCircuitBreakerReporter.run(context);
|
||||||
|
verify(context, times(0)).getRequest();
|
||||||
|
|
||||||
|
doReturn(true).when(reporterProperties).isEnabled();
|
||||||
|
|
||||||
|
EnhancedPluginContext pluginContext = new EnhancedPluginContext();
|
||||||
|
EnhancedRequestContext request = EnhancedRequestContext.builder()
|
||||||
|
.httpMethod(HttpMethod.GET)
|
||||||
|
.url(URI.create("http://0.0.0.0/"))
|
||||||
|
.httpHeaders(new HttpHeaders())
|
||||||
|
.build();
|
||||||
|
EnhancedResponseContext response = EnhancedResponseContext.builder()
|
||||||
|
.httpStatus(200)
|
||||||
|
.build();
|
||||||
|
DefaultServiceInstance serviceInstance = new DefaultServiceInstance();
|
||||||
|
serviceInstance.setServiceId(SERVICE_PROVIDER);
|
||||||
|
|
||||||
|
pluginContext.setRequest(request);
|
||||||
|
pluginContext.setResponse(response);
|
||||||
|
pluginContext.setServiceInstance(serviceInstance);
|
||||||
|
pluginContext.setThrowable(new RuntimeException());
|
||||||
|
|
||||||
|
exceptionCircuitBreakerReporter.run(pluginContext);
|
||||||
|
exceptionCircuitBreakerReporter.getOrder();
|
||||||
|
exceptionCircuitBreakerReporter.getName();
|
||||||
|
exceptionCircuitBreakerReporter.getType();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testHandlerThrowable() {
|
||||||
|
// mock request
|
||||||
|
EnhancedRequestContext request = mock(EnhancedRequestContext.class);
|
||||||
|
// mock response
|
||||||
|
EnhancedResponseContext response = mock(EnhancedResponseContext.class);
|
||||||
|
|
||||||
|
EnhancedPluginContext context = new EnhancedPluginContext();
|
||||||
|
context.setRequest(request);
|
||||||
|
context.setResponse(response);
|
||||||
|
exceptionCircuitBreakerReporter.handlerThrowable(context, new RuntimeException("Mock exception."));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,143 @@
|
|||||||
|
/*
|
||||||
|
* 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.circuitbreaker.reporter;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||||
|
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedRequestContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedResponseContext;
|
||||||
|
import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI;
|
||||||
|
import com.tencent.polaris.client.api.SDKContext;
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockedStatic;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
|
||||||
|
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;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SuccessCircuitBreakerReporterTest.
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
public class SuccessCircuitBreakerReporterTest {
|
||||||
|
|
||||||
|
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||||
|
@Mock
|
||||||
|
private SDKContext sdkContext;
|
||||||
|
@Mock
|
||||||
|
private RpcEnhancementReporterProperties reporterProperties;
|
||||||
|
@InjectMocks
|
||||||
|
private SuccessCircuitBreakerReporter successCircuitBreakerReporter;
|
||||||
|
@Mock
|
||||||
|
private CircuitBreakAPI circuitBreakAPI;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void beforeAll() {
|
||||||
|
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||||
|
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||||
|
.thenReturn("unit-test");
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
static void afterAll() {
|
||||||
|
mockedApplicationContextAwareUtils.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
MetadataContext.LOCAL_NAMESPACE = NAMESPACE_TEST;
|
||||||
|
MetadataContext.LOCAL_SERVICE = SERVICE_PROVIDER;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetName() {
|
||||||
|
assertThat(successCircuitBreakerReporter.getName()).isEqualTo(SuccessCircuitBreakerReporter.class.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testType() {
|
||||||
|
assertThat(successCircuitBreakerReporter.getType()).isEqualTo(EnhancedPluginType.POST);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRun() throws Throwable {
|
||||||
|
|
||||||
|
EnhancedPluginContext context = mock(EnhancedPluginContext.class);
|
||||||
|
// test not report
|
||||||
|
successCircuitBreakerReporter.run(context);
|
||||||
|
verify(context, times(0)).getRequest();
|
||||||
|
|
||||||
|
doReturn(true).when(reporterProperties).isEnabled();
|
||||||
|
|
||||||
|
EnhancedPluginContext pluginContext = new EnhancedPluginContext();
|
||||||
|
EnhancedRequestContext request = EnhancedRequestContext.builder()
|
||||||
|
.httpMethod(HttpMethod.GET)
|
||||||
|
.url(URI.create("http://0.0.0.0/"))
|
||||||
|
.build();
|
||||||
|
EnhancedResponseContext response = EnhancedResponseContext.builder()
|
||||||
|
.httpStatus(200)
|
||||||
|
.build();
|
||||||
|
DefaultServiceInstance serviceInstance = new DefaultServiceInstance();
|
||||||
|
serviceInstance.setServiceId(SERVICE_PROVIDER);
|
||||||
|
|
||||||
|
pluginContext.setRequest(request);
|
||||||
|
pluginContext.setResponse(response);
|
||||||
|
pluginContext.setServiceInstance(serviceInstance);
|
||||||
|
|
||||||
|
successCircuitBreakerReporter.run(pluginContext);
|
||||||
|
successCircuitBreakerReporter.getOrder();
|
||||||
|
successCircuitBreakerReporter.getName();
|
||||||
|
successCircuitBreakerReporter.getType();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testHandlerThrowable() {
|
||||||
|
// mock request
|
||||||
|
EnhancedRequestContext request = mock(EnhancedRequestContext.class);
|
||||||
|
// mock response
|
||||||
|
EnhancedResponseContext response = mock(EnhancedResponseContext.class);
|
||||||
|
|
||||||
|
EnhancedPluginContext context = new EnhancedPluginContext();
|
||||||
|
context.setRequest(request);
|
||||||
|
context.setResponse(response);
|
||||||
|
successCircuitBreakerReporter.handlerThrowable(context, new RuntimeException("Mock exception."));
|
||||||
|
}
|
||||||
|
}
|
@ -1,79 +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.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;
|
|
||||||
|
|
||||||
private long delay;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getDelay() {
|
|
||||||
return delay;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDelay(long delay) {
|
|
||||||
this.delay = delay;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,130 +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.rpc.enhancement.feign.plugin.reporter;
|
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
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.plugin.circuitbreaker.ResourceStat;
|
|
||||||
import com.tencent.polaris.api.rpc.ServiceCallResult;
|
|
||||||
import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI;
|
|
||||||
import com.tencent.polaris.client.api.SDKContext;
|
|
||||||
import feign.Request;
|
|
||||||
import feign.Response;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import org.springframework.core.Ordered;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Polaris reporter when feign call fails.
|
|
||||||
*
|
|
||||||
* @author Haotian Zhang
|
|
||||||
*/
|
|
||||||
public class ExceptionPolarisReporter extends AbstractPolarisReporterAdapter implements EnhancedFeignPlugin {
|
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(ExceptionPolarisReporter.class);
|
|
||||||
private final RpcEnhancementReporterProperties reporterProperties;
|
|
||||||
|
|
||||||
private final ConsumerAPI consumerAPI;
|
|
||||||
|
|
||||||
private final CircuitBreakAPI circuitBreakAPI;
|
|
||||||
|
|
||||||
public ExceptionPolarisReporter(RpcEnhancementReporterProperties reporterProperties,
|
|
||||||
SDKContext context,
|
|
||||||
ConsumerAPI consumerAPI,
|
|
||||||
CircuitBreakAPI circuitBreakAPI) {
|
|
||||||
super(reporterProperties, context);
|
|
||||||
this.reporterProperties = reporterProperties;
|
|
||||||
this.consumerAPI = consumerAPI;
|
|
||||||
this.circuitBreakAPI = circuitBreakAPI;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getName() {
|
|
||||||
return ExceptionPolarisReporter.class.getName();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public EnhancedFeignPluginType getType() {
|
|
||||||
return EnhancedFeignPluginType.EXCEPTION;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run(EnhancedFeignContext context) {
|
|
||||||
if (!reporterProperties.isEnabled()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Request request = context.getRequest();
|
|
||||||
Response response = context.getResponse();
|
|
||||||
Exception exception = context.getException();
|
|
||||||
long delay = context.getDelay();
|
|
||||||
|
|
||||||
HttpHeaders requestHeaders = new HttpHeaders();
|
|
||||||
request.headers().forEach((s, strings) -> requestHeaders.addAll(s, new ArrayList<>(strings)));
|
|
||||||
HttpHeaders responseHeaders = new HttpHeaders();
|
|
||||||
Integer status = null;
|
|
||||||
if (response != null) {
|
|
||||||
response.headers().forEach((s, strings) -> responseHeaders.addAll(s, new ArrayList<>(strings)));
|
|
||||||
status = response.status();
|
|
||||||
}
|
|
||||||
|
|
||||||
ServiceCallResult resultRequest = createServiceCallResult(
|
|
||||||
request.requestTemplate().feignTarget().name(),
|
|
||||||
URI.create(request.url()),
|
|
||||||
requestHeaders,
|
|
||||||
responseHeaders,
|
|
||||||
status,
|
|
||||||
delay,
|
|
||||||
exception
|
|
||||||
);
|
|
||||||
LOG.debug("Will report result of {}. Request=[{} {}]. Response=[{}]. Delay=[{}]ms.",
|
|
||||||
resultRequest.getRetStatus().name(), request.httpMethod().name(), request.url(), status, delay);
|
|
||||||
consumerAPI.updateServiceCallResult(resultRequest);
|
|
||||||
|
|
||||||
ResourceStat resourceStat = createInstanceResourceStat(
|
|
||||||
request.requestTemplate().feignTarget().name(),
|
|
||||||
URI.create(request.url()),
|
|
||||||
status,
|
|
||||||
delay,
|
|
||||||
exception
|
|
||||||
);
|
|
||||||
circuitBreakAPI.report(resourceStat);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,126 +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.rpc.enhancement.feign.plugin.reporter;
|
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
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.plugin.circuitbreaker.ResourceStat;
|
|
||||||
import com.tencent.polaris.api.rpc.ServiceCallResult;
|
|
||||||
import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI;
|
|
||||||
import com.tencent.polaris.client.api.SDKContext;
|
|
||||||
import feign.Request;
|
|
||||||
import feign.Response;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import org.springframework.core.Ordered;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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);
|
|
||||||
|
|
||||||
private final ConsumerAPI consumerAPI;
|
|
||||||
|
|
||||||
private final CircuitBreakAPI circuitBreakAPI;
|
|
||||||
|
|
||||||
public SuccessPolarisReporter(RpcEnhancementReporterProperties properties,
|
|
||||||
SDKContext context,
|
|
||||||
ConsumerAPI consumerAPI,
|
|
||||||
CircuitBreakAPI circuitBreakAPI) {
|
|
||||||
super(properties, context);
|
|
||||||
this.consumerAPI = consumerAPI;
|
|
||||||
this.circuitBreakAPI = circuitBreakAPI;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getName() {
|
|
||||||
return SuccessPolarisReporter.class.getName();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public EnhancedFeignPluginType getType() {
|
|
||||||
return EnhancedFeignPluginType.POST;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run(EnhancedFeignContext context) {
|
|
||||||
if (!reportProperties.isEnabled()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Request request = context.getRequest();
|
|
||||||
Response response = context.getResponse();
|
|
||||||
long delay = context.getDelay();
|
|
||||||
HttpHeaders requestHeaders = new HttpHeaders();
|
|
||||||
request.headers().forEach((s, strings) -> requestHeaders.addAll(s, new ArrayList<>(strings)));
|
|
||||||
HttpHeaders responseHeaders = new HttpHeaders();
|
|
||||||
Integer status = null;
|
|
||||||
if (response != null) {
|
|
||||||
response.headers().forEach((s, strings) -> responseHeaders.addAll(s, new ArrayList<>(strings)));
|
|
||||||
status = response.status();
|
|
||||||
}
|
|
||||||
|
|
||||||
ServiceCallResult resultRequest = createServiceCallResult(
|
|
||||||
request.requestTemplate().feignTarget().name(),
|
|
||||||
URI.create(request.url()),
|
|
||||||
requestHeaders,
|
|
||||||
responseHeaders,
|
|
||||||
status,
|
|
||||||
delay,
|
|
||||||
null
|
|
||||||
);
|
|
||||||
LOG.debug("Will report result of {}. Request=[{} {}]. Response=[{}]. Delay=[{}]ms.",
|
|
||||||
resultRequest.getRetStatus().name(), request.httpMethod().name(), request.url(), status, delay);
|
|
||||||
consumerAPI.updateServiceCallResult(resultRequest);
|
|
||||||
|
|
||||||
ResourceStat resourceStat = createInstanceResourceStat(
|
|
||||||
request.requestTemplate().feignTarget().name(),
|
|
||||||
URI.create(request.url()),
|
|
||||||
status,
|
|
||||||
delay,
|
|
||||||
null
|
|
||||||
);
|
|
||||||
circuitBreakAPI.report(resourceStat);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,90 @@
|
|||||||
|
/*
|
||||||
|
* 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.plugin;
|
||||||
|
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Context used by EnhancedPlugin.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public class EnhancedPluginContext {
|
||||||
|
|
||||||
|
private EnhancedRequestContext request;
|
||||||
|
|
||||||
|
private EnhancedResponseContext response;
|
||||||
|
|
||||||
|
private Throwable throwable;
|
||||||
|
|
||||||
|
private long delay;
|
||||||
|
|
||||||
|
private ServiceInstance serviceInstance;
|
||||||
|
|
||||||
|
public EnhancedRequestContext getRequest() {
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRequest(EnhancedRequestContext request) {
|
||||||
|
this.request = request;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EnhancedResponseContext getResponse() {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResponse(EnhancedResponseContext response) {
|
||||||
|
this.response = response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Throwable getThrowable() {
|
||||||
|
return throwable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setThrowable(Throwable throwable) {
|
||||||
|
this.throwable = throwable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getDelay() {
|
||||||
|
return delay;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDelay(long delay) {
|
||||||
|
this.delay = delay;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServiceInstance getServiceInstance() {
|
||||||
|
return serviceInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setServiceInstance(ServiceInstance serviceInstance) {
|
||||||
|
this.serviceInstance = serviceInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "EnhancedPluginContext{" +
|
||||||
|
"request=" + request +
|
||||||
|
", response=" + response +
|
||||||
|
", throwable=" + throwable +
|
||||||
|
", delay=" + delay +
|
||||||
|
", serviceInstance=" + serviceInstance +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,107 @@
|
|||||||
|
/*
|
||||||
|
* 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.plugin;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EnhancedRequestContext.
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
public class EnhancedRequestContext {
|
||||||
|
|
||||||
|
private HttpMethod httpMethod;
|
||||||
|
|
||||||
|
private HttpHeaders httpHeaders;
|
||||||
|
|
||||||
|
private URI url;
|
||||||
|
|
||||||
|
public HttpMethod getHttpMethod() {
|
||||||
|
return httpMethod;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHttpMethod(HttpMethod httpMethod) {
|
||||||
|
this.httpMethod = httpMethod;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpHeaders getHttpHeaders() {
|
||||||
|
return httpHeaders;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHttpHeaders(HttpHeaders httpHeaders) {
|
||||||
|
this.httpHeaders = httpHeaders;
|
||||||
|
}
|
||||||
|
|
||||||
|
public URI getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUrl(URI url) {
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static EnhancedContextRequestBuilder builder() {
|
||||||
|
return new EnhancedContextRequestBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "EnhancedRequestContext{" +
|
||||||
|
"httpMethod=" + httpMethod +
|
||||||
|
", httpHeaders=" + httpHeaders +
|
||||||
|
", url=" + url +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final class EnhancedContextRequestBuilder {
|
||||||
|
private HttpMethod httpMethod;
|
||||||
|
private HttpHeaders httpHeaders;
|
||||||
|
private URI url;
|
||||||
|
|
||||||
|
private EnhancedContextRequestBuilder() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public EnhancedContextRequestBuilder httpMethod(HttpMethod httpMethod) {
|
||||||
|
this.httpMethod = httpMethod;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EnhancedContextRequestBuilder httpHeaders(HttpHeaders httpHeaders) {
|
||||||
|
this.httpHeaders = httpHeaders;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EnhancedContextRequestBuilder url(URI url) {
|
||||||
|
this.url = url;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EnhancedRequestContext build() {
|
||||||
|
EnhancedRequestContext enhancedRequestContext = new EnhancedRequestContext();
|
||||||
|
enhancedRequestContext.httpMethod = this.httpMethod;
|
||||||
|
enhancedRequestContext.url = this.url;
|
||||||
|
enhancedRequestContext.httpHeaders = this.httpHeaders;
|
||||||
|
return enhancedRequestContext;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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.plugin;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EnhancedResponseContext.
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
public class EnhancedResponseContext {
|
||||||
|
|
||||||
|
private Integer httpStatus;
|
||||||
|
|
||||||
|
private HttpHeaders httpHeaders;
|
||||||
|
|
||||||
|
public Integer getHttpStatus() {
|
||||||
|
return httpStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHttpStatus(Integer httpStatus) {
|
||||||
|
this.httpStatus = httpStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpHeaders getHttpHeaders() {
|
||||||
|
return httpHeaders;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHttpHeaders(HttpHeaders httpHeaders) {
|
||||||
|
this.httpHeaders = httpHeaders;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static EnhancedContextResponseBuilder builder() {
|
||||||
|
return new EnhancedContextResponseBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "EnhancedResponseContext{" +
|
||||||
|
"httpStatus=" + httpStatus +
|
||||||
|
", httpHeaders=" + httpHeaders +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final class EnhancedContextResponseBuilder {
|
||||||
|
private Integer httpStatus;
|
||||||
|
private HttpHeaders httpHeaders;
|
||||||
|
|
||||||
|
private EnhancedContextResponseBuilder() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public EnhancedContextResponseBuilder httpStatus(Integer httpStatus) {
|
||||||
|
this.httpStatus = httpStatus;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EnhancedContextResponseBuilder httpHeaders(HttpHeaders httpHeaders) {
|
||||||
|
this.httpHeaders = httpHeaders;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EnhancedResponseContext build() {
|
||||||
|
EnhancedResponseContext enhancedResponseContext = new EnhancedResponseContext();
|
||||||
|
enhancedResponseContext.setHttpStatus(httpStatus);
|
||||||
|
enhancedResponseContext.setHttpHeaders(httpHeaders);
|
||||||
|
return enhancedResponseContext;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,106 @@
|
|||||||
|
/*
|
||||||
|
* 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.plugin.reporter;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import com.tencent.cloud.rpc.enhancement.AbstractPolarisReporterAdapter;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPlugin;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedRequestContext;
|
||||||
|
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||||
|
import com.tencent.polaris.api.rpc.ServiceCallResult;
|
||||||
|
import com.tencent.polaris.client.api.SDKContext;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Polaris reporter when feign call fails.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public class ExceptionPolarisReporter extends AbstractPolarisReporterAdapter implements EnhancedPlugin {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(ExceptionPolarisReporter.class);
|
||||||
|
|
||||||
|
private final ConsumerAPI consumerAPI;
|
||||||
|
|
||||||
|
public ExceptionPolarisReporter(RpcEnhancementReporterProperties reporterProperties,
|
||||||
|
SDKContext context,
|
||||||
|
ConsumerAPI consumerAPI) {
|
||||||
|
super(reporterProperties, context);
|
||||||
|
this.consumerAPI = consumerAPI;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return ExceptionPolarisReporter.class.getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EnhancedPluginType getType() {
|
||||||
|
return EnhancedPluginType.EXCEPTION;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(EnhancedPluginContext context) {
|
||||||
|
if (!super.reportProperties.isEnabled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
EnhancedRequestContext request = context.getRequest();
|
||||||
|
ServiceInstance serviceInstance = Optional.ofNullable(context.getServiceInstance()).orElse(new DefaultServiceInstance());
|
||||||
|
|
||||||
|
ServiceCallResult resultRequest = createServiceCallResult(
|
||||||
|
serviceInstance.getServiceId(),
|
||||||
|
serviceInstance.getHost(),
|
||||||
|
serviceInstance.getPort(),
|
||||||
|
request.getUrl(),
|
||||||
|
request.getHttpHeaders(),
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
context.getDelay(),
|
||||||
|
context.getThrowable()
|
||||||
|
);
|
||||||
|
|
||||||
|
LOG.debug("Will report ServiceCallResult of {}. Request=[{} {}]. Response=[{}]. Delay=[{}]ms.",
|
||||||
|
resultRequest.getRetStatus().name(), request.getHttpMethod().name(), request.getUrl().getPath(), context.getThrowable().getMessage(), context.getDelay());
|
||||||
|
|
||||||
|
consumerAPI.updateServiceCallResult(resultRequest);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handlerThrowable(EnhancedPluginContext context, Throwable throwable) {
|
||||||
|
LOG.error("ExceptionPolarisReporter runs failed. context=[{}].",
|
||||||
|
context, throwable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return Ordered.HIGHEST_PRECEDENCE + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,106 @@
|
|||||||
|
/*
|
||||||
|
* 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.plugin.reporter;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import com.tencent.cloud.rpc.enhancement.AbstractPolarisReporterAdapter;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPlugin;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedRequestContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedResponseContext;
|
||||||
|
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||||
|
import com.tencent.polaris.api.rpc.ServiceCallResult;
|
||||||
|
import com.tencent.polaris.client.api.SDKContext;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Polaris reporter when feign call is successful.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public class SuccessPolarisReporter extends AbstractPolarisReporterAdapter implements EnhancedPlugin {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(SuccessPolarisReporter.class);
|
||||||
|
|
||||||
|
private final ConsumerAPI consumerAPI;
|
||||||
|
|
||||||
|
public SuccessPolarisReporter(RpcEnhancementReporterProperties properties,
|
||||||
|
SDKContext context,
|
||||||
|
ConsumerAPI consumerAPI) {
|
||||||
|
super(properties, context);
|
||||||
|
this.consumerAPI = consumerAPI;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return SuccessPolarisReporter.class.getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EnhancedPluginType getType() {
|
||||||
|
return EnhancedPluginType.POST;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(EnhancedPluginContext context) {
|
||||||
|
if (!super.reportProperties.isEnabled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
EnhancedRequestContext request = context.getRequest();
|
||||||
|
EnhancedResponseContext response = context.getResponse();
|
||||||
|
ServiceInstance serviceInstance = Optional.ofNullable(context.getServiceInstance()).orElse(new DefaultServiceInstance());
|
||||||
|
|
||||||
|
ServiceCallResult resultRequest = createServiceCallResult(
|
||||||
|
serviceInstance.getServiceId(),
|
||||||
|
serviceInstance.getHost(),
|
||||||
|
serviceInstance.getPort(),
|
||||||
|
request.getUrl(),
|
||||||
|
request.getHttpHeaders(),
|
||||||
|
response.getHttpHeaders(),
|
||||||
|
response.getHttpStatus(),
|
||||||
|
context.getDelay(),
|
||||||
|
null
|
||||||
|
);
|
||||||
|
|
||||||
|
LOG.debug("Will report ServiceCallResult of {}. Request=[{} {}]. Response=[{}]. Delay=[{}]ms.",
|
||||||
|
resultRequest.getRetStatus().name(), request.getHttpMethod().name(), request.getUrl().getPath(), response.getHttpStatus(), context.getDelay());
|
||||||
|
|
||||||
|
consumerAPI.updateServiceCallResult(resultRequest);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handlerThrowable(EnhancedPluginContext context, Throwable throwable) {
|
||||||
|
LOG.error("SuccessPolarisReporter runs failed. context=[{}].",
|
||||||
|
context, throwable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return Ordered.HIGHEST_PRECEDENCE + 1;
|
||||||
|
}
|
||||||
|
}
|
@ -1,131 +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.rpc.enhancement.resttemplate;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.SocketTimeoutException;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import com.tencent.cloud.common.constant.HeaderConstant;
|
|
||||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
|
||||||
import com.tencent.cloud.rpc.enhancement.AbstractPolarisReporterAdapter;
|
|
||||||
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
|
||||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
|
||||||
import com.tencent.polaris.api.plugin.circuitbreaker.ResourceStat;
|
|
||||||
import com.tencent.polaris.api.rpc.ServiceCallResult;
|
|
||||||
import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI;
|
|
||||||
import com.tencent.polaris.client.api.SDKContext;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.http.HttpRequest;
|
|
||||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
|
||||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
|
||||||
import org.springframework.http.client.ClientHttpResponse;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* EnhancedPolarisRestTemplateReporter.
|
|
||||||
*
|
|
||||||
* @author sean yu
|
|
||||||
*/
|
|
||||||
public class EnhancedPolarisRestTemplateReporter extends AbstractPolarisReporterAdapter implements ClientHttpRequestInterceptor {
|
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(EnhancedPolarisRestTemplateReporter.class);
|
|
||||||
|
|
||||||
private final ConsumerAPI consumerAPI;
|
|
||||||
|
|
||||||
private final CircuitBreakAPI circuitBreakAPI;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor With {@link RpcEnhancementReporterProperties} .
|
|
||||||
*
|
|
||||||
* @param reportProperties instance of {@link RpcEnhancementReporterProperties}.
|
|
||||||
*/
|
|
||||||
public EnhancedPolarisRestTemplateReporter(RpcEnhancementReporterProperties reportProperties,
|
|
||||||
SDKContext context,
|
|
||||||
ConsumerAPI consumerAPI,
|
|
||||||
CircuitBreakAPI circuitBreakAPI) {
|
|
||||||
super(reportProperties, context);
|
|
||||||
this.consumerAPI = consumerAPI;
|
|
||||||
this.circuitBreakAPI = circuitBreakAPI;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
|
|
||||||
if (!reportProperties.isEnabled()) {
|
|
||||||
return execution.execute(request, body);
|
|
||||||
}
|
|
||||||
|
|
||||||
long startTime = System.currentTimeMillis();
|
|
||||||
ClientHttpResponse response = null;
|
|
||||||
IOException ex = null;
|
|
||||||
try {
|
|
||||||
response = execution.execute(request, body);
|
|
||||||
}
|
|
||||||
catch (SocketTimeoutException e) {
|
|
||||||
ex = e;
|
|
||||||
}
|
|
||||||
HttpHeaders requestHeaders = request.getHeaders();
|
|
||||||
HttpHeaders responseHeaders = null;
|
|
||||||
Integer status = null;
|
|
||||||
if (response != null) {
|
|
||||||
responseHeaders = response.getHeaders();
|
|
||||||
status = response.getRawStatusCode();
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<String, String> loadBalancerContext = MetadataContextHolder.get().getLoadbalancerMetadata();
|
|
||||||
String targetHost = loadBalancerContext.get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_HOST);
|
|
||||||
Integer targetPort = loadBalancerContext.get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_PORT) != null ? Integer.valueOf(loadBalancerContext.get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_PORT)) : null;
|
|
||||||
long delay = System.currentTimeMillis() - startTime;
|
|
||||||
|
|
||||||
ServiceCallResult resultRequest = createServiceCallResult(
|
|
||||||
request.getURI().getHost(),
|
|
||||||
targetHost,
|
|
||||||
targetPort,
|
|
||||||
request.getURI(),
|
|
||||||
requestHeaders,
|
|
||||||
responseHeaders,
|
|
||||||
status,
|
|
||||||
delay,
|
|
||||||
ex
|
|
||||||
);
|
|
||||||
LOG.debug("Will report result of {}. Request=[{} {}]. Response=[{}]. Delay=[{}]ms.",
|
|
||||||
resultRequest.getRetStatus().name(), request.getMethod(), request.getURI().getPath(), status, delay);
|
|
||||||
consumerAPI.updateServiceCallResult(resultRequest);
|
|
||||||
|
|
||||||
ResourceStat resourceStat = createInstanceResourceStat(
|
|
||||||
request.getURI().getHost(),
|
|
||||||
targetHost,
|
|
||||||
targetPort,
|
|
||||||
request.getURI(),
|
|
||||||
status,
|
|
||||||
delay,
|
|
||||||
ex
|
|
||||||
);
|
|
||||||
circuitBreakAPI.report(resourceStat);
|
|
||||||
|
|
||||||
if (ex != null) {
|
|
||||||
throw ex;
|
|
||||||
}
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,105 @@
|
|||||||
|
/*
|
||||||
|
* 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.resttemplate;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.constant.HeaderConstant;
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginRunner;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedRequestContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedResponseContext;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||||
|
import org.springframework.http.HttpRequest;
|
||||||
|
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||||
|
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||||
|
import org.springframework.http.client.ClientHttpResponse;
|
||||||
|
|
||||||
|
import static com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType.EXCEPTION;
|
||||||
|
import static com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType.FINALLY;
|
||||||
|
import static com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType.POST;
|
||||||
|
import static com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType.PRE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EnhancedRestTemplate.
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
public class EnhancedRestTemplate implements ClientHttpRequestInterceptor {
|
||||||
|
|
||||||
|
private final EnhancedPluginRunner pluginRunner;
|
||||||
|
|
||||||
|
public EnhancedRestTemplate(EnhancedPluginRunner pluginRunner) {
|
||||||
|
this.pluginRunner = pluginRunner;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
|
||||||
|
|
||||||
|
EnhancedPluginContext enhancedPluginContext = new EnhancedPluginContext();
|
||||||
|
|
||||||
|
EnhancedRequestContext enhancedRequestContext = EnhancedRequestContext.builder()
|
||||||
|
.httpHeaders(request.getHeaders())
|
||||||
|
.httpMethod(request.getMethod())
|
||||||
|
.url(request.getURI())
|
||||||
|
.build();
|
||||||
|
enhancedPluginContext.setRequest(enhancedRequestContext);
|
||||||
|
|
||||||
|
// Run pre enhanced plugins.
|
||||||
|
pluginRunner.run(PRE, enhancedPluginContext);
|
||||||
|
long startMillis = System.currentTimeMillis();
|
||||||
|
try {
|
||||||
|
ClientHttpResponse response = execution.execute(request, body);
|
||||||
|
enhancedPluginContext.setDelay(System.currentTimeMillis() - startMillis);
|
||||||
|
|
||||||
|
EnhancedResponseContext enhancedResponseContext = EnhancedResponseContext.builder()
|
||||||
|
.httpStatus(response.getRawStatusCode())
|
||||||
|
.httpHeaders(response.getHeaders())
|
||||||
|
.build();
|
||||||
|
enhancedPluginContext.setResponse(enhancedResponseContext);
|
||||||
|
|
||||||
|
Map<String, String> loadBalancerContext = MetadataContextHolder.get().getLoadbalancerMetadata();
|
||||||
|
DefaultServiceInstance serviceInstance = new DefaultServiceInstance();
|
||||||
|
serviceInstance.setServiceId(request.getURI().getHost());
|
||||||
|
serviceInstance.setHost(loadBalancerContext.get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_HOST));
|
||||||
|
if (loadBalancerContext.get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_PORT) != null) {
|
||||||
|
serviceInstance.setPort(Integer.parseInt(loadBalancerContext.get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_PORT)));
|
||||||
|
}
|
||||||
|
enhancedPluginContext.setServiceInstance(serviceInstance);
|
||||||
|
|
||||||
|
// Run post enhanced plugins.
|
||||||
|
pluginRunner.run(POST, enhancedPluginContext);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
enhancedPluginContext.setDelay(System.currentTimeMillis() - startMillis);
|
||||||
|
enhancedPluginContext.setThrowable(e);
|
||||||
|
// Run exception enhanced plugins.
|
||||||
|
pluginRunner.run(EXCEPTION, enhancedPluginContext);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
// Run finally enhanced plugins.
|
||||||
|
pluginRunner.run(FINALLY, enhancedPluginContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,111 @@
|
|||||||
|
/*
|
||||||
|
* 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.scg;
|
||||||
|
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginRunner;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedRequestContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedResponseContext;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.Response;
|
||||||
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||||
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
|
import static com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType.EXCEPTION;
|
||||||
|
import static com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType.FINALLY;
|
||||||
|
import static com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType.POST;
|
||||||
|
import static com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType.PRE;
|
||||||
|
import static org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter.ROUTE_TO_URL_FILTER_ORDER;
|
||||||
|
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_LOADBALANCER_RESPONSE_ATTR;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EnhancedGatewayGlobalFilter.
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
public class EnhancedGatewayGlobalFilter implements GlobalFilter, Ordered {
|
||||||
|
|
||||||
|
private final EnhancedPluginRunner pluginRunner;
|
||||||
|
|
||||||
|
public EnhancedGatewayGlobalFilter(EnhancedPluginRunner pluginRunner) {
|
||||||
|
this.pluginRunner = pluginRunner;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
|
EnhancedPluginContext enhancedPluginContext = new EnhancedPluginContext();
|
||||||
|
|
||||||
|
EnhancedRequestContext enhancedRequestContext = EnhancedRequestContext.builder()
|
||||||
|
.httpHeaders(exchange.getRequest().getHeaders())
|
||||||
|
.httpMethod(exchange.getRequest().getMethod())
|
||||||
|
.url(exchange.getRequest().getURI())
|
||||||
|
.build();
|
||||||
|
enhancedPluginContext.setRequest(enhancedRequestContext);
|
||||||
|
|
||||||
|
// Run pre enhanced plugins.
|
||||||
|
pluginRunner.run(PRE, enhancedPluginContext);
|
||||||
|
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
return chain.filter(exchange)
|
||||||
|
.doOnSuccess(v -> {
|
||||||
|
enhancedPluginContext.setDelay(System.currentTimeMillis() - startTime);
|
||||||
|
|
||||||
|
EnhancedResponseContext enhancedResponseContext = EnhancedResponseContext.builder()
|
||||||
|
.httpStatus(exchange.getResponse().getRawStatusCode())
|
||||||
|
.httpHeaders(exchange.getResponse().getHeaders())
|
||||||
|
.build();
|
||||||
|
enhancedPluginContext.setResponse(enhancedResponseContext);
|
||||||
|
|
||||||
|
Response<ServiceInstance> serviceInstanceResponse = exchange.getAttribute(GATEWAY_LOADBALANCER_RESPONSE_ATTR);
|
||||||
|
if (serviceInstanceResponse != null && serviceInstanceResponse.hasServer()) {
|
||||||
|
ServiceInstance instance = serviceInstanceResponse.getServer();
|
||||||
|
enhancedPluginContext.setServiceInstance(instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run post enhanced plugins.
|
||||||
|
pluginRunner.run(POST, enhancedPluginContext);
|
||||||
|
})
|
||||||
|
.doOnError(t -> {
|
||||||
|
enhancedPluginContext.setDelay(System.currentTimeMillis() - startTime);
|
||||||
|
|
||||||
|
enhancedPluginContext.setThrowable(t);
|
||||||
|
|
||||||
|
Response<ServiceInstance> serviceInstanceResponse = exchange.getAttribute(GATEWAY_LOADBALANCER_RESPONSE_ATTR);
|
||||||
|
if (serviceInstanceResponse != null && serviceInstanceResponse.hasServer()) {
|
||||||
|
ServiceInstance instance = serviceInstanceResponse.getServer();
|
||||||
|
enhancedPluginContext.setServiceInstance(instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run exception enhanced plugins.
|
||||||
|
pluginRunner.run(EXCEPTION, enhancedPluginContext);
|
||||||
|
})
|
||||||
|
.doFinally(v -> {
|
||||||
|
// Run finally enhanced plugins.
|
||||||
|
pluginRunner.run(FINALLY, enhancedPluginContext);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return ROUTE_TO_URL_FILTER_ORDER + 1;
|
||||||
|
}
|
||||||
|
}
|
@ -1,129 +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.rpc.enhancement.scg;
|
|
||||||
|
|
||||||
import com.tencent.cloud.rpc.enhancement.AbstractPolarisReporterAdapter;
|
|
||||||
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
|
||||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
|
||||||
import com.tencent.polaris.api.plugin.circuitbreaker.ResourceStat;
|
|
||||||
import com.tencent.polaris.api.rpc.ServiceCallResult;
|
|
||||||
import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI;
|
|
||||||
import com.tencent.polaris.client.api.SDKContext;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import reactor.core.publisher.Mono;
|
|
||||||
|
|
||||||
import org.springframework.cloud.client.ServiceInstance;
|
|
||||||
import org.springframework.cloud.client.loadbalancer.Response;
|
|
||||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
||||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
||||||
import org.springframework.core.Ordered;
|
|
||||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
||||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
|
||||||
|
|
||||||
import static org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter.ROUTE_TO_URL_FILTER_ORDER;
|
|
||||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_LOADBALANCER_RESPONSE_ATTR;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* EnhancedPolarisGatewayReporter.
|
|
||||||
*
|
|
||||||
* @author sean yu
|
|
||||||
*/
|
|
||||||
public class EnhancedPolarisGatewayReporter extends AbstractPolarisReporterAdapter implements GlobalFilter, Ordered {
|
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(EnhancedPolarisGatewayReporter.class);
|
|
||||||
|
|
||||||
private final ConsumerAPI consumerAPI;
|
|
||||||
|
|
||||||
private final CircuitBreakAPI circuitBreakAPI;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor With {@link RpcEnhancementReporterProperties} .
|
|
||||||
*
|
|
||||||
* @param reportProperties instance of {@link RpcEnhancementReporterProperties}.
|
|
||||||
*/
|
|
||||||
public EnhancedPolarisGatewayReporter(RpcEnhancementReporterProperties reportProperties,
|
|
||||||
SDKContext context,
|
|
||||||
ConsumerAPI consumerAPI,
|
|
||||||
CircuitBreakAPI circuitBreakAPI) {
|
|
||||||
super(reportProperties, context);
|
|
||||||
this.consumerAPI = consumerAPI;
|
|
||||||
this.circuitBreakAPI = circuitBreakAPI;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
|
||||||
if (!reportProperties.isEnabled()) {
|
|
||||||
return chain.filter(exchange);
|
|
||||||
}
|
|
||||||
long startTime = System.currentTimeMillis();
|
|
||||||
return chain.filter(exchange)
|
|
||||||
.doOnSuccess(v -> instrumentResponse(exchange, null, startTime))
|
|
||||||
.doOnError(t -> instrumentResponse(exchange, t, startTime));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void instrumentResponse(ServerWebExchange exchange, Throwable t, long startTime) {
|
|
||||||
ServerHttpResponse response = exchange.getResponse();
|
|
||||||
ServerHttpRequest request = exchange.getRequest();
|
|
||||||
|
|
||||||
long delay = System.currentTimeMillis() - startTime;
|
|
||||||
String serviceId = null;
|
|
||||||
String targetHost = null;
|
|
||||||
Integer targetPort = null;
|
|
||||||
|
|
||||||
Response<ServiceInstance> serviceInstanceResponse = exchange.getAttribute(GATEWAY_LOADBALANCER_RESPONSE_ATTR);
|
|
||||||
if (serviceInstanceResponse != null && serviceInstanceResponse.hasServer()) {
|
|
||||||
ServiceInstance instance = serviceInstanceResponse.getServer();
|
|
||||||
serviceId = instance.getServiceId();
|
|
||||||
targetHost = instance.getHost();
|
|
||||||
targetPort = instance.getPort();
|
|
||||||
}
|
|
||||||
|
|
||||||
ServiceCallResult resultRequest = createServiceCallResult(
|
|
||||||
serviceId,
|
|
||||||
targetHost,
|
|
||||||
targetPort,
|
|
||||||
request.getURI(),
|
|
||||||
request.getHeaders(),
|
|
||||||
response.getHeaders(),
|
|
||||||
response.getRawStatusCode(),
|
|
||||||
delay,
|
|
||||||
t
|
|
||||||
);
|
|
||||||
LOG.debug("Will report result of {}. Request=[{} {}]. Response=[{}]. Delay=[{}]ms.",
|
|
||||||
resultRequest.getRetStatus().name(), request.getMethod(), request.getURI().getPath(), response.getRawStatusCode(), delay);
|
|
||||||
consumerAPI.updateServiceCallResult(resultRequest);
|
|
||||||
|
|
||||||
ResourceStat resourceStat = createInstanceResourceStat(
|
|
||||||
serviceId,
|
|
||||||
targetHost,
|
|
||||||
targetPort,
|
|
||||||
request.getURI(),
|
|
||||||
response.getRawStatusCode(),
|
|
||||||
delay,
|
|
||||||
t
|
|
||||||
);
|
|
||||||
circuitBreakAPI.report(resourceStat);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getOrder() {
|
|
||||||
return ROUTE_TO_URL_FILTER_ORDER + 1;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,46 +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.rpc.enhancement.feign.plugin;
|
|
||||||
|
|
||||||
import feign.Request;
|
|
||||||
import feign.Response;
|
|
||||||
import org.junit.jupiter.api.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,134 @@
|
|||||||
|
/*
|
||||||
|
* 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.plugin;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||||
|
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.reporter.ExceptionPolarisReporter;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.reporter.SuccessPolarisReporter;
|
||||||
|
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||||
|
import com.tencent.polaris.client.api.SDKContext;
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockedStatic;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
|
||||||
|
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.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.doThrow;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link EnhancedPluginContext}.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
public class EnhancedPluginContextTest {
|
||||||
|
|
||||||
|
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
||||||
|
@Mock
|
||||||
|
private RpcEnhancementReporterProperties reporterProperties;
|
||||||
|
@Mock
|
||||||
|
private SDKContext sdkContext;
|
||||||
|
@Mock
|
||||||
|
private ConsumerAPI consumerAPI;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void beforeAll() {
|
||||||
|
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
||||||
|
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
||||||
|
.thenReturn("unit-test");
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
static void afterAll() {
|
||||||
|
mockedApplicationContextAwareUtils.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
MetadataContext.LOCAL_NAMESPACE = NAMESPACE_TEST;
|
||||||
|
MetadataContext.LOCAL_SERVICE = SERVICE_PROVIDER;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAndSet() throws Throwable {
|
||||||
|
EnhancedRequestContext requestContext = new EnhancedRequestContext();
|
||||||
|
requestContext.setHttpHeaders(new HttpHeaders());
|
||||||
|
requestContext.setUrl(new URI("/"));
|
||||||
|
requestContext.setHttpMethod(HttpMethod.GET);
|
||||||
|
|
||||||
|
EnhancedRequestContext requestContext1 = EnhancedRequestContext.builder()
|
||||||
|
.httpHeaders(requestContext.getHttpHeaders())
|
||||||
|
.url(requestContext.getUrl())
|
||||||
|
.httpMethod(requestContext.getHttpMethod())
|
||||||
|
.build();
|
||||||
|
assertThat(requestContext1.getUrl()).isEqualTo(requestContext.getUrl());
|
||||||
|
|
||||||
|
EnhancedResponseContext responseContext = new EnhancedResponseContext();
|
||||||
|
responseContext.setHttpStatus(200);
|
||||||
|
responseContext.setHttpHeaders(new HttpHeaders());
|
||||||
|
|
||||||
|
EnhancedResponseContext responseContext1 = EnhancedResponseContext.builder()
|
||||||
|
.httpStatus(responseContext.getHttpStatus())
|
||||||
|
.httpHeaders(responseContext.getHttpHeaders())
|
||||||
|
.build();
|
||||||
|
assertThat(responseContext1.getHttpStatus()).isEqualTo(responseContext.getHttpStatus());
|
||||||
|
|
||||||
|
EnhancedPluginContext enhancedPluginContext = new EnhancedPluginContext();
|
||||||
|
enhancedPluginContext.setRequest(requestContext);
|
||||||
|
enhancedPluginContext.setResponse(responseContext);
|
||||||
|
enhancedPluginContext.setServiceInstance(new DefaultServiceInstance());
|
||||||
|
enhancedPluginContext.setThrowable(mock(Exception.class));
|
||||||
|
enhancedPluginContext.setDelay(0);
|
||||||
|
assertThat(enhancedPluginContext.getRequest()).isNotNull();
|
||||||
|
assertThat(enhancedPluginContext.getResponse()).isNotNull();
|
||||||
|
assertThat(enhancedPluginContext.getServiceInstance()).isNotNull();
|
||||||
|
assertThat(enhancedPluginContext.getThrowable()).isNotNull();
|
||||||
|
assertThat(enhancedPluginContext.getDelay()).isNotNull();
|
||||||
|
|
||||||
|
EnhancedPlugin enhancedPlugin = new SuccessPolarisReporter(reporterProperties, sdkContext, consumerAPI);
|
||||||
|
EnhancedPlugin enhancedPlugin1 = new ExceptionPolarisReporter(reporterProperties, sdkContext, consumerAPI);
|
||||||
|
EnhancedPluginRunner enhancedPluginRunner = new DefaultEnhancedPluginRunner(Arrays.asList(enhancedPlugin, enhancedPlugin1));
|
||||||
|
enhancedPluginRunner.run(EnhancedPluginType.POST, enhancedPluginContext);
|
||||||
|
|
||||||
|
EnhancedPlugin enhancedPlugin2 = mock(EnhancedPlugin.class);
|
||||||
|
doThrow(new RuntimeException()).when(enhancedPlugin2).run(any());
|
||||||
|
doReturn(EnhancedPluginType.POST).when(enhancedPlugin2).getType();
|
||||||
|
enhancedPluginRunner = new DefaultEnhancedPluginRunner(Arrays.asList(enhancedPlugin2));
|
||||||
|
enhancedPluginRunner.run(EnhancedPluginType.POST, enhancedPluginContext);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue