- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Feign 使用HttpClient和OkHttp方式由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
在Feign中,Client是一个非常重要的组件,Feign最终发送Request请求以及接收Response响应都是由Client组件来完成的。Client在Feign源码中是一个接口,在默认情况下,Client的实现类是Client.Default。Client.Default是由HttpURLConnection来实现网络请求的。另外,Client还支持HttpClient和OkHttp来进行网络请求.
首先查看FeignRibbonClient的自动配置类FeignRibbonClientAutoConfiguration,该类在程序启动的时候注入一些Bean,其中注入了一个BeanName为feignClient的Client类型的Bean。在省缺配置BeanName为FeignClient的Bean的情况下,会自动注入Client.Default这个对象,跟踪Client.Default源码,Client.Default使用的网络请求框架是HttpURLConnection,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public
static
class
Default
implements
Client {
private
final
SSLSocketFactory sslContextFactory;
private
final
HostnameVerifier hostnameVerifier;
public
Default(SSLSocketFactory sslContextFactory, HostnameVerifier hostnameVerifier) {
this
.sslContextFactory = sslContextFactory;
this
.hostnameVerifier = hostnameVerifier;
}
public
Response execute(Request request, Options options)
throws
IOException {
HttpURLConnection connection =
this
.convertAndSend(request, options);
return
this
.convertResponse(connection, request);
}
......
//代码省略
}
|
这种情况下,由于缺乏连接池的支持,在达到一定流量的后服务肯定会出问题 .
那么如何在Feign中使用HttpClient的框架呢?我们查看FeignAutoConfiguration.HttpClientFeignConfiguration的源码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
@Configuration
@ConditionalOnClass
({ApacheHttpClient.
class
})
@ConditionalOnMissingClass
({
"com.netflix.loadbalancer.ILoadBalancer"
})
@ConditionalOnMissingBean
({CloseableHttpClient.
class
})
@ConditionalOnProperty
(
value = {
"feign.httpclient.enabled"
},
matchIfMissing =
true
)
protected
static
class
HttpClientFeignConfiguration {
private
final
Timer connectionManagerTimer =
new
Timer(
"FeignApacheHttpClientConfiguration.connectionManagerTimer"
,
true
);
@Autowired
(
required =
false
)
private
RegistryBuilder registryBuilder;
private
CloseableHttpClient httpClient;
protected
HttpClientFeignConfiguration() {
}
@Bean
@ConditionalOnMissingBean
({HttpClientConnectionManager.
class
})
public
HttpClientConnectionManager connectionManager(ApacheHttpClientConnectionManagerFactory connectionManagerFactory, FeignHttpClientProperties httpClientProperties) {
final
HttpClientConnectionManager connectionManager = connectionManagerFactory.newConnectionManager(httpClientProperties.isDisableSslValidation(), httpClientProperties.getMaxConnections(), httpClientProperties.getMaxConnectionsPerRoute(), httpClientProperties.getTimeToLive(), httpClientProperties.getTimeToLiveUnit(),
this
.registryBuilder);
this
.connectionManagerTimer.schedule(
new
TimerTask() {
public
void
run() {
connectionManager.closeExpiredConnections();
}
}, 30000L, (
long
)httpClientProperties.getConnectionTimerRepeat());
return
connectionManager;
}
@Bean
public
CloseableHttpClient httpClient(ApacheHttpClientFactory httpClientFactory, HttpClientConnectionManager httpClientConnectionManager, FeignHttpClientProperties httpClientProperties) {
RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(httpClientProperties.getConnectionTimeout()).setRedirectsEnabled(httpClientProperties.isFollowRedirects()).build();
this
.httpClient = httpClientFactory.createBuilder().setConnectionManager(httpClientConnectionManager).setDefaultRequestConfig(defaultRequestConfig).build();
return
this
.httpClient;
}
@Bean
@ConditionalOnMissingBean
({Client.
class
})
public
Client feignClient(HttpClient httpClient) {
return
new
ApacheHttpClient(httpClient);
}
@PreDestroy
public
void
destroy()
throws
Exception {
this
.connectionManagerTimer.cancel();
if
(
this
.httpClient !=
null
) {
this
.httpClient.close();
}
}
}
|
从代码@ConditionalOnClass({ApacheHttpClient.class})注解可知,只需要在pom文件上加上HttpClient依赖即可。另外需要在配置文件中配置feign.httpclient.enabled为true,从@ConditionalOnProperty注解可知,这个配置可以不写,因为在默认情况下就为true
1
2
3
4
5
|
<
dependency
>
<
groupId
>io.github.openfeign</
groupId
>
<
artifactId
>feign-httpclient</
artifactId
>
<
version
>9.4.0</
version
>
</
dependency
>
|
查看FeignAutoConfiguration.HttpClientFeignConfiguration的源码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
@Configuration
@ConditionalOnClass
({OkHttpClient.
class
})
@ConditionalOnMissingClass
({
"com.netflix.loadbalancer.ILoadBalancer"
})
@ConditionalOnMissingBean
({okhttp3.OkHttpClient.
class
})
@ConditionalOnProperty
({
"feign.okhttp.enabled"
})
protected
static
class
OkHttpFeignConfiguration {
private
okhttp3.OkHttpClient okHttpClient;
protected
OkHttpFeignConfiguration() {
}
@Bean
@ConditionalOnMissingBean
({ConnectionPool.
class
})
public
ConnectionPool httpClientConnectionPool(FeignHttpClientProperties httpClientProperties, OkHttpClientConnectionPoolFactory connectionPoolFactory) {
Integer maxTotalConnections = httpClientProperties.getMaxConnections();
Long timeToLive = httpClientProperties.getTimeToLive();
TimeUnit ttlUnit = httpClientProperties.getTimeToLiveUnit();
return
connectionPoolFactory.create(maxTotalConnections, timeToLive, ttlUnit);
}
@Bean
public
okhttp3.OkHttpClient client(OkHttpClientFactory httpClientFactory, ConnectionPool connectionPool, FeignHttpClientProperties httpClientProperties) {
Boolean followRedirects = httpClientProperties.isFollowRedirects();
Integer connectTimeout = httpClientProperties.getConnectionTimeout();
Boolean disableSslValidation = httpClientProperties.isDisableSslValidation();
this
.okHttpClient = httpClientFactory.createBuilder(disableSslValidation).connectTimeout((
long
)connectTimeout, TimeUnit.MILLISECONDS).followRedirects(followRedirects).connectionPool(connectionPool).build();
return
this
.okHttpClient;
}
@PreDestroy
public
void
destroy() {
if
(
this
.okHttpClient !=
null
) {
this
.okHttpClient.dispatcher().executorService().shutdown();
this
.okHttpClient.connectionPool().evictAll();
}
}
@Bean
@ConditionalOnMissingBean
({Client.
class
})
public
Client feignClient(okhttp3.OkHttpClient client) {
return
new
OkHttpClient(client);
}
}
|
同理,如果想要在Feign中使用OkHttp作为网络请求框架,则只需要在pom文件中加上feign-okhttp的依赖,代码如下:
1
2
3
4
5
|
<
dependency
>
<
groupId
>io.github.openfeign</
groupId
>
<
artifactId
>feign-okhttp</
artifactId
>
<
version
>10.2.0</
version
>
</
dependency
>
|
1
2
3
4
|
<
dependency
>
<
groupId
>io.github.openfeign</
groupId
>
<
artifactId
>feign-okhttp</
artifactId
>
</
dependency
>
|
1
2
3
4
5
6
|
feign:
httpclient:
connection-timeout: 2000 #单位ms,默认2000
max-connections: 200 #线程池最大连接数
okhttp:
enabled: true
|
经过上面设置已经可以使用okhttp了,因为在FeignAutoConfiguration中已实现自动装配 。
如果需要对okhttp做更精细的参数设置,那需要自定义okhttp的实现,可以模仿上图中的实现 。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我.
原文链接:https://blog.csdn.net/u010277958/article/details/88730889 。
最后此篇关于Feign 使用HttpClient和OkHttp方式的文章就讲到这里了,如果你想了解更多关于Feign 使用HttpClient和OkHttp方式的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在实现 Server-Sent Events使用 OkHttp 的库。服务器发送事件的工作原理是与服务器保持开放的 HTTP 连接,在该服务器上,“事件”可以流式传输回客户端。连接只会在错误或客户
我需要用 OkHttp 重现错误,这样我就可以提交错误或在 StackOverflow 上提问。 无需大量设置即可执行此操作的最简单方法是什么? 我读过 https://stackoverflow.c
我需要用 OkHttp 重现错误,这样我就可以提交错误或在 StackOverflow 上提问。 无需大量设置即可执行此操作的最简单方法是什么? 我读过 https://stackoverflow.c
我正在尝试使用 OkHttp 与 HTTP/2 服务器进行通信客户端。 添加到 Maven POM: com.squareup.okhttp3 okhttp 3.2.0 这是我的
我在使用我的应用程序时不断在 Logcat 中看到以下日志行: 19098-19147/ W/OkHttpClient: A connection to was leaked. Did you fo
我的团队正受此困扰issue使用 slack 集成来上传文件,因此根据该问题中的评论,我想限制我们的 Kotlin 实现中的请求。 我正在尝试集成 Okio Throttler在 OkHttp 拦截器
如何验证 okhttp 是否成功协商了 http/2 而没有使用 https/1.1?我可以检查回复中的一些信息吗? 谢谢, -托尼 最佳答案 仅供引用,因为 okhttp 3.0 左右 OkHttp
我想提出一个请求并强制它使用 Protocol.HTTP_2。 我尝试了下面的代码: import okhttp3.{OkHttpClient, Protocol, Request} import s
我不太熟悉 OkHttp 库,我对它应该如何用于异步请求有一些疑问。 对于 Android 编程,我将 android 库用于 http,并将 asyncTask 用于某些请求。 但是因为我想尝试 O
我在我的项目中使用 Retrofit-1.6.0 + okhttp-2.0.0-RC1 。当我运行项目时出现错误。 java.lang.RuntimeException: Retrofit detec
OkHttpClient 的默认行为是将 keep-alive 设置为 true。对于我们的特定用例,我需要将其设置为 false。 我没有在 Java 源代码中找到任何设置来执行此操作。 有人遇到过
我试图让 OkHttp 在我的 Android Studio 项目中工作,我创建了一个类并从这里粘贴以下代码:https://github.com/square/okhttp/blob/master/
我注意到 OkHttp 在 Android 5.0+ 中通过 ALPN 支持 h2。 来自 some article ,我了解到我们可以对 http/2 请求使用 NPN 协商。 我可以通过 NPN
我知道,Retrofit 在内部使用 OkHttp。但是,我可以看到一些开发人员提供了以下方法的应用 return new Retrofit.Builder() .baseUrl(Bu
我正在努力让 Android 上的 OkHttpClient 使用自定义证书发出 HTTPS 请求,同时绑定(bind)到特定网络接口(interface)的本地地址。我目前对此的尝试使用以下 OkH
我有一个用于 https 连接演示的 native android 应用程序,我遇到了这样的异常:: 我的连接代码很简单: URL url = new URL("https://192.168.1.5
我正在重写 EventListener 中的 dnsEnd() 函数: @Override public void dnsEnd(Call call, String domainName, L
我有okhttp的android多项目设置,作为多个项目的一部分 在项目A中,okhttp包含在源代码中。 在项目B中,okhttp带有库依赖项的一部分。 构建时,出现以下错误。 Execution
我正在尝试使用pesapal向我的应用程序添加付款但遇到问题,我将它们的依赖实现'com.pesapal.android:pesapalandroid:1.0.5'添加到我的应用程序中,从那时起我就一
我只是尝试使用 OkHttp但是当我将我的 gradle 文件与 Android Studio 同步时,我遇到了以下问题: Error:Failed to find: com.squareup.okh
我是一名优秀的程序员,十分优秀!