Skip to content

DynamicDatabaseRouting

GangCheng edited this page Jan 21, 2024 · 3 revisions

Dynamic database routing configuration properties

  • The mybatis-r2dbc configuration is located in R2dbcMybatisProperties

  • The configuration is located in R2dbcMybatisRoutingConnectionFactoryProperties

  • If you want to customize R2dbcMybatisDynamicRoutingConnectionFactory, you should define a bean in spring environment which type isR2dbcMybatisRoutingConnectionFactoryCustomizer

  • By default, to determine which database to route, it will use the routing key which equals with the name configured in definition.

  • The Sample configuration yaml:

    r2dbc:
      mybatis:
        mapper-locations: classpath:mapper/**/*.xml
        configuration-properties:
          "MySQL": "mysql"
          "MariaDB": "mariadb"
          "PostgreSQL": "postgresql"
          "[Microsoft SQL Server]": "mssql"
          "[Oracle Database]": "oracle"
        configuration:
          map-underscore-to-camel-case: true
    spring:
      r2dbc:
        mybatis:
          routing:
            enabled: true
            definitions:
              - name: defination-name-for-routing-1
                as-default: true # there must be one definition at least to be configured `as-default = true`
                r2dbc-url: xxxxx
                username: xxxxx
                password: xxxxx
                pool:
                  max-idle-time: PT5M
                  validation-query: SELECT 1
                  initial-size: 16
                  max-size: 16
                  acquire-retry: 3
                  validation-depth: REMOTE
                  max-create-connection-time: PT10S
                  validation-squry: SELECT 1
              - name: defination-name-for-routing-2
                r2dbc-url: xxxxx
                username: xxxxx
                password: xxxxx
                pool:
                  max-idle-time: PT5M
                  validation-query: SELECT 1
                  initial-size: 16
                  max-size: 16
                  acquire-retry: 3
                  validation-depth: REMOTE
                  max-create-connection-time: PT10S
                  validation-squry: SELECT 1
  • To performing database routing operation, you should use R2dbcMybatisDatabaseRoutingOperator

public Mono<Void> runWithDynamicRouting() {
    Mono<Void> mysqlExecution = R2dbcMybatisDatabaseRoutingOperator.executeMono(
            MySQLContainer.class.getSimpleName(), // this should equals the `name` property in definition 
            applicationService.runWithoutTransaction()
    );
    Mono<Void> mariadbExecution = R2dbcMybatisDatabaseRoutingOperator.executeMono(
            MariaDBContainer.class.getSimpleName(), // this should equals the `name` property in definition
            applicationService.runWithoutTransaction()
    );
    Mono<Void> postgresExecution = R2dbcMybatisDatabaseRoutingOperator.executeMono(
            PostgreSQLContainer.class.getSimpleName(), // this should equals the `name` property in definition
            applicationService.runWithoutTransaction()
    );
    Mono<Void> mssqlExecution = R2dbcMybatisDatabaseRoutingOperator.executeMono(
            MSSQLServerContainer.class.getSimpleName(), // this should equals the `name` property in definition
            applicationService.runWithoutTransaction()
    );
    Mono<Void> oracleExecution = R2dbcMybatisDatabaseRoutingOperator.executeMono(
            OracleContainer.class.getSimpleName(), // this should equals the `name` property in definition
            applicationService.runWithoutTransaction()
    );
    return Flux.concat(mysqlExecution, mariadbExecution, postgresExecution, mssqlExecution, oracleExecution)
            .then();
}
  • Each routing operation should manage their own transaction themselves.(@Transactional or TransactionDefinition)
  • Dynamic Routing Test
Clone this wiki locally