對於大型互聯網應用來講,面對持續增加的海量業務數據,分佈式數據庫架構幾乎是標準配置。而如何讓業務開發人員在面向分佈式數據庫開發時如同在同一個庫中,讓數據分片對應用開發人員透明,將反作用降到最低,是衆多優秀程序員持續追求的目標。java
本文將經過一個簡單實例,介紹如何使用 ShardingSphere 分片中間件,實現 Enhancer 應用對後臺分佈式數據庫的操做。但願對您使用 Enhancer 應用管理大型分佈式業務數據有所啓發。mysql
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;
[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
[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
4.1. 在工做臺-全局配置-數據庫-鏈接中配置 3.1 所配置的數據庫服務鏈接[用戶 db_account_xxx]和[密碼 password],地址爲 apache-shardingsphere-xxx-shardingsphere-proxy-bin 所在的機器地址,若在本機則填寫爲 127.0.0.1,端口號爲 3307
, 庫名爲 user_db。程序員
SELECT * FROM t_user
本例僅僅是實現了對單一表的分片增、刪、改、查操做。在分佈式數據庫實戰業務開發中,對於一些複雜場景,尤爲是在金融級數據強完整性、強一致性、強可用性的要求下,分片中間件並不能作到徹底對應用開發層透明,好比多個大表跨數據庫 join, 分佈式事務等等。此時通常須要經過應用層合理設計,來補償或者規避這些問題(好比支付寶不容許你跨年查帳,也不會提供一個界面同時呈現總帳和明細帳,須要用戶分屢次多層點開逐步呈現)。對於已經採用 IOE 結構實現的大規模金融級應用來講,在不改動應用層的狀況下就實現去 IOE,還有很長的路要走。而對於通常電商、社交級互聯網應用使用 Enhancer + ShardingSphere 來實現對分佈式數據庫的管理,會是一個不錯的選擇。sql