第 11 章 在 Data Grid 服务器中添加受管数据源
通过在 Data Grid 服务器配置中添加受管数据源,为 JDBC 数据库连接优化连接池和性能。
11.1. 配置受管数据源 复制链接链接已复制到粘贴板!
作为 Data Grid 服务器配置的一部分创建受管数据源,以优化 JDBC 数据库连接的连接池和性能。然后,您可以在缓存中指定受管数据源的 JDNI 名称,它将为您的部署集中 JDBC 连接配置。
先决条件
将数据库驱动程序复制到 Data Grid Server 安装中的
server/lib目录中。提示使用带有 Data Grid 命令行界面(CLI)的
install命令,将所需的驱动程序下载到server/lib目录中,例如:install org.postgresql:postgresql:42.4.3
流程
- 打开 Data Grid Server 配置以进行编辑。
-
在
。data-sources部分添加新的数据源 -
使用
name属性或字段唯一标识数据源。 使用
jndi-name属性或字段为数据源指定 JNDI 名称。提示您可以使用 JNDI 名称在 JDBC 缓存存储配置中指定数据源。
-
将
true设置为statistics属性或字段的值,以通过/metrics端点启用数据源的统计信息。 提供 JDBC 驱动程序详细信息,用于定义如何在
connection-factory部分中连接到数据源。-
使用
driver属性或字段指定数据库驱动程序的名称。 -
使用 url 属性或字段指定 JDBC 连接
url。 -
使用
凭证。用户名和密码属性或字段指定 - 根据需要提供任何其他配置。
-
使用
-
定义 Data Grid Server 节点如何池,并在
connection-pool部分中定义连接池调优属性的连接。 - 保存对配置的更改。
验证
使用 Data Grid 命令行界面(CLI)测试数据源连接,如下所示:
启动 CLI 会话。
bin/cli.sh列出所有数据源,并确认您创建的数据源可用。
server datasource ls测试数据源连接。
server datasource test my-datasource
受管数据源配置
XML
<server xmlns="urn:infinispan:server:15.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