The Guidepost is a solution of Database Read Write Splitting
. It only
supports MySQL with Group replication right
now. (MySQLInnoDB Cluster is supported since it is using Group Replication.) It does the following works.
- Proxying DataSource
The Guidepost creates Pooled Data Source of each available database server, and wrap them into a new Data Source which named GuidepostDataSource. When GuidepostDataSource.getConnection() is called, it determines whether current Transaction is readonly or not, then use a DataSource of a readonly server to get a database connection if current Transaction is readonly, otherwise it used one of the DataSources of writable servers.
- Database Topology Discovery.
The Guidepost retrieve database servers information periodically. It will create DataSource of newly added server automatically. It will also discard the DataSources of removed/offline database servers. And it will periodically check the servers whether they are readonly or writable.
Please check the demo: guidepost-demo
- Depend on the Whistle in maven pom.xml.
<dependency> <groupId>org.coderclan</groupId> <artifactId>spring-boot-starter-guidepost</artifactId> </dependency>
- Configuration a dataSource as What
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties
requires. This dataSource will be used as seed dataSource to get discovery Database Topology. Please checkspring.datasource.*
in guidepost-demo/src/main/resources/application.yml - Grant database user SELECT privilege on table performance_schema.replication_group_members. (SQL: GRANT SELECT ON TABLE performance_schema.replication_group_members TO 'test'@'%')
- Autocommit of GuidepostDataSource will always be set to false.
- Property hibernate.connection.handling_mode need to be set to DELAYED_ACQUISITION_*. Guidepost set it
to
DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION
default.
(If the two above requirements didn't meet, org.springframework.transaction.support.TransactionSynchronizationManager.isCurrentTransactionReadOnly will return false always. )