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 637b3878e9..65a2fb8b8b 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. @@ -106,18 +106,21 @@ public class CouchbaseAutoConfiguration { if (timeouts.getConnect() != null) { builder = builder.connectTimeout(timeouts.getConnect().toMillis()); } - builder = builder.keyValueServiceConfig(KeyValueServiceConfig.create(endpoints.getKeyValue())); + builder = builder.keyValueServiceConfig(KeyValueServiceConfig.create( + endpoints.getKeyValue())); if (timeouts.getKeyValue() != null) { builder = builder.kvTimeout(timeouts.getKeyValue().toMillis()); } - int minQuery = endpoints.getQuery() != 1 ? endpoints.getQuery() : endpoints.getQueryservice().getMinEndpoints(); - int maxQuery = endpoints.getQuery() != 1 ? endpoints.getQuery() : endpoints.getQueryservice().getMaxEndpoints(); - builder = builder.queryServiceConfig(QueryServiceConfig.create(minQuery, maxQuery)); + CouchbaseServiceConfig queryConfig = determineCouchbaseServiceConfig( + endpoints.getQueryservice(), endpoints.getQuery()); + builder = builder.queryServiceConfig(QueryServiceConfig.create( + queryConfig.minEndpoints, queryConfig.maxEndpoints)); if (timeouts.getQuery() != null) { - int minView = endpoints.getView() != 1 ? endpoints.getView() : endpoints.getViewservice().getMinEndpoints(); - int maxView = endpoints.getView() != 1 ? endpoints.getView() : endpoints.getViewservice().getMaxEndpoints(); + CouchbaseServiceConfig viewConfig = determineCouchbaseServiceConfig( + endpoints.getViewservice(), endpoints.getView()); builder = builder.queryTimeout(timeouts.getQuery().toMillis()) - .viewServiceConfig(ViewServiceConfig.create(minView, maxView)); + .viewServiceConfig(ViewServiceConfig.create( + viewConfig.minEndpoints, viewConfig.maxEndpoints)); } if (timeouts.getSocketConnect() != null) { builder = builder.socketConnectTimeout( @@ -139,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 f79b86de0c..f3ddc31bee 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. @@ -118,79 +118,74 @@ 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; - - /** - * Dynamic query service configuration. - */ - private Queryservice queryservice = new Queryservice(); - - /** - * Dynamic view service configuration. - */ - private Viewservice viewservice = new Viewservice(); + private Integer view; public int getKeyValue() { return this.keyValue; } + @Deprecated public void setKeyValue(int keyValue) { this.keyValue = keyValue; } @Deprecated - @DeprecatedConfigurationProperty(replacement = "spring.couchbase.env.endpoints.queryservice") - public int getQuery() { + @DeprecatedConfigurationProperty(replacement = "spring.couchbase.env.endpoints.queryservice.max-endpoints") + public Integer getQuery() { return this.query; } @Deprecated - public void setQuery(int query) { + public void setQuery(Integer query) { this.query = query; } + public CouchbaseService getQueryservice() { + return this.queryservice; + } + @Deprecated - @DeprecatedConfigurationProperty(replacement = "spring.couchbase.env.endpoints.viewservice") - public int getView() { + @DeprecatedConfigurationProperty(replacement = "spring.couchbase.env.endpoints.viewservice.max-endpoints") + public Integer getView() { return this.view; } @Deprecated - public void setView(int view) { + public void setView(Integer view) { this.view = view; } - public Queryservice getQueryservice() { - return this.queryservice; - } - - public void setQueryservice(Queryservice queryservice) { - this.queryservice = queryservice; - } - - public Viewservice getViewservice() { + public CouchbaseService getViewservice() { return this.viewservice; } - public void setViewservice(Viewservice viewservice) { - this.viewservice = viewservice; - } + public static class CouchbaseService { - public static class Queryservice { /** - * Minimum Number of sockets per node against the query (N1QL) service. + * Minimum number of sockets per node. */ private int minEndpoints = 1; + /** - * Maximum Number of sockets per node against the query (N1QL) service. + * Maximum number of sockets per node. */ private int maxEndpoints = 1; @@ -209,34 +204,9 @@ public class CouchbaseProperties { public void setMaxEndpoints(int maxEndpoints) { this.maxEndpoints = maxEndpoints; } + } - public static class Viewservice { - /** - * Minimum Number of sockets per node against the view service. - */ - private int minEndpoints = 1; - /** - * Maximum Number of sockets per node against the view service. - */ - 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; - } - } } 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 14cd62ba2b..44b1659339 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 @@ -43,7 +43,6 @@ import static org.mockito.Mockito.mock; * * @author EddĂș MelĂ©ndez * @author Stephane Nicoll - * @author Yulin Qin */ public class CouchbaseAutoConfigurationTests { @@ -85,53 +84,59 @@ public class CouchbaseAutoConfigurationTests { } @Test - public void customizeEnvEndpointsIfBothStaticAndDynamicAreSetThenStaticEndpointsTakePriorityForBackwardsCompatibility() { + public void customizeEnvEndpoints() { testCouchbaseEnv((env) -> { - assertThat(env.kvServiceConfig().minEndpoints()).isEqualTo(4); - assertThat(env.kvServiceConfig().maxEndpoints()).isEqualTo(4); - assertThat(env.queryServiceConfig().minEndpoints()).isEqualTo(5); - assertThat(env.queryServiceConfig().maxEndpoints()).isEqualTo(5); - assertThat(env.viewServiceConfig().minEndpoints()).isEqualTo(6); - assertThat(env.viewServiceConfig().maxEndpoints()).isEqualTo(6); - }, "spring.couchbase.env.endpoints.keyValue=4", - "spring.couchbase.env.endpoints.queryservice.min-endpoints=2", - "spring.couchbase.env.endpoints.queryservice.max-endpoints=3", - "spring.couchbase.env.endpoints.query=5", - "spring.couchbase.env.endpoints.viewservice.min-endpoints=2", - "spring.couchbase.env.endpoints.viewservice.max-endpoints=3", - "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 - public void customizeEnvEndpointsWhenQueryAndViewStillWork() { + @Deprecated + public void customizeEnvEndpointsWithDeprecatedProperties() { testCouchbaseEnv((env) -> { - assertThat(env.kvServiceConfig().minEndpoints()).isEqualTo(3); - assertThat(env.kvServiceConfig().maxEndpoints()).isEqualTo(3); - assertThat(env.queryServiceConfig().minEndpoints()).isEqualTo(2); - assertThat(env.queryServiceConfig().maxEndpoints()).isEqualTo(2); - assertThat(env.viewServiceConfig().minEndpoints()).isEqualTo(3); - assertThat(env.viewServiceConfig().maxEndpoints()).isEqualTo(3); - }, "spring.couchbase.env.endpoints.keyValue=3", - "spring.couchbase.env.endpoints.query=2", - "spring.couchbase.env.endpoints.view=3"); - } - - - @Test - public void customizeEnvEndpointsIfOnlyDynamicEndpointsAreSet() { - testCouchbaseEnv((env) -> { - assertThat(env.kvServiceConfig().minEndpoints()).isEqualTo(4); - assertThat(env.kvServiceConfig().maxEndpoints()).isEqualTo(4); - assertThat(env.queryServiceConfig().minEndpoints()).isEqualTo(2); + assertThat(env.queryServiceConfig().minEndpoints()).isEqualTo(3); assertThat(env.queryServiceConfig().maxEndpoints()).isEqualTo(3); - assertThat(env.viewServiceConfig().minEndpoints()).isEqualTo(2); - assertThat(env.viewServiceConfig().maxEndpoints()).isEqualTo(3); - }, "spring.couchbase.env.endpoints.keyValue=4", - "spring.couchbase.env.endpoints.queryservice.min-endpoints=2", - "spring.couchbase.env.endpoints.queryservice.max-endpoints=3", - "spring.couchbase.env.endpoints.viewservice.min-endpoints=2", - "spring.couchbase.env.endpoints.viewservice.max-endpoints=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 3551095503..77998bfcc2 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.