Commit 3236306e authored by Yulin Qin's avatar Yulin Qin Committed by Stephane Nicoll

Replace Couchbase's deprecated methods

See gh-12655
parent 92d94797
...@@ -16,6 +16,9 @@ ...@@ -16,6 +16,9 @@
package org.springframework.boot.autoconfigure.couchbase; 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.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;
...@@ -40,6 +43,7 @@ import org.springframework.context.annotation.Primary; ...@@ -40,6 +43,7 @@ import org.springframework.context.annotation.Primary;
* *
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Yulin Qin
* @since 1.4.0 * @since 1.4.0
*/ */
@Configuration @Configuration
...@@ -102,14 +106,18 @@ public class CouchbaseAutoConfiguration { ...@@ -102,14 +106,18 @@ public class CouchbaseAutoConfiguration {
if (timeouts.getConnect() != null) { if (timeouts.getConnect() != null) {
builder = builder.connectTimeout(timeouts.getConnect().toMillis()); builder = builder.connectTimeout(timeouts.getConnect().toMillis());
} }
builder = builder.kvEndpoints(endpoints.getKeyValue()); builder = builder.keyValueServiceConfig(KeyValueServiceConfig.create(endpoints.getKeyValue()));
if (timeouts.getKeyValue() != null) { if (timeouts.getKeyValue() != null) {
builder = builder.kvTimeout(timeouts.getKeyValue().toMillis()); builder = builder.kvTimeout(timeouts.getKeyValue().toMillis());
} }
builder = builder.queryEndpoints(endpoints.getQuery()); 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));
if (timeouts.getQuery() != null) { if (timeouts.getQuery() != null) {
int minView = endpoints.getView() != 1 ? endpoints.getView() : endpoints.getViewservice().getMinEndpoints();
int maxView = endpoints.getView() != 1 ? endpoints.getView() : endpoints.getViewservice().getMaxEndpoints();
builder = builder.queryTimeout(timeouts.getQuery().toMillis()) builder = builder.queryTimeout(timeouts.getQuery().toMillis())
.viewEndpoints(endpoints.getView()); .viewServiceConfig(ViewServiceConfig.create(minView, maxView));
} }
if (timeouts.getSocketConnect() != null) { if (timeouts.getSocketConnect() != null) {
builder = builder.socketConnectTimeout( builder = builder.socketConnectTimeout(
......
...@@ -20,6 +20,7 @@ import java.time.Duration; ...@@ -20,6 +20,7 @@ import java.time.Duration;
import java.util.List; import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
...@@ -27,6 +28,7 @@ import org.springframework.util.StringUtils; ...@@ -27,6 +28,7 @@ import org.springframework.util.StringUtils;
* *
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Yulin Qin
* @since 1.4.0 * @since 1.4.0
*/ */
@ConfigurationProperties(prefix = "spring.couchbase") @ConfigurationProperties(prefix = "spring.couchbase")
...@@ -126,6 +128,16 @@ public class CouchbaseProperties { ...@@ -126,6 +128,16 @@ public class CouchbaseProperties {
*/ */
private int view = 1; private int view = 1;
/**
* Dynamic query service configuration.
*/
private Queryservice queryservice = new Queryservice();
/**
* Dynamic view service configuration.
*/
private Viewservice viewservice = new Viewservice();
public int getKeyValue() { public int getKeyValue() {
return this.keyValue; return this.keyValue;
} }
...@@ -134,24 +146,100 @@ public class CouchbaseProperties { ...@@ -134,24 +146,100 @@ public class CouchbaseProperties {
this.keyValue = keyValue; this.keyValue = keyValue;
} }
@Deprecated
@DeprecatedConfigurationProperty(replacement = "spring.couchbase.env.endpoints.queryservice")
public int getQuery() { public int getQuery() {
return this.query; return this.query;
} }
@Deprecated
public void setQuery(int query) { public void setQuery(int query) {
this.query = query; this.query = query;
} }
@Deprecated
@DeprecatedConfigurationProperty(replacement = "spring.couchbase.env.endpoints.viewservice")
public int getView() { public int getView() {
return this.view; return this.view;
} }
@Deprecated
public void setView(int view) { public void setView(int view) {
this.view = view; this.view = view;
} }
public Queryservice getQueryservice() {
return this.queryservice;
}
public void setQueryservice(Queryservice queryservice) {
this.queryservice = queryservice;
}
public Viewservice getViewservice() {
return this.viewservice;
}
public void setViewservice(Viewservice viewservice) {
this.viewservice = viewservice;
}
public static class Queryservice {
/**
* Minimum Number of sockets per node against the query (N1QL) service.
*/
private int minEndpoints = 1;
/**
* Maximum Number of sockets per node against the query (N1QL) 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;
}
}
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;
}
}
} }
public static class Ssl { public static class Ssl {
/** /**
......
...@@ -43,6 +43,7 @@ import static org.mockito.Mockito.mock; ...@@ -43,6 +43,7 @@ import static org.mockito.Mockito.mock;
* *
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Yulin Qin
*/ */
public class CouchbaseAutoConfigurationTests { public class CouchbaseAutoConfigurationTests {
...@@ -84,16 +85,55 @@ public class CouchbaseAutoConfigurationTests { ...@@ -84,16 +85,55 @@ public class CouchbaseAutoConfigurationTests {
} }
@Test @Test
public void customizeEnvEndpoints() { public void customizeEnvEndpointsIfBothStaticAndDynamicAreSetThenStaticEndpointsTakePriorityForBackwardsCompatibility() {
testCouchbaseEnv((env) -> { testCouchbaseEnv((env) -> {
assertThat(env.kvEndpoints()).isEqualTo(4); assertThat(env.kvServiceConfig().minEndpoints()).isEqualTo(4);
assertThat(env.queryEndpoints()).isEqualTo(5); assertThat(env.kvServiceConfig().maxEndpoints()).isEqualTo(4);
assertThat(env.viewEndpoints()).isEqualTo(6); 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.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.query=5",
"spring.couchbase.env.endpoints.viewservice.min-endpoints=2",
"spring.couchbase.env.endpoints.viewservice.max-endpoints=3",
"spring.couchbase.env.endpoints.view=6"); "spring.couchbase.env.endpoints.view=6");
} }
@Test
public void customizeEnvEndpointsWhenQueryAndViewStillWork() {
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().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");
}
@Test @Test
public void customizeEnvTimeouts() { public void customizeEnvTimeouts() {
testCouchbaseEnv((env) -> { testCouchbaseEnv((env) -> {
......
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