Commit 1454bce7 authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #12655 from diamondblack:update-couchbase-deprecated-endpoint

* pr/12655:
  Polish "Replace Couchbase's deprecated methods"
  Replace Couchbase's deprecated methods
parents 92d94797 6692301d
/* /*
* 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.
...@@ -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,21 @@ public class CouchbaseAutoConfiguration { ...@@ -102,14 +106,21 @@ 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()); CouchbaseServiceConfig queryConfig = determineCouchbaseServiceConfig(
endpoints.getQueryservice(), endpoints.getQuery());
builder = builder.queryServiceConfig(QueryServiceConfig.create(
queryConfig.minEndpoints, queryConfig.maxEndpoints));
if (timeouts.getQuery() != null) { if (timeouts.getQuery() != null) {
CouchbaseServiceConfig viewConfig = determineCouchbaseServiceConfig(
endpoints.getViewservice(), endpoints.getView());
builder = builder.queryTimeout(timeouts.getQuery().toMillis()) builder = builder.queryTimeout(timeouts.getQuery().toMillis())
.viewEndpoints(endpoints.getView()); .viewServiceConfig(ViewServiceConfig.create(
viewConfig.minEndpoints, viewConfig.maxEndpoints));
} }
if (timeouts.getSocketConnect() != null) { if (timeouts.getSocketConnect() != null) {
builder = builder.socketConnectTimeout( builder = builder.socketConnectTimeout(
...@@ -131,6 +142,28 @@ public class CouchbaseAutoConfiguration { ...@@ -131,6 +142,28 @@ public class CouchbaseAutoConfiguration {
return builder; 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;
}
}
} }
/** /**
......
/* /*
* 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.
...@@ -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")
...@@ -116,42 +118,98 @@ public class CouchbaseProperties { ...@@ -116,42 +118,98 @@ public class CouchbaseProperties {
*/ */
private int keyValue = 1; 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. * 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. * Number of sockets per node against the view service.
*/ */
private int view = 1; private Integer view;
public int getKeyValue() { public int getKeyValue() {
return this.keyValue; return this.keyValue;
} }
@Deprecated
public void setKeyValue(int keyValue) { public void setKeyValue(int keyValue) {
this.keyValue = keyValue; this.keyValue = keyValue;
} }
public int getQuery() { @Deprecated
@DeprecatedConfigurationProperty(replacement = "spring.couchbase.env.endpoints.queryservice.max-endpoints")
public Integer getQuery() {
return this.query; return this.query;
} }
public void setQuery(int query) { @Deprecated
public void setQuery(Integer query) {
this.query = 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; return this.view;
} }
public void setView(int view) { @Deprecated
public void setView(Integer view) {
this.view = 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 { public static class Ssl {
/** /**
......
...@@ -86,12 +86,57 @@ public class CouchbaseAutoConfigurationTests { ...@@ -86,12 +86,57 @@ public class CouchbaseAutoConfigurationTests {
@Test @Test
public void customizeEnvEndpoints() { public void customizeEnvEndpoints() {
testCouchbaseEnv((env) -> { testCouchbaseEnv((env) -> {
assertThat(env.kvEndpoints()).isEqualTo(4); assertThat(env.kvServiceConfig().minEndpoints()).isEqualTo(2);
assertThat(env.queryEndpoints()).isEqualTo(5); assertThat(env.kvServiceConfig().maxEndpoints()).isEqualTo(2);
assertThat(env.viewEndpoints()).isEqualTo(6); assertThat(env.queryServiceConfig().minEndpoints()).isEqualTo(3);
}, "spring.couchbase.env.endpoints.keyValue=4", assertThat(env.queryServiceConfig().maxEndpoints()).isEqualTo(5);
"spring.couchbase.env.endpoints.query=5", assertThat(env.viewServiceConfig().minEndpoints()).isEqualTo(4);
"spring.couchbase.env.endpoints.view=6"); 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 @Test
......
...@@ -573,8 +573,10 @@ content into your application. Rather, pick only the properties that you need. ...@@ -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.name=default # Name of the bucket to connect to.
spring.couchbase.bucket.password= # Password of the bucket. 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.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.queryservice.min-endpoints=1 # Minimum number of sockets per node.
spring.couchbase.env.endpoints.view=1 # Number of sockets per node against the view service. 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.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= # Path to the JVM key store that holds the certificates.
spring.couchbase.env.ssl.key-store-password= # Password used to access the key store. spring.couchbase.env.ssl.key-store-password= # Password used to access the key store.
......
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