> For the complete documentation index, see [llms.txt](https://xu-min-chang.gitbook.io/caster-develop-note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xu-min-chang.gitbook.io/caster-develop-note/java/apache-shardingsphere.md).

# Apache ShardingSphere

Apache ShardingSphere（原名Sharding-JDBC）是一個開源的、分布式的數據庫中間件，專注於數據庫分片（Sharding）、分散式數據庫訪問（Sharding）、數據庫代理（Proxy）、和分布式事務（Distributed Transaction）等功能。

## Apache ShardingSphere 是甚麼呢?

Apache ShardingSphere 是一個將任何數據庫轉換為分散式數據庫系統，並增強其功能，包括分片、彈性擴展、加密等等的生態系統。

該項目致力於提供一個多源異構、增強型的數據庫平台，並進一步在平台上層構建生態系統。Apache ShardingSphere 的設計理念是 `Database Plus`\`，旨在構建異構數據庫上層的標準和生態系統。它的重點在於如何充分合理地利用現有數據庫的計算和存儲能力，而不是創建全新的數據庫。它更加強調多個數據庫之間的協作，而不僅僅是單個數據庫本身。

Apache ShardingSphere 包括兩個獨立的客戶端：ShardingSphere-JDBC 和 ShardingSphere-Proxy。它們都提供數據的分散、分布式事務和分布式治理功能，適用於各種場景，例如Java同構、異構語言和雲原生環境。本次僅說明 ShardingSphere Proxy 應用及實戰，並且使用Docker部署服務。

### ShardingSphere Proxy

ShardingSphere-Proxy 是一個透明的數據庫代理，提供一個封裝數據庫二進制協議以支援異構語言的數據庫服務器。

ShardingSphere Proxy 提供了以下關鍵功能：

1. 數據庫路由：ShardingSphere Proxy 可以根據SQL語句的解析和分析，將數據庫請求路由到相應的後端數據庫實例。這使得應用程序無需關心數據庫集群的具體配置，簡化了代碼和數據庫切換的操作。
2. 讀寫分離：ShardingSphere Proxy 支持讀寫分離策略，允許將讀取請求路由到讀庫，以減輕主庫的壓力。這提高了系統的性能和伸縮性。
3. SQL 收集和監控：ShardingSphere Proxy 可以收集SQL請求並提供性能監控信息，讓你能夠追蹤和分析應用程序對數據庫的使用情況，進行性能調優和故障排除。
4. 安全性：ShardingSphere Proxy 可以提供身份驗證、授權和數據加密等安全功能，保護數據庫系統免受潛在的風險。

### 實戰練習

廢話不多說，先上扣 [GitHub](https://github.com/MinchangHsu/ShardingSphere-Proxy/tree/main)

依照`Readme`  操作指引建立 資料庫(MySQL or PostgresSQL)、shardingSphere-proxy

1. 建立 資料庫(MySQL or PostgresSQL)
2. 建立 schema, PostgresSQL 會比較特別，他是要建立database，但 schema 僅能使用預設 `pulibc` 來操作，官方有說暫不支援指定schema。
3. 編輯 sharding config 文件，但我已經寫好的，後續想調整就自己再調整。
4. 透過 docker 啟動 sharing
5. 利用DB連線工具，連結 sharding 代理伺服器，不是連到實體資料庫喔，連線資訊可以看 spring boot yml ，並且建立Table。
6. 查看分庫分表是否成功，確認成功才繼續往下走，失敗則排查錯誤內容，這邊就要考驗各位的敏銳度了，以及資料庫的排查能力，理論上是不會錯啦，因為我測試好多次了。
7. 啟動 spring boot
8. import post collection json 我預先建立了幾支api 對資料庫操作(select & insert)
9. 呼叫API 查看 console log

### 實戰結果

<div><figure><img src="https://1934262382-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FRmnDr9RXeSmhkUmaUsX7%2Fuploads%2FaUWg4bone0fgSF1MNZxL%2Fsharding_db_13.png?alt=media&amp;token=d6101ca0-57f3-462b-b1c0-8fc1b39f0667" alt=""><figcaption><p>sharding_db connect settings</p></figcaption></figure> <figure><img src="https://1934262382-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FRmnDr9RXeSmhkUmaUsX7%2Fuploads%2Fo4f5kSBriaMzEyW3o8Wk%2Fsharding_db_16.png?alt=media&amp;token=6bc54b5b-6bd0-461b-9834-38b113c62784" alt=""><figcaption><p>create table result</p></figcaption></figure> <figure><img src="https://1934262382-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FRmnDr9RXeSmhkUmaUsX7%2Fuploads%2FrtnRNVcR8W0zrLUM1JFl%2Fsharding_db_17.png?alt=media&amp;token=54001219-07bb-4106-956c-31c5198c2409" alt=""><figcaption><p>find all api</p></figcaption></figure> <figure><img src="https://1934262382-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FRmnDr9RXeSmhkUmaUsX7%2Fuploads%2FvdtuVHYvICuzGlpk72sE%2Fsharding_db_18.png?alt=media&amp;token=6e0e45f1-5346-4b60-b449-3bd7a3413e71" alt=""><figcaption><p>find by condition</p></figcaption></figure></div>

### 結論

ShardingSphere Proxy 是一個功能豐富的數據庫代理，可以幫助應用程序實現高性能、可伸縮、安全的數據庫訪問。它通過路由、分離、監控和安全性等功能，使應用程序更容易管理和維護與數據庫的交互。這對需要處理大型數據和分布式數據庫的應用程序特別有用。

### 參考來源

* [Apache ShardingSphere 官方網站](https://shardingsphere.apache.org/document/current/en/overview/#what-is-shardingsphere)
* [Apache ShardingSphere Blog](https://shardingsphere.apache.org/blog/cn/material/proxyintroduce/)
* [Apache ShardingSphere GitHub](https://github.com/apache/shardingsphere)
* [Apache ShardingSphere GitHub - Yaml Setting (v 5.4.0)範例](https://github.com/apache/shardingsphere/blob/aac0d3026e00575114701be603ec189a02a45747/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/config-sharding.yaml)
* [國人資源 - 秀妮\_5519](https://www.cfanz.cn/resource/detail/DKADBwPkJjoxn)
