Dev Services for MongoDB
Quarkus 支持一项称为 Dev Services 的功能,该功能允许您无需任何配置创建各种数据源。对于 MongoDB,此支持扩展到默认 MongoDB 连接。这在实践中意味着,如果您尚未配置 quarkus.mongodb.connection-string
,Quarkus 将在运行测试或处于开发模式时自动启动 MongoDB 容器,并自动配置连接。
Quarkus supports a feature called Dev Services that allows you to create various datasources without any config. In the case of MongoDB this support extends to the default MongoDB connection.
What that means practically, is that if you have not configured quarkus.mongodb.connection-string
, Quarkus will automatically start a MongoDB container when running tests or in dev mode,
and automatically configure the connection.
MongoDB Dev Services 基于 Testcontainers MongoDB module,它将启动一个单节点副本集。
MongoDB Dev Services is based on Testcontainers MongoDB module that will start a single node replicaset.
在运行应用程序的生产版本时,需要像往常一样配置 MongoDB 连接,因此如果您想在 application.properties
中包含生产数据库配置并继续使用 Dev Services,我们建议您使用 %prod.
配置文件来定义 MongoDB 设置。
When running the production version of the application, the MongoDB connection need to be configured as normal, so if you want to include a production database config in your
application.properties
and continue to use Dev Services we recommend that you use the %prod.
profile to define your MongoDB settings.
Shared server
大多数时候,您需要在应用程序之间共享服务器。MongoDB 的 Dev Services 为以 dev 模式运行的多个 Quarkus 应用程序实现了一个 service discovery 机制,以共享单个服务器。
Most of the time you need to share the server between applications. Dev Services for MongoDB implements a service discovery mechanism for your multiple Quarkus applications running in dev mode to share a single server.
MongoDB Dev Services 会启动带有 |
Dev Services for MongoDB starts the container with the |
如果您需要多个(共享的)服务器,可以配置 quarkus.mongodb.devservices.service-name
属性并指明服务器名称。它会查找带有相同值的容器,或者在找不到容器时启动一个新容器。默认服务名称为 mongodb
。
If you need multiple (shared) servers, you can configure the quarkus.mongodb.devservices.service-name
attribute and indicate the server name.
It looks for a container with the same value, or starts a new one if none can be found.
The default service name is mongodb
.
共享在开发模式中默认启用,但在测试模式中禁用。您可以使用 quarkus.mongodb.devservices.shared=false
禁用共享。
Sharing is enabled by default in dev mode, but disabled in test mode.
You can disable the sharing with quarkus.mongodb.devservices.shared=false
.