From 7f8e34d3e394ac72be9522b2acca63125466ec52 Mon Sep 17 00:00:00 2001 From: yukdawn Date: Wed, 6 Apr 2022 21:22:57 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 3 +- .../config/RetrofitAutoConfiguration.java | 9 ++++ .../spring/boot/test/RetrofitStarterTest.java | 26 ++++++++++ .../spring/boot/test/http/HttpApi4.java | 52 +++++++++++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/HttpApi4.java diff --git a/pom.xml b/pom.xml index d8e8eda..56bd2f7 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,7 @@ 1.17.5 1.6.3 2.12.6.1 + 1.7.1 @@ -163,7 +164,7 @@ io.github.resilience4j resilience4j-spring - 1.7.1 + ${resilience4j-spring.version} provided diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitAutoConfiguration.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitAutoConfiguration.java index a03f300..f8e9389 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitAutoConfiguration.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitAutoConfiguration.java @@ -5,6 +5,7 @@ import java.util.concurrent.TimeUnit; import com.github.lianjiatech.retrofit.spring.boot.degrade.DegradeRuleRegister; +import com.github.lianjiatech.retrofit.spring.boot.degrade.SentinelDegradeRuleRegister; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; @@ -13,6 +14,7 @@ import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -128,6 +130,13 @@ public RetrofitConfigBean retrofitConfigBean(ObjectProvider return retrofitConfigBean; } + @Bean + @ConditionalOnMissingBean + @ConditionalOnClass(com.alibaba.csp.sentinel.SphU.class) + public DegradeRuleRegister sentinelDegradeRuleRegister(){ + return new SentinelDegradeRuleRegister(retrofitProperties.getDegrade()); + } + @Bean @ConditionalOnMissingBean diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/RetrofitStarterTest.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/RetrofitStarterTest.java index 8af32c7..5411f6c 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/RetrofitStarterTest.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/RetrofitStarterTest.java @@ -8,7 +8,9 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import com.github.lianjiatech.retrofit.spring.boot.test.http.HttpApi4; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -56,6 +58,9 @@ public class RetrofitStarterTest { @Autowired private HttpApi3 httpApi3; + @Autowired + private HttpApi4 httpApi4; + private static final ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) .setSerializationInclusion(JsonInclude.Include.NON_NULL); @@ -417,4 +422,25 @@ public void testBoolean() { System.out.println(apiBoolean); } + @Test + public void testDegrade() { + for (int i = 0; i < 10; i++) { + try { + MockResponse response = new MockResponse() + .setResponseCode(400) + .addHeader("Content-Type", "application/text; charset=utf-8") + .addHeader("Cache-Control", "no-cache") + .setBody("false") + .setHeadersDelay(5, TimeUnit.SECONDS); + server.enqueue(response); + System.out.println(httpApi4.getPerson(2L).getCode()); + }catch (Exception e){ + System.out.println(e.getMessage()); + }finally { + System.out.println("当前请求轮次: "+ (i+1)); + } + } + + } + } diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/HttpApi4.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/HttpApi4.java new file mode 100644 index 0000000..3c98b72 --- /dev/null +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/HttpApi4.java @@ -0,0 +1,52 @@ +package com.github.lianjiatech.retrofit.spring.boot.test.http; + +import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient; +import com.github.lianjiatech.retrofit.spring.boot.degrade.Degrade; +import com.github.lianjiatech.retrofit.spring.boot.degrade.FallbackFactory; +import com.github.lianjiatech.retrofit.spring.boot.test.entity.Person; +import com.github.lianjiatech.retrofit.spring.boot.test.entity.Result; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; +import retrofit2.http.GET; +import retrofit2.http.Query; + +/** + * @author 陈添明 + */ +@RetrofitClient(baseUrl = "${test.baseUrl}", fallbackFactory = HttpApi4.HttpDegradeFallbackFactory.class) +@Degrade(count = 0.5) +public interface HttpApi4 { + + /** + * . + * + * @param id . + * @return . + */ + @GET("person") + Result getPerson(@Query("id") Long id); + + @Service + public class HttpDegradeFallbackFactory implements FallbackFactory { + Logger log = LoggerFactory.getLogger(HttpDegradeFallbackFactory.class); + /** + * Returns an instance of the fallback appropriate for the given cause + * + * @param cause fallback cause + * @return 实现了retrofit接口的实例。an instance that implements the retrofit interface. + */ + @Override + public HttpApi4 create(Throwable cause) { + log.error("触发熔断了! ", cause.getMessage(), cause); + return id -> { + Result fallback = new Result<>(); + fallback.setCode(100) + .setMsg("fallback") + .setData(new Person()); + return fallback; + }; + } + } + +}