Add support for InfluxDB 2.x
Closes gh-25891
This commit is contained in:
@@ -16,6 +16,10 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.influx;
|
||||
|
||||
import com.influxdb.client.InfluxDBClient;
|
||||
import com.influxdb.client.InfluxDBClientFactory;
|
||||
import com.influxdb.client.InfluxDBClientOptions;
|
||||
import com.influxdb.client.InfluxDBClientOptions.Builder;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.influxdb.InfluxDB;
|
||||
import org.influxdb.impl.InfluxDBImpl;
|
||||
@@ -28,6 +32,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for InfluxDB.
|
||||
@@ -38,21 +43,10 @@ import org.springframework.context.annotation.Configuration;
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(InfluxDB.class)
|
||||
@ConditionalOnProperty("spring.influx.url")
|
||||
@EnableConfigurationProperties(InfluxDbProperties.class)
|
||||
public class InfluxDbAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnProperty("spring.influx.url")
|
||||
public InfluxDB influxDb(InfluxDbProperties properties, ObjectProvider<InfluxDbOkHttpClientBuilderProvider> builder,
|
||||
ObjectProvider<InfluxDbCustomizer> customizers) {
|
||||
InfluxDB influxDb = new InfluxDBImpl(properties.getUrl(), properties.getUser(), properties.getPassword(),
|
||||
determineBuilder(builder.getIfAvailable()));
|
||||
customizers.orderedStream().forEach((customizer) -> customizer.customize(influxDb));
|
||||
return influxDb;
|
||||
}
|
||||
|
||||
private static OkHttpClient.Builder determineBuilder(InfluxDbOkHttpClientBuilderProvider builder) {
|
||||
if (builder != null) {
|
||||
return builder.get();
|
||||
@@ -60,4 +54,40 @@ public class InfluxDbAutoConfiguration {
|
||||
return new OkHttpClient.Builder();
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(InfluxDB.class)
|
||||
static class Influx1xConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
InfluxDB influxDb(InfluxDbProperties properties, ObjectProvider<InfluxDbOkHttpClientBuilderProvider> builder,
|
||||
ObjectProvider<InfluxDbCustomizer> customizers) {
|
||||
InfluxDB influxDb = new InfluxDBImpl(properties.getUrl(), properties.getUser(), properties.getPassword(),
|
||||
determineBuilder(builder.getIfAvailable()));
|
||||
customizers.orderedStream().forEach((customizer) -> customizer.customize(influxDb));
|
||||
return influxDb;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(InfluxDBClient.class)
|
||||
static class Influx2xConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
InfluxDBClient influxDbClient(InfluxDbProperties properties,
|
||||
ObjectProvider<InfluxDbOkHttpClientBuilderProvider> httpClientBuilder,
|
||||
ObjectProvider<InfluxDbClientOptionsBuilderCustomizer> customizers) {
|
||||
Builder builder = InfluxDBClientOptions.builder().url(properties.getUrl());
|
||||
if (StringUtils.hasText(properties.getUser()) && StringUtils.hasText(properties.getPassword())) {
|
||||
builder.authenticate(properties.getUser(), properties.getPassword().toCharArray());
|
||||
}
|
||||
builder.okHttpClient(determineBuilder(httpClientBuilder.getIfAvailable()));
|
||||
customizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
|
||||
return InfluxDBClientFactory.create(builder.build());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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.boot.autoconfigure.influx;
|
||||
|
||||
import com.influxdb.client.InfluxDBClient;
|
||||
import com.influxdb.client.InfluxDBClientOptions;
|
||||
|
||||
/**
|
||||
* Callback interface that can be implemented by beans wishing to further customize
|
||||
* {@link InfluxDBClientOptions} used to configure an {@link InfluxDBClient} whilst
|
||||
* retaining default auto-configuration.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.6.0
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface InfluxDbClientOptionsBuilderCustomizer {
|
||||
|
||||
/**
|
||||
* Customize the {@link InfluxDBClientOptions}.
|
||||
* @param builder the influxDB client options builder to customize
|
||||
*/
|
||||
void customize(InfluxDBClientOptions.Builder builder);
|
||||
|
||||
}
|
||||
@@ -17,8 +17,12 @@
|
||||
package org.springframework.boot.autoconfigure.influx;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import com.influxdb.client.InfluxDBClient;
|
||||
import com.influxdb.client.InfluxDBClientOptions;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.assertj.core.api.InstanceOfAssertFactories;
|
||||
import org.influxdb.InfluxDB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import retrofit2.Retrofit;
|
||||
@@ -26,6 +30,7 @@ import retrofit2.Retrofit;
|
||||
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.context.runner.ContextConsumer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
@@ -86,6 +91,42 @@ class InfluxDbAutoConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void influxDbClientRequiresUrl() {
|
||||
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(InfluxDBClient.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void influxDbClientCanBeCustomized() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.influx.url=http://localhost", "spring.influx.user=user",
|
||||
"spring.influx.password=password")
|
||||
.run((context) -> assertThat(context).hasSingleBean(InfluxDBClient.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void influxDbClientCanBeCreatedWithoutCredentials() {
|
||||
this.contextRunner.withPropertyValues("spring.influx.url=http://localhost").run(assertInfluxDbClientOptions(
|
||||
(options) -> assertThat(options.getOkHttpClient().build().readTimeoutMillis()).isEqualTo(10000)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void influxDbClientWithOkHttpClientBuilderProvider() {
|
||||
this.contextRunner.withUserConfiguration(CustomOkHttpClientBuilderProviderConfig.class)
|
||||
.withPropertyValues("spring.influx.url=http://localhost")
|
||||
.run(assertInfluxDbClientOptions(
|
||||
(options) -> assertThat(options.getOkHttpClient().build().readTimeoutMillis())
|
||||
.isEqualTo(40000)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void influxDbClientWithCustomizer() {
|
||||
this.contextRunner
|
||||
.withBean(InfluxDbClientOptionsBuilderCustomizer.class, () -> (options) -> options.org("my_org"))
|
||||
.withPropertyValues("spring.influx.url=http://localhost")
|
||||
.run(assertInfluxDbClientOptions((options) -> assertThat(options.getOrg()).isEqualTo("my_org")));
|
||||
}
|
||||
|
||||
private int getReadTimeoutProperty(AssertableApplicationContext context) {
|
||||
InfluxDB influxDb = context.getBean(InfluxDB.class);
|
||||
Retrofit retrofit = (Retrofit) ReflectionTestUtils.getField(influxDb, "retrofit");
|
||||
@@ -93,6 +134,16 @@ class InfluxDbAutoConfigurationTests {
|
||||
return callFactory.readTimeoutMillis();
|
||||
}
|
||||
|
||||
private ContextConsumer<AssertableApplicationContext> assertInfluxDbClientOptions(
|
||||
Consumer<InfluxDBClientOptions> options) {
|
||||
return (context) -> {
|
||||
assertThat(context).hasSingleBean(InfluxDBClient.class);
|
||||
assertThat(context).getBean(InfluxDBClient.class)
|
||||
.extracting("options", InstanceOfAssertFactories.type(InfluxDBClientOptions.class))
|
||||
.satisfies(options);
|
||||
};
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class CustomOkHttpClientBuilderProviderConfig {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user