您的位置:首页 > 数据库

PostgreSQL jdbc 9.4 支持load balance 和 connection failover了

2015-10-14 21:47 633 查看


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群: 100910388PostgreSQL专业3群: 150657323


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

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

Connection Fail-overTo 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/databaseThe 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
targetServerTypeAllows 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 = booleanIn 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不需要了。

[参考]
1. https://jdbc.postgresql.org/documentation/94/connect.html#connection-parameters

2. http://ha-jdbc.github.io/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  postgresql 数据库 jdbc