Skip to content

Commit

Permalink
update retrofit 2.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed Jun 1, 2024
1 parent a5e24ba commit 6b5562c
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 129 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>3.0.3</version>
<version>3.1.0</version>

<name>retrofit-spring-boot-starter</name>
<description>retrofit-spring-boot-starter</description>
Expand Down Expand Up @@ -37,9 +37,9 @@
</developers>

<properties>
<retrofit.version>2.9.0</retrofit.version>
<retrofit.version>2.11.0</retrofit.version>
<javax.annotation-api.version>1.3.2</javax.annotation-api.version>
<spring-boot.version>3.1.6</spring-boot.version>
<spring-boot.version>3.2.6</spring-boot.version>
<okhttp.version>3.14.9</okhttp.version>
<sentinel.version>1.6.3</sentinel.version>
<lombok.version>1.18.24</lombok.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.github.lianjiatech.retrofit.spring.boot.degrade;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.github.lianjiatech.retrofit.spring.boot.core.RetrofitClient;
import com.github.lianjiatech.retrofit.spring.boot.util.RetrofitUtils;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.env.Environment;

import com.github.lianjiatech.retrofit.spring.boot.core.RetrofitClient;
import com.github.lianjiatech.retrofit.spring.boot.util.RetrofitUtils;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* @author 陈添明
Expand All @@ -25,13 +24,13 @@ public abstract class BaseRetrofitDegrade implements RetrofitDegrade, ResourceNa
protected Environment environment;

@Override
public String parseResourceName(Method method) {
public String parseResourceName(Method method, Class<?> service) {
String resourceName = RESOURCE_NAME_CACHE.get(method);
if (resourceName != null) {
return resourceName;
}
RetrofitClient retrofitClient =
AnnotatedElementUtils.findMergedAnnotation(method.getDeclaringClass(), RetrofitClient.class);
AnnotatedElementUtils.findMergedAnnotation(service, RetrofitClient.class);
String baseUrl = RetrofitUtils.convertBaseUrl(retrofitClient, retrofitClient.baseUrl(), environment);
HttpMethodPath httpMethodPath = parseHttpMethodPath(method);
resourceName = formatResourceName(baseUrl, httpMethodPath);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package com.github.lianjiatech.retrofit.spring.boot.degrade;

import java.lang.reflect.Method;
import retrofit2.http.*;

import retrofit2.http.DELETE;
import retrofit2.http.GET;
import retrofit2.http.HEAD;
import retrofit2.http.HTTP;
import retrofit2.http.PATCH;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import java.lang.reflect.Method;

/**
* @author 陈添明
Expand All @@ -18,11 +12,12 @@ public interface ResourceNameParser {

/**
* 解析资源名称
* @param method 方法
*
* @param method 方法
* @param service 接口类
* @return 资源名称
*/
String parseResourceName(Method method);
String parseResourceName(Method method, Class<?> service);

/**
* 解析方法路径
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
package com.github.lianjiatech.retrofit.spring.boot.degrade.resilience4j;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import com.github.lianjiatech.retrofit.spring.boot.util.RetrofitUtils;
import org.springframework.core.annotation.AnnotatedElementUtils;

import com.github.lianjiatech.retrofit.spring.boot.degrade.BaseRetrofitDegrade;
import com.github.lianjiatech.retrofit.spring.boot.degrade.RetrofitBlockException;
import com.github.lianjiatech.retrofit.spring.boot.util.AnnotationExtendUtils;

import io.github.resilience4j.circuitbreaker.CallNotPermittedException;
import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
import io.github.resilience4j.core.StopWatch;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.core.annotation.AnnotatedElementUtils;
import retrofit2.Invocation;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
* @author 陈添明
* @since 2022/5/1 8:02 下午
Expand Down Expand Up @@ -59,15 +56,14 @@ public void loadDegradeRules(Class<?> retrofitInterface) {
continue;
}
Resilience4jDegrade resilience4jDegrade =
AnnotationExtendUtils.findMergedAnnotation(method, method.getDeclaringClass(),
Resilience4jDegrade.class);
AnnotationExtendUtils.findMergedAnnotation(method, retrofitInterface, Resilience4jDegrade.class);
if (!needDegrade(resilience4jDegrade)) {
continue;
}
String circuitBreakerConfigName =
resilience4jDegrade == null ? globalResilience4jDegradeProperty.getCircuitBreakerConfigName()
: resilience4jDegrade.circuitBreakerConfigName();
circuitBreakerRegistry.circuitBreaker(parseResourceName(method),
circuitBreakerRegistry.circuitBreaker(parseResourceName(method, retrofitInterface),
circuitBreakerConfigRegistry.get(circuitBreakerConfigName));
}
}
Expand All @@ -86,12 +82,12 @@ protected boolean needDegrade(Resilience4jDegrade resilience4jDegrade) {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Method method = RetrofitUtils.getMethodFormRequest(request);
if (method == null) {
Invocation invocation = request.tag(Invocation.class);
if (invocation == null) {
return chain.proceed(request);
}
CircuitBreaker circuitBreaker =
circuitBreakerRegistry.find(parseResourceName(method)).orElse(null);
circuitBreakerRegistry.find(parseResourceName(invocation.method(), invocation.service())).orElse(null);
if (Objects.isNull(circuitBreaker)) {
// 断路器为空则直接调用返回
return chain.proceed(request);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
package com.github.lianjiatech.retrofit.spring.boot.degrade.sentinel;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Objects;

import com.github.lianjiatech.retrofit.spring.boot.util.RetrofitUtils;
import org.springframework.core.annotation.AnnotatedElementUtils;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.ResourceTypeConstants;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.*;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.github.lianjiatech.retrofit.spring.boot.degrade.BaseRetrofitDegrade;
import com.github.lianjiatech.retrofit.spring.boot.degrade.RetrofitBlockException;
import com.github.lianjiatech.retrofit.spring.boot.util.AnnotationExtendUtils;

import okhttp3.Request;
import okhttp3.Response;
import org.springframework.core.annotation.AnnotatedElementUtils;
import retrofit2.Invocation;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Collections;

/**
* @author 陈添明
*/
Expand Down Expand Up @@ -58,7 +50,7 @@ public void loadDegradeRules(Class<?> retrofitInterface) {
}
// 获取熔断配置
SentinelDegrade sentinelDegrade =
AnnotationExtendUtils.findMergedAnnotation(method, method.getDeclaringClass(),
AnnotationExtendUtils.findMergedAnnotation(method, retrofitInterface,
SentinelDegrade.class);

if (!needDegrade(sentinelDegrade)) {
Expand All @@ -71,7 +63,7 @@ public void loadDegradeRules(Class<?> retrofitInterface) {
: sentinelDegrade.timeWindow())
.setGrade(sentinelDegrade == null ? globalSentinelDegradeProperty.getGrade()
: sentinelDegrade.grade());
String resourceName = parseResourceName(method);
String resourceName = parseResourceName(method, retrofitInterface);
degradeRule.setResource(resourceName);
DegradeRuleManager.setRulesForResource(resourceName, Collections.singleton(degradeRule));
}
Expand All @@ -91,16 +83,18 @@ protected boolean needDegrade(SentinelDegrade sentinelDegrade) {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Method method = RetrofitUtils.getMethodFormRequest(request);
if (method == null) {
Invocation invocation = request.tag(Invocation.class);
if (invocation == null) {
return chain.proceed(request);
}
SentinelDegrade sentinelDegrade = AnnotationExtendUtils.findMergedAnnotation(method, method.getDeclaringClass(),
Method method = invocation.method();
Class<?> service = invocation.service();
SentinelDegrade sentinelDegrade = AnnotationExtendUtils.findMergedAnnotation(method, service,
SentinelDegrade.class);
if (!needDegrade(sentinelDegrade)) {
return chain.proceed(request);
}
String resourceName = parseResourceName(method);
String resourceName = parseResourceName(method, service);
Entry entry = null;
try {
entry = SphU.entry(resourceName, ResourceTypeConstants.COMMON_WEB, EntryType.OUT);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
package com.github.lianjiatech.retrofit.spring.boot.interceptor;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Objects;

import com.github.lianjiatech.retrofit.spring.boot.util.RetrofitUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.AnnotatedElementUtils;

import com.github.lianjiatech.retrofit.spring.boot.core.ErrorDecoder;
import com.github.lianjiatech.retrofit.spring.boot.core.RetrofitClient;
import com.github.lianjiatech.retrofit.spring.boot.util.AppContextUtils;

import lombok.SneakyThrows;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.AnnotatedElementUtils;
import retrofit2.Invocation;

import java.io.IOException;

/**
* @author 陈添明
*/
Expand All @@ -31,12 +26,12 @@ public class ErrorDecoderInterceptor implements Interceptor, ApplicationContextA
@SneakyThrows
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Method method = RetrofitUtils.getMethodFormRequest(request);
if (method == null) {
Invocation invocation = request.tag(Invocation.class);
if (invocation == null) {
return chain.proceed(request);
}
RetrofitClient retrofitClient =
AnnotatedElementUtils.findMergedAnnotation(method.getDeclaringClass(), RetrofitClient.class);
AnnotatedElementUtils.findMergedAnnotation(invocation.service(), RetrofitClient.class);
ErrorDecoder errorDecoder =
AppContextUtils.getBeanOrNew(applicationContext, retrofitClient.errorDecoder());
boolean decoded = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package com.github.lianjiatech.retrofit.spring.boot.interceptor;

import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Objects;

import com.github.lianjiatech.retrofit.spring.boot.util.RetrofitUtils;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.util.StringUtils;

import com.github.lianjiatech.retrofit.spring.boot.core.RetrofitClient;
import com.github.lianjiatech.retrofit.spring.boot.core.ServiceInstanceChooser;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.util.StringUtils;
import retrofit2.Invocation;

import java.io.IOException;
import java.net.URI;

/**
* @author 陈添明
*/
Expand All @@ -32,13 +27,12 @@ public ServiceChooseInterceptor(ServiceInstanceChooser serviceDiscovery) {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Method method = RetrofitUtils.getMethodFormRequest(request);
if (method == null) {
Invocation invocation = request.tag(Invocation.class);
if (invocation == null) {
return chain.proceed(request);
}
Class<?> declaringClass = method.getDeclaringClass();
RetrofitClient retrofitClient =
AnnotatedElementUtils.findMergedAnnotation(declaringClass, RetrofitClient.class);
AnnotatedElementUtils.findMergedAnnotation(invocation.service(), RetrofitClient.class);
String baseUrl = retrofitClient.baseUrl();
if (StringUtils.hasText(baseUrl)) {
return chain.proceed(request);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package com.github.lianjiatech.retrofit.spring.boot.log;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Objects;

import com.github.lianjiatech.retrofit.spring.boot.util.AnnotationExtendUtils;

import com.github.lianjiatech.retrofit.spring.boot.util.RetrofitUtils;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Invocation;

import java.io.IOException;

/**
* @author 陈添明
* @since 2022/4/30 8:21 下午
Expand Down Expand Up @@ -41,17 +38,18 @@ public Response intercept(Chain chain) throws IOException {
.setLevel(HttpLoggingInterceptor.Level.valueOf(logStrategy.name()));
Response response = httpLoggingInterceptor.intercept(chain);
if (aggregate) {
((BufferingLogger)logger).flush();
((BufferingLogger) logger).flush();
}
return response;
}

protected Logging findLogging(Chain chain) {
Method method = RetrofitUtils.getMethodFormRequest(chain.request());
if (method == null) {
Request request = chain.request();
Invocation invocation = request.tag(Invocation.class);
if (invocation == null) {
return null;
}
return AnnotationExtendUtils.findMergedAnnotation(method, method.getDeclaringClass(), Logging.class);
return AnnotationExtendUtils.findMergedAnnotation(invocation.method(), invocation.service(), Logging.class);
}

protected boolean needLog(Logging logging) {
Expand Down
Loading

0 comments on commit 6b5562c

Please sign in to comment.