649 lines
29 KiB
Plaintext
649 lines
29 KiB
Plaintext
[[features.nosql]]
|
|
== Working with NoSQL Technologies
|
|
Spring Data provides additional projects that help you access a variety of NoSQL technologies, including:
|
|
|
|
* {spring-data-mongodb}[MongoDB]
|
|
* {spring-data-neo4j}[Neo4J]
|
|
* {spring-data-elasticsearch}[Elasticsearch]
|
|
* {spring-data-redis}[Redis]
|
|
* {spring-data-gemfire}[GemFire] or {spring-data-geode}[Geode]
|
|
* {spring-data-cassandra}[Cassandra]
|
|
* {spring-data-couchbase}[Couchbase]
|
|
* {spring-data-ldap}[LDAP]
|
|
|
|
Spring Boot provides auto-configuration for Redis, MongoDB, Neo4j, Solr, Elasticsearch, Cassandra, Couchbase, LDAP and InfluxDB.
|
|
You can make use of the other projects, but you must configure them yourself.
|
|
Refer to the appropriate reference documentation at {spring-data}.
|
|
|
|
|
|
|
|
[[features.nosql.redis]]
|
|
=== Redis
|
|
https://redis.io/[Redis] is a cache, message broker, and richly-featured key-value store.
|
|
Spring Boot offers basic auto-configuration for the https://github.com/lettuce-io/lettuce-core/[Lettuce] and https://github.com/xetorthio/jedis/[Jedis] client libraries and the abstractions on top of them provided by https://github.com/spring-projects/spring-data-redis[Spring Data Redis].
|
|
|
|
There is a `spring-boot-starter-data-redis` "`Starter`" for collecting the dependencies in a convenient way.
|
|
By default, it uses https://github.com/lettuce-io/lettuce-core/[Lettuce].
|
|
That starter handles both traditional and reactive applications.
|
|
|
|
TIP: We also provide a `spring-boot-starter-data-redis-reactive` "`Starter`" for consistency with the other stores with reactive support.
|
|
|
|
|
|
|
|
[[features.nosql.redis.connecting]]
|
|
==== Connecting to Redis
|
|
You can inject an auto-configured `RedisConnectionFactory`, `StringRedisTemplate`, or vanilla `RedisTemplate` instance as you would any other Spring Bean.
|
|
By default, the instance tries to connect to a Redis server at `localhost:6379`.
|
|
The following listing shows an example of such a bean:
|
|
|
|
[source,java,indent=0,subs="verbatim"]
|
|
----
|
|
include::{docs-java}/features/nosql/redis/connecting/MyBean.java[]
|
|
----
|
|
|
|
TIP: You can also register an arbitrary number of beans that implement `LettuceClientConfigurationBuilderCustomizer` for more advanced customizations.
|
|
`ClientResources` can also be customized using `ClientResourcesBuilderCustomizer`.
|
|
If you use Jedis, `JedisClientConfigurationBuilderCustomizer` is also available.
|
|
|
|
If you add your own `@Bean` of any of the auto-configured types, it replaces the default (except in the case of `RedisTemplate`, when the exclusion is based on the bean name, `redisTemplate`, not its type).
|
|
|
|
By default, a pooled connection factory is auto-configured if `commons-pool2` is on the classpath.
|
|
|
|
|
|
|
|
[[features.nosql.mongodb]]
|
|
=== MongoDB
|
|
https://www.mongodb.com/[MongoDB] is an open-source NoSQL document database that uses a JSON-like schema instead of traditional table-based relational data.
|
|
Spring Boot offers several conveniences for working with MongoDB, including the `spring-boot-starter-data-mongodb` and `spring-boot-starter-data-mongodb-reactive` "`Starters`".
|
|
|
|
|
|
|
|
[[features.nosql.mongodb.connecting]]
|
|
==== Connecting to a MongoDB Database
|
|
To access MongoDB databases, you can inject an auto-configured `org.springframework.data.mongodb.MongoDatabaseFactory`.
|
|
By default, the instance tries to connect to a MongoDB server at `mongodb://localhost/test`.
|
|
The following example shows how to connect to a MongoDB database:
|
|
|
|
[source,java,indent=0,subs="verbatim"]
|
|
----
|
|
include::{docs-java}/features/nosql/mongodb/connecting/MyBean.java[]
|
|
----
|
|
|
|
If you have defined your own `MongoClient`, it will be used to auto-configure a suitable `MongoDatabaseFactory`.
|
|
|
|
The auto-configured `MongoClient` is created using a `MongoClientSettings` bean.
|
|
If you have defined your own `MongoClientSettings`, it will be used without modification and the `spring.data.mongodb` properties will be ignored.
|
|
Otherwise a `MongoClientSettings` will be auto-configured and will have the `spring.data.mongodb` properties applied to it.
|
|
In either case, you can declare one or more `MongoClientSettingsBuilderCustomizer` beans to fine-tune the `MongoClientSettings` configuration.
|
|
Each will be called in order with the `MongoClientSettings.Builder` that is used to build the `MongoClientSettings`.
|
|
|
|
You can set the configprop:spring.data.mongodb.uri[] property to change the URL and configure additional settings such as the _replica set_, as shown in the following example:
|
|
|
|
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
|
|
----
|
|
spring:
|
|
data:
|
|
mongodb:
|
|
uri: "mongodb://user:secret@mongo1.example.com:12345,mongo2.example.com:23456/test"
|
|
----
|
|
|
|
Alternatively, you can specify connection details using discrete properties.
|
|
For example, you might declare the following settings in your `application.properties`:
|
|
|
|
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
|
|
----
|
|
spring:
|
|
data:
|
|
mongodb:
|
|
host: "mongoserver.example.com"
|
|
port: 27017
|
|
database: "test"
|
|
username: "user"
|
|
password: "secret"
|
|
----
|
|
|
|
TIP: If `spring.data.mongodb.port` is not specified, the default of `27017` is used.
|
|
You could delete this line from the example shown earlier.
|
|
|
|
TIP: If you do not use Spring Data MongoDB, you can inject a `MongoClient` bean instead of using `MongoDatabaseFactory`.
|
|
If you want to take complete control of establishing the MongoDB connection, you can also declare your own `MongoDatabaseFactory` or `MongoClient` bean.
|
|
|
|
NOTE: If you are using the reactive driver, Netty is required for SSL.
|
|
The auto-configuration configures this factory automatically if Netty is available and the factory to use hasn't been customized already.
|
|
|
|
|
|
|
|
[[features.nosql.mongodb.template]]
|
|
==== MongoTemplate
|
|
{spring-data-mongodb}[Spring Data MongoDB] provides a {spring-data-mongodb-api}/core/MongoTemplate.html[`MongoTemplate`] class that is very similar in its design to Spring's `JdbcTemplate`.
|
|
As with `JdbcTemplate`, Spring Boot auto-configures a bean for you to inject the template, as follows:
|
|
|
|
[source,java,indent=0,subs="verbatim"]
|
|
----
|
|
include::{docs-java}/features/nosql/mongodb/template/MyBean.java[]
|
|
----
|
|
|
|
See the {spring-data-mongodb-api}/core/MongoOperations.html[`MongoOperations` Javadoc] for complete details.
|
|
|
|
|
|
|
|
[[features.nosql.mongodb.repositories]]
|
|
==== Spring Data MongoDB Repositories
|
|
Spring Data includes repository support for MongoDB.
|
|
As with the JPA repositories discussed earlier, the basic principle is that queries are constructed automatically, based on method names.
|
|
|
|
In fact, both Spring Data JPA and Spring Data MongoDB share the same common infrastructure.
|
|
You could take the JPA example from earlier and, assuming that `City` is now a MongoDB data class rather than a JPA `@Entity`, it works in the same way, as shown in the following example:
|
|
|
|
[source,java,indent=0,subs="verbatim"]
|
|
----
|
|
include::{docs-java}/features/nosql/mongodb/repositories/CityRepository.java[]
|
|
----
|
|
|
|
TIP: You can customize document scanning locations by using the `@EntityScan` annotation.
|
|
|
|
TIP: For complete details of Spring Data MongoDB, including its rich object mapping technologies, refer to its {spring-data-mongodb}[reference documentation].
|
|
|
|
|
|
|
|
[[features.nosql.mongodb.embedded]]
|
|
==== Embedded Mongo
|
|
Spring Boot offers auto-configuration for https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo[Embedded Mongo].
|
|
To use it in your Spring Boot application, add a dependency on `de.flapdoodle.embed:de.flapdoodle.embed.mongo`.
|
|
|
|
The port that Mongo listens on can be configured by setting the configprop:spring.data.mongodb.port[] property.
|
|
To use a randomly allocated free port, use a value of 0.
|
|
The `MongoClient` created by `MongoAutoConfiguration` is automatically configured to use the randomly allocated port.
|
|
|
|
NOTE: If you do not configure a custom port, the embedded support uses a random port (rather than 27017) by default.
|
|
|
|
If you have SLF4J on the classpath, the output produced by Mongo is automatically routed to a logger named `org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongo`.
|
|
|
|
You can declare your own `IMongodConfig` and `IRuntimeConfig` beans to take control of the Mongo instance's configuration and logging routing.
|
|
The download configuration can be customized by declaring a `DownloadConfigBuilderCustomizer` bean.
|
|
|
|
|
|
|
|
[[features.nosql.neo4j]]
|
|
=== Neo4j
|
|
https://neo4j.com/[Neo4j] is an open-source NoSQL graph database that uses a rich data model of nodes connected by first class relationships, which is better suited for connected big data than traditional RDBMS approaches.
|
|
Spring Boot offers several conveniences for working with Neo4j, including the `spring-boot-starter-data-neo4j` "`Starter`".
|
|
|
|
|
|
|
|
[[features.nosql.neo4j.connecting]]
|
|
==== Connecting to a Neo4j Database
|
|
To access a Neo4j server, you can inject an auto-configured `org.neo4j.driver.Driver`.
|
|
By default, the instance tries to connect to a Neo4j server at `localhost:7687` using the Bolt protocol.
|
|
The following example shows how to inject a Neo4j `Driver` that gives you access, amongst other things, to a `Session`:
|
|
|
|
[source,java,indent=0,subs="verbatim"]
|
|
----
|
|
include::{docs-java}/features/nosql/neo4j/connecting/MyBean.java[]
|
|
----
|
|
|
|
You can configure various aspects of the driver using `spring.neo4j.*` properties.
|
|
The following example shows how to configure the uri and credentials to use:
|
|
|
|
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
|
|
----
|
|
spring:
|
|
neo4j:
|
|
uri: "bolt://my-server:7687"
|
|
authentication:
|
|
username: "neo4j"
|
|
password: "secret"
|
|
----
|
|
|
|
The auto-configured `Driver` is created using `ConfigBuilder`.
|
|
To fine-tune its configuration, declare one or more `ConfigBuilderCustomizer` beans.
|
|
Each will be called in order with the `ConfigBuilder` that is used to build the `Driver`.
|
|
|
|
|
|
|
|
[[features.nosql.neo4j.repositories]]
|
|
==== Spring Data Neo4j Repositories
|
|
Spring Data includes repository support for Neo4j.
|
|
For complete details of Spring Data Neo4j, refer to the {spring-data-neo4j-docs}[reference documentation].
|
|
|
|
Spring Data Neo4j shares the common infrastructure with Spring Data JPA as many other Spring Data modules do.
|
|
You could take the JPA example from earlier and define `City` as Spring Data Neo4j `@Node` rather than JPA `@Entity` and the repository abstraction works in the same way, as shown in the following example:
|
|
|
|
[source,java,indent=0,subs="verbatim"]
|
|
----
|
|
include::{docs-java}/features/nosql/neo4j/repositories/CityRepository.java[]
|
|
----
|
|
|
|
The `spring-boot-starter-data-neo4j` "`Starter`" enables the repository support as well as transaction management.
|
|
Spring Boot supports both classic and reactive Neo4j repositories, using the `Neo4jTemplate` or `ReactiveNeo4jTemplate` beans.
|
|
When Project Reactor is available on the classpath, the reactive style is also auto-configured.
|
|
|
|
You can customize the locations to look for repositories and entities by using `@EnableNeo4jRepositories` and `@EntityScan` respectively on a `@Configuration`-bean.
|
|
|
|
[NOTE]
|
|
====
|
|
In an application using the reactive style, a `ReactiveTransactionManager` is not auto-configured.
|
|
To enable transaction management, the following bean must be defined in your configuration:
|
|
|
|
[source,java,indent=0,subs="verbatim"]
|
|
----
|
|
include::{docs-java}/features/nosql/neo4j/repositories/MyNeo4jConfiguration.java[]
|
|
----
|
|
====
|
|
|
|
|
|
|
|
[[features.nosql.solr]]
|
|
=== Solr
|
|
https://lucene.apache.org/solr/[Apache Solr] is a search engine.
|
|
Spring Boot offers basic auto-configuration for the Solr 5 client library.
|
|
|
|
|
|
|
|
[[features.nosql.solr.connecting]]
|
|
==== Connecting to Solr
|
|
You can inject an auto-configured `SolrClient` instance as you would any other Spring bean.
|
|
By default, the instance tries to connect to a server at `http://localhost:8983/solr`.
|
|
The following example shows how to inject a Solr bean:
|
|
|
|
[source,java,indent=0,subs="verbatim"]
|
|
----
|
|
include::{docs-java}/features/nosql/solr/connecting/MyBean.java[]
|
|
----
|
|
|
|
If you add your own `@Bean` of type `SolrClient`, it replaces the default.
|
|
|
|
|
|
|
|
[[features.nosql.elasticsearch]]
|
|
=== Elasticsearch
|
|
https://www.elastic.co/products/elasticsearch[Elasticsearch] is an open source, distributed, RESTful search and analytics engine.
|
|
Spring Boot offers basic auto-configuration for Elasticsearch.
|
|
|
|
Spring Boot supports several clients:
|
|
|
|
* The official Java "Low Level" and "High Level" REST clients
|
|
* The `ReactiveElasticsearchClient` provided by Spring Data Elasticsearch
|
|
|
|
Spring Boot provides a dedicated "`Starter`", `spring-boot-starter-data-elasticsearch`.
|
|
|
|
|
|
|
|
[[features.nosql.elasticsearch.connecting-using-rest]]
|
|
==== Connecting to Elasticsearch using REST clients
|
|
Elasticsearch ships https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/index.html[two different REST clients] that you can use to query a cluster: the "Low Level" client and the "High Level" client.
|
|
Spring Boot provides support for the "High Level" client, which ships with `org.elasticsearch.client:elasticsearch-rest-high-level-client`.
|
|
|
|
If you have this dependency on the classpath, Spring Boot will auto-configure and register a `RestHighLevelClient` bean that by default targets `http://localhost:9200`.
|
|
You can further tune how `RestHighLevelClient` is configured, as shown in the following example:
|
|
|
|
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
|
|
----
|
|
spring:
|
|
elasticsearch:
|
|
rest:
|
|
uris: "https://search.example.com:9200"
|
|
read-timeout: "10s"
|
|
username: "user"
|
|
password: "secret"
|
|
----
|
|
|
|
You can also register an arbitrary number of beans that implement `RestClientBuilderCustomizer` for more advanced customizations.
|
|
To take full control over the registration, define a `RestClientBuilder` bean.
|
|
|
|
TIP: If your application needs access to a "Low Level" `RestClient`, you can get it by calling `client.getLowLevelClient()` on the auto-configured `RestHighLevelClient`.
|
|
|
|
Additionally, if `elasticsearch-rest-client-sniffer` is on the classpath, a `Sniffer` is auto-configured to automatically discover nodes from a running Elasticsearch cluster and set them to the `RestHighLevelClient` bean.
|
|
You can further tune how `Sniffer` is configured, as shown in the following example:
|
|
|
|
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
|
|
----
|
|
spring:
|
|
elasticsearch:
|
|
rest:
|
|
sniffer:
|
|
interval: 10m
|
|
delay-after-failure: 30s
|
|
----
|
|
|
|
|
|
|
|
[[features.nosql.elasticsearch.connecting-using-reactive-rest]]
|
|
==== Connecting to Elasticsearch using Reactive REST clients
|
|
{spring-data-elasticsearch}[Spring Data Elasticsearch] ships `ReactiveElasticsearchClient` for querying Elasticsearch instances in a reactive fashion.
|
|
It is built on top of WebFlux's `WebClient`, so both `spring-boot-starter-elasticsearch` and `spring-boot-starter-webflux` dependencies are useful to enable this support.
|
|
|
|
By default, Spring Boot will auto-configure and register a `ReactiveElasticsearchClient`
|
|
bean that targets `http://localhost:9200`.
|
|
You can further tune how it is configured, as shown in the following example:
|
|
|
|
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
|
|
----
|
|
spring:
|
|
data:
|
|
elasticsearch:
|
|
client:
|
|
reactive:
|
|
endpoints: "search.example.com:9200"
|
|
use-ssl: true
|
|
socket-timeout: "10s"
|
|
username: "user"
|
|
password: "secret"
|
|
----
|
|
|
|
If the configuration properties are not enough and you'd like to fully control the client
|
|
configuration, you can register a custom `ClientConfiguration` bean.
|
|
|
|
|
|
|
|
[[features.nosql.elasticsearch.connecting-using-spring-data]]
|
|
==== Connecting to Elasticsearch by Using Spring Data
|
|
To connect to Elasticsearch, a `RestHighLevelClient` bean must be defined,
|
|
auto-configured by Spring Boot or manually provided by the application (see previous sections).
|
|
With this configuration in place, an
|
|
`ElasticsearchRestTemplate` can be injected like any other Spring bean,
|
|
as shown in the following example:
|
|
|
|
[source,java,indent=0,subs="verbatim"]
|
|
----
|
|
include::{docs-java}/features/nosql/elasticsearch/connectingusingspringdata/MyBean.java[]
|
|
----
|
|
|
|
In the presence of `spring-data-elasticsearch` and the required dependencies for using a `WebClient` (typically `spring-boot-starter-webflux`), Spring Boot can also auto-configure a <<features#features.nosql.elasticsearch.connecting-using-reactive-rest,ReactiveElasticsearchClient>> and a `ReactiveElasticsearchTemplate` as beans.
|
|
They are the reactive equivalent of the other REST clients.
|
|
|
|
|
|
|
|
[[features.nosql.elasticsearch.repositories]]
|
|
==== Spring Data Elasticsearch Repositories
|
|
Spring Data includes repository support for Elasticsearch.
|
|
As with the JPA repositories discussed earlier, the basic principle is that queries are constructed for you automatically based on method names.
|
|
|
|
In fact, both Spring Data JPA and Spring Data Elasticsearch share the same common infrastructure.
|
|
You could take the JPA example from earlier and, assuming that `City` is now an Elasticsearch `@Document` class rather than a JPA `@Entity`, it works in the same way.
|
|
|
|
TIP: For complete details of Spring Data Elasticsearch, refer to the {spring-data-elasticsearch-docs}[reference documentation].
|
|
|
|
Spring Boot supports both classic and reactive Elasticsearch repositories, using the `ElasticsearchRestTemplate` or `ReactiveElasticsearchTemplate` beans.
|
|
Most likely those beans are auto-configured by Spring Boot given the required dependencies are present.
|
|
|
|
If you wish to use your own template for backing the Elasticsearch repositories, you can add your own `ElasticsearchRestTemplate` or `ElasticsearchOperations` `@Bean`, as long as it is named `"elasticsearchTemplate"`.
|
|
Same applies to `ReactiveElasticsearchTemplate` and `ReactiveElasticsearchOperations`, with the bean name `"reactiveElasticsearchTemplate"`.
|
|
|
|
You can choose to disable the repositories support with the following property:
|
|
|
|
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
|
|
----
|
|
spring:
|
|
data:
|
|
elasticsearch:
|
|
repositories:
|
|
enabled: false
|
|
----
|
|
|
|
|
|
|
|
[[features.nosql.cassandra]]
|
|
=== Cassandra
|
|
https://cassandra.apache.org/[Cassandra] is an open source, distributed database management system designed to handle large amounts of data across many commodity servers.
|
|
Spring Boot offers auto-configuration for Cassandra and the abstractions on top of it provided by https://github.com/spring-projects/spring-data-cassandra[Spring Data Cassandra].
|
|
There is a `spring-boot-starter-data-cassandra` "`Starter`" for collecting the dependencies in a convenient way.
|
|
|
|
|
|
|
|
[[features.nosql.cassandra.connecting]]
|
|
==== Connecting to Cassandra
|
|
You can inject an auto-configured `CassandraTemplate` or a Cassandra `CqlSession` instance as you would with any other Spring Bean.
|
|
The `spring.data.cassandra.*` properties can be used to customize the connection.
|
|
Generally, you provide `keyspace-name` and `contact-points` as well the local datacenter name, as shown in the following example:
|
|
|
|
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
|
|
----
|
|
spring:
|
|
data:
|
|
cassandra:
|
|
keyspace-name: "mykeyspace"
|
|
contact-points: "cassandrahost1:9042,cassandrahost2:9042"
|
|
local-datacenter: "datacenter1"
|
|
----
|
|
|
|
If the port is the same for all your contact points you can use a shortcut and only specify the host names, as shown in the following example:
|
|
|
|
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
|
|
----
|
|
spring:
|
|
data:
|
|
cassandra:
|
|
keyspace-name: "mykeyspace"
|
|
contact-points: "cassandrahost1,cassandrahost2"
|
|
local-datacenter: "datacenter1"
|
|
----
|
|
|
|
TIP: Those two examples are identical as the port default to `9042`.
|
|
If you need to configure the port, use `spring.data.cassandra.port`.
|
|
|
|
[NOTE]
|
|
====
|
|
The Cassandra driver has its own configuration infrastructure that loads an `application.conf` at the root of the classpath.
|
|
|
|
Spring Boot does not look for such a file by default but can load one using `spring.data.cassandra.config`.
|
|
If a property is both present in `+spring.data.cassandra.*+` and the configuration file, the value in `+spring.data.cassandra.*+` takes precedence.
|
|
|
|
For more advanced driver customizations, you can register an arbitrary number of beans that implement `DriverConfigLoaderBuilderCustomizer`.
|
|
The `CqlSession` can be customized with a bean of type `CqlSessionBuilderCustomizer`.
|
|
====
|
|
|
|
NOTE: If you're using `CqlSessionBuilder` to create multiple `CqlSession` beans, keep in mind the builder is mutable so make sure to inject a fresh copy for each session.
|
|
|
|
The following code listing shows how to inject a Cassandra bean:
|
|
|
|
[source,java,indent=0,subs="verbatim"]
|
|
----
|
|
include::{docs-java}/features/nosql/cassandra/connecting/MyBean.java[]
|
|
----
|
|
|
|
If you add your own `@Bean` of type `CassandraTemplate`, it replaces the default.
|
|
|
|
|
|
|
|
[[features.nosql.cassandra.repositories]]
|
|
==== Spring Data Cassandra Repositories
|
|
Spring Data includes basic repository support for Cassandra.
|
|
Currently, this is more limited than the JPA repositories discussed earlier and needs to annotate finder methods with `@Query`.
|
|
|
|
TIP: For complete details of Spring Data Cassandra, refer to the https://docs.spring.io/spring-data/cassandra/docs/[reference documentation].
|
|
|
|
|
|
|
|
[[features.nosql.couchbase]]
|
|
=== Couchbase
|
|
https://www.couchbase.com/[Couchbase] is an open-source, distributed, multi-model NoSQL document-oriented database that is optimized for interactive applications.
|
|
Spring Boot offers auto-configuration for Couchbase and the abstractions on top of it provided by https://github.com/spring-projects/spring-data-couchbase[Spring Data Couchbase].
|
|
There are `spring-boot-starter-data-couchbase` and `spring-boot-starter-data-couchbase-reactive` "`Starters`" for collecting the dependencies in a convenient way.
|
|
|
|
|
|
|
|
[[features.nosql.couchbase.connecting]]
|
|
==== Connecting to Couchbase
|
|
You can get a `Cluster` by adding the Couchbase SDK and some configuration.
|
|
The `spring.couchbase.*` properties can be used to customize the connection.
|
|
Generally, you provide the https://github.com/couchbaselabs/sdk-rfcs/blob/master/rfc/0011-connection-string.md[connection string], username, and password, as shown in the following example:
|
|
|
|
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
|
|
----
|
|
spring:
|
|
couchbase:
|
|
connection-string: "couchbase://192.168.1.123"
|
|
username: "user"
|
|
password: "secret"
|
|
----
|
|
|
|
It is also possible to customize some of the `ClusterEnvironment` settings.
|
|
For instance, the following configuration changes the timeout to use to open a new `Bucket` and enables SSL support:
|
|
|
|
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
|
|
----
|
|
spring:
|
|
couchbase:
|
|
env:
|
|
timeouts:
|
|
connect: "3s"
|
|
ssl:
|
|
key-store: "/location/of/keystore.jks"
|
|
key-store-password: "secret"
|
|
----
|
|
|
|
TIP: Check the `spring.couchbase.env.*` properties for more details.
|
|
To take more control, one or more `ClusterEnvironmentBuilderCustomizer` beans can be used.
|
|
|
|
|
|
|
|
[[features.nosql.couchbase.repositories]]
|
|
==== Spring Data Couchbase Repositories
|
|
Spring Data includes repository support for Couchbase.
|
|
For complete details of Spring Data Couchbase, refer to the {spring-data-couchbase-docs}[reference documentation].
|
|
|
|
You can inject an auto-configured `CouchbaseTemplate` instance as you would with any other Spring Bean, provided a `CouchbaseClientFactory` bean is available.
|
|
This happens when a `Cluster` is available, as described above, and a bucket name has been specified:
|
|
|
|
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
|
|
----
|
|
spring:
|
|
data:
|
|
couchbase:
|
|
bucket-name: "my-bucket"
|
|
----
|
|
|
|
The following examples shows how to inject a `CouchbaseTemplate` bean:
|
|
|
|
[source,java,indent=0,subs="verbatim"]
|
|
----
|
|
include::{docs-java}/features/nosql/couchbase/repositories/MyBean.java[]
|
|
----
|
|
|
|
There are a few beans that you can define in your own configuration to override those provided by the auto-configuration:
|
|
|
|
* A `CouchbaseMappingContext` `@Bean` with a name of `couchbaseMappingContext`.
|
|
* A `CustomConversions` `@Bean` with a name of `couchbaseCustomConversions`.
|
|
* A `CouchbaseTemplate` `@Bean` with a name of `couchbaseTemplate`.
|
|
|
|
To avoid hard-coding those names in your own config, you can reuse `BeanNames` provided by Spring Data Couchbase.
|
|
For instance, you can customize the converters to use, as follows:
|
|
|
|
[source,java,indent=0,subs="verbatim"]
|
|
----
|
|
include::{docs-java}/features/nosql/couchbase/repositories/MyCouchbaseConfiguration.java[]
|
|
----
|
|
|
|
|
|
|
|
[[features.nosql.ldap]]
|
|
=== LDAP
|
|
https://en.wikipedia.org/wiki/Lightweight_Directory_Access_Protocol[LDAP] (Lightweight Directory Access Protocol) is an open, vendor-neutral, industry standard application protocol for accessing and maintaining distributed directory information services over an IP network.
|
|
Spring Boot offers auto-configuration for any compliant LDAP server as well as support for the embedded in-memory LDAP server from https://ldap.com/unboundid-ldap-sdk-for-java/[UnboundID].
|
|
|
|
LDAP abstractions are provided by https://github.com/spring-projects/spring-data-ldap[Spring Data LDAP].
|
|
There is a `spring-boot-starter-data-ldap` "`Starter`" for collecting the dependencies in a convenient way.
|
|
|
|
|
|
|
|
[[features.nosql.ldap.connecting]]
|
|
==== Connecting to an LDAP Server
|
|
To connect to an LDAP server, make sure you declare a dependency on the `spring-boot-starter-data-ldap` "`Starter`" or `spring-ldap-core` and then declare the URLs of your server in your application.properties, as shown in the following example:
|
|
|
|
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
|
|
----
|
|
spring:
|
|
ldap:
|
|
urls: "ldap://myserver:1235"
|
|
username: "admin"
|
|
password: "secret"
|
|
----
|
|
|
|
If you need to customize connection settings, you can use the `spring.ldap.base` and `spring.ldap.base-environment` properties.
|
|
|
|
An `LdapContextSource` is auto-configured based on these settings.
|
|
If a `DirContextAuthenticationStrategy` bean is available, it is associated to the auto-configured `LdapContextSource`.
|
|
If you need to customize it, for instance to use a `PooledContextSource`, you can still inject the auto-configured `LdapContextSource`.
|
|
Make sure to flag your customized `ContextSource` as `@Primary` so that the auto-configured `LdapTemplate` uses it.
|
|
|
|
|
|
|
|
[[features.nosql.ldap.repositories]]
|
|
==== Spring Data LDAP Repositories
|
|
Spring Data includes repository support for LDAP.
|
|
For complete details of Spring Data LDAP, refer to the https://docs.spring.io/spring-data/ldap/docs/1.0.x/reference/html/[reference documentation].
|
|
|
|
You can also inject an auto-configured `LdapTemplate` instance as you would with any other Spring Bean, as shown in the following example:
|
|
|
|
|
|
[source,java,indent=0,subs="verbatim"]
|
|
----
|
|
include::{docs-java}/features/nosql/ldap/repositories/MyBean.java[]
|
|
----
|
|
|
|
|
|
|
|
[[features.nosql.ldap.embedded]]
|
|
==== Embedded In-memory LDAP Server
|
|
For testing purposes, Spring Boot supports auto-configuration of an in-memory LDAP server from https://ldap.com/unboundid-ldap-sdk-for-java/[UnboundID].
|
|
To configure the server, add a dependency to `com.unboundid:unboundid-ldapsdk` and declare a configprop:spring.ldap.embedded.base-dn[] property, as follows:
|
|
|
|
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
|
|
----
|
|
spring:
|
|
ldap:
|
|
embedded:
|
|
base-dn: "dc=spring,dc=io"
|
|
----
|
|
|
|
[NOTE]
|
|
====
|
|
It is possible to define multiple base-dn values, however, since distinguished names usually contain commas, they must be defined using the correct notation.
|
|
|
|
In yaml files, you can use the yaml list notation. In properties files, you must include the index as part of the property name:
|
|
|
|
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
|
|
----
|
|
spring.ldap.embedded.base-dn:
|
|
- dc=spring,dc=io
|
|
- dc=pivotal,dc=io
|
|
----
|
|
====
|
|
|
|
By default, the server starts on a random port and triggers the regular LDAP support.
|
|
There is no need to specify a configprop:spring.ldap.urls[] property.
|
|
|
|
If there is a `schema.ldif` file on your classpath, it is used to initialize the server.
|
|
If you want to load the initialization script from a different resource, you can also use the configprop:spring.ldap.embedded.ldif[] property.
|
|
|
|
By default, a standard schema is used to validate `LDIF` files.
|
|
You can turn off validation altogether by setting the configprop:spring.ldap.embedded.validation.enabled[] property.
|
|
If you have custom attributes, you can use configprop:spring.ldap.embedded.validation.schema[] to define your custom attribute types or object classes.
|
|
|
|
|
|
|
|
[[features.nosql.influxdb]]
|
|
=== InfluxDB
|
|
https://www.influxdata.com/[InfluxDB] is an open-source time series database optimized for fast, high-availability storage and retrieval of time series data in fields such as operations monitoring, application metrics, Internet-of-Things sensor data, and real-time analytics.
|
|
|
|
|
|
|
|
[[features.nosql.influxdb.connecting]]
|
|
==== Connecting to InfluxDB
|
|
Spring Boot auto-configures a client instance, provided that either `influxdb-java` (Influx 1.x) or `influxdb-client-java` (Influx 2.x) is on the classpath and the URL of the database is set, as shown in the following example:
|
|
|
|
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
|
|
----
|
|
spring:
|
|
influx:
|
|
url: "https://172.0.0.1:8086"
|
|
----
|
|
|
|
If the connection to InfluxDB requires a user and password, you can set the `spring.influx.user` and `spring.influx.password` properties accordingly.
|
|
|
|
InfluxDB relies on OkHttp.
|
|
If you need to tune the http client `InfluxDB` uses behind the scenes, you can register an `InfluxDbOkHttpClientBuilderProvider` bean.
|
|
|
|
If you need more control over the configuration, consider registering an `InfluxDbCustomizer` (Influx 1.x), or an `InfluxDbClientOptionsBuilderCustomizer` (Influx 2.x) bean.
|