文章目錄
- Github
- 官網
- 文檔
- ShardingSphere-Proxy
- mysql-connector-java 驅動下載
- conf 配置
- global.yaml
- database-sharding.yaml
- database-readwrite-splitting.yaml
- docker
- docker-compose.yml
Apache ShardingSphere 是一款分布式的數據庫生態系統, 可以將任意數據庫轉換為分布式數據庫,并通過數據分片、彈性伸縮、加密等能力對原有數據庫進行增強。
特性 | 定義 |
---|---|
數據分片 | 數據分片,是應對海量數據存儲與計算的有效手段。ShardingSphere 基于底層數據庫提供分布式數據庫解決方案,可以水平擴展計算和存儲。 |
分布式事務 | 事務能力,是保障數據庫完整、安全的關鍵技術,也是數據庫的核心技術。基于 XA 和 BASE 的混合事務引擎,ShardingSphere 提供在獨立數據庫上的分布式事務功能,保證跨數據源的數據安全。 |
讀寫分離 | 讀寫分離,是應對高壓力業務訪問的手段。基于對 SQL 語義理解及對底層數據庫拓撲感知能力,ShardingSphere 提供靈活的讀寫流量拆分和讀流量負載均衡。 |
數據遷移 | 數據遷移,是打通數據生態的關鍵能力。ShardingSphere 提供跨數據源的數據遷移能力,并可支持重分片擴展。 |
聯邦查詢 | 聯邦查詢,是面對復雜數據環境下利用數據的有效手段。ShardingSphere 提供跨數據源的復雜查詢分析能力,實現跨源的數據關聯與聚合。 |
數據加密 | 數據加密,是保證數據安全的基本手段。ShardingSphere 提供完整、透明、安全、低成本的數據加密解決方案。 |
影子庫 | 在全鏈路壓測場景下,ShardingSphere 支持不同工作負載下的數據隔離,避免測試數據污染生產環境。 |
-
ShardingSphere-JDBC: 定位為輕量級 Java 框架,在 Java 的 JDBC 層提供的額外服務。
-
ShardingSphere-Proxy: 的定位為透明化的數據庫代理,理論上支持任何使用 MySQL、PostgreSQL、openGauss 協議的客戶端操作數據,對異構語言、運維場景更友好。
ShardingSphere-JDBC | ShardingSphere-Proxy | |
---|---|---|
數據庫 | 任意 | MySQL/PostgreSQL |
連接消耗數 | 高 | 低 |
異構語言 | 僅 Java | 任意 |
性能 | 損耗低 | 損耗略高 |
無中心化 | 是 | 否 |
靜態入口 | 無 | 有 |
Apache ShardingSphere 設計哲學為 Database Plus,旨在構建異構數據庫上層的標準和生態。 它關注如何充分合理地利用數據庫的計算和存儲能力,而并非實現一個全新的數據庫。 它站在數據庫的上層視角,關注它們之間的協作多于數據庫自身。
Github
- https://github.com/apache/shardingsphere
官網
- https://shardingsphere.apache.org/index_zh.html
文檔
- https://shardingsphere.apache.org/document/current/cn/overview/
ShardingSphere-Proxy
- https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-proxy/startup/docker/
- https://hub.docker.com/r/apache/shardingsphere-proxy
docker pull apache/shardingsphere-proxy:latest
- 創建縮主機目錄
cd /home
mkdir shardingsphere && cd shardingsphere
mkdir ext-lib
mysql-connector-java 驅動下載
- https://repo.maven.apache.org/maven2/mysql/mysql-connector-java/
注: 將下載的 mysql-connector-java-5.1.9.jar 包復制到縮主機 /home/shardingsphere/ext-lib 目錄下,注意自己當前 數據庫版本 選擇相應的驅動包。
conf 配置
- 拷貝容器 conf 到縮主機
# 創建容器
docker run -d --name shardingsphere --entrypoint=bash apache/shardingsphere-proxy
# 復制配置
docker cp shardingsphere:/opt/shardingsphere-proxy/conf /home/shardingsphere
# 刪除容器
docker rm shardingsphere
-
database-encrypt.yaml: 定義數據庫的加密策略和配置。可能包括對特定表或列的數據加密算法、加密密鑰管理等配置。
-
database-readwrite-splitting.yaml: 定義數據庫讀寫分離的配置。讀寫分離可以根據不同的規則將讀操作和寫操作分發到不同的數據庫節點或者數據庫副本上,以提升數據庫的讀寫性能和負載均衡能力。
-
global.yaml: 定義全局的配置信息,例如全局的數據源配置、全局的規則配置等。這些配置通常會影響整個 ShardingSphere 的運行行為和規則解析。
-
database-hbase.yaml: 配置和管理連接到 HBase 數據庫的相關設置。這可能包括連接信息、表映射、列簇定義等。
-
database-shadow.yaml: 配置數據庫影子復制(Shadow Replica)的相關設置。影子復制是一種技術,用于在生產環境中實時復制數據庫的操作,以便在測試或分析時使用真實的數據副本而不影響生產系統。
-
database-mask.yaml: 配置數據脫敏(Data Masking)策略。數據脫敏用于隱藏或掩蓋數據庫中敏感信息,確保只有經過授權的用戶可以查看特定數據的明文。
-
database-sharding.yaml: 配置數據庫分片(Sharding)的策略和規則。數據庫分片是一種將數據庫表按照某種規則分布到多個數據庫節點中的技術,以提升數據庫的擴展性和性能。
global.yaml
#mode:
# type: Cluster
# repository:
# type: ZooKeeper
# props:
# namespace: governance_ds
# server-lists: localhost:2181
# retryIntervalMilliseconds: 500
# timeToLiveSeconds: 60
# maxRetries: 3
# operationTimeoutMilliseconds: 500authority:users:- user: root@%password: '4NuMDwIzp05BdKp7Bdmf'- user: shardingpassword: shardingprivilege:type: ALL_PERMITTED# 分布式事務
#transaction:
# defaultType: XA
# providerType: Atomikos# SQL 解析
#sqlParser:
# sqlStatementCache:
# initialCapacity: 2000
# maximumSize: 65535
# parseTreeCache:
# initialCapacity: 128
# maximumSize: 1024logging:loggers:- loggerName: ShardingSphere-SQLadditivity: truelevel: INFOprops:enable: false# 聯邦查詢
sqlFederation:sqlFederationEnabled: falseexecutionPlanCache:initialCapacity: 2000maximumSize: 65535props:# 系統日志輸出級別,支持 DEBUG、INFO、WARN 和 ERROR,默認級別是 INFO。system-log-level: INFO# 一次查詢請求在每個數據庫實例中所能使用的最大連接數。max-connections-size-per-query: 1# 用于設置任務處理線程池的大小。每個 ShardingSphereDataSource 使用一個獨立的線程池,同一個 JVM 的不同數據源不共享線程池。kernel-executor-size: 16 # Infinite by default.# 在 ShardingSphere-Proxy 中設置傳輸數據條數的 IO 刷新閾值。proxy-frontend-flush-threshold: 128 # The default value is 128.# 是否在日志中打印 SQL。打印 SQL 可以幫助開發者快速定位系統問題。日志內容包含:邏輯 SQL,真實 SQL 和 SQL 解析結果。如果開啟配置,日志將使用 Topic ShardingSphere-SQL,日志級別是 INFO。sql-show: true# 在程序啟動和更新時,是否檢查分片元數據的結構一致性。check-table-metadata-enabled: false# Proxy 后端與數據庫交互的每次獲取數據行數(使用游標的情況下)。數值增大可能會增加 ShardingSphere Proxy 的內存使用。默認值為 -1,代表設置為 JDBC 驅動的最小值。proxy-backend-query-fetch-size: -1# Proxy 前端 Netty 線程池線程數量,默認值 0 代表使用 Netty 默認值。proxy-frontend-executor-size: 0 # Proxy frontend executor size. The default value is 0, which means let Netty decide.# 允許連接 Proxy 的最大客戶端數量,默認值 0 代表不限制。proxy-frontend-max-connections: 0 # Less than or equal to 0 means no limitation.# Proxy 前端協議類型,支持 MySQL,PostgreSQL 和 openGaussproxy-frontend-database-protocol-type: MySQL
# proxy-default-port: 3307 # Proxy default port.
# proxy-netty-backlog: 1024 # Proxy netty backlog.
# cdc-server-port: 33071 # CDC server port
# proxy-frontend-ssl-enabled: false
# proxy-frontend-ssl-cipher: ''
# proxy-frontend-ssl-version: TLSv1.2,TLSv1.3
database-sharding.yaml
databaseName: sharding_dbdataSources:ds_0:url: jdbc:mysql://192.168.0.100:3306/demo_0?useSSL=falseusername: test1password: 'te123456'connectionTimeoutMilliseconds: 30000idleTimeoutMilliseconds: 60000maxLifetimeMilliseconds: 1800000maxPoolSize: 50minPoolSize: 1ds_1:url: jdbc:mysql://192.168.0.101:3306/demo_1?useSSL=falseusername: test2password: 'te123456'connectionTimeoutMilliseconds: 30000idleTimeoutMilliseconds: 60000maxLifetimeMilliseconds: 1800000maxPoolSize: 50minPoolSize: 1#rules:
#- !SHARDING
# tables:
# t_order:
# actualDataNodes: ds_${0..1}.t_order_${0..1}
...
database-readwrite-splitting.yaml
databaseName: readwrite_splitting_dbdataSources:write_ds:url: jdbc:mysql://192.168.0.100:3306/demo_0?useSSL=falseusername: test1password: 'te123456'connectionTimeoutMilliseconds: 30000idleTimeoutMilliseconds: 60000maxLifetimeMilliseconds: 1800000maxPoolSize: 50minPoolSize: 1read_ds_0:url: jdbc:mysql://192.168.0.101:3306/demo_1?useSSL=falseusername: test2password: 'te123456'connectionTimeoutMilliseconds: 30000idleTimeoutMilliseconds: 60000maxLifetimeMilliseconds: 1800000maxPoolSize: 50minPoolSize: 1
docker
docker run -d --name shardingsphere \
-v /home/shardingsphere/conf:/opt/shardingsphere-proxy/conf \
-v /home/shardingsphere/ext-lib:/opt/shardingsphere-proxy/ext-lib \
-e JVM_OPTS="-Djava.awt.headless=true" \
-e CGROUP_MEM_OPTS="-XX:InitialRAMPercentage=80.0 -XX:MaxRAMPercentage=80.0 -XX:MinRAMPercentage=80.0" \
-e PORT=3308 \
-p 3308:3308 \
apache/shardingsphere-proxy:latest
docker-compose.yml
services:shardingsphere:image: apache/shardingsphere-proxy:latestcontainer_name: shardingsphererestart: alwaysports:- 3308:3308volumes:- /etc/localtime:/etc/localtime- /home/shardingsphere/conf:/opt/shardingsphere-proxy/conf- /home/shardingsphere/ext-lib:/opt/shardingsphere-proxy/ext-libenvironment:PORT: 3308JVM_OPTS: "-Djava.awt.headless=true"CGROUP_MEM_OPTS: "-XX:InitialRAMPercentage=80.0 -XX:MaxRAMPercentage=80.0 -XX:MinRAMPercentage=80.0"
docker-compose up -d shardingsphere