PostgreSQL jdbc 9.4 支持load balance 和 connection failover了

Postgres2015全国用户大会将于11月20至21日在北京丽亭华苑酒店召开。本次大会嘉宾阵容强大,国内顶级PostgreSQL数据库专家将悉数到场,并特邀欧洲、俄罗斯、日本、美国等国家和地区的数据库方面专家助阵:

  • Postgres-XC项目的发起人铃木市一(SUZUKI Koichi)
  • Postgres-XL的项目发起人Mason Sharp
  • pgpool的作者石井达夫(Tatsuo Ishii)
  • PG-Strom的作者海外浩平(Kaigai Kohei)
  • Greenplum研发总监姚延栋
  • 周正中(德哥), PostgreSQL中国用户会创始人之一
  • 汪洋,平安科技数据库技术部经理
  • ……
 
  • 2015年度PG大象会报名地址:http://postgres2015.eventdove.com/
  • PostgreSQL中国社区: http://postgres.cn/
  • PostgreSQL专业1群: 3336901(已满)
  • PostgreSQL专业2群: 100910388
  • PostgreSQL专业3群: 150657323



好消息,PostgreSQL的jdbc驱动支持load balance 和 connection failover了。

通过配置多对主机和端口的信息,可以实现简单的连接FALIOVER,直到取到一个正常的连接为止。

Connection Fail-over
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 to once connect to each of them in order until the connection succeeds. If none succeed, a normal connection exception is thrown.
The syntax for the connection url is:
jdbc:postgresql://host1:port1,host2:port2/database
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.
For example an application can create two connection pools. One data source is for writes, another for reads. 

结合 targetServerType,可以非常方便的实现负载均衡,对于读写数据源,配置为master,
对于只读数据源可以配置为 preferSlave或者slave。

The write pool limits connections only to master node:
jdbc:postgresql://node1,node2,node3/accounting?targetServerType=master . 

And read pool balances connections between slaves nodes, but allows connections also to master if no slaves are available:
jdbc:postgresql://node1,node2,node3/accounting?targetServerType=preferSlave&loadBalanceHosts=true

targetServerType
Allows opening connections to only servers with required state, the allowed values are any, master, slave and preferSlave. The master/slave distinction is currently done by observing if the server allows writes. The value preferSlave tries to connect to slaves if any are available, otherwise allows falls back to connecting also to master.

loadBalanceHosts = boolean
In default mode (disabled) hosts are connected in the given order. If enabled hosts are chosen randomly from the set of suitable candidates.


使用这种方法的好处大大滴,举个例子:
node1,node2,node3使用流复制组成了一主2备的环境,其中有一个VIP,对应到主节点,所有的recover.conf都指向这个VIP。(这个VIP只是方便recover.conf的统一性,其实jdbc的failover和load balance都不依赖它)
然后我们在配置jdbc时,只需要配置2个数据源,一个对应master一个对应preferSlave。
需要写操作时,指定master的数据源,需要读操作时,使用preferSlave的数据源。

那么JDBC是怎么判断数据源的类型的呢?
其实很简单,数据库提供了pg_is_in_recovery()函数,true就是slave, false就是master。jdbc应该就是用它来判断的。
(pgpool-II也是这么干的)

jdbc-HA比pgpool-II只差了自动的读写分离这个功能,但是这块在应用层其实很好控制,开发应用时,大家保持好配置2个数据源的这个姿势吧。

或者你可以把jdbc9.4和plproxy,或者pg-xc的coordinator节点结合来使用,实现完全的负载均衡和failover。
因为plproxy节点和coordinator节点都是对等节点,所以非常适合用来做负载均衡,以前我们可能需要结合LVS来达到类似的目的,有了jdbc 9.4不需要了。

[参考]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值