Enhancer + ShardingSphere 實現分佈式數據庫應用開發

對於大型互聯網應用來講,面對持續增加的海量業務數據,分佈式數據庫架構幾乎是標準配置。而如何讓業務開發人員在面向分佈式數據庫開發時如同在同一個庫中,讓數據分片對應用開發人員透明,將反作用降到最低,是衆多優秀程序員持續追求的目標。java

Enhancer + ShardingSphere 實現分佈式數據庫應用開發

本文將經過一個簡單實例,介紹如何使用 ShardingSphere 分片中間件,實現 Enhancer 應用對後臺分佈式數據庫的操做。但願對您使用 Enhancer 應用管理大型分佈式業務數據有所啓發。mysql

目標效果

Enhancer + ShardingSphere 實現分佈式數據庫應用開發

實現步驟

1. 準備 4 臺 mysql 服務器並建立相關的實驗數據庫和表

  • 1.1. 建立四個數據庫分別命名爲 user_db_0, user_db_1, user_db_2, user_db_3
  • 1.2. 在每一個庫中建立用戶表
    CREATE TABLE `t_user` (
    `id` int(11) NOT NULL DEFAULT '0',
    `name` varchar(40) DEFAULT NULL,
    `gender` char(1) DEFAULT NULL,
    `phone` varchar(20) DEFAULT NULL,
    `address` varchar(255) DEFAULT NULL,
    `created` datetime DEFAULT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2. 下載並安裝 ShardingSphere

  • 2.1.下載。實際上須要使用的是 ShardingSphere 的跨語言中間件產品 ShardingProxy : 下載地址

Enhancer + ShardingSphere 實現分佈式數據庫應用開發

  • 2.2. 解壓 tar 包,獲得 apache-shardingsphere-xxx-shardingsphere-proxy-bin 目錄。
  • 2.3. 下載 mysql 鏈接器 jar 包: mysql-connector-java-5.1.47.jar,並將該 jar 包放入 apache-shardingsphere-xxx-shardingsphere-proxy-bin/lib 目錄中。

3. 配置代理服務器和數據庫分片規則

  • 3.1 配置代理服務器 apache-shardingsphere-xxx-shardingsphere-proxy-bin/conf/server.yaml 文件,
    此文件描述了未來用做 Enhancer 工做臺配置數據庫鏈接的相關信息。注意配置文件中 [xxxx] (含中括號)爲須要自行修改部分:
authentication:
  users:
    root:
      password: [xxx]
    [db_account_xxx]:
      password: [xxx]
      authorizedSchemas: user_db

props:
  max-connections-size-per-query: 1
  acceptor-size: 16  # The default value is available processors count * 2.
  executor-size: 16  # Infinite by default.
  proxy-frontend-flush-threshold: 128  # The default value is 128.
    # LOCAL: Proxy will run with LOCAL transaction.
    # XA: Proxy will run with XA transaction.
    # BASE: Proxy will run with B.A.S.E transaction.
  proxy-transaction-type: LOCAL
  proxy-opentracing-enabled: false
  proxy-hint-enabled: false
  query-with-cipher-column: true
  sql-show: false
  check-table-metadata-enabled: false
  • 3.2 配置分片規則 apache-shardingsphere-xxx-shardingsphere-proxy-bin/conf/config-sharding.yaml 文件,此文件配置了分片數據庫源信息和
    t_user 表在分佈式數據庫中的分片規則信息。注意配置文件中 [xxxx] (含中括號)爲須要自行修改部分:
schemaName: user_db

dataSourceCommon:
 connectionTimeoutMilliseconds: 30000
 idleTimeoutMilliseconds: 60000
 maxLifetimeMilliseconds: 1800000
 maxPoolSize: 50
 minPoolSize: 1
 maintenanceIntervalMilliseconds: 30000

dataSources:
  ds_0:
    url: jdbc:mysql://[數據庫IP地址0]:3306/user_db_0?serverTimezone=UTC&useSSL=false&characterEncoding=utf8
    username: [xxx]
    password: [xxx]
  ds_1:
    url: jdbc:mysql://[數據庫IP地址1]:3306/user_db_1?serverTimezone=UTC&useSSL=false&characterEncoding=utf8
    username: [xxx]
    password: [xxx]
  ds_2:
    url: jdbc:mysql://[數據庫IP地址2]:3306/user_db_2?serverTimezone=UTC&useSSL=false&characterEncoding=utf8
    username: [xxx]
    password: [xxx]
  ds_3:
    url: jdbc:mysql://[數據庫IP地址3]:3306/user_db_3?serverTimezone=UTC&useSSL=false&characterEncoding=utf8
    username: [xxx]
    password: [xxx]
rules:
- !SHARDING
  tables:
    t_user:
      actualDataNodes: ds_${0..3}.t_user # 配置表的實際數據節點數 0 ... n。
      keyGenerateStrategy:
        column: id
        keyGeneratorName: snowflake
  bindingTables:
    - t_user
  defaultDatabaseStrategy:
    standard:
      shardingColumn: id
      shardingAlgorithmName: database_inline
  defaultTableStrategy:
    none:

  shardingAlgorithms:
    database_inline:
        type: INLINE
        props:
          algorithm-expression: ds_${id % 4}  # 使用簡單的用 t_user 表的 id 字段對數據庫節點數 n 求餘規則來分片。
          allow-range-query-with-inline-sharding: true
  keyGenerators:
    snowflake:
      type: SNOWFLAKE
      props:
          worker-id: 123
  • 3.3 在 apache-shardingsphere-xxx-shardingsphere-proxy-bin 目錄執行 ./bin/start.bat 或者 ./bin/start.sh 啓動服務。

4. Enhancer 工做臺配置鏈接 ShardingProxy,後續開發如常

  • 4.1. 在工做臺-全局配置-數據庫-鏈接中配置 3.1 所配置的數據庫服務鏈接[用戶 db_account_xxx]和[密碼 password],地址爲 apache-shardingsphere-xxx-shardingsphere-proxy-bin 所在的機器地址,若在本機則填寫爲 127.0.0.1,端口號爲 3307, 庫名爲 user_db。
    Enhancer + ShardingSphere 實現分佈式數據庫應用開發程序員

  • 4.2. 在工做臺-頁面管理中建立一個頁面,添加一個窗口組件 enhancer-jqgrid, 配置綁定 SQL:
    SELECT * FROM t_user

至此,保存後能夠運行起來查看效果,後續相關開發如常。

結語

本例僅僅是實現了對單一表的分片增、刪、改、查操做。在分佈式數據庫實戰業務開發中,對於一些複雜場景,尤爲是在金融級數據強完整性、強一致性、強可用性的要求下,分片中間件並不能作到徹底對應用開發層透明,好比多個大表跨數據庫 join, 分佈式事務等等。此時通常須要經過應用層合理設計,來補償或者規避這些問題(好比支付寶不容許你跨年查帳,也不會提供一個界面同時呈現總帳和明細帳,須要用戶分屢次多層點開逐步呈現)。對於已經採用 IOE 結構實現的大規模金融級應用來講,在不改動應用層的狀況下就實現去 IOE,還有很長的路要走。而對於通常電商、社交級互聯網應用使用 Enhancer + ShardingSphere 來實現對分佈式數據庫的管理,會是一個不錯的選擇。sql

參考

相關文章
相關標籤/搜索