1

我正在尝试通过包装在 dbcp 的 basicDatasource 中的 jdbc mysql 驱动程序连接到 mysql 集群。

这是我的 bean 配置:

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql:loadbalance://slave1:3306,slave2:3306/mobile_detection"/>
      <property name="username" value="username"/>
      <property name="password" value=""/>
      <property name="initialSize" value="10" />
      <property name="maxActive" value="100" />
      <property name="maxIdle" value="50" />
      <property name="minIdle" value="10" />
    </bean>

在我的 mysql 日志文件中,我看到两台服务器在服务器启动时都收到了连接请求,但只有第一个 slave1 接收到了 READ 查询。slave2 仅在 slave1 关闭时用作故障转移。

我错过了一些让负载平衡选项正常工作的东西吗?

4

1 回答 1

0

There are multiple things you need to watch for apparently.

First you need to make sure you never specify loadbalance policy, not sure where this comes from, but it doesn't work otherwise.

Second, DBCP by default starts with only one connection and creates the others on extreme demand. What this means is that you will have only one connection associated with only one MySQL host being reused all the time (especially if your transactions are small) and all the traffic is focused there. You can beat this by specifying a larger initial pool size - this will create multiple connections to each server. I personally load balance across 3 servers and use a fixed pool of 36 connections. Only then DBCP will try to take connections associated with different MySQL hosts somewhat fairly. If you only specify 3 connections there is a chance the connections will be load balanced to a single host and you still end up with unfair distribution. My guess is if they fix the roundRobin policy this will work just fine, but for now it doesn't work for me.

Keep in mind that DBCP load balances the connections in the pool and they live for very long time (or forever). Whatever assignment you get initially it will stay this way. Pools are not a good idea for dynamic load balancing. The JMX bean that MySql J Connection eposes to add new MySQL hosts without restarting will not work very well.

于 2013-03-05T16:39:16.503 回答