|
|
|
@ -18,44 +18,31 @@
|
|
|
|
|
package com.tencent.cloud.polaris.circuitbreaker.resttemplate;
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.net.URI;
|
|
|
|
|
import java.net.URISyntaxException;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
import com.google.protobuf.util.JsonFormat;
|
|
|
|
|
import com.tencent.cloud.polaris.circuitbreaker.PolarisCircuitBreakerFactory;
|
|
|
|
|
import com.tencent.cloud.polaris.circuitbreaker.config.PolarisCircuitBreakerFeignClientAutoConfiguration;
|
|
|
|
|
import com.tencent.cloud.polaris.circuitbreaker.config.PolarisCircuitBreakerProperties;
|
|
|
|
|
import com.tencent.cloud.polaris.circuitbreaker.reporter.ExceptionCircuitBreakerReporter;
|
|
|
|
|
import com.tencent.cloud.polaris.circuitbreaker.reporter.SuccessCircuitBreakerReporter;
|
|
|
|
|
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
|
|
|
|
|
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
|
|
|
|
import com.tencent.polaris.api.pojo.ServiceKey;
|
|
|
|
|
import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI;
|
|
|
|
|
import com.tencent.polaris.circuitbreak.factory.CircuitBreakAPIFactory;
|
|
|
|
|
import com.tencent.polaris.client.util.Utils;
|
|
|
|
|
import com.tencent.polaris.specification.api.v1.fault.tolerance.CircuitBreakerProto;
|
|
|
|
|
import com.tencent.polaris.test.common.TestUtils;
|
|
|
|
|
import com.tencent.polaris.test.mock.discovery.NamingServer;
|
|
|
|
|
import org.junit.jupiter.api.AfterAll;
|
|
|
|
|
import org.junit.jupiter.api.BeforeAll;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.DisposableBean;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
|
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
|
|
|
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
|
|
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
|
|
|
|
|
import org.springframework.cloud.client.circuitbreaker.Customizer;
|
|
|
|
|
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
|
|
|
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
@ -74,7 +61,6 @@ import org.springframework.web.client.RestTemplate;
|
|
|
|
|
import org.springframework.web.util.DefaultUriBuilderFactory;
|
|
|
|
|
|
|
|
|
|
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
|
|
|
|
import static com.tencent.polaris.test.common.TestUtils.SERVER_ADDRESS_ENV;
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|
|
|
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
|
|
|
@ -90,6 +76,7 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
|
|
|
|
|
classes = PolarisCircuitBreakerRestTemplateIntegrationTest.TestConfig.class,
|
|
|
|
|
properties = {
|
|
|
|
|
"spring.cloud.gateway.enabled=false",
|
|
|
|
|
"spring.cloud.polaris.address=grpc://127.0.0.1:10081",
|
|
|
|
|
"feign.circuitbreaker.enabled=true",
|
|
|
|
|
"spring.cloud.polaris.namespace=" + NAMESPACE_TEST,
|
|
|
|
|
"spring.cloud.polaris.service=test"
|
|
|
|
@ -98,6 +85,8 @@ public class PolarisCircuitBreakerRestTemplateIntegrationTest {
|
|
|
|
|
|
|
|
|
|
private static final String TEST_SERVICE_NAME = "test-service-callee";
|
|
|
|
|
|
|
|
|
|
private static NamingServer namingServer;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
@Qualifier("defaultRestTemplate")
|
|
|
|
|
private RestTemplate defaultRestTemplate;
|
|
|
|
@ -125,6 +114,29 @@ public class PolarisCircuitBreakerRestTemplateIntegrationTest {
|
|
|
|
|
@Autowired
|
|
|
|
|
private ApplicationContext applicationContext;
|
|
|
|
|
|
|
|
|
|
@BeforeAll
|
|
|
|
|
static void beforeAll() throws Exception {
|
|
|
|
|
PolarisSDKContextManager.innerDestroy();
|
|
|
|
|
namingServer = NamingServer.startNamingServer(10081);
|
|
|
|
|
ServiceKey serviceKey = new ServiceKey(NAMESPACE_TEST, TEST_SERVICE_NAME);
|
|
|
|
|
CircuitBreakerProto.CircuitBreakerRule.Builder circuitBreakerRuleBuilder = CircuitBreakerProto.CircuitBreakerRule.newBuilder();
|
|
|
|
|
InputStream inputStream = PolarisCircuitBreakerRestTemplateIntegrationTest.class.getClassLoader()
|
|
|
|
|
.getResourceAsStream("circuitBreakerRule.json");
|
|
|
|
|
String json = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines()
|
|
|
|
|
.collect(Collectors.joining(""));
|
|
|
|
|
JsonFormat.parser().ignoringUnknownFields().merge(json, circuitBreakerRuleBuilder);
|
|
|
|
|
CircuitBreakerProto.CircuitBreakerRule circuitBreakerRule = circuitBreakerRuleBuilder.build();
|
|
|
|
|
CircuitBreakerProto.CircuitBreaker circuitBreaker = CircuitBreakerProto.CircuitBreaker.newBuilder()
|
|
|
|
|
.addRules(circuitBreakerRule).build();
|
|
|
|
|
namingServer.getNamingService().setCircuitBreaker(serviceKey, circuitBreaker);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@AfterAll
|
|
|
|
|
static void afterAll() {
|
|
|
|
|
if (null != namingServer) {
|
|
|
|
|
namingServer.terminate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testRestTemplate() throws URISyntaxException {
|
|
|
|
@ -151,7 +163,8 @@ public class PolarisCircuitBreakerRestTemplateIntegrationTest {
|
|
|
|
|
Utils.sleepUninterrupted(2000);
|
|
|
|
|
assertThat(restTemplateFallbackFromCode2.getForObject("/example/service/b/info", String.class)).isEqualTo("\"this is a fallback class\"");
|
|
|
|
|
Utils.sleepUninterrupted(2000);
|
|
|
|
|
assertThat(restTemplateFallbackFromCode3.getForEntity("/example/service/b/info", String.class).getStatusCode()).isEqualTo(HttpStatus.OK);
|
|
|
|
|
assertThat(restTemplateFallbackFromCode3.getForEntity("/example/service/b/info", String.class)
|
|
|
|
|
.getStatusCode()).isEqualTo(HttpStatus.OK);
|
|
|
|
|
Utils.sleepUninterrupted(2000);
|
|
|
|
|
assertThat(restTemplateFallbackFromCode4.getForObject("/example/service/b/info", String.class)).isEqualTo("fallback");
|
|
|
|
|
Utils.sleepUninterrupted(2000);
|
|
|
|
@ -168,13 +181,6 @@ public class PolarisCircuitBreakerRestTemplateIntegrationTest {
|
|
|
|
|
@EnableFeignClients
|
|
|
|
|
public static class TestConfig {
|
|
|
|
|
|
|
|
|
|
@Autowired(required = false)
|
|
|
|
|
private List<Customizer<PolarisCircuitBreakerFactory>> customizers = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
PolarisSDKContextManager.innerDestroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
@com.tencent.cloud.polaris.circuitbreaker.resttemplate.PolarisCircuitBreaker(fallback = "fallback")
|
|
|
|
|
public RestTemplate defaultRestTemplate() {
|
|
|
|
@ -246,59 +252,6 @@ public class PolarisCircuitBreakerRestTemplateIntegrationTest {
|
|
|
|
|
return new CustomPolarisCircuitBreakerFallback3();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
public NamingServer namingServer() throws IOException {
|
|
|
|
|
NamingServer namingServer = NamingServer.startNamingServer(-1);
|
|
|
|
|
System.setProperty(SERVER_ADDRESS_ENV, String.format("127.0.0.1:%d", namingServer.getPort()));
|
|
|
|
|
ServiceKey serviceKey = new ServiceKey(NAMESPACE_TEST, TEST_SERVICE_NAME);
|
|
|
|
|
CircuitBreakerProto.CircuitBreakerRule.Builder circuitBreakerRuleBuilder = CircuitBreakerProto.CircuitBreakerRule.newBuilder();
|
|
|
|
|
InputStream inputStream = PolarisCircuitBreakerRestTemplateIntegrationTest.class.getClassLoader()
|
|
|
|
|
.getResourceAsStream("circuitBreakerRule.json");
|
|
|
|
|
String json = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines()
|
|
|
|
|
.collect(Collectors.joining(""));
|
|
|
|
|
JsonFormat.parser().ignoringUnknownFields().merge(json, circuitBreakerRuleBuilder);
|
|
|
|
|
CircuitBreakerProto.CircuitBreakerRule circuitBreakerRule = circuitBreakerRuleBuilder.build();
|
|
|
|
|
CircuitBreakerProto.CircuitBreaker circuitBreaker = CircuitBreakerProto.CircuitBreaker.newBuilder()
|
|
|
|
|
.addRules(circuitBreakerRule).build();
|
|
|
|
|
namingServer.getNamingService().setCircuitBreaker(serviceKey, circuitBreaker);
|
|
|
|
|
return namingServer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
public PreDestroy preDestroy(NamingServer namingServer) {
|
|
|
|
|
return new PreDestroy(namingServer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
public CircuitBreakAPI circuitBreakAPI(NamingServer namingServer) {
|
|
|
|
|
com.tencent.polaris.api.config.Configuration configuration = TestUtils.configWithEnvAddress();
|
|
|
|
|
return CircuitBreakAPIFactory.createCircuitBreakAPIByConfig(configuration);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
@ConditionalOnMissingBean(SuccessCircuitBreakerReporter.class)
|
|
|
|
|
public SuccessCircuitBreakerReporter successCircuitBreakerReporter(RpcEnhancementReporterProperties properties,
|
|
|
|
|
CircuitBreakAPI circuitBreakAPI) {
|
|
|
|
|
return new SuccessCircuitBreakerReporter(properties, circuitBreakAPI);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
@ConditionalOnMissingBean(ExceptionCircuitBreakerReporter.class)
|
|
|
|
|
public ExceptionCircuitBreakerReporter exceptionCircuitBreakerReporter(RpcEnhancementReporterProperties properties,
|
|
|
|
|
CircuitBreakAPI circuitBreakAPI) {
|
|
|
|
|
return new ExceptionCircuitBreakerReporter(properties, circuitBreakAPI);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
@ConditionalOnMissingBean(CircuitBreakerFactory.class)
|
|
|
|
|
public CircuitBreakerFactory polarisCircuitBreakerFactory(CircuitBreakAPI circuitBreakAPI,
|
|
|
|
|
PolarisSDKContextManager polarisSDKContextManager, PolarisCircuitBreakerProperties polarisCircuitBreakerProperties) {
|
|
|
|
|
PolarisCircuitBreakerFactory factory = new PolarisCircuitBreakerFactory(
|
|
|
|
|
circuitBreakAPI, polarisSDKContextManager.getConsumerAPI(), polarisCircuitBreakerProperties);
|
|
|
|
|
customizers.forEach(customizer -> customizer.customize(factory));
|
|
|
|
|
return factory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/example/service/b")
|
|
|
|
|
public class ServiceBController {
|
|
|
|
@ -347,19 +300,4 @@ public class PolarisCircuitBreakerRestTemplateIntegrationTest {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class PreDestroy implements DisposableBean {
|
|
|
|
|
|
|
|
|
|
private final NamingServer namingServer;
|
|
|
|
|
|
|
|
|
|
public PreDestroy(NamingServer namingServer) {
|
|
|
|
|
this.namingServer = namingServer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void destroy() throws Exception {
|
|
|
|
|
namingServer.terminate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|