|
1 | 1 | package org.couchbase.quickstart.configs; |
2 | 2 |
|
3 | | -import com.couchbase.client.core.deps.io.netty.handler.ssl.util.InsecureTrustManagerFactory; |
4 | | -import com.couchbase.client.core.env.IoConfig; |
5 | | -import com.couchbase.client.core.env.SecurityConfig; |
6 | | -import com.couchbase.client.core.error.BucketExistsException; |
7 | 3 | import com.couchbase.client.core.msg.kv.DurabilityLevel; |
8 | 4 | import com.couchbase.client.java.Bucket; |
9 | 5 | import com.couchbase.client.java.Cluster; |
10 | 6 | import com.couchbase.client.java.ClusterOptions; |
11 | | -import com.couchbase.client.java.env.ClusterEnvironment; |
12 | 7 | import com.couchbase.client.java.manager.bucket.BucketSettings; |
13 | 8 | import com.couchbase.client.java.manager.bucket.BucketType; |
14 | 9 | import org.springframework.beans.factory.annotation.Autowired; |
15 | 10 | import org.springframework.context.annotation.Bean; |
16 | 11 | import org.springframework.context.annotation.Configuration; |
17 | 12 |
|
| 13 | +import java.nio.file.Paths; |
| 14 | + |
18 | 15 | @Configuration |
19 | 16 | public class CouchbaseConfig { |
20 | 17 |
|
21 | 18 | @Autowired |
22 | 19 | private DBProperties dbProp; |
23 | 20 |
|
24 | 21 | /** |
25 | | - * NOTE: To connect with Couchbase CAPELLA please use the commented method bellow as it requires TLS |
26 | | - */ |
27 | | -/* @Bean |
28 | | - public Cluster getCouchbaseCluster(){ |
29 | | - ClusterEnvironment env = ClusterEnvironment.builder() |
30 | | - .securityConfig(SecurityConfig.enableTls(true) |
31 | | - .trustManagerFactory(InsecureTrustManagerFactory.INSTANCE)) |
32 | | - .ioConfig(IoConfig.enableDnsSrv(true)) |
33 | | - .build(); |
34 | | - return Cluster.connect(dbProp.getHostName(), |
35 | | - ClusterOptions.clusterOptions(dbProp.getUsername(), dbProp.getPassword()).environment(env)); |
36 | | - } |
37 | | -*/ |
38 | | - |
39 | | - /** |
40 | | - * NOTE: To connect with Couchbase locally use the methode bellow |
| 22 | + * NOTE: If connecting to Couchbase Capella, you must enable TLS. |
| 23 | + * <p> |
| 24 | + * The simplest way to enable TLS is to edit {@code application.properties} |
| 25 | + * and make sure the {@code spring.couchbase.bootstrap-hosts} config property |
| 26 | + * starts with "couchbases://" (note the final 's'), like this: |
| 27 | + * <pre> |
| 28 | + * spring.couchbase.bootstrap-hosts=couchbases://my-cluster.cloud.couchbase.com |
| 29 | + * </pre> |
| 30 | + * Alternatively, you can enable TLS by writing code to configure the cluster environment; |
| 31 | + * see the commented-out code in this method for an example. |
41 | 32 | */ |
42 | | - @Bean |
43 | | - public Cluster getCouchbaseCluster(){ |
| 33 | + @Bean(destroyMethod = "disconnect") |
| 34 | + public Cluster getCouchbaseCluster() { |
44 | 35 | return Cluster.connect(dbProp.getHostName(), dbProp.getUsername(), dbProp.getPassword()); |
| 36 | + |
| 37 | + // Here is an alternative version that enables TLS by configuring the cluster environment. |
| 38 | +/* return Cluster.connect( |
| 39 | + dbProp.getHostName(), |
| 40 | + ClusterOptions.clusterOptions(dbProp.getUsername(), dbProp.getPassword()) |
| 41 | + .environment(env -> { // Configure cluster environment properties here |
| 42 | + env.securityConfig().enableTls(true); |
| 43 | +
|
| 44 | + // If you're connecting to Capella, the SDK already knows which certificates to trust. |
| 45 | + // When using TLS with non-Capella clusters, you must tell the SDK which certificates to trust. |
| 46 | + env.securityConfig().trustCertificate( |
| 47 | + Paths.get("/path/to/trusted-root-certificate.pem") |
| 48 | + ); |
| 49 | + }) |
| 50 | + ); |
| 51 | + */ |
45 | 52 | } |
46 | 53 |
|
47 | 54 | @Bean |
48 | | - public Bucket getCouchbaseBucket(Cluster cluster){ |
| 55 | + public Bucket getCouchbaseBucket(Cluster cluster) { |
49 | 56 |
|
50 | 57 | // Creates the cluster if it does not exist yet |
51 | 58 | if (!cluster.buckets().getAllBuckets().containsKey(dbProp.getBucketName())) { |
|
0 commit comments