Skip to content

Commit

Permalink
全局超时时间配置
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed May 24, 2021
1 parent f713d2a commit b1e2060
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,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.2.10</version>
<version>2.2.11</version>
</dependency>
```

Expand All @@ -61,7 +61,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.10</version>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
Expand Down
4 changes: 2 additions & 2 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.10</version>
<version>2.2.11</version>
</dependency>
```

Expand All @@ -53,7 +53,7 @@ This project depends on Retrofit-2.9.0, okhttp-3.14.9, and okio-1.17.5 versions.
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.10</version>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
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.2.10</version>
<version>2.2.11</version>

<name>retrofit-spring-boot-starter</name>
<description>retrofit-spring-boot-starter</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,26 +132,29 @@
/**
* Sets the default connect timeout for new connections. A value of 0 means no timeout,
* otherwise values must be between 1 and Integer.MAX_VALUE when converted to milliseconds.
* If it is configured as -1, the global default configuration is used.
*
* @return connectTimeoutMs
*/
int connectTimeoutMs() default 10_000;
int connectTimeoutMs() default -1;

/**
* Sets the default read timeout for new connections. A value of 0 means no timeout,
* otherwise values must be between 1 and Integer.MAX_VALUE when converted to milliseconds.
* If it is configured as -1, the global default configuration is used.
*
* @return readTimeoutMs
*/
int readTimeoutMs() default 10_000;
int readTimeoutMs() default -1;

/**
* Sets the default write timeout for new connections. A value of 0 means no timeout,
* otherwise values must be between 1 and Integer.MAX_VALUE when converted to milliseconds
* otherwise values must be between 1 and Integer.MAX_VALUE when converted to milliseconds.
* If it is configured as -1, the global default configuration is used.
*
* @return writeTimeoutMs
*/
int writeTimeoutMs() default 10_000;
int writeTimeoutMs() default -1;


/**
Expand All @@ -160,7 +163,7 @@
*
* @return callTimeout
*/
int callTimeoutMs() default 0;
int callTimeoutMs() default -1;

/**
* Sets the interval between HTTP/2 and web socket pings initiated by this client.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ public class RetrofitProperties {
*/
private boolean disableVoidReturnType = false;

/**
* 全局连接超时时间
*/
private int globalConnectTimeoutMs = 10_000;

/**
* 全局读取超时时间
*/
private int globalReadTimeoutMs = 10_000;

/**
* 全局写入超时时间
*/
private int globalWriteTimeoutMs = 10_000;

/**
* 全局完整调用超时时间
*/
private int globalCallTimeoutMs = 0;


/**
* 全局转换器工厂,转换器实例优先从Spring容器获取,如果没有获取到,则反射创建。
* global converter factories, The converter instance is first obtained from the Spring container. If it is not obtained, it is created by reflection.
Expand Down Expand Up @@ -130,4 +151,36 @@ public LogProperty getLog() {
public void setLog(LogProperty log) {
this.log = log;
}

public int getGlobalConnectTimeoutMs() {
return globalConnectTimeoutMs;
}

public void setGlobalConnectTimeoutMs(int globalConnectTimeoutMs) {
this.globalConnectTimeoutMs = globalConnectTimeoutMs;
}

public int getGlobalReadTimeoutMs() {
return globalReadTimeoutMs;
}

public void setGlobalReadTimeoutMs(int globalReadTimeoutMs) {
this.globalReadTimeoutMs = globalReadTimeoutMs;
}

public int getGlobalWriteTimeoutMs() {
return globalWriteTimeoutMs;
}

public void setGlobalWriteTimeoutMs(int globalWriteTimeoutMs) {
this.globalWriteTimeoutMs = globalWriteTimeoutMs;
}

public int getGlobalCallTimeoutMs() {
return globalCallTimeoutMs;
}

public void setGlobalCallTimeoutMs(int globalCallTimeoutMs) {
this.globalCallTimeoutMs = globalCallTimeoutMs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,19 @@ private synchronized OkHttpClient getOkHttpClient(Class<?> retrofitClientInterfa
okHttpClientBuilder = (OkHttpClient.Builder) method.invoke(null);
} else {
okhttp3.ConnectionPool connectionPool = getConnectionPool(retrofitClientInterfaceClass);

final int connectTimeoutMs = retrofitClient.connectTimeoutMs() == -1 ? retrofitProperties.getGlobalConnectTimeoutMs() : retrofitClient.connectTimeoutMs();
final int readTimeoutMs = retrofitClient.readTimeoutMs() == -1 ? retrofitProperties.getGlobalReadTimeoutMs() : retrofitClient.readTimeoutMs();
final int writeTimeoutMs = retrofitClient.writeTimeoutMs() == -1 ? retrofitProperties.getGlobalWriteTimeoutMs() : retrofitClient.writeTimeoutMs();
final int callTimeoutMs = retrofitClient.callTimeoutMs() == -1 ? retrofitProperties.getGlobalCallTimeoutMs() : retrofitClient.callTimeoutMs();


// Construct an OkHttpClient object
okHttpClientBuilder = new OkHttpClient.Builder()
.connectTimeout(retrofitClient.connectTimeoutMs(), TimeUnit.MILLISECONDS)
.readTimeout(retrofitClient.readTimeoutMs(), TimeUnit.MILLISECONDS)
.writeTimeout(retrofitClient.writeTimeoutMs(), TimeUnit.MILLISECONDS)
.callTimeout(retrofitClient.callTimeoutMs(), TimeUnit.MILLISECONDS)
.connectTimeout(connectTimeoutMs, TimeUnit.MILLISECONDS)
.readTimeout(readTimeoutMs, TimeUnit.MILLISECONDS)
.writeTimeout(writeTimeoutMs, TimeUnit.MILLISECONDS)
.callTimeout(callTimeoutMs, TimeUnit.MILLISECONDS)
.retryOnConnectionFailure(retrofitClient.retryOnConnectionFailure())
.followRedirects(retrofitClient.followRedirects())
.followSslRedirects(retrofitClient.followSslRedirects())
Expand Down
9 changes: 9 additions & 0 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ retrofit:
# 是否禁用void返回值类型
disable-void-return-type: false


# 全局转换器工厂
global-converter-factories:
- com.github.lianjiatech.retrofit.spring.boot.core.BasicTypeConverterFactory
Expand Down Expand Up @@ -56,6 +57,14 @@ retrofit:
degrade-type: sentinel
# 熔断资源名称解析器
resource-name-parser: com.github.lianjiatech.retrofit.spring.boot.degrade.DefaultResourceNameParser
# 全局连接超时时间
global-connect-timeout-ms: 5000
# 全局读取超时时间
global-read-timeout-ms: 5000
# 全局写入超时时间
global-write-timeout-ms: 5000
# 全局完整调用超时时间
global-call-timeout-ms: 0

test:
baseUrl: http://localhost:8080/api/test/
Expand Down

0 comments on commit b1e2060

Please sign in to comment.