Add WebClientBuilderConfig

This commit is contained in:
hongqiaowei
2021-09-10 20:56:22 +08:00
parent 0aa981b3ed
commit 9c30b9f9e2
3 changed files with 61 additions and 5 deletions

View File

@@ -14,6 +14,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package we; package we;
import org.slf4j.Logger; import org.slf4j.Logger;

View File

@@ -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 <https://www.gnu.org/licenses/>.
*/
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<WebClientCustomizer> customizerProvider) {
this.webClientBuilder = WebClient.builder();
customizerProvider.orderedStream().forEach(
(customizer) -> {
customizer.customize(this.webClientBuilder);
}
);
}
public WebClient.Builder getBuilder() {
return this.webClientBuilder.clone();
}
}

View File

@@ -33,7 +33,6 @@ import reactor.netty.tcp.TcpClient;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.net.ssl.SSLException; import javax.net.ssl.SSLException;
import java.time.Duration;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
@@ -127,8 +126,11 @@ public abstract class WebClientConfig {
// @Resource // @Resource
// ReactorClientHttpConnector reactorClientHttpConnector; // ReactorClientHttpConnector reactorClientHttpConnector;
// @Resource
// WebClient.Builder webClientBuilder;
@Resource @Resource
WebClient.Builder webClientBuilder; WebClientBuilderConfig webClientBuilderConfig;
public WebClient webClient() { public WebClient webClient() {
@@ -175,9 +177,10 @@ public abstract class WebClientConfig {
} }
} }
return webClientBuilder.exchangeStrategies( return webClientBuilderConfig.getBuilder()
ExchangeStrategies.builder().codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1)) .exchangeStrategies(
.build() ExchangeStrategies.builder().codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1))
.build()
) )
.clientConnector(new ReactorClientHttpConnector(httpClient)) .clientConnector(new ReactorClientHttpConnector(httpClient))
.build(); .build();