-
Notifications
You must be signed in to change notification settings - Fork 757
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
cca510c
commit 8020b88
Showing
12 changed files
with
635 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
.../java/org/springframework/cloud/openfeign/clientconfig/Http2ClientFeignConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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.clientconfig; | ||
|
||
import java.net.http.HttpClient; | ||
import java.time.Duration; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.cloud.openfeign.support.FeignHttpClientProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* Default configuration for {@link HttpClient}. | ||
* | ||
* @author changjin wei(魏昌进) | ||
*/ | ||
@Configuration(proxyBeanMethods = false) | ||
@ConditionalOnMissingBean(HttpClient.class) | ||
public class Http2ClientFeignConfiguration { | ||
|
||
@Bean | ||
public HttpClient httpClient(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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...ringframework/cloud/openfeign/loadbalancer/Http2ClientFeignLoadBalancerConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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.loadbalancer; | ||
|
||
import java.net.http.HttpClient; | ||
import java.util.List; | ||
|
||
import feign.Client; | ||
import feign.http2client.Http2Client; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory; | ||
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; | ||
import org.springframework.cloud.client.loadbalancer.LoadBalancerClientsProperties; | ||
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Conditional; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* Configuration instantiating a {@link LoadBalancerClient}-based {@link Client} object | ||
* that uses {@link Http2Client} under the hood. | ||
* | ||
* @author changjin wei(魏昌进) | ||
*/ | ||
@Configuration(proxyBeanMethods = false) | ||
@ConditionalOnClass({ Http2Client.class, HttpClient.class }) | ||
@ConditionalOnBean({ LoadBalancerClient.class, LoadBalancerClientFactory.class }) | ||
@ConditionalOnProperty("spring.cloud.openfeign.http2client.enabled") | ||
@EnableConfigurationProperties(LoadBalancerClientsProperties.class) | ||
class Http2ClientFeignLoadBalancerConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
@Conditional(OnRetryNotEnabledCondition.class) | ||
public Client feignClient(LoadBalancerClient loadBalancerClient, HttpClient httpClient, | ||
LoadBalancerClientFactory loadBalancerClientFactory, | ||
List<LoadBalancerFeignRequestTransformer> transformers) { | ||
Client delegate = new Http2Client(httpClient); | ||
return new FeignBlockingLoadBalancerClient(delegate, loadBalancerClient, loadBalancerClientFactory, | ||
transformers); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
@ConditionalOnClass(name = "org.springframework.retry.support.RetryTemplate") | ||
@ConditionalOnBean(LoadBalancedRetryFactory.class) | ||
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.retry.enabled", havingValue = "true", | ||
matchIfMissing = true) | ||
public Client feignRetryClient(LoadBalancerClient loadBalancerClient, HttpClient httpClient, | ||
LoadBalancedRetryFactory loadBalancedRetryFactory, LoadBalancerClientFactory loadBalancerClientFactory, | ||
List<LoadBalancerFeignRequestTransformer> transformers) { | ||
Client delegate = new Http2Client(httpClient); | ||
return new RetryableFeignBlockingLoadBalancerClient(delegate, loadBalancerClient, loadBalancedRetryFactory, | ||
loadBalancerClientFactory, transformers); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...src/test/java/org/springframework/cloud/openfeign/FeignHttp2ClientConfigurationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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; | ||
|
||
import java.net.http.HttpClient; | ||
import java.time.Duration; | ||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.boot.WebApplicationType; | ||
import org.springframework.boot.builder.SpringApplicationBuilder; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author changjin wei(魏昌进) | ||
*/ | ||
class FeignHttp2ClientConfigurationTests { | ||
|
||
private ConfigurableApplicationContext context; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
context = new SpringApplicationBuilder() | ||
.properties("debug=true", "spring.cloud.openfeign.http2client.enabled=true", | ||
"spring.cloud.openfeign.httpclient.http2.version=HTTP_1_1", | ||
"spring.cloud.openfeign.httpclient.connectionTimeout=15") | ||
.web(WebApplicationType.NONE).sources(FeignAutoConfiguration.class).run(); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
if (context != null) { | ||
context.close(); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldConfigureConnectTimeout() { | ||
HttpClient httpClient = context.getBean(HttpClient.class); | ||
|
||
assertThat(httpClient.connectTimeout()).isEqualTo(Optional.ofNullable(Duration.ofMillis(15))); | ||
} | ||
|
||
@Test | ||
void shouldResolveVersionFromProperties() { | ||
HttpClient httpClient = context.getBean(HttpClient.class); | ||
|
||
assertThat(httpClient.version()).isEqualTo(HttpClient.Version.HTTP_1_1); | ||
} | ||
|
||
} |
Oops, something went wrong.