简体   繁体   English

Mysql ReplicationDriver-故障处理

[英]Mysql ReplicationDriver - failure handling

I have a MySql Master/Slave replication question that google couldn't seem to answer. 我有一个MySql Master / Slave复制问题,谷歌似乎无法回答。 When using com.mysql.jdbc.ReplicationDriver, how does the driver handle failures on read replicas? 使用com.mysql.jdbc.ReplicationDriver时,驱动程序如何处理只读副本上的故障? Does it blacklist them, does it try just continue to try them and throw an exception each time (after whatever timeouts are configured)? 它是否将它们列入黑名单,是否尝试继续尝试它们并每次(在配置了任何超时之后)都抛出异常? From my testing it seems that my application is just hanging when I kill a read replica. 从我的测试看来,当我杀死一个只读副本时,我的应用程序刚刚挂起。 I'm using tomcat and here is my context.xml.... 我正在使用tomcat,这是我的context.xml。

<Resource auth="Container" 
        driverClassName="com.mysql.jdbc.ReplicationDriver" 
        defaultAutoCommit="false"
        initialSize="10" 
        minIdle="5"
        logAbandoned="false"
        maxIdle="10" 
        maxWait="10000" 
        name="jdbc/db" 
        removeAbandoned="true" 
        testOnBorrow="true"  
        removeAbandonedTimeout="86400"
        testWhileIdle="true" 
        type="javax.sql.DataSource" 
        factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" 
        username="powerptc" 
        password="password"   
        url="jdbc:mysql:replication://localhost:3306,host1,host2:3306/db?allowSlavesDownConnections=true&amp;readFromMasterWhenNoSlaves=true" 
        validationQuery="/* ping */ SELECT 1"
        validationQueryTimeout="5" />

Is there a way to have the driver blacklist a failed read replica ( for x minutes ) instead of just retrying it over and over again? 有没有办法让驱动程序将失败的只读副本列入黑名单(持续x分钟),而不是一次又一次地重试它?

In this case MySQL driver uses LoadBalanced driver for slaves and switch to master only if picking connection from LoadBalanced cluster of slaves fails. 在这种情况下,MySQL驱动程序将LoadBalanced驱动程序用于从属,并仅当从LoadBalanced从属群集选择连接失败时才切换到主驱动程序 Application hanging because default value for retriesAllDown = 120 . 应用程序挂起,因为retriesAllDown = 120默认值为retriesAllDown = 120 If you set retriesAllDown = 4 , then Load Balancer will sleep 4 times for 250 milliseconds before switching to master. 如果您将retriesAllDown = 4设置retriesAllDown = 4 ,那么负载平衡器将在进入主机之前休眠4次,持续250毫秒

By default loadBalanceBlacklistTimeout = 0 , it means that load balancer for slaves does not use blacklist. 默认情况下, loadBalanceBlacklistTimeout = 0 ,这意味着从站的负载平衡器不使用黑名单。 Even if you set loadBalanceBlacklistTimeout > 0 , it does not help, because strange implementation of blacklist, which is empty if all hosts are added to blacklist. 即使将loadBalanceBlacklistTimeout > 0设置loadBalanceBlacklistTimeout > 0 ,它也无济于事,因为黑名单的实现很奇怪,如果将所有主机都添加到黑名单,则黑名单是空的。 But you can use next trick: Use ServerAffinityStrategy and put master hostname to slaves list, but set only slaves as affinity servers. 但是您可以使用下一个技巧:使用ServerAffinityStrategy并将主主机名放入从属列表,但仅将从属服务器设置为相似性服务器。

My working url is: 我的工作网址是:

jdbc:mysql:replication://master:3306,slave1,slave2:3306/db?allowSlaveDownConnections=true&readFromMasterWhenNoSlaves=true&loadBalanceBlacklistTimeout=30000&retriesAllDown=4&loadBalanceStrategy=serverAffinity&serverAffinityOrder=slave1,slave2

In result, master will be used only if there is no available slave 结果,仅当没有可用的从属设备时才使用主设备

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM