io.irain.reactor.http.autoconfigure.WebClientConfiguration Maven / Gradle / Ivy
package io.irain.reactor.http.autoconfigure;
import io.netty.channel.ChannelOption;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.reactive.function.client.WebClientCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.http.client.ReactorResourceFactory;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
import reactor.netty.tcp.DefaultSslContextSpec;
import reactor.netty.transport.logging.AdvancedByteBufFormat;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.function.Function;
/**
* @author youta
**/
@Configuration
@ConditionalOnClass(WebClient.class)
public class WebClientConfiguration {
/**
* webClient 配置
*
* @param builder the web client builder
* @return WebClient web client
*/
@Bean
@ConditionalOnMissingBean
public WebClient webClient(WebClient.Builder builder) {
return builder.build();
}
/**
* The type Reactor client configuration.
*/
@Configuration
@ConditionalOnClass(HttpClient.class)
public static class ReactorClientConfiguration {
/**
* Reactor resource factory reactor resource factory.
*
* @return the reactor resource factory
*/
@Bean
public ReactorResourceFactory reactorResourceFactory() {
ReactorResourceFactory factory = new ReactorResourceFactory();
factory.setUseGlobalResources(false);
return factory;
}
/**
* Reactor client web client customizer web client customizer.
*
* @param reactorResourceFactory the reactor resource factory
* @return the web client customizer
*/
@Bean
@ConditionalOnClass(ReactorResourceFactory.class)
public WebClientCustomizer shoreWebClientCustomizer(ReactorResourceFactory reactorResourceFactory) {
Function function = httpClient ->
httpClient.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3_000)
.wiretap(WebClient.class.getName(), LogLevel.DEBUG,
AdvancedByteBufFormat.TEXTUAL, StandardCharsets.UTF_8)
.responseTimeout(Duration.ofSeconds(15))
.secure(sslContextSpec -> sslContextSpec.sslContext(
DefaultSslContextSpec.forClient().configure(builder ->
builder.trustManager(InsecureTrustManagerFactory.INSTANCE))))
.doOnConnected(connection ->
connection.addHandlerLast(new ReadTimeoutHandler(20))
.addHandlerLast(new WriteTimeoutHandler(20)));
ReactorClientHttpConnector connector = new ReactorClientHttpConnector(reactorResourceFactory, function);
return webClientBuilder -> webClientBuilder.clientConnector(connector);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy