Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
OlgaMaciaszek committed Mar 20, 2024
2 parents 0ee8de2 + 3eb6937 commit d2a7d4f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2023 the original author or authors.
* Copyright 2013-2024 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.
Expand All @@ -18,8 +18,10 @@

import java.net.http.HttpClient;
import java.time.Duration;
import java.util.List;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.openfeign.clientconfig.http2client.Http2ClientCustomizer;
import org.springframework.cloud.openfeign.support.FeignHttpClientProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -28,18 +30,26 @@
* Default configuration for {@link HttpClient}.
*
* @author changjin wei(魏昌进)
* @author Luis Duarte
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(HttpClient.class)
public class Http2ClientFeignConfiguration {

@Bean
public HttpClient httpClient(FeignHttpClientProperties httpClientProperties) {
@ConditionalOnMissingBean
public HttpClient.Builder httpClientBuilder(FeignHttpClientProperties httpClientProperties) {
return HttpClient.newBuilder()
.followRedirects(httpClientProperties.isFollowRedirects() ? HttpClient.Redirect.ALWAYS
: HttpClient.Redirect.NEVER)
.version(HttpClient.Version.valueOf(httpClientProperties.getHttp2().getVersion()))
.connectTimeout(Duration.ofMillis(httpClientProperties.getConnectionTimeout())).build();
.connectTimeout(Duration.ofMillis(httpClientProperties.getConnectionTimeout()));
}

@Bean
public HttpClient httpClient(HttpClient.Builder httpClientBuilder, List<Http2ClientCustomizer> customizers) {
customizers.forEach(customizer -> customizer.customize(httpClientBuilder));
return httpClientBuilder.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2013-2024 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.clientconfig.http2client;

import java.net.http.HttpClient;

/**
* Callback interface that can be implemented by beans wishing to further customize the
* {@link HttpClient} through {@link HttpClient.Builder} retaining its default
* auto-configuration.
*
* @author Luís Duarte
* @since 4.1.1
*/
@FunctionalInterface
public interface Http2ClientCustomizer {

/**
* Customize JDK's HttpClient.
* @param builder the HttpClient.Builder to customize
*/
void customize(HttpClient.Builder builder);

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

/**
* @author changjin wei(魏昌进)
* @author Luis Duarte
*/
class FeignHttp2ClientConfigurationTests {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2023 the original author or authors.
* Copyright 2013-2024 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.
Expand All @@ -16,6 +16,8 @@

package org.springframework.cloud.openfeign.test;

import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.http.HttpClient;

import feign.Client;
Expand All @@ -27,6 +29,7 @@
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.clientconfig.http2client.Http2ClientCustomizer;
import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;
import org.springframework.context.annotation.Bean;
import org.springframework.test.annotation.DirtiesContext;
Expand All @@ -45,15 +48,21 @@ class Http2ClientConfigurationTests {
@Autowired
FeignBlockingLoadBalancerClient feignClient;

private static final HttpClient defaultHttpClient = HttpClient.newHttpClient();
@Autowired
HttpClient underlyingHttpClient;

@Test
void shouldInstantiateFeignHttp2Client() {
Client delegate = feignClient.getDelegate();
assertThat(delegate instanceof Http2Client).isTrue();
Http2Client http2Client = (Http2Client) delegate;
HttpClient httpClient = getField(http2Client, "client");
assertThat(httpClient).isEqualTo(defaultHttpClient);
assertThat(httpClient).isEqualTo(underlyingHttpClient);
}

@Test
void customizesHttpClient() {
assertThat(underlyingHttpClient.proxy()).isNotEmpty();
}

@SuppressWarnings("unchecked")
Expand All @@ -72,8 +81,8 @@ interface FooClient {
static class TestConfig {

@Bean
public HttpClient client() {
return defaultHttpClient;
public Http2ClientCustomizer customizer() {
return builder -> builder.proxy(ProxySelector.of(new InetSocketAddress("localhost", 1234)));
}

}
Expand Down

0 comments on commit d2a7d4f

Please sign in to comment.