Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

okhttp supports custom protocols. gh-809 #820

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import feign.okhttp.OkHttpClient;
import jakarta.annotation.PreDestroy;
import okhttp3.ConnectionPool;
import okhttp3.Protocol;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand Down Expand Up @@ -92,6 +93,7 @@
* @author Sam Kruglov
* @author Wojciech Mąka
* @author Dangzhicairang(小水牛)
* @author changjin wei(魏昌进)
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(Feign.class)
Expand Down Expand Up @@ -259,11 +261,13 @@ public okhttp3.OkHttpClient okHttpClient(okhttp3.OkHttpClient.Builder builder, C
int connectTimeout = httpClientProperties.getConnectionTimeout();
boolean disableSslValidation = httpClientProperties.isDisableSslValidation();
Duration readTimeout = httpClientProperties.getOkHttp().getReadTimeout();
List<Protocol> protocols = httpClientProperties.getOkHttp().getProtocols();
if (disableSslValidation) {
disableSsl(builder);
}
this.okHttpClient = builder.connectTimeout(connectTimeout, TimeUnit.MILLISECONDS)
.followRedirects(followRedirects).readTimeout(readTimeout).connectionPool(connectionPool).build();
.followRedirects(followRedirects).readTimeout(readTimeout).connectionPool(connectionPool)
.protocols(protocols).build();
return this.okHttpClient;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
package org.springframework.cloud.openfeign.support;

import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;

import feign.okhttp.OkHttpClient;
import okhttp3.Protocol;

import org.springframework.boot.context.properties.ConfigurationProperties;

Expand Down Expand Up @@ -336,6 +338,11 @@ public static class OkHttp {
*/
private Duration readTimeout = Duration.ofSeconds(60);

/**
* Configure the protocols used by this client to communicate with remote servers.
*/
private List<Protocol> protocols = List.of(Protocol.HTTP_2, Protocol.HTTP_1_1);

public Duration getReadTimeout() {
return readTimeout;
}
Expand All @@ -344,6 +351,14 @@ public void setReadTimeout(Duration readTimeout) {
this.readTimeout = readTimeout;
}

public List<Protocol> getProtocols() {
return protocols;
}

public void setProtocols(List<Protocol> protocols) {
this.protocols = protocols;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
package org.springframework.cloud.openfeign;

import java.lang.reflect.Field;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what is causing the build checkstyle failure - please revert this line deletion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I resubmitted the code

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, thanks.

import javax.net.ssl.HostnameVerifier;

import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -34,6 +34,7 @@

/**
* @author Ryan Baxter
* @author changjin wei(魏昌进)
*/
class FeignOkHttpConfigurationTests {

Expand All @@ -45,7 +46,8 @@ void setUp() {
.properties("debug=true", "spring.cloud.openfeign.httpclient.disableSslValidation=true",
"spring.cloud.openfeign.okhttp.enabled=true",
"spring.cloud.openfeign.httpclient.hc5.enabled=false",
"spring.cloud.openfeign.httpclient.okhttp.read-timeout=9s")
"spring.cloud.openfeign.httpclient.okhttp.read-timeout=9s",
"spring.cloud.openfeign.httpclient.okhttp.protocols=H2_PRIOR_KNOWLEDGE")
.web(WebApplicationType.NONE).sources(FeignAutoConfiguration.class).run();
}

Expand All @@ -71,6 +73,13 @@ void shouldConfigureReadTimeout() {
assertThat(httpClient.readTimeoutMillis()).isEqualTo(9000);
}

@Test
void shouldResolveProtocolFromProperties() {
OkHttpClient httpClient = context.getBean(OkHttpClient.class);

assertThat(httpClient.protocols()).containsExactly(Protocol.H2_PRIOR_KNOWLEDGE);
}

protected Object getField(Object target, String name) {
Field field = ReflectionUtils.findField(target.getClass(), name);
ReflectionUtils.makeAccessible(field);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.openfeign.protocol;

import java.lang.reflect.Field;

import feign.Client;
import okhttp3.Protocol;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;
import org.springframework.cloud.openfeign.test.NoSecurityConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.annotation.RestController;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author changjin wei(魏昌进)
*/
@SpringBootTest(classes = FeignOkProtocolsTests.Application.class, webEnvironment = WebEnvironment.RANDOM_PORT,
value = { "spring.application.name=feignclienttest", "spring.cloud.openfeign.circuitbreaker.enabled=false",
"spring.cloud.openfeign.httpclient.hc5.enabled=false", "spring.cloud.openfeign.okhttp.enabled=true",
"spring.cloud.httpclientfactories.ok.enabled=true", "spring.cloud.loadbalancer.retry.enabled=false",
"server.http2.enabled=true", "spring.cloud.openfeign.httpclient.okhttp.protocols=H2_PRIOR_KNOWLEDGE" })
@DirtiesContext
class FeignOkProtocolsTests {

@Autowired
private Client feignClient;

@Test
void shouldCreateCorrectFeignClientBeanWithProtocolFromProperties() {
assertThat(feignClient).isInstanceOf(FeignBlockingLoadBalancerClient.class);
FeignBlockingLoadBalancerClient client = (FeignBlockingLoadBalancerClient) feignClient;
Client delegate = client.getDelegate();
assertThat(delegate).isInstanceOf(feign.okhttp.OkHttpClient.class);
okhttp3.OkHttpClient OkHttpClient = (okhttp3.OkHttpClient) getField(delegate, "delegate");
assertThat(OkHttpClient.protocols()).containsExactly(Protocol.H2_PRIOR_KNOWLEDGE);

OlgaMaciaszek marked this conversation as resolved.
Show resolved Hide resolved
}

protected Object getField(Object target, String name) {
Field field = ReflectionUtils.findField(target.getClass(), name);
ReflectionUtils.makeAccessible(field);
return ReflectionUtils.getField(field, target);
}

@Configuration(proxyBeanMethods = false)
@EnableAutoConfiguration
@RestController
@LoadBalancerClients
@Import(NoSecurityConfiguration.class)
public static class Application {
OlgaMaciaszek marked this conversation as resolved.
Show resolved Hide resolved

}

}