使用手册

使用手册

环境要求

纯 JAVA 开发,JDK 建议 1.8 以上版本。

支持迁移场景如下:

源端目标端是否支持
MySQL(5.1.15 ~ 5.7.x)ShardingSphere-Proxy
PostgreSQL(9.4 ~ )ShardingSphere-Proxy

注意

如果后端连接 MySQL 数据库,请下载 mysql-connector-java-5.1.47.jar,并将其放入 ${shardingsphere-scaling}\lib 目录。

权限要求

MySQL 需要开启 binlogbinlog format 为Row模式,且迁移时所使用用户需要赋予 Replication 相关权限。

  1. +-----------------------------------------+---------------------------------------+
  2. | Variable_name | Value |
  3. +-----------------------------------------+---------------------------------------+
  4. | log_bin | ON |
  5. | binlog_format | ROW |
  6. +-----------------------------------------+---------------------------------------+
  7. +------------------------------------------------------------------------------+
  8. |Grants for ${username}@${host} |
  9. +------------------------------------------------------------------------------+
  10. |GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO ${username}@${host} |
  11. |....... |
  12. +------------------------------------------------------------------------------+

PostgreSQL 需要开启 test_decoding

API接口

弹性迁移组件提供了简单的 HTTP API 接口

创建迁移任务

接口描述:POST /scaling/job/start

请求体:

参数描述
ruleConfig.source源端数据源相关配置
ruleConfig.target目标端数据源相关配置
jobConfiguration.concurrency迁移并发度,举例:如果设置为3,则待迁移的表将会有三个线程同时对该表进行迁移,前提是该表有整数型主键

数据源配置:

参数描述
type数据源类型(可选参数:shardingSphereJdbc、jdbc)
parameter数据源参数

*** 注意 ***

当前 source type 必须是 shardingSphereJdbc

示例:

  1. curl -X POST \
  2. http://localhost:8888/scaling/job/start \
  3. -H 'content-type: application/json' \
  4. -d '{
  5. "ruleConfig": {
  6. "source": {
  7. "type": "shardingSphereJdbc",
  8. "parameter": "
  9. dataSources:
  10. ds_0:
  11. dataSourceClassName: com.zaxxer.hikari.HikariDataSource
  12. jdbcUrl: jdbc:mysql://127.0.0.1:3306/scaling_0?useSSL=false
  13. username: scaling
  14. password: scaling
  15. ds_1:
  16. dataSourceClassName: com.zaxxer.hikari.HikariDataSource
  17. jdbcUrl: jdbc:mysql://127.0.0.1:3306/scaling_1?useSSL=false
  18. username: scaling
  19. password: scaling
  20. rules:
  21. - !SHARDING
  22. tables:
  23. t_order:
  24. actualDataNodes: ds_$->{0..1}.t_order_$->{0..1}
  25. databaseStrategy:
  26. standard:
  27. shardingColumn: order_id
  28. shardingAlgorithmName: t_order_db_algorith
  29. logicTable: t_order
  30. tableStrategy:
  31. standard:
  32. shardingColumn: user_id
  33. shardingAlgorithmName: t_order_tbl_algorith
  34. shardingAlgorithms:
  35. t_order_db_algorith:
  36. type: INLINE
  37. props:
  38. algorithm-expression: ds_$->{order_id % 2}
  39. t_order_tbl_algorith:
  40. type: INLINE
  41. props:
  42. algorithm-expression: t_order_$->{user_id % 2}
  43. "
  44. },
  45. "target": {
  46. "type": "jdbc",
  47. "parameter": "
  48. username: root
  49. password: root
  50. jdbcUrl: jdbc:mysql://127.0.0.1:3307/sharding_db?serverTimezone=UTC&useSSL=false
  51. "
  52. }
  53. },
  54. "jobConfiguration":{
  55. "concurrency":"3"
  56. }
  57. }'

返回信息:

  1. {
  2. "success": true,
  3. "errorCode": 0,
  4. "errorMsg": null,
  5. "model": null
  6. }

查询迁移任务进度

接口描述:GET /scaling/job/progress/{jobId}

示例:

  1. curl -X GET \
  2. http://localhost:8888/scaling/job/progress/1

返回信息:

  1. {
  2. "success": true,
  3. "errorCode": 0,
  4. "errorMsg": null,
  5. "model": {
  6. "id": 1,
  7. "jobName": "Local Sharding Scaling Job",
  8. "status": "RUNNING/STOPPED"
  9. "syncTaskProgress": [{
  10. "id": "127.0.0.1-3306-test",
  11. "status": "PREPARING/MIGRATE_HISTORY_DATA/SYNCHRONIZE_REALTIME_DATA/STOPPING/STOPPED",
  12. "historySyncTaskProgress": [{
  13. "id": "history-test-t1#0",
  14. "estimatedRows": 41147,
  15. "syncedRows": 41147
  16. }, {
  17. "id": "history-test-t1#1",
  18. "estimatedRows": 42917,
  19. "syncedRows": 42917
  20. }, {
  21. "id": "history-test-t1#2",
  22. "estimatedRows": 43543,
  23. "syncedRows": 43543
  24. }, {
  25. "id": "history-test-t2#0",
  26. "estimatedRows": 39679,
  27. "syncedRows": 39679
  28. }, {
  29. "id": "history-test-t2#1",
  30. "estimatedRows": 41483,
  31. "syncedRows": 41483
  32. }, {
  33. "id": "history-test-t2#2",
  34. "estimatedRows": 42107,
  35. "syncedRows": 42107
  36. }],
  37. "realTimeSyncTaskProgress": {
  38. "id": "realtime-test",
  39. "delayMillisecond": 1576563771372,
  40. "position": {
  41. "filename": "ON.000007",
  42. "position": 177532875,
  43. "serverId": 0
  44. }
  45. }
  46. }]
  47. }
  48. }

查询所有迁移任务

接口描述:GET /scaling/job/list

示例:

  1. curl -X GET \
  2. http://localhost:8888/scaling/job/list

返回信息:

  1. {
  2. "success": true,
  3. "errorCode": 0,
  4. "model": [
  5. {
  6. "jobId": 1,
  7. "jobName": "Local Sharding Scaling Job",
  8. "status": "RUNNING"
  9. }
  10. ]
  11. }

停止迁移任务

接口描述:GET /scaling/job/stop

请求体:

参数描述
jobIdjob id

示例:

  1. curl -X GET \
  2. http://localhost:8888/scaling/job/stop/1

返回信息:

  1. {
  2. "success": true,
  3. "errorCode": 0,
  4. "errorMsg": null,
  5. "model": null
  6. }

通过UI界面来操作

ShardingSphere-Scaling 与 ShardingSphere-UI 集成了用户界面,所以上述所有任务相关的操作都可以通过 UI 界面点点鼠标来实现,当然本质上还是调用了上述基本接口。

更多信息请参考 ShardingSphere-UI 项目。