"MySQL official driver" master-slave separated from the main separation

If you work in future, require fast read and write separation function of MySQL, you will think of this article. If you come back here again, to prove that you have an urgent need for a simple and quick solution - that is, MySQL 官方驱动to read and write to achieve separation layer, small minority, but it works.

JDBC Driver

MySQL driver jar package we often use, in fact, the default has great features, it is separated from the main and HA. If you just need a master-slave separate, failover function, do not sharding. A drive enough, what intermediate layer need not be introduced.

This thing is Replication agreement. Mysql JDBC Connector after 5.1.X release adds these features to support "multi-host" access paradigm cluster topology. This function is implemented in the driver layer, and since it is the driver layer, then inevitably there are some problems driving layer.

Our connection is so common jdbc

jdbc: mysql: //127.0.0.1: 3306 / test characterEncoding = UTF-8 copy the code?

And after jdbc after the agreement transformation connection, it looks to be longer, bigger!

jdbc:mysql:replication://127.0.0.1:3306,127.0.0.1:3307,127.0.0.1:3308/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=false&loadBalanceStrategy=random 复制代码

Of course, you can also have the wording of ipv6, more intuitive

jdbc:mysql://address=(type=master)(host=master1host),address=(type=master)(host=master2host),address=(type=slave)(host=slave1host)/db 复制代码

8.0 documentation look here:  dev.mysql.com/doc/connect...  Description:

The first agreement 连接, represents the main library Master behind 一堆连接, expressed from the library Slave, of course, there can be multiple when you put Masterthe 连接back also on the back of the pile where it also has a "Reading Library" of the property there is a bunch of parameters to control it 所有连接, how to get along in the end

Thus, the so-called primary separation function, as long as the connection string configured, on the Kumufengchun.

Code layer

First you have to change the driver class, no longer Driver, but rather the

Copy the code com.mysql.jdbc.ReplicationDriver

In this case DataSource.getConnection () Gets the connection is actually ReplicationConnection, this connection is virtual, and real database connection is a 1-to-many relationship, so remember to cook a MySQL each have a corresponding machine authorization.

So how to distinguish this request is a read write it? By the fact that Connectionthe readonlyproperty that is requested by headerthe "readonly" passed. So if there is a request in a write operation, this entire transaction is necessarily a readonly = false.

For the Spring it, you can use @Transactionalannotations to control the properties of. A transaction can not span two connections, so that reading is written, there is the highest level decision.

The following code snippet shows some elements you should configure, yes, that comment.

public interface UserManager {     public UserDO get(int id);     public void insert(UserDO user);     public void update(int id); } 复制代码
@Component public class UserManagerImpl implements UserManager{     @Autowired     private UserDao userDao;     ...     @Override     @Transactional(readOnly = false,propagation = Propagation.REQUIRED)     public void insert(UserDO user) {         this.userDao.insert(user);     } } 复制代码

Is not it simple? And so on, not happy too early.

parameter

Do not think it is the official driver, you can use wayward. This jdbc-driven parameters is very rich, the cost will learn a little bit high. In some good running under low flow, but the problem occurs frequently in high concurrency environments. Here only the most important that the next pick.

A virtual connection, corresponding to a true master database connection and a plurality of connection from the library. For the survival of the host and back online, we have to consider three cases:

  • Master have died

  • Slave have died

  • Master, Slave all dead

In either case, MySQL drivers have shown some strange behavior, the default parameters are so bad.

You might think that driving is about the management of several connections only, but the situation is much more complex than this. First Photo to look this complex relationship.


11、readFromMasterWhenNoSlaves 当所有的salve死掉后,此参数用来控制主库是否参与读。如果从库的流量很大,配置此参数对主库有很大风险;但如果你关掉,请求则会快速失败。 2、loadBalanceStrategy 策略用来指定从库的轮询规则。有轮询,也有权重,也可以指定具体的策略实现。当你维护或者迁移某个实例时,先置空流量,这会非常有用。或许,你会给某DB一个预热的可能。 3、allowMasterDownConnections 如果主机当机,当连接池获取新的连接时,会失败。但如果打开此参数,则虚拟连接只会创建Slave连接组,整个连接会降级为只读,不论你设置了什么注解。 4、allowSlavesDownConnections 如果没有只读库了,是否允许创建新的连接。在这种情况下,此参数开启,读操作有很大可能会失败。 5、retriesAllDown 当所有的hosts都无法连接时重试的最大次数(依次循环重试),默认为120。重试次数达到阈值仍然无法获取有效链接,将会抛出SQLException。 6、autoReconnect 实例既然有下线、就有上线。上线以后要能够继续服务,此参数用来控制断线情况下自动重连而不抛出异常。这会破坏事务的完整性,但还是默认开启。


然而MySQL驱动提供了更加丰富的参数来控制这个过程,如果你的业务要求比较苛刻,这些参数可能要测个遍才会放心。 但大多数情况下,它运行的很好。

管理

仅有配置参数,此协议就算一个半成品而已。所幸,此驱动提供了JMX管理的方式,可以基于其做一些配置变更之类的功能。市面上并没有这种配置管理工具,可能还是因为它太小众了。

 public abstract void addSlaveHost(String groupFilter, String host) throws SQLException;  public abstract void removeSlaveHost(String groupFilter, String host) throws SQLException;  public abstract void promoteSlaveToMaster(String groupFilter, String host) throws SQLException;  public abstract void removeMasterHost(String groupFilter, String host) throws SQLException;  public abstract String getMasterHostsList(String group);  public abstract String getSlaveHostsList(String group);  public abstract String getRegisteredConnectionGroups();  public abstract int getActiveMasterHostCount(String group);  public abstract int getActiveSlaveHostCount(String group);  public abstract int getSlavePromotionCount(String group);  public abstract long getTotalLogicalConnectionCount(String group);  public abstract long getActiveLogicalConnectionCount(String group); 复制代码

我们顺便提一下阿里的德鲁伊数据库连接池。如图,某些功能,只支持默认的单连接,对multi-host支持还是有限。


1


结尾

MySQL 5.1.x官方驱动出了这么个东西以后,其实宣告了很多小公司自研的某些小中间件的死亡。翻来服务,改写JDBC,不过就是为了管理个连接集合。

本文对象为专注基础设施研发的同学。有人看到的,不过是一堆参数而已;而真正去深入使用的人,会感到背脊通彻的寒冷。

当它小众时,你对它不屑一顾。然而当你一旦采用了某种方案,你却希望全世界都是关于它的描写。人生从来就没那么幸运,很多坑,还需要自己来踩。


Guess you like

Origin blog.51cto.com/14028890/2421115