In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about how Feign uses HttpClient and OkHttp respectively. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Using HttpClient and OkHttp
In Feign, Client is a very important component, and Feign finally sends Request requests and receives Response responses by Client components. Client is an interface in the Feign source code, and by default, the implementation class of Client is Client.Default. Client.Default is implemented by HttpURLConnection for network requests. In addition, Client supports HttpClient and OkHttp to make network requests.
First take a look at FeignRibbonClient's autoconfiguration class FeignRibbonClientAutoConfiguration, which injects some Bean when the program starts, which injects a Bean whose BeanName is feignClient's Client type. If Bean is configured with BeanName as FeignClient by default, the object Client.Default will be automatically injected to track the Client.Default source code. The network request framework used by Client.Default is HttpURLConnection, as shown below:
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) }. / / Code ellipsis}
In this case, due to the lack of connection pool support, there will be problems with the service after reaching a certain amount of traffic.
Use HttpClient
So how do you use HttpClient's framework in Feign? Let's take a look at the source code of FeignAutoConfiguration.HttpClientFeignConfiguration:
@ 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 ();}}, 3000L, (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 () }}}
As you can see from the code @ ConditionalOnClass ({ApacheHttpClient.class}) annotation, you only need to add the HttpClient dependency to the pom file. In addition, you need to configure feign.httpclient.enabled as true in the configuration file. As you can see from the @ ConditionalOnProperty annotation, this configuration can be left unwritten, because it is true by default:
Io.github.openfeign feign-httpclient 9.4.0 using OkHttp
Check the source code of FeignAutoConfiguration.HttpClientFeignConfiguration:
@ 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);}}
Similarly, if you want to use OkHttp as the network request framework in Feign, you only need to add the feign-okhttp dependency to the pom file, as follows:
Io.github.openfeign feign-okhttp 10.2.0OpenFeign is replaced by feign-okhttp io.github.openfeign feign-okhttp introduced in OkHttppom. Configure okhttpfeign: httpclient: connection-timeout: 2000 # unit ms in application.yml. Default is 2000 max-connections: 2000 # maximum number of connections in thread pool okhttp: enabled: true
With the above settings, okhttp is ready to use because automatic assembly has been implemented in FeignAutoConfiguration
If you need to set more fine parameters for okhttp, you need to customize the implementation of okhttp, which can be imitated in the figure above.
This is how the Feign shared by the editor uses HttpClient and OkHttp, respectively. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.