postgresql 初始化驱动支持高可用配置

条件

  • jdk 1.6及以上

配置

 jdbc:postgresql://host1:port1,host2:port2/database

连接故障转移

To support simple connection fail-over it is possible to define multiple endpoints (host and port pairs) in the connection url separated by commas. The driver will try once to connect to each of them in order until the connection succeeds. If none succeeds a normal connection exception is thrown.

为了支持简单的故障转移, 可以配置多个端点(host:port),多个端点之间以逗号隔开。驱动程序将尝试按顺序连接到它们中的每一个,直到连接成功。如果没有成功,则引发正常连接异常。

The simple connection fail-over is useful when running against a high availability postgres installation that has identical data on each node. For example streaming replication postgres or postgres-xc cluster.

当每个节点上具有相同数据的高可用性时,简单的连接故障转移非常有用。例如,流式复制postgres或postgres-xc集群。

For example an application can create two connection pools. One data source is for writes, another for reads. The write pool limits connections only to a primary node:jdbc:postgresql://node1,node2,node3/accounting?targetServerType=primary .

例如,一个应用程序可以创建两个连接池。一个数据源用于写入,另一个用于读取。写池仅限制与主节点的连接:jdbc:postgresql://node1,node2,node3/accounting?targetServerType=primary

And the read pool balances connections between secondary nodes, but allows connections also to a primary if no secondaries are available: jdbc:postgresql://node1,node2,node3/accounting?targetServerType=preferSecondary&loadBalanceHosts=true

读池平衡辅助节点之间的连接,但如果没有辅助节点可用,也允许连接到主节点:jdbc:postgresql://node1,node2,node3/accounting?targetServerType=preferSecondary&loadBalanceHosts=true

If a secondary fails, all secondaries in the list will be tried first. In the case that there are no available secondaries the primary will be tried. If all the servers are marked as “can’t connect” in the cache then an attempt will be made to connect to all the hosts in the URL, in order.

如果辅助节点失败,将首先尝试列表中的所有辅助节点。如果没有可用的辅助节点,将尝试使用主节点。如果所有服务器在缓存中都标记为“无法连接”,则将尝试按顺序连接到URL中的所有节点。

参考文献 https://jdbc.postgresql.org/documentation/use/#connection-fail-over

你可能感兴趣的:(postgresql,数据库)