diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfiguration.java index 15b68cf7e79..65a2fb8b8bd 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfiguration.java @@ -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. @@ -16,6 +16,9 @@ package org.springframework.boot.autoconfigure.couchbase; +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.CouchbaseBucket; @@ -40,6 +43,7 @@ import org.springframework.context.annotation.Primary; * * @author Eddú Meléndez * @author Stephane Nicoll + * @author Yulin Qin * @since 1.4.0 */ @Configuration @@ -102,14 +106,21 @@ public class CouchbaseAutoConfiguration { if (timeouts.getConnect() != null) { builder = builder.connectTimeout(timeouts.getConnect().toMillis()); } - builder = builder.kvEndpoints(endpoints.getKeyValue()); + builder = builder.keyValueServiceConfig(KeyValueServiceConfig.create( + endpoints.getKeyValue())); if (timeouts.getKeyValue() != null) { builder = builder.kvTimeout(timeouts.getKeyValue().toMillis()); } - builder = builder.queryEndpoints(endpoints.getQuery()); + CouchbaseServiceConfig queryConfig = determineCouchbaseServiceConfig( + endpoints.getQueryservice(), endpoints.getQuery()); + builder = builder.queryServiceConfig(QueryServiceConfig.create( + queryConfig.minEndpoints, queryConfig.maxEndpoints)); if (timeouts.getQuery() != null) { + CouchbaseServiceConfig viewConfig = determineCouchbaseServiceConfig( + endpoints.getViewservice(), endpoints.getView()); builder = builder.queryTimeout(timeouts.getQuery().toMillis()) - .viewEndpoints(endpoints.getView()); + .viewServiceConfig(ViewServiceConfig.create( + viewConfig.minEndpoints, viewConfig.maxEndpoints)); } if (timeouts.getSocketConnect() != null) { builder = builder.socketConnectTimeout( @@ -131,6 +142,28 @@ public class CouchbaseAutoConfiguration { return builder; } + private CouchbaseServiceConfig determineCouchbaseServiceConfig( + CouchbaseProperties.Endpoints.CouchbaseService couchbaseService, + Integer fallback) { + if (couchbaseService.getMinEndpoints() != 1 + || couchbaseService.getMaxEndpoints() != 1) { + return new CouchbaseServiceConfig(couchbaseService.getMinEndpoints(), + couchbaseService.getMaxEndpoints()); + } + int endpoints = (fallback != null ? fallback : 1); + return new CouchbaseServiceConfig(endpoints, endpoints); + } + + private static class CouchbaseServiceConfig { + private int minEndpoints; + private int maxEndpoints; + + CouchbaseServiceConfig(int minEndpoints, int maxEndpoints) { + this.minEndpoints = minEndpoints; + this.maxEndpoints = maxEndpoints; + } + } + } /** diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseProperties.java index 31599aa382a..f3ddc31beeb 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseProperties.java @@ -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.time.Duration; import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.DeprecatedConfigurationProperty; import org.springframework.util.StringUtils; /** @@ -27,6 +28,7 @@ import org.springframework.util.StringUtils; * * @author Eddú Meléndez * @author Stephane Nicoll + * @author Yulin Qin * @since 1.4.0 */ @ConfigurationProperties(prefix = "spring.couchbase") @@ -116,42 +118,98 @@ public class CouchbaseProperties { */ private int keyValue = 1; + /** + * Query (N1QL) service configuration. + */ + private final CouchbaseService queryservice = new CouchbaseService(); + + /** + * View service configuration. + */ + private final CouchbaseService viewservice = new CouchbaseService(); + /** * Number of sockets per node against the query (N1QL) service. */ - private int query = 1; + private Integer query; /** * Number of sockets per node against the view service. */ - private int view = 1; + private Integer view; public int getKeyValue() { return this.keyValue; } + @Deprecated public void setKeyValue(int keyValue) { this.keyValue = keyValue; } - public int getQuery() { + @Deprecated + @DeprecatedConfigurationProperty(replacement = "spring.couchbase.env.endpoints.queryservice.max-endpoints") + public Integer getQuery() { return this.query; } - public void setQuery(int query) { + @Deprecated + public void setQuery(Integer query) { this.query = query; } - public int getView() { + public CouchbaseService getQueryservice() { + return this.queryservice; + } + + @Deprecated + @DeprecatedConfigurationProperty(replacement = "spring.couchbase.env.endpoints.viewservice.max-endpoints") + public Integer getView() { return this.view; } - public void setView(int view) { + @Deprecated + public void setView(Integer view) { this.view = view; } + public CouchbaseService getViewservice() { + return this.viewservice; + } + + public static class CouchbaseService { + + /** + * Minimum number of sockets per node. + */ + private int minEndpoints = 1; + + /** + * Maximum number of sockets per node. + */ + private int maxEndpoints = 1; + + public int getMinEndpoints() { + return this.minEndpoints; + } + + public void setMinEndpoints(int minEndpoints) { + this.minEndpoints = minEndpoints; + } + + public int getMaxEndpoints() { + return this.maxEndpoints; + } + + public void setMaxEndpoints(int maxEndpoints) { + this.maxEndpoints = maxEndpoints; + } + + } + } + public static class Ssl { /** diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfigurationTests.java index eb30f3ff8a7..44b16593393 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfigurationTests.java @@ -86,12 +86,57 @@ public class CouchbaseAutoConfigurationTests { @Test public void customizeEnvEndpoints() { testCouchbaseEnv((env) -> { - assertThat(env.kvEndpoints()).isEqualTo(4); - assertThat(env.queryEndpoints()).isEqualTo(5); - assertThat(env.viewEndpoints()).isEqualTo(6); - }, "spring.couchbase.env.endpoints.keyValue=4", - "spring.couchbase.env.endpoints.query=5", - "spring.couchbase.env.endpoints.view=6"); + assertThat(env.kvServiceConfig().minEndpoints()).isEqualTo(2); + assertThat(env.kvServiceConfig().maxEndpoints()).isEqualTo(2); + assertThat(env.queryServiceConfig().minEndpoints()).isEqualTo(3); + assertThat(env.queryServiceConfig().maxEndpoints()).isEqualTo(5); + assertThat(env.viewServiceConfig().minEndpoints()).isEqualTo(4); + assertThat(env.viewServiceConfig().maxEndpoints()).isEqualTo(6); + }, "spring.couchbase.env.endpoints.key-value=2", + "spring.couchbase.env.endpoints.queryservice.min-endpoints=3", + "spring.couchbase.env.endpoints.queryservice.max-endpoints=5", + "spring.couchbase.env.endpoints.viewservice.min-endpoints=4", + "spring.couchbase.env.endpoints.viewservice.max-endpoints=6"); + } + + @Test + @Deprecated + public void customizeEnvEndpointsWithDeprecatedProperties() { + testCouchbaseEnv((env) -> { + assertThat(env.queryServiceConfig().minEndpoints()).isEqualTo(3); + assertThat(env.queryServiceConfig().maxEndpoints()).isEqualTo(3); + assertThat(env.viewServiceConfig().minEndpoints()).isEqualTo(4); + assertThat(env.viewServiceConfig().maxEndpoints()).isEqualTo(4); + }, "spring.couchbase.env.endpoints.query=3", + "spring.couchbase.env.endpoints.view=4"); + } + + @Test + public void customizeEnvEndpointsUsesNewInfrastructure() { + testCouchbaseEnv((env) -> { + assertThat(env.queryServiceConfig().minEndpoints()).isEqualTo(3); + assertThat(env.queryServiceConfig().maxEndpoints()).isEqualTo(5); + assertThat(env.viewServiceConfig().minEndpoints()).isEqualTo(4); + assertThat(env.viewServiceConfig().maxEndpoints()).isEqualTo(6); + }, "spring.couchbase.env.endpoints.query=33", + "spring.couchbase.env.endpoints.queryservice.min-endpoints=3", + "spring.couchbase.env.endpoints.queryservice.max-endpoints=5", + "spring.couchbase.env.endpoints.view=44", + "spring.couchbase.env.endpoints.viewservice.min-endpoints=4", + "spring.couchbase.env.endpoints.viewservice.max-endpoints=6"); + } + + @Test + public void customizeEnvEndpointsUsesNewInfrastructureWithOnlyMax() { + testCouchbaseEnv((env) -> { + assertThat(env.queryServiceConfig().minEndpoints()).isEqualTo(1); + assertThat(env.queryServiceConfig().maxEndpoints()).isEqualTo(5); + assertThat(env.viewServiceConfig().minEndpoints()).isEqualTo(1); + assertThat(env.viewServiceConfig().maxEndpoints()).isEqualTo(6); + }, "spring.couchbase.env.endpoints.query=33", + "spring.couchbase.env.endpoints.queryservice.max-endpoints=5", + "spring.couchbase.env.endpoints.view=44", + "spring.couchbase.env.endpoints.viewservice.max-endpoints=6"); } @Test diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 3551095503f..77998bfcc21 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -573,8 +573,10 @@ content into your application. Rather, pick only the properties that you need. spring.couchbase.bucket.name=default # Name of the bucket to connect to. spring.couchbase.bucket.password= # Password of the bucket. spring.couchbase.env.endpoints.key-value=1 # Number of sockets per node against the key/value service. - spring.couchbase.env.endpoints.query=1 # Number of sockets per node against the query (N1QL) service. - spring.couchbase.env.endpoints.view=1 # Number of sockets per node against the view service. + spring.couchbase.env.endpoints.queryservice.min-endpoints=1 # Minimum number of sockets per node. + spring.couchbase.env.endpoints.queryservice.max-endpoints=1 # Maximum number of sockets per node. + spring.couchbase.env.endpoints.viewservice.min-endpoints=1 # Minimum number of sockets per node. + spring.couchbase.env.endpoints.viewservice.max-endpoints=1 # Maximum number of sockets per node. spring.couchbase.env.ssl.enabled= # Whether to enable SSL support. Enabled automatically if a "keyStore" is provided unless specified otherwise. spring.couchbase.env.ssl.key-store= # Path to the JVM key store that holds the certificates. spring.couchbase.env.ssl.key-store-password= # Password used to access the key store.