Method Example of okhttp3 Timeout Setting in SpringBoot Java Backend

  • 2021-11-29 06:54:17
  • OfStack

Preface to the table of contents Import Brief introduction of okhttp3 method Usage of timeout settings in two versions Summarize

Preface

okhttp is an open source project for handling network requests, which is the hottest lightweight framework on Android and developed by mobile payment Square. OkHttp is a default efficient HTTP client. OkHttp3 can be used by Java and Android, and Android also has a famous network library called Volley, which can only be used by Android.

The official github address of okttp3 is: Official Address

HTTP/2 support allows all requests to the same host to share 1 socket. Connection pooling reduces request latency (if HTTP/2 is not available). Transparent GZIP can reduce the download size. Response caching completely avoids repeated network requests.

OkHttp insists when the network goes wrong: It will silently recover from common connection problems. If your service has more than one IP address, OkHttp will try an alternate address if the first connection fails. This is necessary for IPv4+IPv6 and services hosted in redundant data centers. OkHttp supports modern TLS features (TLS 1.3, ALPN, certificate locking). It can be configured to roll back for extensive connections.

It is easy to use OkHttp. Its request/response API design has a smooth builder and invariance. It supports synchronous blocking calls and asynchronous calls with callbacks.

Import

maven


<!-- ok http -->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.2.2</version>
        </dependency>

gradle mode


compile 'com.squareup.okhttp3:okhttp:4.2.2'

Brief introduction of okhttp3 method

1 A total of 5 timeouts can be set as follows: About the timeout of OkHttp


OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)// Connection timeout ( Unit : Seconds )
.callTimeout(120, TimeUnit.SECONDS)// Time-out time spent in the whole process ( Unit : Seconds )-- Very few people use it 
.pingInterval(5, TimeUnit.SECONDS)//websocket Interval of rotation training ( Unit : Seconds )
.readTimeout(60, TimeUnit.SECONDS)// Read timeout ( Unit : Seconds )
.writeTimeout(60, TimeUnit.SECONDS)// Write timeout ( Unit : Seconds )
.build();
api 描述 生效机制
callTimeout() 整个流程耗费的超时时间 AsyncTimeout + WatchDog实现
connectTimeout() 3次握手 + SSL建立耗时 socket.connect(address, connectTimeout)
readTimeout() source读取耗时\rawSocket读取耗时 source.timeout(readTimeout)AsyncTimeout + WatchDog实现\rawSocket.setSoTimeout(readTimeout)
writeTimeout() sink写入耗时 sink.timeout(writeTimeout)AsyncTimeout + WatchDog实现
pingInterval() websocket轮训间隔(单位:秒) 这个值只有http2和webSocket中有使用

Usage of timeout settings in two versions

okhttp3 to set client. Builder


OkHttpClient client = new OkHttpClient.Builder()
         .connectTimeout(15, TimeUnit.SECONDS)
         .readTimeout(15,TimeUnit.SECONDS)
         .writeTimeout(15,TimeUnit.SECONDS)
         .build();

Previous versions set client


client .connectTimeout(15, TimeUnit.SECONDS)
          .readTimeout(15,TimeUnit.SECONDS)
          .writeTimeout(15,TimeUnit.SECONDS)

Summarize


Related articles: