Docs 菜单
Docs 主页
/ / /
Java Reactive Streams 驱动程序
/

连接至 MongoDB

在此页面上

  • 先决条件
  • MongoClient
  • 连接到独立运行的实例 MongoDB 部署
  • 连接到副本集
  • 连接到分片集群
  • 连接选项

本指南介绍如何使用 Java Reactive Streams 驱动程序连接到 MongoDB。

使用 MongoClients.create()方法连接到正在运行的 MongoDB 部署。

重要

以下示例并未提供实例化MongoClient的方法的详尽列表。 有关MongoClient 工厂方法的完整列表,请参阅 MongoClients API 文档。

注意

我们强烈建议为系统 keep-alive 设置配置较短的超时时间。

请参阅TCP keepalive 时间是否影响 MongoDB 部署?服务器手册常见问题解答中的问题与解答,了解更多信息。

您必须设置以下组件才能运行本指南中的代码示例:

  • 要连接的正在运行的 MongoDB 部署。例如,要连接到独立运行的实例,您必须有权访问正在运行的独立运行的实例。

  • 项目中已安装驱动程序依赖项。要了解如何安装驱动程序,请参阅安装指南。

  • 以下 import 语句:

    import com.mongodb.reactivestreams.client.MongoClients;
    import com.mongodb.reactivestreams.client.MongoClient;
    import com.mongodb.MongoClientSettings;
    import com.mongodb.ConnectionString;
    import com.mongodb.ServerAddress;
    import com.mongodb.MongoCredential;
    import java.util.Arrays;

MongoClient实例表示数据库的连接池。即使在运行多个并发操作时,也只需要一个MongoClient实例。

重要

通常,您仅为给定 MongoDB 部署(例如独立部署、副本集或分片集群)仅创建一个MongoClient实例,并在整个应用程序中使用该客户端。 但是,如果您确实创建了多个实例,请记住以下几点:

  • 所有资源使用限制(例如最大连接数)均适用于每个MongoClient实例。

  • 要丢弃实例,请调用MongoClient.close()方法来清理资源。

以下示例展示了连接到单个 MongoDB 部署的几种方法。

您可以通过以下方式连接到单个 MongoDB 部署:

  • 实例化一个不带任何参数的MongoClient对象,以连接到在本地主机上运行的 MongoDB 服务器的端口27017

    MongoClient mongoClient = MongoClients.create();
  • 显式指定hostname以连接到在指定主机的端口27017上运行的 MongoDB 实例:

    MongoClient mongoClient = MongoClients.create(
    MongoClientSettings.builder()
    .applyToClusterSettings(builder ->
    builder.hosts(Arrays.asList(new ServerAddress("hostOne"))))
    .build());
  • 显式指定hostnameport

    MongoClient mongoClient = MongoClients.create(
    MongoClientSettings.builder()
    .applyToClusterSettings(builder ->
    builder.hosts(Arrays.asList(new ServerAddress("hostOne", 27018))))
    .build());
  • 指定ConnectionString

    MongoClient mongoClient = MongoClients.create("mongodb://hostOne:27017");

要连接到副本集,必须为MongoClients.create()方法指定一个或多个成员。 要了解有关副本集的更多信息,请参阅 MongoDB Server手册中的 复制 。

注意

MongoDB 自动发现副本集中的主节点和从节点。

您可以通过在ConnectionString中指定节点来连接到 MongoDB 副本集。

以下示例显示如何指定副本集的三个成员:

MongoClient mongoClient = MongoClients.create("mongodb://host1:27017,host2:27017,host3:27017");

以下示例展示如何指定副本集的成员以及使用副本集名称的replicaSet选项:

MongoClient mongoClient = MongoClients.create("mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myReplicaSet");

以下示例显示如何指定与所有副本集节点相对应的ServerAddress实例列表:

MongoClient mongoClient = MongoClients.create(
MongoClientSettings.builder()
.applyToClusterSettings(builder ->
builder.hosts(Arrays.asList(
new ServerAddress("host1", 27017),
new ServerAddress("host2", 27017),
new ServerAddress("host3", 27017))))
.build());

要连接到分片集群,请为MongoClients.create()方法指定一个或多个mongos实例。 要了解有关分片集群的更多信息,请参阅MongoDB MongoDB Server手册中的分片。

您可以通过以下方式连接到单个mongos实例:

  • ConnectionString中指定主机名和端口:

    MongoClient mongoClient = MongoClients.create( "mongodb://localhost:27017" );
  • 如果 mongoslocalhost:27017 上运行,则排除连接string :

    MongoClient mongoClient = MongoClients.create();

您可以通过以下方式连接到多个mongos实例:

  • 指定ConnectionString以包含其主机名和端口:

    MongoClient mongoClient = MongoClients.create("mongodb://host1:27017,host2:27017");
  • 指定与每个实例对应的ServerAddress对象的列表:

    MongoClient mongoClient = MongoClients.create(
    MongoClientSettings.builder()
    .applyToClusterSettings(builder ->
    builder.hosts(Arrays.asList(
    new ServerAddress("host1", 27017),
    new ServerAddress("host2", 27017))))
    .build());

您可以使用ConnectionString和/或MongoClientSettings类型来指定连接设置。

例如,您可以在连接string中指定 TLS/SSL 和身份验证设置:

MongoClient mongoClient = MongoClients.create("mongodb://user1:pwd1@host1/?authSource=db1&ssl=true");

您还可以使用MongoClientSettings实例指定 TLS/SSL 和MongoCredential类型来存储身份验证信息:

String user; // the username
String database; // the name of the database in which the user is defined
char[] password; // the password as a character array
// ...
MongoCredential credential = MongoCredential.createCredential(user, database, password);
MongoClientSettings settings = MongoClientSettings.builder()
.credential(credential)
.applyToSslSettings(builder -> builder.enabled(true))
.applyToClusterSettings(builder ->
builder.hosts(Arrays.asList(new ServerAddress("host1", 27017))))
.build();
MongoClient mongoClient = MongoClients.create(settings);

在某些情况下,您可能需要将连接字符串与编程配置结合使用:

ConnectionString connectionString = new ConnectionString("mongodb://host1:27107,host2:27017/?ssl=true");
CommandListener myCommandListener = ...;
MongoClientSettings settings = MongoClientSettings.builder()
.addCommandListener(myCommandListener)
.applyConnectionString(connectionString)
.build();
MongoClient mongoClient = MongoClients.create(settings);

后退

教程

来年

TLS/SSL