当面时问道“Okhttp连接池是咋实现“时,app优化的内容及策略

originalRequest, this, client.connectTimeoutMillis(),

client.readTimeoutMillis(), client.writeTimeoutMillis());

try {

Response response = chain.proceed(originalRequest);

return response;

} catch (IOException e) {

} finally {

}

}

}

2.ConnectInterceptor#intercept

连接池在ConnectInterceptor中包装起来了,我们进去瞄一眼:

@Override

public Response intercept(Chain chain) throws IOException {

RealInterceptorChain realChain = (RealInterceptorChain) chain;

Request request = realChain.request();

//拿到chain的Transmitter,里面包装了RealCall、okhttpClient、connectionPool等信息

Transmitter transmitter = realChain.transmitter();

//是否不是get请求

boolean doExtensiveHealthChecks = !request.method().equals(“GET”);

Exchange exchange = transmitter.newExchange(chain, doExtensiveHealthChecks);

return realChain.proceed(request, transmitter, exchange);

}

3.Transmitter#newExchange

拿到chain的Transmitter对象,该对象是RealCallokhttpClientconnectionPool等信息的包装类,将是否不是get请求的标识传给了transmitternewExchange方法:

Exchange newExchange(Interceptor.Chain chain, boolean doExtensiveHealthChecks) {

//exchangeFinder是对connectionPool、connectionPool、request等信息的包装,它是在RetryAndFollowUpInterceptor拦截器中初始化出来的

ExchangeCodec codec = exchangeFinder.find(client, chain, doExtensiveHealthChecks);

Exchange result = new Exchange(this, call, eventListener, exchangeFinder, codec);

synchronized (connectionPool) {

this.exchange = result;

return result;

}

}

4.ExchangeCodec#find

该方法中将ExchangeCodec的获取交给了exchangeFinder对象,ExchangeCodec是一个接口,实现类有Http2ExchangeCodecHttp1ExchangeCodec,这两个类表示http1和http2的建立连接的类,里面实现了writeRequestHeaderscreateRequestBody等方法,这两个方法是在CallServerInterceptor拦截器中使用的。exchangeFinder是对connectionPool、connectionPool、request等信息的包装,它是在RetryAndFollowUpInterceptor拦截器中初始化出来的。我们接着看exchangeFinder的find方法:

public ExchangeCodec find(

OkHttpClient client, Interceptor.Chain chain, boolean doExtensiveHealthChecks) {

int connectTimeout = chain.connectT

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中使用OkHttp连接池可以通过配置`HttpClient` bean来实现。以下是一个示例: ```java import okhttp3.ConnectionPool; import okhttp3.OkHttpClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.concurrent.TimeUnit; @Configuration public class OkHttpConfig { @Bean public OkHttpClient okHttpClient() { int maxIdleConnections = 100; // 最大空闲连接数 long keepAliveDuration = 5; // 连接存活间(分钟) ConnectionPool connectionPool = new ConnectionPool(maxIdleConnections, keepAliveDuration, TimeUnit.MINUTES); return new OkHttpClient.Builder() .connectionPool(connectionPool) .build(); } } ``` 在上述示例中,我们创建了一个`OkHttpClient`的bean,并通过`ConnectionPool`设置了最大空闲连接数和连接存活间。你可以根据自己的需求进行调整。 然后,在需要使用OkHttp的地方,你可以通过注入`OkHttpClient`来使用连接池: ```java import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.IOException; @Service public class MyService { @Autowired private OkHttpClient okHttpClient; public String makeRequest() throws IOException { Request request = new Request.Builder() .url("https://example.com") .build(); try (Response response = okHttpClient.newCall(request).execute()) { return response.body().string(); } } } ``` 在上述示例中,我们通过`@Autowired`注解将`OkHttpClient`注入到`MyService`中,并在`makeRequest()`方法中使用连接池发送请求。 这样,你就可以在Spring Boot中使用OkHttp连接池了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值