Skip to content

Commit

Permalink
重构自定义OkHttpClient属性
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed May 24, 2022
1 parent 4ed7462 commit e7a1a5e
Show file tree
Hide file tree
Showing 15 changed files with 210 additions and 365 deletions.
123 changes: 21 additions & 102 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]

## 功能特性

- [x] [超时时间设置](#超时时间设置)
- [x] [自定义OkHttpClient属性](#自定义OkHttpClient属性)
- [x] [注解式拦截器](#注解式拦截器)
- [x] [日志打印](#日志打印)
- [x] [请求重试](#请求重试)
- [x] [熔断降级](#熔断降级)
- [x] [错误解码器](#错误解码器)
- [x] [自定义注入OkHttpClient](#自定义注入OkHttpClient)
- [x] [微服务之间的HTTP调用](#微服务之间的HTTP调用)
- [x] [连接池管理](#连接池管理)
- [x] [全局拦截器](#全局拦截器)
- [x] [调用适配器](#调用适配器)
- [x] [数据转换器](#数据转码器)
Expand All @@ -52,7 +50,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.3.2</version>
<version>2.3.3</version>
</dependency>
```

Expand All @@ -65,7 +63,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.3.2</version>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
Expand Down Expand Up @@ -151,15 +149,6 @@ public class TestService {

```yaml
retrofit:
# 连接池配置
pool:
# default连接池
default:
# 最大空闲连接数
max-idle-connections: 5
# 连接保活时间(秒)
keep-alive-second: 300

# 全局转换器工厂
global-converter-factories:
- com.github.lianjiatech.retrofit.spring.boot.core.BasicTypeConverterFactory
Expand All @@ -178,7 +167,7 @@ retrofit:
# 全局日志打印策略
log-strategy: basic

# 重试配置
# 全局重试配置
global-retry:
# 是否启用全局重试
enable: false
Expand Down Expand Up @@ -227,53 +216,39 @@ retrofit:
# 指定断路器应保持半开多长时间的等待持续时间,可选配置,大于1才是有效配置。
max-wait-duration-in-half-open-state-seconds: 0
# 忽略的异常类列表,只有配置值之后才会加载。
ignore-exceptions: []
ignore-exceptions: [ ]
# 记录的异常类列表,只有配置值之后才会加载。
record-exceptions: []
record-exceptions: [ ]
# 慢调用比例阈值
slow-call-rate-threshold: 100
# 慢调用阈值秒数,超过该秒数视为慢调用
slow-call-duration-threshold-seconds: 60
# 启用可写堆栈跟踪的标志
writable-stack-trace-enabled: true

# 全局连接超时时间
global-connect-timeout-ms: 10000
# 全局读取超时时间
global-read-timeout-ms: 10000
# 全局写入超时时间
global-write-timeout-ms: 10000
# 全局完整调用超时时间
global-call-timeout-ms: 0

```
## 高级功能
### 超时时间设置

`retrofit-spring-boot-starter`支持两种方式设置超时时间,一种是全局超时时间设置,另一种是注解超时时间设置。
### 自定义OkHttpClient属性
#### 全局超时时间设置
根据`@RetrofitClient`的`baseOkHttpClientBeanName`,根据该名称从Spring容器中对应的`OkHttpClient`来初始化当前接口的`OkHttpClient`。
通过这种方式,用户可以配置超时时间、代理、连接池、分发等等`OkHttpClient`属性。

**在yaml文件中可配置全局超时时间,对所有接口生效**
默认是`defaultBaseOkHttpClient`,可以按下面方式覆盖Spring配置:

```yaml
retrofit:
# 全局连接超时时间
global-connect-timeout-ms: 5000
# 全局读取超时时间
global-read-timeout-ms: 5000
# 全局写入超时时间
global-write-timeout-ms: 5000
# 全局完整调用超时时间
global-call-timeout-ms: 0
```java
@Bean
@Primary
OkHttpClient defaultBaseOkHttpClient(){
return new OkHttpClient.Builder()
.addInterceptor(chain->{
log.info("=======替换defaultBaseOkHttpClient=====");
return chain.proceed(chain.request());
})
.build();
}
```

#### 注解式超时时间设置
`@RetrofitClient`注解上可以设置超时时间,针对当前接口生效,优先级更高。具体字段有`connectTimeoutMs`、`readTimeoutMs`、`writeTimeoutMs`、`callTimeoutMs`等。

### 注解式拦截器

很多时候,我们希望某个接口下的某些http请求执行统一的拦截处理逻辑。为了支持这个功能,`retrofit-spring-boot-starter`提供了**注解式拦截器**,做到了**基于url路径的匹配拦截**。使用的步骤主要分为2步:
Expand Down Expand Up @@ -696,32 +671,6 @@ public interface ErrorDecoder {
```

### 自定义注入OkHttpClient

通常情况下,通过`@RetrofitClient`注解属性动态创建`OkHttpClient`对象能够满足大部分使用场景。但是在某些情况下,用户可能需要自定义`OkHttpClient`
,这个时候,可以在接口上定义返回类型是`OkHttpClient.Builder`的静态方法来实现。代码示例如下:

```java
@RetrofitClient(baseUrl = "http://ke.com")
public interface HttpApi3 {
@OkHttpClientBuilder
static OkHttpClient.Builder okhttpClientBuilder() {
return new OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.SECONDS)
.readTimeout(1, TimeUnit.SECONDS)
.writeTimeout(1, TimeUnit.SECONDS);
}
@GET
Result<Person> getPerson(@Url String url, @Query("id") Long id);
}
```

> 方法必须使用`@OkHttpClientBuilder`注解标记!

### 微服务之间的HTTP调用

为了能够使用微服务调用,需要进行如下配置:
Expand Down Expand Up @@ -749,36 +698,6 @@ public interface ApiCountService {
}
```

### 连接池管理

默认情况下,所有通过`Retrofit`发送的http请求都会使用`max-idle-connections=5 keep-alive-second=300`
的默认连接池。当然,我们也可以在配置文件中配置多个自定义的连接池,然后通过`@RetrofitClient`的`poolName`属性来指定使用。比如我们要让某个接口下的请求全部使用`poolName=test1`的连接池,代码实现如下:

1. 配置连接池。

```yaml
retrofit:
# 连接池配置
pool:
# test1连接池配置
test1:
# 最大空闲连接数
max-idle-connections: 3
# 连接保活时间(秒)
keep-alive-second: 100
```

2. 通过`@RetrofitClient`的`poolName`属性来指定使用的连接池。

```java
@RetrofitClient(baseUrl = "${test.baseUrl}", poolName="test1")
public interface HttpApi {
@GET("person")
Result<Person> getPerson(@Query("id") Long id);
}
```

## 全局拦截器

### 全局应用拦截器
Expand Down
2 changes: 1 addition & 1 deletion 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>2.3.2</version>
<version>2.3.3</version>

<name>retrofit-spring-boot-starter</name>
<description>retrofit-spring-boot-starter</description>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.github.lianjiatech.retrofit.spring.boot.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
Expand All @@ -13,13 +10,15 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.lianjiatech.retrofit.spring.boot.core.AutoConfiguredRetrofitScannerRegistrar;
import com.github.lianjiatech.retrofit.spring.boot.core.BasicTypeConverterFactory;
import com.github.lianjiatech.retrofit.spring.boot.core.BodyCallAdapterFactory;
import com.github.lianjiatech.retrofit.spring.boot.core.Constants;
import com.github.lianjiatech.retrofit.spring.boot.core.ErrorDecoder;
import com.github.lianjiatech.retrofit.spring.boot.core.PathMatchInterceptorBdfProcessor;
import com.github.lianjiatech.retrofit.spring.boot.core.ResponseCallAdapterFactory;
Expand All @@ -36,7 +35,7 @@
import com.github.lianjiatech.retrofit.spring.boot.retry.RetryInterceptor;

import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import retrofit2.converter.jackson.JacksonConverterFactory;

/**
Expand Down Expand Up @@ -77,16 +76,6 @@ public RetrofitConfigBean retrofitConfigBean(@Autowired(required = false) Retrof
retrofitConfigBean.setRetryInterceptor(retryInterceptor);
retrofitConfigBean.setLoggingInterceptor(loggingInterceptor);
retrofitConfigBean.setErrorDecoderInterceptor(errorDecoderInterceptor);

Map<String, ConnectionPool> poolRegistry = new HashMap<>(4);
retrofitProperties.getPool().forEach((poolName, poolConfig) -> {
long keepAliveSecond = poolConfig.getKeepAliveSecond();
int maxIdleConnections = poolConfig.getMaxIdleConnections();
ConnectionPool connectionPool =
new ConnectionPool(maxIdleConnections, keepAliveSecond, TimeUnit.SECONDS);
poolRegistry.put(poolName, connectionPool);
});
retrofitConfigBean.setPoolRegistry(poolRegistry);
retrofitConfigBean.setGlobalCallAdapterFactoryClasses(retrofitProperties.getGlobalCallAdapterFactories());
retrofitConfigBean.setGlobalConverterFactoryClasses(retrofitProperties.getGlobalConverterFactories());
return retrofitConfigBean;
Expand Down Expand Up @@ -126,7 +115,7 @@ public RetryInterceptor retryInterceptor() {

@Bean
@ConditionalOnMissingBean
public LoggingInterceptor logInterceptor() {
public LoggingInterceptor loggingInterceptor() {
return new LoggingInterceptor(retrofitProperties.getGlobalLog());
}

Expand All @@ -142,18 +131,26 @@ ServiceChooseInterceptor serviceChooseInterceptor(@Autowired ServiceInstanceChoo
return new ServiceChooseInterceptor(serviceInstanceChooser);
}

@Bean
@Primary
@ConditionalOnMissingBean(name = Constants.DEFAULT_BASE_OK_HTTP_CLIENT)
OkHttpClient defaultBaseOkHttpClient() {
return new OkHttpClient.Builder()
.build();
}

@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(name = "com.alibaba.csp.sentinel.SphU")
@ConditionalOnProperty(name = "retrofit.degrade.degrade-type", havingValue = RetrofitDegrade.SENTINEL)
@ConditionalOnClass(name = Constants.SPH_U_CLASS_NAME)
@ConditionalOnProperty(name = Constants.DEGRADE_TYPE, havingValue = RetrofitDegrade.SENTINEL)
public RetrofitDegrade sentinelRetrofitDegrade() {
return new SentinelRetrofitDegrade(retrofitProperties.getDegrade().getGlobalSentinelDegrade());
}

@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(name = "io.github.resilience4j.circuitbreaker.CircuitBreaker")
@ConditionalOnProperty(name = "retrofit.degrade.degrade-type", havingValue = RetrofitDegrade.RESILIENCE4J)
@ConditionalOnClass(name = Constants.CIRCUIT_BREAKER_CLASS_NAME)
@ConditionalOnProperty(name = Constants.DEGRADE_TYPE, havingValue = RetrofitDegrade.RESILIENCE4J)
public RetrofitDegrade resilience4jRetrofitDegrade() {
return new Resilience4jRetrofitDegrade(CircuitBreakerRegistry.ofDefaults(),
retrofitProperties.getDegrade().getGlobalResilience4jDegrade());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.github.lianjiatech.retrofit.spring.boot.degrade.RetrofitDegrade;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.ErrorDecoderInterceptor;
Expand All @@ -13,7 +12,6 @@
import com.github.lianjiatech.retrofit.spring.boot.retry.RetryInterceptor;

import lombok.Data;
import okhttp3.ConnectionPool;
import retrofit2.CallAdapter;
import retrofit2.Converter;

Expand All @@ -25,8 +23,6 @@ public class RetrofitConfigBean {

private final RetrofitProperties retrofitProperties;

private Map<String, ConnectionPool> poolRegistry;

private List<GlobalInterceptor> globalInterceptors;

private List<NetworkInterceptor> networkInterceptors;
Expand Down
Loading

0 comments on commit e7a1a5e

Please sign in to comment.