简体   繁体   English

测试容器,mariadb,shardingsphere 和 flyway

[英]testcontainers, mariadb, shardingsphere and flyway

I'm looking for some really good examples that can inspire me how to setup my integration test given a setup with Testcontainers, mariadb, shardingsphere and flyway.我正在寻找一些非常好的示例,这些示例可以启发我如何在给定 Testcontainers、mariadb、shardingsphere 和 flyway 的设置的情况下设置我的集成测试。

Wanting 3 shards, I assume I'll need to init 3 different datasources, one for each shard like the following想要 3 个分片,我假设我需要初始化 3 个不同的数据源,每个分片一个,如下所示

spring:
  config:
    activate:
      on-profile: test
  shardingsphere:
    datasource:
      names: test-api-1, test-api-2, test-api-3,
      test-api-1:
        type: org.apache.commons.dbcp2.BasicDataSource
        driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
        url: jdbc:tc:mariadb:10.6.7:///
        validationQuery: /* ping */ select 1
        name: testapi01
        username: root
        password: root1234
      test-api-2:
        type: org.apache.commons.dbcp2.BasicDataSource
        driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
        url: jdbc:tc:mariadb:10.6.7:///
        validationQuery: /* ping */ select 1
        name: testapi02
        username: root
        password: root1234
      test-api-3:
        type: org.apache.commons.dbcp2.BasicDataSource
        driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
        url: jdbc:tc:mariadb:10.6.7:///
        validationQuery: /* ping */ select 1
        name: testapi03
        username: root
        password: root1234

What I can't figure out is how to configure flyway properly.我想不通的是如何正确配置 flyway。 So bottom line, I'm looking for a couple of good practical examples that can point me in the right direction.所以归根结底,我正在寻找一些可以为我指明正确方向的实用示例。 Also feel free to correct me if my above assumptions are wrong.如果我的上述假设是错误的,也请随时纠正我。

So far we've been running integration tests that are based on a single datasource as the main focus has been on testing with a production like database migration.到目前为止,我们一直在运行基于单个数据源的集成测试,因为主要重点是使用数据库迁移等生产进行测试。 But as we're experimenting with changing our shard selection we need to test with multiple shards as well.但是当我们尝试改变我们的分片选择时,我们也需要使用多个分片进行测试。

Actually, there are two methods,其实有两种方法,

First, if you use shardingsphere-jdbc with flyway, it's possible to configure them separately.首先,如果您将 shardingsphere-jdbc 与 flyway 一起使用,则可以单独配置它们。 That means here you use 3 DBs for sharding and need to tell flyway all these 3 DBs and all the sharding actual tables.这意味着您在这里使用 3 个 DB 进行分片,并且需要告诉 flyway 所有这 3 个 DB 和所有分片实际表。 Notice, this way will cause the drift issue since shardingsphere-jdbc can not be aware of changes from the flyway.请注意,这种方式会导致漂移问题,因为 shardingsphere-jdbc 无法感知 flyway 的变化。

Second, use shardingsphere-proxy to do sharding and flyway for the migration.其次,使用 shardingsphere-proxy 进行分片和 flyway 迁移。 Because shardingsphere-proxy shares the same functions with shardingsphere-jdbc and works as a database server .因为 shardingsphere-proxy 与 shardingsphere-jdbc 功能相同,都是作为database server的。 Therefore, you can create a logic-sharding database with many logic-sharding tables by ShardingSphere-Proxy [1].因此,您可以通过 ShardingSphere-Proxy [1] 创建一个包含许多逻辑分片表的逻辑分片数据库。 Then tell flyway with this logic DB and sharding tables, username and password to do the migration.然后用这个逻辑数据库和分片表、用户名和密码告诉 flyway 进行迁移。

flyway.user=shardingsphere-proxy-username # Defined in a server.yaml file.
flyway.password=shardingsphere-proxy-databasePassword # Defined in a server.yaml file.
flyway.schemas=shardingsphere-proxy-schemaName # Defined in a config-sharding.yaml file.

[1] https://shardingsphere.apache.org/document/5.2.1/en/user-manual/shardingsphere-proxy/startup/ [1] https://shardingsphere.apache.org/document/5.2.1/en/user-manual/shardingsphere-proxy/startup/

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

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