Merge pull request #25319 from eddumelendez
* pr/25319: Polish "Add more customization options for InfluxDB" Add more customization options for InfluxDB Closes gh-25319
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* 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.
|
||||
@@ -45,10 +45,12 @@ public class InfluxDbAutoConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnProperty("spring.influx.url")
|
||||
public InfluxDB influxDb(InfluxDbProperties properties,
|
||||
ObjectProvider<InfluxDbOkHttpClientBuilderProvider> builder) {
|
||||
return new InfluxDBImpl(properties.getUrl(), properties.getUser(), properties.getPassword(),
|
||||
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) {
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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 org.influxdb.InfluxDB;
|
||||
|
||||
/**
|
||||
* Callback interface that can be implemented by beans wishing to further customize
|
||||
* {@code InfluxDB} whilst retaining default auto-configuration.
|
||||
*
|
||||
* @author Eddú Meléndez
|
||||
* @since 2.5.0
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface InfluxDbCustomizer {
|
||||
|
||||
/**
|
||||
* Customize the {@link InfluxDB}.
|
||||
* @param influxDB the influxDB instance to customize
|
||||
*/
|
||||
void customize(InfluxDB influxDB);
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* 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.
|
||||
@@ -46,21 +46,21 @@ class InfluxDbAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
void influxDbRequiresUrl() {
|
||||
this.contextRunner.run((context) -> assertThat(context.getBeansOfType(InfluxDB.class)).isEmpty());
|
||||
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(InfluxDB.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void influxDbCanBeCustomized() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.influx.url=http://localhost", "spring.influx.password:password",
|
||||
"spring.influx.user:user")
|
||||
.run(((context) -> assertThat(context.getBeansOfType(InfluxDB.class)).hasSize(1)));
|
||||
.withPropertyValues("spring.influx.url=http://localhost", "spring.influx.user=user",
|
||||
"spring.influx.password=password")
|
||||
.run((context) -> assertThat(context).hasSingleBean(InfluxDB.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void influxDbCanBeCreatedWithoutCredentials() {
|
||||
this.contextRunner.withPropertyValues("spring.influx.url=http://localhost").run((context) -> {
|
||||
assertThat(context.getBeansOfType(InfluxDB.class)).hasSize(1);
|
||||
assertThat(context).hasSingleBean(InfluxDB.class);
|
||||
int readTimeout = getReadTimeoutProperty(context);
|
||||
assertThat(readTimeout).isEqualTo(10_000);
|
||||
});
|
||||
@@ -70,12 +70,23 @@ class InfluxDbAutoConfigurationTests {
|
||||
void influxDbWithOkHttpClientBuilderProvider() {
|
||||
this.contextRunner.withUserConfiguration(CustomOkHttpClientBuilderProviderConfig.class)
|
||||
.withPropertyValues("spring.influx.url=http://localhost").run((context) -> {
|
||||
assertThat(context.getBeansOfType(InfluxDB.class)).hasSize(1);
|
||||
assertThat(context).hasSingleBean(InfluxDB.class);
|
||||
int readTimeout = getReadTimeoutProperty(context);
|
||||
assertThat(readTimeout).isEqualTo(40_000);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void influxDbWithCustomizer() {
|
||||
this.contextRunner.withBean(InfluxDbCustomizer.class, () -> (influxDb) -> influxDb.setDatabase("test"))
|
||||
.withPropertyValues("spring.influx.url=http://localhost", "spring.influx.database=sample-db")
|
||||
.run((context) -> {
|
||||
assertThat(context).hasSingleBean(InfluxDB.class);
|
||||
InfluxDB influxDb = context.getBean(InfluxDB.class);
|
||||
assertThat(influxDb).hasFieldOrPropertyWithValue("database", "test");
|
||||
});
|
||||
}
|
||||
|
||||
private int getReadTimeoutProperty(AssertableApplicationContext context) {
|
||||
InfluxDB influxDB = context.getBean(InfluxDB.class);
|
||||
Retrofit retrofit = (Retrofit) ReflectionTestUtils.getField(influxDB, "retrofit");
|
||||
|
||||
@@ -4922,6 +4922,8 @@ If the connection to InfluxDB requires a user and password, you can set the `spr
|
||||
InfluxDB relies on OkHttp.
|
||||
If you need to tune the http client `InfluxDB` uses behind the scenes, you can register an `InfluxDbOkHttpClientBuilderProvider` bean.
|
||||
|
||||
If you need more control over the configuration, consider registering a `InfluxDbCustomizer` bean.
|
||||
|
||||
|
||||
|
||||
[[boot-features-caching]]
|
||||
|
||||
Reference in New Issue
Block a user