复古 护眼 海天 深邃 暗黑 默认

高可用

背景信息

通过 YAML 格式,ShardingSphere 会根据 YAML 配置,自动完成 ShardingSphereDataSource 对象的创建,减少用户不必要的编码工作。

参数解释

rules:
- !READWRITE_SPLITTING
  dataSources:
    replica_ds:
      dynamicStrategy: Dynamic # 动态读写分离
        autoAwareDataSourceName: # 高可用规则逻辑数据源名称

- !DB_DISCOVERY
  dataSources:
    <data_source_name> (+): # 逻辑数据源名称
      dataSourceNames: # 数据源名称列表
        - <data_source>
        - <data_source>
      discoveryHeartbeatName: # 检测心跳名称
      discoveryTypeName: # 数据库发现类型名称
  
  # 心跳检测配置
  discoveryHeartbeats:
    <discovery_heartbeat_name> (+): # 心跳名称
      props:
        keep-alive-cron: # cron 表达式,如:'0/5 * * * * ?'
  
  # 数据库发现类型配置
  discoveryTypes:
    <discovery_type_name> (+): # 数据库发现类型名称
      type: # 数据库发现类型,如:MySQL.MGR,MySQL.NORMAL_REPLICATION,openGauss.NORMAL_REPLICATION 
      props (?):
        group-name: 92504d5b-6dec-11e8-91ea-246e9612aaf1 # 数据库发现类型必要参数,如 MGR 的 group-name

配置示例

databaseName: database_discovery_db

dataSources:
  ds_0:
    url: jdbc:mysql://127.0.0.1:33306/primary_demo_ds?serverTimezone=UTC&useSSL=false
    username: root
    password:
    connectionTimeoutMilliseconds: 3000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
  ds_1:
    url: jdbc:mysql://127.0.0.1:33307/primary_demo_ds?serverTimezone=UTC&useSSL=false
    username: root
    password:
    connectionTimeoutMilliseconds: 3000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
  ds_2:
    url: jdbc:mysql://127.0.0.1:33308/primary_demo_ds?serverTimezone=UTC&useSSL=false
    username: root
    password:
    connectionTimeoutMilliseconds: 3000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1

rules:
  - !READWRITE_SPLITTING
    dataSources:
      replica_ds:
        dynamicStrategy:
          autoAwareDataSourceName: readwrite_ds

  - !DB_DISCOVERY
    dataSources:
      readwrite_ds:
        dataSourceNames:
          - ds_0
          - ds_1
          - ds_2
        discoveryHeartbeatName: mgr_heartbeat
        discoveryTypeName: mgr
    discoveryHeartbeats:
      mgr_heartbeat:
        props:
          keep-alive-cron: '0/5 * * * * ?'
    discoveryTypes:
      mgr:
        type: MySQL.MGR
        props:
          group-name: 558edd3c-02ec-11ea-9bb3-080027e39bd2

相关参考