Skip to content

Commit ff7f755

Browse files
authored
Make JDK HttpClient customizable (#1000)
1 parent 55a78d9 commit ff7f755

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
lines changed

spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/clientconfig/Http2ClientFeignConfiguration.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2023 the original author or authors.
2+
* Copyright 2013-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,8 +18,10 @@
1818

1919
import java.net.http.HttpClient;
2020
import java.time.Duration;
21+
import java.util.List;
2122

2223
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
24+
import org.springframework.cloud.openfeign.clientconfig.http2client.Http2ClientCustomizer;
2325
import org.springframework.cloud.openfeign.support.FeignHttpClientProperties;
2426
import org.springframework.context.annotation.Bean;
2527
import org.springframework.context.annotation.Configuration;
@@ -28,18 +30,26 @@
2830
* Default configuration for {@link HttpClient}.
2931
*
3032
* @author changjin wei(魏昌进)
33+
* @author Luis Duarte
3134
*/
3235
@Configuration(proxyBeanMethods = false)
3336
@ConditionalOnMissingBean(HttpClient.class)
3437
public class Http2ClientFeignConfiguration {
3538

3639
@Bean
37-
public HttpClient httpClient(FeignHttpClientProperties httpClientProperties) {
40+
@ConditionalOnMissingBean
41+
public HttpClient.Builder httpClientBuilder(FeignHttpClientProperties httpClientProperties) {
3842
return HttpClient.newBuilder()
3943
.followRedirects(httpClientProperties.isFollowRedirects() ? HttpClient.Redirect.ALWAYS
4044
: HttpClient.Redirect.NEVER)
4145
.version(HttpClient.Version.valueOf(httpClientProperties.getHttp2().getVersion()))
42-
.connectTimeout(Duration.ofMillis(httpClientProperties.getConnectionTimeout())).build();
46+
.connectTimeout(Duration.ofMillis(httpClientProperties.getConnectionTimeout()));
47+
}
48+
49+
@Bean
50+
public HttpClient httpClient(HttpClient.Builder httpClientBuilder, List<Http2ClientCustomizer> customizers) {
51+
customizers.forEach(customizer -> customizer.customize(httpClientBuilder));
52+
return httpClientBuilder.build();
4353
}
4454

4555
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2013-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.openfeign.clientconfig.http2client;
18+
19+
import java.net.http.HttpClient;
20+
21+
/**
22+
* Callback interface that can be implemented by beans wishing to further customize the
23+
* {@link HttpClient} through {@link HttpClient.Builder} retaining its default
24+
* auto-configuration.
25+
*
26+
* @author Luís Duarte
27+
* @since 4.1.1
28+
*/
29+
@FunctionalInterface
30+
public interface Http2ClientCustomizer {
31+
32+
/**
33+
* Customize JDK's HttpClient.
34+
* @param builder the HttpClient.Builder to customize
35+
*/
36+
void customize(HttpClient.Builder builder);
37+
38+
}

spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/FeignHttp2ClientConfigurationTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
/**
3434
* @author changjin wei(魏昌进)
35+
* @author Luis Duarte
3536
*/
3637
class FeignHttp2ClientConfigurationTests {
3738

spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/test/Http2ClientConfigurationTests.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2023 the original author or authors.
2+
* Copyright 2013-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.cloud.openfeign.test;
1818

19+
import java.net.InetSocketAddress;
20+
import java.net.ProxySelector;
1921
import java.net.http.HttpClient;
2022

2123
import feign.Client;
@@ -27,6 +29,7 @@
2729
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2830
import org.springframework.boot.test.context.SpringBootTest;
2931
import org.springframework.cloud.openfeign.FeignClient;
32+
import org.springframework.cloud.openfeign.clientconfig.http2client.Http2ClientCustomizer;
3033
import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;
3134
import org.springframework.context.annotation.Bean;
3235
import org.springframework.test.annotation.DirtiesContext;
@@ -45,15 +48,21 @@ class Http2ClientConfigurationTests {
4548
@Autowired
4649
FeignBlockingLoadBalancerClient feignClient;
4750

48-
private static final HttpClient defaultHttpClient = HttpClient.newHttpClient();
51+
@Autowired
52+
HttpClient underlyingHttpClient;
4953

5054
@Test
5155
void shouldInstantiateFeignHttp2Client() {
5256
Client delegate = feignClient.getDelegate();
5357
assertThat(delegate instanceof Http2Client).isTrue();
5458
Http2Client http2Client = (Http2Client) delegate;
5559
HttpClient httpClient = getField(http2Client, "client");
56-
assertThat(httpClient).isEqualTo(defaultHttpClient);
60+
assertThat(httpClient).isEqualTo(underlyingHttpClient);
61+
}
62+
63+
@Test
64+
void customizesHttpClient() {
65+
assertThat(underlyingHttpClient.proxy()).isNotEmpty();
5766
}
5867

5968
@SuppressWarnings("unchecked")
@@ -72,8 +81,9 @@ interface FooClient {
7281
static class TestConfig {
7382

7483
@Bean
75-
public HttpClient client() {
76-
return defaultHttpClient;
84+
public Http2ClientCustomizer customizer() {
85+
return builder -> builder
86+
.proxy(ProxySelector.of(new InetSocketAddress("localhost", 1234)));
7787
}
7888

7989
}

0 commit comments

Comments
 (0)