搜索

第 10 章 将受管数据源添加到 Data Grid 服务器

download PDF

通过在 Data Grid 服务器配置中添加受管数据源,优化 JDBC 数据库连接的连接池和性能。

10.1. 配置受管数据源

作为 Data Grid 服务器配置的一部分创建受管 datasources,以优化 JDBC 数据库连接的连接池和性能。然后,您可以在缓存中指定受管数据源的 JDNI 名称,这会集中部署的 JDBC 连接配置。

先决条件

  • 将数据库驱动程序复制到 Data Grid Server 安装中的 server/lib 目录中。

    提示

    使用 install 命令和 Data Grid 命令行界面(CLI)将所需的驱动程序下载到 server/lib 目录,例如:

    install org.postgresql:postgresql:42.1.3

流程

  1. 打开 Data Grid Server 配置进行编辑。
  2. 将新的 数据源添加到 data- sources 部分。
  3. 通过 name 属性或字段唯一标识数据源。
  4. 使用 jndi-name 属性或字段为数据源指定 JNDI 名称。

    提示

    您可以使用 JNDI 名称在 JDBC 缓存存储配置中指定数据源。

  5. true 设置为 statistics 属性或字段的值,以通过 /metrics 端点为数据源启用统计。
  6. 提供 JDBC 驱动程序详细信息,以定义如何在 connection-factory 部分中定义数据源连接。

    1. 使用 driver 属性或字段指定数据库驱动程序的名称。
    2. 使用 url 属性或字段指定 JDBC 连接 url
    3. 使用 用户名和密码 属性或字段指定凭证。
    4. 根据需要提供任何其他配置。
  7. connection-pool 部分中,定义 Data Grid Server 节点池并重复使用与连接池调整属性的连接。
  8. 保存对您的配置的更改。

验证

使用 Data Grid 命令行界面(CLI)测试数据源连接,如下所示:

  1. 启动 CLI 会话。

    bin/cli.sh
  2. 列出所有数据源,并确认您创建的数据源可用。

    server datasource ls
  3. 测试数据源连接。

    server datasource test my-datasource

管理的数据源配置

XML

<server xmlns="urn:infinispan:server:14.0">
  <data-sources>
     <!-- Defines a unique name for the datasource and JNDI name that you
          reference in JDBC cache store configuration.
          Enables statistics for the datasource, if required. -->
     <data-source name="ds"
                  jndi-name="jdbc/postgres"
                  statistics="true">
        <!-- Specifies the JDBC driver that creates connections. -->
        <connection-factory driver="org.postgresql.Driver"
                            url="jdbc:postgresql://localhost:5432/postgres"
                            username="postgres"
                            password="changeme">
           <!-- Sets optional JDBC driver-specific connection properties. -->
           <connection-property name="name">value</connection-property>
        </connection-factory>
        <!-- Defines connection pool tuning properties. -->
        <connection-pool initial-size="1"
                         max-size="10"
                         min-size="3"
                         background-validation="1000"
                         idle-removal="1"
                         blocking-timeout="1000"
                         leak-detection="10000"/>
     </data-source>
  </data-sources>
</server>

JSON

{
  "server": {
    "data-sources": [{
      "name": "ds",
      "jndi-name": "jdbc/postgres",
      "statistics": true,
      "connection-factory": {
        "driver": "org.postgresql.Driver",
        "url": "jdbc:postgresql://localhost:5432/postgres",
        "username": "postgres",
        "password": "changeme",
        "connection-properties": {
          "name": "value"
        }
      },
      "connection-pool": {
        "initial-size": 1,
        "max-size": 10,
        "min-size": 3,
        "background-validation": 1000,
        "idle-removal": 1,
        "blocking-timeout": 1000,
        "leak-detection": 10000
      }
    }]
  }
}

YAML

server:
  dataSources:
    - name: ds
      jndiName: 'jdbc/postgres'
      statistics: true
      connectionFactory:
        driver: "org.postgresql.Driver"
        url: "jdbc:postgresql://localhost:5432/postgres"
        username: "postgres"
        password: "changeme"
        connectionProperties:
          name: value
      connectionPool:
        initialSize: 1
        maxSize: 10
        minSize: 3
        backgroundValidation: 1000
        idleRemoval: 1
        blockingTimeout: 1000
        leakDetection: 10000

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.