Commit 867367b0 authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #25319 from eddumelendez

* pr/25319:
  Polish "Add more customization options for InfluxDB"
  Add more customization options for InfluxDB

Closes gh-25319
parents 89555a87 d9dcfeba
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -45,10 +45,12 @@ public class InfluxDbAutoConfiguration { ...@@ -45,10 +45,12 @@ public class InfluxDbAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
@ConditionalOnProperty("spring.influx.url") @ConditionalOnProperty("spring.influx.url")
public InfluxDB influxDb(InfluxDbProperties properties, public InfluxDB influxDb(InfluxDbProperties properties, ObjectProvider<InfluxDbOkHttpClientBuilderProvider> builder,
ObjectProvider<InfluxDbOkHttpClientBuilderProvider> builder) { ObjectProvider<InfluxDbCustomizer> customizers) {
return new InfluxDBImpl(properties.getUrl(), properties.getUser(), properties.getPassword(), InfluxDB influxDb = new InfluxDBImpl(properties.getUrl(), properties.getUser(), properties.getPassword(),
determineBuilder(builder.getIfAvailable())); determineBuilder(builder.getIfAvailable()));
customizers.orderedStream().forEach((customizer) -> customizer.customize(influxDb));
return influxDb;
} }
private static OkHttpClient.Builder determineBuilder(InfluxDbOkHttpClientBuilderProvider builder) { private static OkHttpClient.Builder determineBuilder(InfluxDbOkHttpClientBuilderProvider builder) {
......
/*
* 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);
}
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -46,21 +46,21 @@ class InfluxDbAutoConfigurationTests { ...@@ -46,21 +46,21 @@ class InfluxDbAutoConfigurationTests {
@Test @Test
void influxDbRequiresUrl() { void influxDbRequiresUrl() {
this.contextRunner.run((context) -> assertThat(context.getBeansOfType(InfluxDB.class)).isEmpty()); this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(InfluxDB.class));
} }
@Test @Test
void influxDbCanBeCustomized() { void influxDbCanBeCustomized() {
this.contextRunner this.contextRunner
.withPropertyValues("spring.influx.url=http://localhost", "spring.influx.password:password", .withPropertyValues("spring.influx.url=http://localhost", "spring.influx.user=user",
"spring.influx.user:user") "spring.influx.password=password")
.run(((context) -> assertThat(context.getBeansOfType(InfluxDB.class)).hasSize(1))); .run((context) -> assertThat(context).hasSingleBean(InfluxDB.class));
} }
@Test @Test
void influxDbCanBeCreatedWithoutCredentials() { void influxDbCanBeCreatedWithoutCredentials() {
this.contextRunner.withPropertyValues("spring.influx.url=http://localhost").run((context) -> { 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); int readTimeout = getReadTimeoutProperty(context);
assertThat(readTimeout).isEqualTo(10_000); assertThat(readTimeout).isEqualTo(10_000);
}); });
...@@ -70,12 +70,23 @@ class InfluxDbAutoConfigurationTests { ...@@ -70,12 +70,23 @@ class InfluxDbAutoConfigurationTests {
void influxDbWithOkHttpClientBuilderProvider() { void influxDbWithOkHttpClientBuilderProvider() {
this.contextRunner.withUserConfiguration(CustomOkHttpClientBuilderProviderConfig.class) this.contextRunner.withUserConfiguration(CustomOkHttpClientBuilderProviderConfig.class)
.withPropertyValues("spring.influx.url=http://localhost").run((context) -> { .withPropertyValues("spring.influx.url=http://localhost").run((context) -> {
assertThat(context.getBeansOfType(InfluxDB.class)).hasSize(1); assertThat(context).hasSingleBean(InfluxDB.class);
int readTimeout = getReadTimeoutProperty(context); int readTimeout = getReadTimeoutProperty(context);
assertThat(readTimeout).isEqualTo(40_000); 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) { private int getReadTimeoutProperty(AssertableApplicationContext context) {
InfluxDB influxDB = context.getBean(InfluxDB.class); InfluxDB influxDB = context.getBean(InfluxDB.class);
Retrofit retrofit = (Retrofit) ReflectionTestUtils.getField(influxDB, "retrofit"); 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 ...@@ -4922,6 +4922,8 @@ If the connection to InfluxDB requires a user and password, you can set the `spr
InfluxDB relies on OkHttp. 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 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]] [[boot-features-caching]]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment