Description of the Problem:
spring-cloud-openfeign-core-2.2.5.RELEASE is the version we use.
okhttp3.OkHttpClient is used in service and injected as a bean for the rest of the http scenarios, not in feign
The code as follows:
@Configuration
public class OkHttpConfig {
@Bean
public OkHttpClient okHttpClient() {
return new OkHttpClient.Builder()
//.sslSocketFactory(sslSocketFactory(), x509TrustManager())
.retryOnConnectionFailure(false).connectionPool(pool()).connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS).build();
}
@Bean
public SSLSocketFactory sslSocketFactory() {
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, null, new SecureRandom());
return sslContext.getSocketFactory();
} catch (KeyManagementException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
@Bean
public ConnectionPool pool() {
return new ConnectionPool(200, 5, TimeUnit.MINUTES);
}
}
Under the above premise, our configuration of feign is as follows:
feign.httpclient.enabled=true
feign.okhttp.enabled=false
feign.compression.response.enabled=true
The header of FeignRequest is expected to be added with Accept-Encoding: gzip, deflate, because it is the function of FeignAcceptGzipEncodingAutoConfiguration and FeignAcceptGzipEncodingInterceptor, but it does not work now.
Our investigation shows that:
In FeignAcceptGzipEncodingAutoConfiguration, the @ConditionalOnMissingBean(type = "okhttp3.OkHttpClient") affects the loading of FeignAcceptGzipEncodingAutoConfiguration. The reason is that okhttp3.OkHttpClient is used in service and injected as a bean, so that ApacheHttpClient cannot use 'http compression' in the calling interface with feign.
Our advice:
'FeignAcceptGzipEncodingAutoConfiguration' & 'FeignContentGzipEncodingAutoConfiguration'
Before: @ConditionalOnMissingBean(type = "okhttp3.OkHttpClient")
After: @ConditionalOnMissingBean(type = "feign.okhttp.OkHttpClient")
Comment From: OlgaMaciaszek
Hello @shengyinjiang, thanks for reporting the issue. 2.2.5.RELEASE has not been supported for a long time now. Please upgrade to a supported version (3.1.4) and verify again. If the issue persists, please provide a minimal, complete, verifiable example that reproduces the issue.
Comment From: shengyinjiang
@OlgaMaciaszek Thanks for your reply. There is an example in my github to reproduce the problem.
Steps:
- Run the server application
- set "custom.okhttp.enabled=false" in client-side/resources/application.yml
- Run the client application
- HTTP GET from http://localhost:8002/client/get
you will get
[accept-encoding:"gzip", "deflate", accept:"/", content-length:"0", host:"localhost:8001", connection:"Keep-Alive", user-agent:"Apache-HttpClient/4.5.13 (Java/11.0.1)"] - set "custom.okhttp.enabled=true" in client-side/resources/application.yml
- Rerun the client application
- HTTP GET from http://localhost:8002/client/get
you will get
[accept:"/", content-length:"0", host:"localhost:8001", connection:"Keep-Alive", user-agent:"Apache-HttpClient/4.5.13 (Java/11.0.1)"]
Phenomenon:
The header of FeignRequest is expected to be added with Accept-Encoding: gzip, deflate, because it is the function of FeignAcceptGzipEncodingAutoConfiguration and FeignAcceptGzipEncodingInterceptor, but it does not work now.
Comment From: OlgaMaciaszek
Thanks, @shengyinjiang. I was able to reproduce the issue. Looks like a bug, but the fix will probably have to be a bit more complex, since while feign.okhttp.OkHttpClient will always be used for OkHttpClient-backed Feign Clients, it won't always be a bean, but might be a delegate of a different bean (see https://github.com/spring-cloud/spring-cloud-openfeign/blob/54a6a56b3673495050cc5f1f57e3ce671f8719e9/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/loadbalancer/OkHttpFeignLoadBalancerConfiguration.java#L59-L60 and https://github.com/spring-cloud/spring-cloud-openfeign/blob/54a6a56b3673495050cc5f1f57e3ce671f8719e9/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/loadbalancer/OkHttpFeignLoadBalancerConfiguration.java#L73-L74) . Will work on a fix.
Comment From: OlgaMaciaszek
Fixed.