From b1e206073ddcffcdd218584d6ace55ff4b0be060 Mon Sep 17 00:00:00 2001 From: chentianming Date: Mon, 24 May 2021 18:41:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E5=B1=80=E8=B6=85=E6=97=B6=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +- README_EN.md | 4 +- pom.xml | 2 +- .../boot/annotation/RetrofitClient.java | 13 +++-- .../boot/config/RetrofitProperties.java | 53 +++++++++++++++++++ .../spring/boot/core/RetrofitFactoryBean.java | 15 ++++-- src/test/resources/application.yml | 9 ++++ 7 files changed, 86 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 148794a..37ab270 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter] com.github.lianjiatech retrofit-spring-boot-starter - 2.2.10 + 2.2.11 ``` @@ -61,7 +61,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter] com.github.lianjiatech retrofit-spring-boot-starter - 2.2.10 + 2.2.11 com.squareup.okhttp3 diff --git a/README_EN.md b/README_EN.md index ace9772..0505df1 100644 --- a/README_EN.md +++ b/README_EN.md @@ -43,7 +43,7 @@ com.github.lianjiatech retrofit-spring-boot-starter - 2.2.10 + 2.2.11 ``` @@ -53,7 +53,7 @@ This project depends on Retrofit-2.9.0, okhttp-3.14.9, and okio-1.17.5 versions. com.github.lianjiatech retrofit-spring-boot-starter - 2.2.10 + 2.2.11 com.squareup.okhttp3 diff --git a/pom.xml b/pom.xml index dd6c7e7..a4ada55 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.lianjiatech retrofit-spring-boot-starter - 2.2.10 + 2.2.11 retrofit-spring-boot-starter retrofit-spring-boot-starter diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/annotation/RetrofitClient.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/annotation/RetrofitClient.java index f3efbd6..429a1ec 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/annotation/RetrofitClient.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/annotation/RetrofitClient.java @@ -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; /** @@ -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. diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitProperties.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitProperties.java index aab0b0e..cdbaa6c 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitProperties.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/config/RetrofitProperties.java @@ -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. @@ -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; + } } diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/RetrofitFactoryBean.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/RetrofitFactoryBean.java index c6b0b0b..8f693da 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/RetrofitFactoryBean.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/RetrofitFactoryBean.java @@ -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()) diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index f291583..0806f11 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -12,6 +12,7 @@ retrofit: # 是否禁用void返回值类型 disable-void-return-type: false + # 全局转换器工厂 global-converter-factories: - com.github.lianjiatech.retrofit.spring.boot.core.BasicTypeConverterFactory @@ -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/