Working with NoSQL Technologies
Spring Data 提供了其他项目,可帮助你访问各种 NoSQL 技术,包括:
Spring Data provides additional projects that help you access a variety of NoSQL technologies, including:
-
{url-spring-data-cassandra-site}[Cassandra]
-
{url-spring-data-couchbase-site}[Couchbase]
-
{url-spring-data-elasticsearch-site}[Elasticsearch]
-
{url-spring-data-gemfire-site}[GemFire] or {url-spring-data-geode-site}[Geode]
-
{url-spring-data-ldap-site}[LDAP]
-
{url-spring-data-mongodb-site}[MongoDB]
-
{url-spring-data-neo4j-site}[Neo4J]
-
{url-spring-data-redis-site}[Redis]
在这之中,Spring Boot 为 Cassandra、Couchbase、Elasticsearch、LDAP、MongoDB、Neo4J 和 Redis 提供自动配置。此外,{url-spring-boot-for-apache-geode-site}[Spring Boot for Apache Geode] 还提供 {url-spring-boot-for-apache-geode-docs}#geode-repositories[Apache Geode 的自动配置]。你可以使用其他项目,但你必须自己配置它们。请参阅 {url-spring-data-site} 上的相应参考文档。
Of these, Spring Boot provides auto-configuration for Cassandra, Couchbase, Elasticsearch, LDAP, MongoDB, Neo4J and Redis. Additionally, {url-spring-boot-for-apache-geode-site}[Spring Boot for Apache Geode] provides {url-spring-boot-for-apache-geode-docs}#geode-repositories[auto-configuration for Apache Geode]. You can make use of the other projects, but you must configure them yourself. See the appropriate reference documentation at {url-spring-data-site}.
Spring Boot 还为 InfluxDB 客户端提供自动配置,但已弃用以支持 the new InfluxDB Java client,它提供了自己的 Spring Boot 集成。
Spring Boot also provides auto-configuration for the InfluxDB client but it is deprecated in favor of the new InfluxDB Java client that provides its own Spring Boot integration.
Redis
Redis是一个缓存、消息代理和功能丰富的键值存储。Spring Boot 为 Lettuce和 Jedis客户端库以及 Spring Data Redis提供的之上抽象提供基本的自动配置。
Redis is a cache, message broker, and richly-featured key-value store. Spring Boot offers basic auto-configuration for the Lettuce and Jedis client libraries and the abstractions on top of them provided by Spring Data Redis.
有一个 spring-boot-starter-data-redis
"`Starter`"用于以一种便捷的方式收集依赖项。默认情况下,它使用 Lettuce。该 starter 处理传统应用程序和反应式应用程序。
There is a spring-boot-starter-data-redis
“Starter” for collecting the dependencies in a convenient way.
By default, it uses Lettuce.
That starter handles both traditional and reactive applications.
我们还为 "`Starter`"提供一个 |
We also provide a |
Connecting to Redis
你可以注入一个自动配置的 RedisConnectionFactory
、StringRedisTemplate
、或普通的 `RedisTemplate`实例,就像注入任何其他 Spring Bean 一样。以下清单展示了此类 bean 的示例:
You can inject an auto-configured RedisConnectionFactory
, StringRedisTemplate
, or vanilla RedisTemplate
instance as you would any other Spring Bean.
The following listing shows an example of such a bean:
默认情况下,该实例会尝试连接到 `localhost:6379`的 Redis 服务器。你可以使用 `spring.data.redis.*`属性指定自定义连接详细信息,如下例所示:
By default, the instance tries to connect to a Redis server at localhost:6379
.
You can specify custom connection details using spring.data.redis.*
properties, as shown in the following example:
spring: data: redis: host: "localhost" port: 6379 database: 0 username: "user" password: "secret"
你还可以注册任意数目的 bean 来实现 |
You can also register an arbitrary number of beans that implement |
如果你添加的是任何自动配置类型的 @Bean
,则它会替换默认值(除非是 RedisTemplate
,此时排除的依据是 bean 名称 redisTemplate
,而不是它的类型)。
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).
默认情况下,如果 `commons-pool2`在类路径中,则会自动配置一个连接池工厂。
By default, a pooled connection factory is auto-configured if commons-pool2
is on the classpath.
可以将自动配置的 `RedisConnectionFactory`配置为使用 SSL 与服务器通信,方法是设置属性,如本例所示:
The auto-configured RedisConnectionFactory
can be configured to use SSL for communication with the server by setting the properties as shown in this example:
spring: data: redis: ssl: enabled: true
可以在一个 SSL bundle中配置自定义的 SSL 信任资料,并将其应用到 RedisConnectionFactory
,如本例所示:
Custom SSL trust material can be configured in an SSL bundle and applied to the RedisConnectionFactory
as shown in this example:
spring: data: redis: ssl: bundle: "example"
MongoDB
MongoDB是一个开源 NoSQL 文档数据库,它使用类似于 JSON 的模式,而不是传统的基于表的关联数据。Spring Boot 为使用 MongoDB 提供了多种便利,包括 spring-boot-starter-data-mongodb`和 `spring-boot-starter-data-mongodb-reactive
“Starters”。
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”.
Connecting to a MongoDB Database
要访问 MongoDB 数据库,你可以注入一个自动配置的 org.springframework.data.mongodb.MongoDatabaseFactory
。默认情况下,该实例会尝试连接到 `mongodb://localhost/test`的 MongoDB 服务器。以下示例展示了如何连接到 MongoDB 数据库:
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:
如果您定义了自己的 MongoClient
,它将用于自动配置一个合适的 MongoDatabaseFactory
。
If you have defined your own MongoClient
, it will be used to auto-configure a suitable MongoDatabaseFactory
.
自动配置的 MongoClient`使用 `MongoClientSettings`bean 创建。如果您定义了自己的 `MongoClientSettings
,它将按原样使用,并且会忽略 spring.data.mongodb`属性。否则,将自动配置 `MongoClientSettings
,并将 spring.data.mongodb`属性应用于它。在这两种情况下,您可以声明一个或多个 `MongoClientSettingsBuilderCustomizer`bean 来微调 `MongoClientSettings`配置。每个 bean 都将按顺序使用 `MongoClientSettings.Builder
,用于构建 MongoClientSettings
。
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
.
您可以设置 configprop:spring.data.mongodb.uri[] 属性以更改 URL 并配置其他设置,例如 replica set,如下例所示:
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:
spring: data: mongodb: uri: "mongodb://user:secret@mongoserver1.example.com:27017,mongoserver2.example.com:23456/test"
或者,您可以使用离散属性指定连接详细信息。例如,您可以在 `application.properties`中声明以下设置:
Alternatively, you can specify connection details using discrete properties.
For example, you might declare the following settings in your application.properties
:
spring: data: mongodb: host: "mongoserver1.example.com" port: 27017 additional-hosts: - "mongoserver2.example.com:23456" database: "test" username: "user" password: "secret"
可以通过设置如本例所示的属性来配置自动配置的 `MongoClient`在与服务器通信时使用 SSL:
The auto-configured MongoClient
can be configured to use SSL for communication with the server by setting the properties as shown in this example:
spring: data: mongodb: uri: "mongodb://user:secret@mongoserver1.example.com:27017,mongoserver2.example.com:23456/test" ssl: enabled: true
可以在 SSL bundle中配置自定义 SSL 信任材料,并将其应用于 MongoClient
,如下例所示:
Custom SSL trust material can be configured in an SSL bundle and applied to the MongoClient
as shown in this example:
spring: data: mongodb: uri: "mongodb://user:secret@mongoserver1.example.com:27017,mongoserver2.example.com:23456/test" ssl: bundle: "example"
如果未指定 If 您还可以使用 `host:port`语法将端口指定为主机地址的一部分。如果您需要更改 `additional-hosts`条目的端口,应该使用此格式。 You can also specify the port as part of the host address by using the |
如果您不使用 Spring Data MongoDB,则可以注入 |
If you do not use Spring Data MongoDB, you can inject a |
如果您使用的是反应式驱动程序,SSL 需要 Netty。如果 Netty 可用且尚未自定义要使用的工厂,则自动配置会自动配置此工厂。 |
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 has not been customized already. |
MongoTemplate
{url-spring-data-mongodb-site}[Spring Data MongoDB] 提供了一个在设计上与 Spring 的 JdbcTemplate`非常相似的 {url-spring-data-mongodb-javadoc}/org/springframework/data/mongodb/core/MongoTemplate.html[`MongoTemplate
] 类。与 `JdbcTemplate`一样,Spring Boot 会自动为您配置一个 bean,以便注入模板,如下所示:
{url-spring-data-mongodb-site}[Spring Data MongoDB] provides a {url-spring-data-mongodb-javadoc}/org/springframework/data/mongodb/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:
有关完整详细信息,请参阅 {url-spring-data-mongodb-javadoc}/org/springframework/data/mongodb/core/MongoOperations.html[`MongoOperations`Javadoc]。
See the {url-spring-data-mongodb-javadoc}/org/springframework/data/mongodb/core/MongoOperations.html[MongoOperations
Javadoc] for complete details.
Spring Data MongoDB Repositories
Spring Data 包括对 MongoDB 的存储库支持。与前面讨论的 JPA 存储库一样,基本原则是根据方法名称自动构造查询。
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.
实际上,Spring Data JPA 和 Spring Data MongoDB 都共享相同的通用基础设施。您可以获取前面的 JPA 示例,假设 City`现在是一个 MongoDB 数据类,而不是 JPA `@Entity
,它以相同的方式工作,如下例所示:
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:
存储库和文档是通过扫描找到的。默认情况下,扫描 auto-configuration packages。您可以通过分别使用 `@EnableMongoRepositories`和 `@EntityScan`来自定义要查找存储库和文档的位置。
Repositories and documents are found through scanning.
By default, the auto-configuration packages are scanned.
You can customize the locations to look for repositories and documents by using @EnableMongoRepositories
and @EntityScan
respectively.
有关 Spring Data MongoDB 的完整详细信息,包括其丰富的对象映射技术,请参阅其 {url-spring-data-mongodb-docs}[参考文档]。 |
For complete details of Spring Data MongoDB, including its rich object mapping technologies, see its {url-spring-data-mongodb-docs}[reference documentation]. |
Neo4j
Neo4j是一个开放源代码 NoSQL 图数据库,它使用由一级关系连接的节点的丰富数据模型,它比传统 RDBMS 方法更适合连接的大数据。Spring Boot 提供了多种便利措施来处理 Neo4j,包括 spring-boot-starter-data-neo4j
“Starter”。
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”.
Connecting to a Neo4j Database
要访问 Neo4j 服务器,您可以注入一个自动配置的 org.neo4j.driver.Driver
。默认情况下,该实例尝试使用 Bolt 协议连接到 localhost:7687`的 Neo4j 服务器。以下示例显示了如何注入 Neo4j `Driver
,它能让您访问 Session
,以及其他内容:
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
:
可以使用 @1
属性配置驱动程序的各方面。以下示例演示如何配置要使用的 URI 和凭证:
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:
spring: neo4j: uri: "bolt://my-server:7687" authentication: username: "neo4j" password: "secret"
自动配置的 @2
是使用 @3
创建的。要微调其配置,请声明一个或多个 @4
Bean。每个 bean 都会按顺序使用 @5
对 @6
进行构建。
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
.
Spring Data Neo4j Repositories
Spring Data 包括 Neo4j 存储库支持。有关 Spring Data Neo4j 的完整详细信息,请参阅 {url-spring-data-neo4j-docs}[参考文档]。
Spring Data includes repository support for Neo4j. For complete details of Spring Data Neo4j, see the {url-spring-data-neo4j-docs}[reference documentation].
Spring Data Neo4j 与 Spring Data JPA 共享通用基础架构,与许多其他 Spring Data 模块一样。你可以获取前面的 JPA 示例,并将 @7
定义为 Spring Data Neo4j @8
而不是 JPA @9
,存储库抽象工作方式相同,如下面的示例中所示:
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:
@11
@10
启用了存储库支持以及事务管理。Spring Boot 使用 @12
或 @13
Bean 支持经典和响应式 Neo4j 存储库。当类路径上提供 Project Reactor 时,还会自动配置响应式样式。
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.
通过扫描找到存储库和实体。默认情况下,会扫描 @16
。你可以使用 @14
和 @15
分别自定义查找存储库和实体的位置。
Repositories and entities are found through scanning.
By default, the auto-configuration packages are scanned.
You can customize the locations to look for repositories and entities by using @EnableNeo4jRepositories
and @EntityScan
respectively.
在使用响应式样式的应用程序中,不会自动配置 In an application using the reactive style, a |
Elasticsearch
@19
是一个开源、分布式、RESTful 搜索和分析引擎。Spring Boot 为 Elasticsearch 客户端提供了基本的自动配置。
Elasticsearch is an open source, distributed, RESTful search and analytics engine. Spring Boot offers basic auto-configuration for Elasticsearch clients.
Spring Boot 支持多个客户端:
Spring Boot supports several clients:
-
The official low-level REST client
-
The official Java API client
-
The
ReactiveElasticsearchClient
provided by Spring Data Elasticsearch
Spring Boot 提供了一个专门的 @21
, @22
。
Spring Boot provides a dedicated “Starter”, spring-boot-starter-data-elasticsearch
.
Connecting to Elasticsearch Using REST clients
Elasticsearch 提供了两个不同的 REST 客户端,你可以使用它们来查询集群:来自 @23
模块的 @28
和来自 @24
模块的 @29
。此外,Spring Boot 提供了对来自 @25
模块的响应式客户端的支持。默认情况下,客户端将针对 @26
。你可以使用 @27
属性进一步微调客户端的配置,如下面的示例所示:
Elasticsearch ships two different REST clients that you can use to query a cluster: the low-level client from the org.elasticsearch.client:elasticsearch-rest-client
module and the Java API client from the co.elastic.clients:elasticsearch-java
module.
Additionally, Spring Boot provides support for a reactive client from the org.springframework.data:spring-data-elasticsearch
module.
By default, the clients will target http://localhost:9200
.
You can use spring.elasticsearch.*
properties to further tune how the clients are configured, as shown in the following example:
spring: elasticsearch: uris: "https://search.example.com:9200" socket-timeout: "10s" username: "user" password: "secret"
Connecting to Elasticsearch Using RestClient
如果你在类路径上有 @30
,Spring Boot 将自动配置并注册一个 @31
Bean。除了前面描述的属性之外,为了微调 @32
,你可以注册任意数量实现 @33
的 Bean,以进行更高级别的自定义。要完全控制客户端的配置,请定义一个 @34
Bean。
If you have elasticsearch-rest-client
on the classpath, Spring Boot will auto-configure and register a RestClient
bean.
In addition to the properties described previously, to fine-tune the RestClient
you can register an arbitrary number of beans that implement RestClientBuilderCustomizer
for more advanced customizations.
To take full control over the clients' configuration, define a RestClientBuilder
bean.
此外,如果类路径上存在 @35
,则会自动配置 @36
,以从正在运行的 Elasticsearch 集群中自动发现节点并将它们设置在 @37
Bean 上。你可以进一步微调 @38
的配置,如下面的示例所示:
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 on the RestClient
bean.
You can further tune how Sniffer
is configured, as shown in the following example:
spring: elasticsearch: restclient: sniffer: interval: "10m" delay-after-failure: "30s"
Connecting to Elasticsearch Using ElasticsearchClient
如果你在类路径上有 @39
,Spring Boot 将自动配置并注册 @40
Bean。
If you have co.elastic.clients:elasticsearch-java
on the classpath, Spring Boot will auto-configure and register an ElasticsearchClient
bean.
{ElasticsearchClient
} 使用依赖于前面所述的 {RestClient
} 的传输。因此,先前述及的属性可用来配置 {ElasticsearchClient
}。另外,您可以定义一个 {RestClientOptions
} bean 以进一步控制传输的行为。
The ElasticsearchClient
uses a transport that depends upon the previously described RestClient
.
Therefore, the properties described previously can be used to configure the ElasticsearchClient
.
Furthermore, you can define a RestClientOptions
bean to take further control of the behavior of the transport.
Connecting to Elasticsearch using ReactiveElasticsearchClient
{@ url-spring-data-elasticsearch-site}[Spring Data Elasticsearch]} 提供 {ReactiveElasticsearchClient
} 用以以响应式方式查询 Elasticsearch 实例。如果您在类路径中有 Spring Data Elasticsearch 和 Reactor,Spring Boot 将自动配置并注册一个 {ReactiveElasticsearchClient
}。
{url-spring-data-elasticsearch-site}[Spring Data Elasticsearch] ships ReactiveElasticsearchClient
for querying Elasticsearch instances in a reactive fashion.
If you have Spring Data Elasticsearch and Reactor on the classpath, Spring Boot will auto-configure and register a ReactiveElasticsearchClient
.
{ReactiveElasticsearchclient
} 使用依赖于前面所述的 {RestClient
} 的传输。因此,先前述及的属性可用来配置 {ReactiveElasticsearchClient
}。另外,您可以定义一个 {RestClientOptions
} bean 以进一步控制传输的行为。
The ReactiveElasticsearchclient
uses a transport that depends upon the previously described RestClient
.
Therefore, the properties described previously can be used to configure the ReactiveElasticsearchClient
.
Furthermore, you can define a RestClientOptions
bean to take further control of the behavior of the transport.
Connecting to Elasticsearch by Using Spring Data
要连接至 Elasticsearch,必须定义一个 {ElasticsearchClient
} bean,由 Spring Boot 自动配置或由应用程序手动提供(请参见前面的部分)。有了此配置,就可以注入一个 {ElasticsearchTemplate
} 就像注入任何其他 Spring bean 一样,如下例所示:
To connect to Elasticsearch, an ElasticsearchClient
bean must be defined,
auto-configured by Spring Boot or manually provided by the application (see previous sections).
With this configuration in place, an
ElasticsearchTemplate
can be injected like any other Spring bean,
as shown in the following example:
在存在 {spring-data-elasticsearch
} 和 Reactor 的情况下,Spring Boot 还可以将 {ReactiveElasticsearchClient} 和 {ReactiveElasticsearchTemplate
} 自动配置为 bean。它们是其他 REST 客户端的响应式等效项。
In the presence of spring-data-elasticsearch
and Reactor, Spring Boot can also auto-configure a ReactiveElasticsearchClient and a ReactiveElasticsearchTemplate
as beans.
They are the reactive equivalent of the other REST clients.
Spring Data Elasticsearch Repositories
Spring Data 包括对 Elasticsearch 的存储库支持。与先前讨论的 JPA 存储库一样,基本原理是基于方法名称自动为您构建查询。
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.
事实上,Spring Data JPA 和 Spring Data Elasticsearch 都共享相同的公共基础架构。您可以使用前文提到的 JPA 示例,假设 {City
} 现在是一个 Elasticsearch {@Document
} 类,而不是一个 JPA {@Entity
},它的工作方式是一样的。
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.
将通过扫描查找存储库和文档。默认情况下,将扫描 {auto-configuration packages}。您可以使用 {@EnableElasticsearchRepositories
} 和 {@EntityScan
} 分别来自定义查找存储库和文档的位置。
Repositories and documents are found through scanning.
By default, the auto-configuration packages are scanned.
You can customize the locations to look for repositories and documents by using @EnableElasticsearchRepositories
and @EntityScan
respectively.
有关 Spring Data Elasticsearch 的详细信息,请参见 {@ url-spring-data-elasticsearch-docs}[参考文档]。 |
For complete details of Spring Data Elasticsearch, see the {url-spring-data-elasticsearch-docs}[reference documentation]. |
Spring Boot 使用 {ElasticsearchRestTemplate
} 或 {ReactiveElasticsearchTemplate
} bean 支持经典和响应式 Elasticsearch 存储库。如果存在需要的依赖项,Spring Boot 很可能自动配置这些 bean。
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.
如果您希望使用自己的模板作为 Elasticsearch 存储库的后端,您可以添加自己的 {ElasticsearchRestTemplate
} 或 {ElasticsearchOperations
} {@Bean
},只要命名为 {"elasticsearchTemplate"
} 即可。与 {ReactiveElasticsearchTemplate
} 和 {ReactiveElasticsearchOperations
} 相同,bean 名称是 {"reactiveElasticsearchTemplate"
}。
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:
spring: data: elasticsearch: repositories: enabled: false
Cassandra
{ Cassandra} 是一个开源的分布式数据库管理系统,旨在跨多台商用服务器处理海量数据。Spring Boot 为 Cassandra 和它之上的抽象(由 {@ url-spring-data-cassandra-site}[Spring Data Cassandra]} 提供)提供了自动配置。有一个 {spring-boot-starter-data-cassandra
} {“Starter”} 用于以便捷的方式收集依赖项。
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 {url-spring-data-cassandra-site}[Spring Data Cassandra].
There is a spring-boot-starter-data-cassandra
“Starter” for collecting the dependencies in a convenient way.
Connecting to Cassandra
您可以注入自动配置的 {CassandraTemplate
} 或 Cassandra {CqlSession
} 实例,就像注入任何其他 Spring Bean 一样。{spring.cassandra.*
} 属性可用于自定连接。通常,您会提供 {keyspace-name
} 和 {contact-points
},以及本地数据中心名称,如下例所示:
You can inject an auto-configured CassandraTemplate
or a Cassandra CqlSession
instance as you would with any other Spring Bean.
The spring.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:
spring: 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:
spring: cassandra: keyspace-name: "mykeyspace" contact-points: "cassandrahost1,cassandrahost2" local-datacenter: "datacenter1"
这两个示例相同,因为端口默认为 { |
Those two examples are identical as the port default to |
可以将自动配置的 {CqlSession
} 配置为使用 SSL 与服务器通信,方法是按此示例所示设置属性:
The auto-configured CqlSession
can be configured to use SSL for communication with the server by setting the properties as shown in this example:
spring: cassandra: keyspace-name: "mykeyspace" contact-points: "cassandrahost1,cassandrahost2" local-datacenter: "datacenter1" ssl: enabled: true
可以在 {SSL bundle} 中配置自定义 SSL 信任材料,并将其应用于 {CqlSession
},如下例所示:
Custom SSL trust material can be configured in an SSL bundle and applied to the CqlSession
as shown in this example:
spring: cassandra: keyspace-name: "mykeyspace" contact-points: "cassandrahost1,cassandrahost2" local-datacenter: "datacenter1" ssl: bundle: "example"
Cassandra 驱动程序有它自己的配置基础架构,该基础架构在类路径的根目录加载一个 { The Cassandra driver has its own configuration infrastructure that loads an Spring Boot 在默认情况下不会查找此类文件,但是可以使用 Spring Boot does not look for such a file by default but can load one using 针对更高级的驱动器自定义,您可以注册实现 For more advanced driver customizations, you can register an arbitrary number of beans that implement |
如果您使用 |
If you use |
以下代码清单展示了如何注入一个 Cassandra Bean:
The following code listing shows how to inject a Cassandra bean:
如果您添加自己类型为 CassandraTemplate
的 @Bean
,它将替换默认值。
If you add your own @Bean
of type CassandraTemplate
, it replaces the default.
Spring Data Cassandra Repositories
Spring Data 包含用于 Cassandra 的基本存储库支持。目前,这比前面讨论的 JPA 存储库更为受限,需要 @Query
带注释的查找器方法。
Spring Data includes basic repository support for Cassandra.
Currently, this is more limited than the JPA repositories discussed earlier and needs @Query
annotated finder methods.
存储库和实体会通过扫描找到。默认情况下,会扫描 auto-configuration packages。您可以通过分别使用 @EnableCassandraRepositories
和 @EntityScan
,自定义查找存储库和实体的位置。
Repositories and entities are found through scanning.
By default, the auto-configuration packages are scanned.
You can customize the locations to look for repositories and entities by using @EnableCassandraRepositories
and @EntityScan
respectively.
有关 Spring Data Cassandra 的完整详细信息,请参见 reference documentation。 |
For complete details of Spring Data Cassandra, see the reference documentation. |
Couchbase
Couchbase 是一款开源、分布式、多模型非 SQL 面向文档的数据库,它针对交互式应用程序进行了优化。Spring Boot 为 Couchbase 和基于其之上由 Spring Data Couchbase 提供的抽象提供自动配置。有 spring-boot-starter-data-couchbase
和 spring-boot-starter-data-couchbase-reactive
“Starters”,用于以一种方便的方式收集依赖关系。
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 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.
Connecting to Couchbase
您可以通过添加 Couchbase SDK 以及一些配置来获取一个 Cluster
。可以使用 spring.couchbase.*
属性来自定义连接。通常,您需要提供 connection string、用户名和密码,如下面的示例所示:
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 connection string, username, and password, as shown in the following example:
spring: couchbase: connection-string: "couchbase://192.168.1.123" username: "user" password: "secret"
还可以自定义某些 ClusterEnvironment
设置。例如,以下配置将打开一个新的 Bucket
的超时,并利用对一个已配置 SSL bundle 的引用启用 SSL 支持:
It is also possible to customize some of the ClusterEnvironment
settings.
For instance, the following configuration changes the timeout to open a new Bucket
and enables SSL support with a reference to a configured SSL bundle:
spring: couchbase: env: timeouts: connect: "3s" ssl: bundle: "example"
查看 |
Check the |
Spring Data Couchbase Repositories
Spring Data 包含 Couchbase 的存储库支持。
Spring Data includes repository support for Couchbase.
存储库和文档会通过扫描找到。默认情况下,会扫描 auto-configuration packages。您可以通过分别使用 @EnableCouchbaseRepositories
和 @EntityScan
,自定义查找存储库和文档的位置。
Repositories and documents are found through scanning.
By default, the auto-configuration packages are scanned.
You can customize the locations to look for repositories and documents by using @EnableCouchbaseRepositories
and @EntityScan
respectively.
有关 Spring Data Couchbase 的完整详细信息,请参见 {url-spring-data-couchbase-docs}[参考文档]。
For complete details of Spring Data Couchbase, see the {url-spring-data-couchbase-docs}[reference documentation].
您可以注入自动配置的 CouchbaseTemplate
实例,这与您使用任何其他 Spring Bean 的方式相同,前提是提供了 CouchbaseClientFactory
Bean。当一个 Cluster
可用时会发生这种情况,如上所述,并且已经指定了一个 Bucket 名称:
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:
spring: data: couchbase: bucket-name: "my-bucket"
以下示例展示了如何注入一个 CouchbaseTemplate
Bean:
The following examples shows how to inject a CouchbaseTemplate
bean:
您可以在自己的配置中定义一些 Bean,以覆盖自动配置所提供的 Bean:
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 ofcouchbaseMappingContext
. -
A
CustomConversions
@Bean
with a name ofcouchbaseCustomConversions
. -
A
CouchbaseTemplate
@Bean
with a name ofcouchbaseTemplate
.
为了避免在自己的配置中硬编码这些名称,你可以重用 Spring Data Couchbase 提供的 BeanNames
。例如,你可以自定义要使用的转换器,如下所示:
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:
LDAP
LDAP(轻量级目录访问协议)是一个开放的、供应商中立的、行业标准应用程序协议,用于通过 IP 网络访问和维护分布式目录信息服务。Spring Boot 为任何兼容的 LDAP 服务器提供自动配置,并支持 UnboundID 中的嵌入式内存中 LDAP 服务器。
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 UnboundID.
LDAP 抽象由 Spring Data LDAP 提供。有一个 spring-boot-starter-data-ldap
“Starter” 可用于便捷地收集依赖项。
LDAP abstractions are provided by Spring Data LDAP.
There is a spring-boot-starter-data-ldap
“Starter” for collecting the dependencies in a convenient way.
Connecting to an LDAP Server
若要连接到 LDAP 服务器,请确保声明对 spring-boot-starter-data-ldap
“Starter” 或 spring-ldap-core
的依赖,然后在 application.properties 中声明服务器的 URL,如下例所示:
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:
spring: ldap: urls: "ldap://myserver:1235" username: "admin" password: "secret"
如果需要自定义连接设置,则可以使用 spring.ldap.base
和 spring.ldap.base-environment
属性。
If you need to customize connection settings, you can use the spring.ldap.base
and spring.ldap.base-environment
properties.
基于这些设置,会自动配置一个 LdapContextSource
。如果 DirContextAuthenticationStrategy
bean 可用,则将其与自动配置的 LdapContextSource
关联。如果你需要自定义它(例如为了使用 PooledContextSource
),你仍然可以注入自动配置的 LdapContextSource
。确保将自定义的 ContextSource
标记为 @Primary
,这样自动配置的 LdapTemplate
就会使用它。
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.
Spring Data LDAP Repositories
Spring Data 包含对 LDAP 的存储库支持。
Spring Data includes repository support for LDAP.
通过扫描找到存储库和文档。默认情况下,扫描 auto-configuration packages。你可以分别使用 @EnableLdapRepositories
和 @EntityScan
来自定义要查找存储库和文档的位置。
Repositories and documents are found through scanning.
By default, the auto-configuration packages are scanned.
You can customize the locations to look for repositories and documents by using @EnableLdapRepositories
and @EntityScan
respectively.
有关 Spring Data LDAP 的完整详细信息,请参阅 reference documentation。
For complete details of Spring Data LDAP, see the reference documentation.
你还可以注入自动配置的 LdapTemplate
实例,就像你使用任何其他 Spring Bean 一样,如下例所示:
You can also inject an auto-configured LdapTemplate
instance as you would with any other Spring Bean, as shown in the following example:
Embedded In-memory LDAP Server
出于测试目的,Spring Boot 支持自动配置 UnboundID 中的内存中 LDAP 服务器。若要配置服务器,请添加对 com.unboundid:unboundid-ldapsdk
的依赖,并声明一个 configprop:spring.ldap.embedded.base-dn[] 属性,如下所示:
For testing purposes, Spring Boot supports auto-configuration of an in-memory LDAP server from 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:
spring: ldap: embedded: base-dn: "dc=spring,dc=io"
可以定义多个基本 DN 值,但由于 DN 通常包含逗号,因此必须使用正确的符号来定义它们。 It is possible to define multiple base-dn values, however, since distinguished names usually contain commas, they must be defined using the correct notation. 在 Yaml 文件中,可以使用 Yaml 列表符号。在属性文件中,必须将索引包含在属性名称中: In yaml files, you can use the yaml list notation. In properties files, you must include the index as part of the property name: spring.ldap.embedded.base-dn: - "dc=spring,dc=io" - "dc=vmware,dc=com" |
默认情况下,服务器在随机端口上启动并触发常规 LDAP 支持。无需指定 configprop:spring.ldap.urls[] 属性。
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.
如果在你的类路径上存在一个 schema.ldif
文件,它将用于初始化服务器。如果你想从其他资源中加载初始化脚本,你也可以使用 configprop:spring.ldap.embedded.ldif[] 属性。
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.
默认情况下,一个标准模式用于验证 LDIF
文件。你可以通过设置 configprop:spring.ldap.embedded.validation.enabled[] 属性完全关闭验证。如果你有自定义属性,你可以使用 configprop:spring.ldap.embedded.validation.schema[] 定义你自定义的属性类型或对象类。
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.
InfluxDB
用于 InfluxDB 的自动配置已被弃用,计划在 Spring Boot 3.4 中删除,以支持 the new InfluxDB Java client,它提供了它自己的 Spring Boot 集成。
Auto-configuration for InfluxDB is deprecated and scheduled for removal in Spring Boot 3.4 in favor of the new InfluxDB Java client that provides its own Spring Boot integration.
InfluxDB 是一个开源的时间序列数据库,针对快速、高可用存储和检索诸如操作监控、应用程序指标、物联网传感器数据和实时分析等领域的时间序列数据进行了优化。
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.
Connecting to InfluxDB
Spring Boot 自动配置一个 InfluxDB
实例,前提是 influxdb-java
客户端在类路径中,并且数据库的 URL 使用 configprop:spring.influx.url[已弃用] 设置。
Spring Boot auto-configures an InfluxDB
instance, provided the influxdb-java
client is on the classpath and the URL of the database is set using configprop:spring.influx.url[deprecated].
如果连接到 InfluxDB 需要一个用户和密码,你就可以相应地设置 configprop:spring.influx.user[已弃用] 和 configprop:spring.influx.password[已弃用] 属性。
If the connection to InfluxDB requires a user and password, you can set the configprop:spring.influx.user[deprecated] and configprop:spring.influx.password[deprecated] properties accordingly.
InfluxDB 依赖于 OkHttp。如果你需要调整 http 客户端在幕后使用的 InfluxDB
,你可以注册一个 InfluxDbOkHttpClientBuilderProvider
Bean。
InfluxDB relies on OkHttp.
If you need to tune the http client InfluxDB
uses behind the scenes, you can register an InfluxDbOkHttpClientBuilderProvider
bean.
如果你需要更多地控制配置,请考虑注册一个 InfluxDbCustomizer
Bean。
If you need more control over the configuration, consider registering an InfluxDbCustomizer
bean.