Improve Couchbase auto-configuration
This commit improves the couchbase auto-configuration so that it is easier to customize the way the connection to the couchbase server is initiated. See gh-11146
This commit is contained in:
parent
66872a0f96
commit
b090280b0c
|
@ -16,31 +16,18 @@
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.couchbase;
|
package org.springframework.boot.autoconfigure.couchbase;
|
||||||
|
|
||||||
import java.util.function.BiFunction;
|
|
||||||
|
|
||||||
import com.couchbase.client.core.env.KeyValueServiceConfig;
|
|
||||||
import com.couchbase.client.core.env.QueryServiceConfig;
|
|
||||||
import com.couchbase.client.core.env.ViewServiceConfig;
|
|
||||||
import com.couchbase.client.java.Bucket;
|
|
||||||
import com.couchbase.client.java.Cluster;
|
import com.couchbase.client.java.Cluster;
|
||||||
import com.couchbase.client.java.CouchbaseBucket;
|
import com.couchbase.client.java.CouchbaseBucket;
|
||||||
import com.couchbase.client.java.CouchbaseCluster;
|
|
||||||
import com.couchbase.client.java.cluster.ClusterInfo;
|
|
||||||
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
|
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
|
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.autoconfigure.couchbase.CouchbaseProperties.Endpoints;
|
|
||||||
import org.springframework.boot.autoconfigure.couchbase.CouchbaseProperties.Endpoints.CouchbaseService;
|
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Conditional;
|
import org.springframework.context.annotation.Conditional;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.DependsOn;
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.context.annotation.Primary;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link EnableAutoConfiguration Auto-configuration} for Couchbase.
|
* {@link EnableAutoConfiguration Auto-configuration} for Couchbase.
|
||||||
|
@ -58,109 +45,8 @@ public class CouchbaseAutoConfiguration {
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnMissingBean(value = CouchbaseConfiguration.class, type = "org.springframework.data.couchbase.config.CouchbaseConfigurer")
|
@ConditionalOnMissingBean(value = CouchbaseConfiguration.class, type = "org.springframework.data.couchbase.config.CouchbaseConfigurer")
|
||||||
public static class CouchbaseConfiguration {
|
@Import(CouchbaseConfiguration.class)
|
||||||
|
static class DefaultCouchbaseConfiguration {
|
||||||
private final CouchbaseProperties properties;
|
|
||||||
|
|
||||||
public CouchbaseConfiguration(CouchbaseProperties properties) {
|
|
||||||
this.properties = properties;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@Primary
|
|
||||||
public DefaultCouchbaseEnvironment couchbaseEnvironment() throws Exception {
|
|
||||||
return initializeEnvironmentBuilder(this.properties).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@Primary
|
|
||||||
public Cluster couchbaseCluster() throws Exception {
|
|
||||||
return CouchbaseCluster.create(couchbaseEnvironment(),
|
|
||||||
this.properties.getBootstrapHosts());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@Primary
|
|
||||||
@DependsOn("couchbaseClient")
|
|
||||||
public ClusterInfo couchbaseClusterInfo() throws Exception {
|
|
||||||
return couchbaseCluster()
|
|
||||||
.clusterManager(this.properties.getBucket().getName(),
|
|
||||||
this.properties.getBucket().getPassword())
|
|
||||||
.info();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@Primary
|
|
||||||
public Bucket couchbaseClient() throws Exception {
|
|
||||||
return couchbaseCluster().openBucket(this.properties.getBucket().getName(),
|
|
||||||
this.properties.getBucket().getPassword());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize an environment builder based on the specified settings.
|
|
||||||
* @param properties the couchbase properties to use
|
|
||||||
* @return the {@link DefaultCouchbaseEnvironment} builder.
|
|
||||||
*/
|
|
||||||
protected DefaultCouchbaseEnvironment.Builder initializeEnvironmentBuilder(
|
|
||||||
CouchbaseProperties properties) {
|
|
||||||
CouchbaseProperties.Endpoints endpoints = properties.getEnv().getEndpoints();
|
|
||||||
CouchbaseProperties.Timeouts timeouts = properties.getEnv().getTimeouts();
|
|
||||||
DefaultCouchbaseEnvironment.Builder builder = DefaultCouchbaseEnvironment
|
|
||||||
.builder();
|
|
||||||
if (timeouts.getConnect() != null) {
|
|
||||||
builder = builder.connectTimeout(timeouts.getConnect().toMillis());
|
|
||||||
}
|
|
||||||
builder = builder.keyValueServiceConfig(
|
|
||||||
KeyValueServiceConfig.create(endpoints.getKeyValue()));
|
|
||||||
if (timeouts.getKeyValue() != null) {
|
|
||||||
builder = builder.kvTimeout(timeouts.getKeyValue().toMillis());
|
|
||||||
}
|
|
||||||
if (timeouts.getQuery() != null) {
|
|
||||||
builder = builder.queryTimeout(timeouts.getQuery().toMillis());
|
|
||||||
builder = builder.queryServiceConfig(getQueryServiceConfig(endpoints));
|
|
||||||
builder = builder.viewServiceConfig(getViewServiceConfig(endpoints));
|
|
||||||
}
|
|
||||||
if (timeouts.getSocketConnect() != null) {
|
|
||||||
builder = builder.socketConnectTimeout(
|
|
||||||
(int) timeouts.getSocketConnect().toMillis());
|
|
||||||
}
|
|
||||||
if (timeouts.getView() != null) {
|
|
||||||
builder = builder.viewTimeout(timeouts.getView().toMillis());
|
|
||||||
}
|
|
||||||
CouchbaseProperties.Ssl ssl = properties.getEnv().getSsl();
|
|
||||||
if (ssl.getEnabled()) {
|
|
||||||
builder = builder.sslEnabled(true);
|
|
||||||
if (ssl.getKeyStore() != null) {
|
|
||||||
builder = builder.sslKeystoreFile(ssl.getKeyStore());
|
|
||||||
}
|
|
||||||
if (ssl.getKeyStorePassword() != null) {
|
|
||||||
builder = builder.sslKeystorePassword(ssl.getKeyStorePassword());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return builder;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
private QueryServiceConfig getQueryServiceConfig(Endpoints endpoints) {
|
|
||||||
return getServiceConfig(endpoints.getQueryservice(), endpoints.getQuery(),
|
|
||||||
QueryServiceConfig::create);
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
private ViewServiceConfig getViewServiceConfig(Endpoints endpoints) {
|
|
||||||
return getServiceConfig(endpoints.getViewservice(), endpoints.getView(),
|
|
||||||
ViewServiceConfig::create);
|
|
||||||
}
|
|
||||||
|
|
||||||
private <T> T getServiceConfig(CouchbaseService service, Integer fallback,
|
|
||||||
BiFunction<Integer, Integer, T> factory) {
|
|
||||||
if (service.getMinEndpoints() != 1 || service.getMaxEndpoints() != 1) {
|
|
||||||
return factory.apply(service.getMinEndpoints(),
|
|
||||||
service.getMaxEndpoints());
|
|
||||||
}
|
|
||||||
int endpoints = (fallback != null ? fallback : 1);
|
|
||||||
return factory.apply(endpoints, endpoints);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,157 @@
|
||||||
|
/*
|
||||||
|
* 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.couchbase;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
|
import com.couchbase.client.core.env.KeyValueServiceConfig;
|
||||||
|
import com.couchbase.client.core.env.QueryServiceConfig;
|
||||||
|
import com.couchbase.client.core.env.ViewServiceConfig;
|
||||||
|
import com.couchbase.client.java.Bucket;
|
||||||
|
import com.couchbase.client.java.Cluster;
|
||||||
|
import com.couchbase.client.java.CouchbaseCluster;
|
||||||
|
import com.couchbase.client.java.cluster.ClusterInfo;
|
||||||
|
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.couchbase.CouchbaseProperties.Endpoints;
|
||||||
|
import org.springframework.boot.autoconfigure.couchbase.CouchbaseProperties.Endpoints.CouchbaseService;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.DependsOn;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Support class to configure Couchbase based on {@link CouchbaseProperties}.
|
||||||
|
*
|
||||||
|
* @author Stephane Nicoll
|
||||||
|
* @since 2.1.0
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class CouchbaseConfiguration {
|
||||||
|
|
||||||
|
private final CouchbaseProperties properties;
|
||||||
|
|
||||||
|
public CouchbaseConfiguration(CouchbaseProperties properties) {
|
||||||
|
this.properties = properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Primary
|
||||||
|
public DefaultCouchbaseEnvironment couchbaseEnvironment() {
|
||||||
|
return initializeEnvironmentBuilder(this.properties).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Primary
|
||||||
|
public Cluster couchbaseCluster() {
|
||||||
|
return CouchbaseCluster.create(couchbaseEnvironment(),
|
||||||
|
determineBootstrapHosts());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine the Couchbase nodes to bootstrap from.
|
||||||
|
* @return the Couchbase nodes to bootstrap from
|
||||||
|
*/
|
||||||
|
protected List<String> determineBootstrapHosts() {
|
||||||
|
return this.properties.getBootstrapHosts();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Primary
|
||||||
|
@DependsOn("couchbaseClient")
|
||||||
|
public ClusterInfo couchbaseClusterInfo() {
|
||||||
|
return couchbaseCluster()
|
||||||
|
.clusterManager(this.properties.getBucket().getName(),
|
||||||
|
this.properties.getBucket().getPassword())
|
||||||
|
.info();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Primary
|
||||||
|
public Bucket couchbaseClient() {
|
||||||
|
return couchbaseCluster().openBucket(this.properties.getBucket().getName(),
|
||||||
|
this.properties.getBucket().getPassword());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize an environment builder based on the specified settings.
|
||||||
|
* @param properties the couchbase properties to use
|
||||||
|
* @return the {@link DefaultCouchbaseEnvironment} builder.
|
||||||
|
*/
|
||||||
|
protected DefaultCouchbaseEnvironment.Builder initializeEnvironmentBuilder(
|
||||||
|
CouchbaseProperties properties) {
|
||||||
|
CouchbaseProperties.Endpoints endpoints = properties.getEnv().getEndpoints();
|
||||||
|
CouchbaseProperties.Timeouts timeouts = properties.getEnv().getTimeouts();
|
||||||
|
DefaultCouchbaseEnvironment.Builder builder = DefaultCouchbaseEnvironment
|
||||||
|
.builder();
|
||||||
|
if (timeouts.getConnect() != null) {
|
||||||
|
builder = builder.connectTimeout(timeouts.getConnect().toMillis());
|
||||||
|
}
|
||||||
|
builder = builder.keyValueServiceConfig(
|
||||||
|
KeyValueServiceConfig.create(endpoints.getKeyValue()));
|
||||||
|
if (timeouts.getKeyValue() != null) {
|
||||||
|
builder = builder.kvTimeout(timeouts.getKeyValue().toMillis());
|
||||||
|
}
|
||||||
|
if (timeouts.getQuery() != null) {
|
||||||
|
builder = builder.queryTimeout(timeouts.getQuery().toMillis());
|
||||||
|
builder = builder.queryServiceConfig(getQueryServiceConfig(endpoints));
|
||||||
|
builder = builder.viewServiceConfig(getViewServiceConfig(endpoints));
|
||||||
|
}
|
||||||
|
if (timeouts.getSocketConnect() != null) {
|
||||||
|
builder = builder.socketConnectTimeout(
|
||||||
|
(int) timeouts.getSocketConnect().toMillis());
|
||||||
|
}
|
||||||
|
if (timeouts.getView() != null) {
|
||||||
|
builder = builder.viewTimeout(timeouts.getView().toMillis());
|
||||||
|
}
|
||||||
|
CouchbaseProperties.Ssl ssl = properties.getEnv().getSsl();
|
||||||
|
if (ssl.getEnabled()) {
|
||||||
|
builder = builder.sslEnabled(true);
|
||||||
|
if (ssl.getKeyStore() != null) {
|
||||||
|
builder = builder.sslKeystoreFile(ssl.getKeyStore());
|
||||||
|
}
|
||||||
|
if (ssl.getKeyStorePassword() != null) {
|
||||||
|
builder = builder.sslKeystorePassword(ssl.getKeyStorePassword());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
private QueryServiceConfig getQueryServiceConfig(Endpoints endpoints) {
|
||||||
|
return getServiceConfig(endpoints.getQueryservice(), endpoints.getQuery(),
|
||||||
|
QueryServiceConfig::create);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
private ViewServiceConfig getViewServiceConfig(Endpoints endpoints) {
|
||||||
|
return getServiceConfig(endpoints.getViewservice(), endpoints.getView(),
|
||||||
|
ViewServiceConfig::create);
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> T getServiceConfig(CouchbaseService service, Integer fallback,
|
||||||
|
BiFunction<Integer, Integer, T> factory) {
|
||||||
|
if (service.getMinEndpoints() != 1 || service.getMaxEndpoints() != 1) {
|
||||||
|
return factory.apply(service.getMinEndpoints(),
|
||||||
|
service.getMaxEndpoints());
|
||||||
|
}
|
||||||
|
int endpoints = (fallback != null ? fallback : 1);
|
||||||
|
return factory.apply(endpoints, endpoints);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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");
|
* 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.
|
||||||
|
@ -19,7 +19,7 @@ package org.springframework.boot.autoconfigure.data.couchbase;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration.CouchbaseConfiguration;
|
import org.springframework.boot.autoconfigure.couchbase.CouchbaseConfiguration;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.data.couchbase.config.CouchbaseConfigurer;
|
import org.springframework.data.couchbase.config.CouchbaseConfigurer;
|
||||||
|
|
|
@ -28,12 +28,9 @@ import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration.CouchbaseConfiguration;
|
|
||||||
import org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration;
|
|
||||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Import;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
@ -203,7 +200,6 @@ public class CouchbaseAutoConfigurationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@Import(CouchbaseDataAutoConfiguration.class)
|
|
||||||
static class CustomCouchbaseConfiguration extends CouchbaseConfiguration {
|
static class CustomCouchbaseConfiguration extends CouchbaseConfiguration {
|
||||||
|
|
||||||
CustomCouchbaseConfiguration(CouchbaseProperties properties) {
|
CustomCouchbaseConfiguration(CouchbaseProperties properties) {
|
||||||
|
|
Loading…
Reference in New Issue