Use a dedicated hook point to customize InfluxDB's HTTP client
This commit introduces a dedicated hook point for InfluxDB's http client builder and retains backward compatibility, in a deprecated fashion, for looking up a OkHttpClient.Builder bean. Closes gh-14709
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2018 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.
|
||||
@@ -17,6 +17,8 @@
|
||||
package org.springframework.boot.autoconfigure.influx;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.influxdb.InfluxDB;
|
||||
import org.influxdb.impl.InfluxDBImpl;
|
||||
|
||||
@@ -42,14 +44,36 @@ import org.springframework.context.annotation.Configuration;
|
||||
@EnableConfigurationProperties(InfluxDbProperties.class)
|
||||
public class InfluxDbAutoConfiguration {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(InfluxDbAutoConfiguration.class);
|
||||
|
||||
private final InfluxDbProperties properties;
|
||||
|
||||
private final OkHttpClient.Builder builder;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public InfluxDbAutoConfiguration(InfluxDbProperties properties,
|
||||
ObjectProvider<OkHttpClient.Builder> builder) {
|
||||
ObjectProvider<InfluxDbOkHttpClientBuilderProvider> builder,
|
||||
ObjectProvider<OkHttpClient.Builder> deprecatedBuilder) {
|
||||
this.properties = properties;
|
||||
this.builder = builder.getIfAvailable(OkHttpClient.Builder::new);
|
||||
this.builder = determineBuilder(builder.getIfAvailable(),
|
||||
deprecatedBuilder.getIfAvailable());
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
private static OkHttpClient.Builder determineBuilder(
|
||||
InfluxDbOkHttpClientBuilderProvider builder,
|
||||
OkHttpClient.Builder deprecatedBuilder) {
|
||||
if (builder != null) {
|
||||
return builder.get();
|
||||
}
|
||||
else if (deprecatedBuilder != null) {
|
||||
logger.warn(
|
||||
"InfluxDB client customizations using a OkHttpClient.Builder is deprecated, register a "
|
||||
+ InfluxDbOkHttpClientBuilderProvider.class.getSimpleName()
|
||||
+ " bean instead");
|
||||
return deprecatedBuilder;
|
||||
}
|
||||
return new OkHttpClient.Builder();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2012-2018 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
|
||||
*
|
||||
* http://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.boot.autoconfigure.influx;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.influxdb.InfluxDB;
|
||||
|
||||
/**
|
||||
* Provide the {@link OkHttpClient.Builder} to use to customize the auto-configured
|
||||
* {@link InfluxDB} instance.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.1.0
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface InfluxDbOkHttpClientBuilderProvider
|
||||
extends Supplier<OkHttpClient.Builder> {
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2018 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.
|
||||
@@ -20,6 +20,7 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.influxdb.InfluxDB;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
@@ -27,6 +28,7 @@ import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.boot.test.rule.OutputCapture;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@@ -41,6 +43,9 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
public class InfluxDbAutoConfigurationTests {
|
||||
|
||||
@Rule
|
||||
public OutputCapture output = new OutputCapture();
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(InfluxDbAutoConfiguration.class));
|
||||
|
||||
@@ -71,25 +76,43 @@ public class InfluxDbAutoConfigurationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void influxDbWithoutCredentialsAndOkHttpClientBuilder() {
|
||||
public void influxDbWithOkHttpClientBuilderProvider() {
|
||||
this.contextRunner
|
||||
.withUserConfiguration(CustomOkHttpClientBuilderProviderConfig.class)
|
||||
.withPropertyValues("spring.influx.url=http://localhost")
|
||||
.run((context) -> {
|
||||
assertThat(context.getBeansOfType(InfluxDB.class)).hasSize(1);
|
||||
int readTimeout = getReadTimeoutProperty(context);
|
||||
assertThat(readTimeout).isEqualTo(40_000);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void influxDbWithOkHttpClientBuilderProviderIgnoreOkHttpClientBuilder() {
|
||||
this.contextRunner
|
||||
.withUserConfiguration(CustomOkHttpClientBuilderConfig.class,
|
||||
CustomOkHttpClientBuilderProviderConfig.class)
|
||||
.withPropertyValues("spring.influx.url=http://localhost")
|
||||
.run((context) -> {
|
||||
assertThat(context.getBeansOfType(InfluxDB.class)).hasSize(1);
|
||||
int readTimeout = getReadTimeoutProperty(context);
|
||||
assertThat(readTimeout).isEqualTo(40_000);
|
||||
assertThat(this.output.toString()).doesNotContain(
|
||||
"InfluxDB client customizations using a OkHttpClient.Builder is deprecated");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@Deprecated
|
||||
public void influxDbWithOkHttpClientBuilder() {
|
||||
this.contextRunner.withUserConfiguration(CustomOkHttpClientBuilderConfig.class)
|
||||
.withPropertyValues("spring.influx.url=http://localhost")
|
||||
.run((context) -> {
|
||||
assertThat(context.getBeansOfType(InfluxDB.class)).hasSize(1);
|
||||
int readTimeout = getReadTimeoutProperty(context);
|
||||
assertThat(readTimeout).isEqualTo(30_000);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void influxDbWithOkHttpClientBuilder() {
|
||||
this.contextRunner.withUserConfiguration(CustomOkHttpClientBuilderConfig.class)
|
||||
.withPropertyValues("spring.influx.url=http://localhost",
|
||||
"spring.influx.password:password", "spring.influx.user:user")
|
||||
.run((context) -> {
|
||||
assertThat(context.getBeansOfType(InfluxDB.class)).hasSize(1);
|
||||
int readTimeout = getReadTimeoutProperty(context);
|
||||
assertThat(readTimeout).isEqualTo(30_000);
|
||||
assertThat(this.output.toString()).contains(
|
||||
"InfluxDB client customizations using a OkHttpClient.Builder is deprecated");
|
||||
});
|
||||
}
|
||||
|
||||
@@ -102,6 +125,16 @@ public class InfluxDbAutoConfigurationTests {
|
||||
return callFactory.readTimeoutMillis();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class CustomOkHttpClientBuilderProviderConfig {
|
||||
|
||||
@Bean
|
||||
public InfluxDbOkHttpClientBuilderProvider influxDbOkHttpClientBuilderProvider() {
|
||||
return () -> new OkHttpClient.Builder().readTimeout(40, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class CustomOkHttpClientBuilderConfig {
|
||||
|
||||
|
||||
@@ -4829,7 +4829,7 @@ If the connection to InfluxDB requires a user and password, you can set the
|
||||
`spring.influx.user` and `spring.influx.password` properties accordingly.
|
||||
|
||||
InfluxDB relies on OkHttp. If you need to tune the http client `InfluxDB` uses behind the
|
||||
scenes, you can register an `OkHttpClient.Builder` bean.
|
||||
scenes, you can register an `InfluxDbOkHttpClientBuilderProvider` bean.
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user