From 4ed7462d1669a69d098f02581ff62f610113eac2 Mon Sep 17 00:00:00 2001 From: chentianming Date: Sun, 8 May 2022 16:13:16 +0800 Subject: [PATCH] README.md --- README.md | 1 + .../boot/test/degrade/DegradeR4jApi.java | 2 + .../boot/test/degrade/DegradeSentinelApi.java | 56 ++++++++----------- .../test/degrade/DegradeSentinelTest.java | 2 + .../test/interceptor/SignInterceptor.java | 18 +++--- 5 files changed, 34 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 807d82d..caf3724 100644 --- a/README.md +++ b/README.md @@ -311,6 +311,7 @@ public class TimeStampInterceptor extends BasePathMatchInterceptor { ```java @RetrofitClient(baseUrl = "${test.baseUrl}") @Intercept(handler = TimeStampInterceptor.class, include = {"/api/**"}, exclude = "/api/test/savePerson") +@Intercept(handler = TimeStamp2Interceptor.class) // 需要多个,直接添加即可 public interface HttpApi { @GET("person") diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeR4jApi.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeR4jApi.java index 3c4f426..c7eab1a 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeR4jApi.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeR4jApi.java @@ -7,6 +7,7 @@ import com.github.lianjiatech.retrofit.spring.boot.core.RetrofitClient; import com.github.lianjiatech.retrofit.spring.boot.degrade.FallbackFactory; import com.github.lianjiatech.retrofit.spring.boot.degrade.resilience4j.Resilience4jDegrade; +import com.github.lianjiatech.retrofit.spring.boot.retry.Retry; import com.github.lianjiatech.retrofit.spring.boot.test.entity.Person; import com.github.lianjiatech.retrofit.spring.boot.test.entity.Result; @@ -17,6 +18,7 @@ /** * @author 陈添明 */ +@Retry(enable = false) @RetrofitClient(baseUrl = "${test.baseUrl}", fallbackFactory = DegradeR4jApi.HttpDegradeFallbackFactory.class) @Resilience4jDegrade(slidingWindowType = CircuitBreakerConfig.SlidingWindowType.TIME_BASED, failureRateThreshold = 30, minimumNumberOfCalls = 10, permittedNumberOfCallsInHalfOpenState = 5) diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelApi.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelApi.java index 17089e5..265e18a 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelApi.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelApi.java @@ -1,22 +1,22 @@ package com.github.lianjiatech.retrofit.spring.boot.test.degrade; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import com.github.lianjiatech.retrofit.spring.boot.core.RetrofitClient; -import com.github.lianjiatech.retrofit.spring.boot.degrade.FallbackFactory; import com.github.lianjiatech.retrofit.spring.boot.degrade.sentinel.SentinelDegrade; +import com.github.lianjiatech.retrofit.spring.boot.retry.Retry; import com.github.lianjiatech.retrofit.spring.boot.test.entity.Person; import com.github.lianjiatech.retrofit.spring.boot.test.entity.Result; +import lombok.extern.slf4j.Slf4j; import retrofit2.http.GET; import retrofit2.http.Query; /** * @author 陈添明 */ -@RetrofitClient(baseUrl = "${test.baseUrl}", fallbackFactory = DegradeSentinelApi.HttpDegradeFallbackFactory.class) +@RetrofitClient(baseUrl = "${test.baseUrl}", fallback = DegradeSentinelApi.DegradeSentinelApiFallback.class) +@Retry(enable = false) public interface DegradeSentinelApi { /** @@ -31,38 +31,26 @@ public interface DegradeSentinelApi { Result getPerson2(@Query("id") Long id); @Service - 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. - */ + @Slf4j + class DegradeSentinelApiFallback implements DegradeSentinelApi { @Override - public DegradeSentinelApi create(Throwable cause) { - log.error("触发熔断了! ", cause.getMessage(), cause); - return new DegradeSentinelApi() { - @Override - public Result getPerson1(Long id) { - Result fallback = new Result<>(); - fallback.setCode(-1) - .setMsg("熔断Person1") - .setData(new Person()); - return fallback; - } + public Result getPerson1(Long id) { + log.info("触发熔断了"); + Result fallback = new Result<>(); + fallback.setCode(-1) + .setMsg("熔断Person1") + .setData(new Person()); + return fallback; + } - @Override - public Result getPerson2(Long id) { - Result fallback = new Result<>(); - fallback.setCode(-1) - .setMsg("熔断Person2") - .setData(new Person()); - return fallback; - } - }; + @Override + public Result getPerson2(Long id) { + log.info("触发熔断了"); + Result fallback = new Result<>(); + fallback.setCode(-1) + .setMsg("熔断Person2") + .setData(new Person()); + return fallback; } } - } \ No newline at end of file diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelTest.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelTest.java index de5cb02..66cf5da 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelTest.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelTest.java @@ -21,6 +21,7 @@ import com.github.lianjiatech.retrofit.spring.boot.test.entity.Person; import com.github.lianjiatech.retrofit.spring.boot.test.entity.Result; +import lombok.extern.slf4j.Slf4j; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; @@ -30,6 +31,7 @@ @ActiveProfiles("sentinel") @SpringBootTest(classes = RetrofitTestApplication.class) @RunWith(SpringRunner.class) +@Slf4j public class DegradeSentinelTest { @Autowired diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/SignInterceptor.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/SignInterceptor.java index b11c4e2..a4d28b4 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/SignInterceptor.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/interceptor/SignInterceptor.java @@ -1,11 +1,14 @@ package com.github.lianjiatech.retrofit.spring.boot.test.interceptor; +import java.io.IOException; + +import org.springframework.stereotype.Component; + import com.github.lianjiatech.retrofit.spring.boot.interceptor.BasePathMatchInterceptor; + +import lombok.Setter; import okhttp3.Request; import okhttp3.Response; -import org.springframework.stereotype.Component; - -import java.io.IOException; /** * 加签拦截器,取加签值,放到header中 @@ -13,20 +16,13 @@ * @author 陈添明 */ @Component +@Setter public class SignInterceptor extends BasePathMatchInterceptor { private String accessKeyId; private String accessKeySecret; - public void setAccessKeyId(String accessKeyId) { - this.accessKeyId = accessKeyId; - } - - public void setAccessKeySecret(String accessKeySecret) { - this.accessKeySecret = accessKeySecret; - } - @Override public Response doIntercept(Chain chain) throws IOException { Request request = chain.request();