From 9c30b9f9e29905ece6841d5c27ae7ad5110f0a3c Mon Sep 17 00:00:00 2001 From: hongqiaowei Date: Fri, 10 Sep 2021 20:56:22 +0800 Subject: [PATCH] Add WebClientBuilderConfig --- .../java/we/FizzBootstrapApplication.java | 1 + .../we/config/WebClientBuilderConfig.java | 52 +++++++++++++++++++ .../main/java/we/config/WebClientConfig.java | 13 +++-- 3 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 fizz-common/src/main/java/we/config/WebClientBuilderConfig.java diff --git a/fizz-bootstrap/src/main/java/we/FizzBootstrapApplication.java b/fizz-bootstrap/src/main/java/we/FizzBootstrapApplication.java index 08e6e68..4e5b27a 100644 --- a/fizz-bootstrap/src/main/java/we/FizzBootstrapApplication.java +++ b/fizz-bootstrap/src/main/java/we/FizzBootstrapApplication.java @@ -14,6 +14,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ + package we; import org.slf4j.Logger; diff --git a/fizz-common/src/main/java/we/config/WebClientBuilderConfig.java b/fizz-common/src/main/java/we/config/WebClientBuilderConfig.java new file mode 100644 index 0000000..516725f --- /dev/null +++ b/fizz-common/src/main/java/we/config/WebClientBuilderConfig.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2020 the original author or authors. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package we.config; + +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration; +import org.springframework.boot.autoconfigure.web.reactive.function.client.ClientHttpConnectorAutoConfiguration; +import org.springframework.boot.web.reactive.function.client.WebClientCustomizer; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.reactive.function.client.WebClient; + +/** + * @author hongqiaowei + */ + +@Configuration(proxyBeanMethods = false) +@ConditionalOnClass(WebClient.class) +@AutoConfigureAfter({CodecsAutoConfiguration.class, ClientHttpConnectorAutoConfiguration.class}) +public class WebClientBuilderConfig { + + private final WebClient.Builder webClientBuilder; + + public WebClientBuilderConfig(ObjectProvider customizerProvider) { + this.webClientBuilder = WebClient.builder(); + customizerProvider.orderedStream().forEach( + (customizer) -> { + customizer.customize(this.webClientBuilder); + } + ); + } + + public WebClient.Builder getBuilder() { + return this.webClientBuilder.clone(); + } +} diff --git a/fizz-common/src/main/java/we/config/WebClientConfig.java b/fizz-common/src/main/java/we/config/WebClientConfig.java index a57df26..4ff3e34 100644 --- a/fizz-common/src/main/java/we/config/WebClientConfig.java +++ b/fizz-common/src/main/java/we/config/WebClientConfig.java @@ -33,7 +33,6 @@ import reactor.netty.tcp.TcpClient; import javax.annotation.Resource; import javax.net.ssl.SSLException; -import java.time.Duration; import java.util.concurrent.TimeUnit; /** @@ -127,8 +126,11 @@ public abstract class WebClientConfig { // @Resource // ReactorClientHttpConnector reactorClientHttpConnector; + // @Resource + // WebClient.Builder webClientBuilder; + @Resource - WebClient.Builder webClientBuilder; + WebClientBuilderConfig webClientBuilderConfig; public WebClient webClient() { @@ -175,9 +177,10 @@ public abstract class WebClientConfig { } } - return webClientBuilder.exchangeStrategies( - ExchangeStrategies.builder().codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1)) - .build() + return webClientBuilderConfig.getBuilder() + .exchangeStrategies( + ExchangeStrategies.builder().codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1)) + .build() ) .clientConnector(new ReactorClientHttpConnector(httpClient)) .build();