Sharding-Proxy分庫分表和數(shù)據(jù)加密使用場景分析
更新時間:2022年04月13日 09:34:01 作者:堅持是一種態(tài)度
這篇文章主要介紹了Sharding-Proxy分庫分表和數(shù)據(jù)加密使用經驗分享,通過場景模擬分析結合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下
Sharding-Proxy分庫分表和數(shù)據(jù)加密
主要將實際項目中使用shardingshpere-proxy的經歷經驗,總結分享一下。
使用場景
- 公司規(guī)劃研發(fā)了兩款針對政務新媒體和數(shù)字鄉(xiāng)村的SaaS平臺,作為新的利潤增長點??紤]到以后的用戶數(shù)量和數(shù)據(jù)數(shù)量,決定按照租戶(簽約客戶)進行分庫分表。對于一些敏感數(shù)據(jù),例如身份證號、手機號等,使用數(shù)據(jù)庫級別的加密解密,不存儲明文數(shù)據(jù)。
- 考察了網上已有的一些數(shù)據(jù)庫中間件和分庫分表解決方案,公司決定使用
Sharding-Proxy
作為分庫分表和數(shù)據(jù)加密的數(shù)據(jù)庫中間件。主要原因是,它對代碼的侵入性很小,開發(fā)人員也不需要關注它,減少了學習成本,對DBA也很友好。另一方面,ShardingSphere已進入Apache孵化器,它完全開源免費,社區(qū)也很活躍,版本迭代也很快。 - 本次使用的ShardingSphere-5.0
配置文件講解
server.yaml
- resources文件夾,conf文件夾下面
- 主要有注冊中心配置、登錄連接配置和基礎配置
mode: type: Cluster # 集群 repository: type: ZooKeeper # 使用zookeeper props: namespace: governance_ds server-lists: 192.168.1.100:2181 retryIntervalMilliseconds: 50000 timeToLiveSeconds: 60 maxRetries: 3 operationTimeoutMilliseconds: 50000 overwrite: true
mode.type: Cluster
使用集群配置,單個部署也可以設置為Cluster
,沒影響mode.repository
配置存儲方式,可以選擇使用ZooKeeper
mode.overwrite
,配置加載方式,本地配置是否覆蓋配置中心配置。true是可覆蓋,以本地為準,將本地配置同步到zookeeper;false則以zookeeper為準- 下面還有一些基礎配置,是否打印SQL等,暫時可都是要默認
config-sharding.yaml
- resources文件夾,conf文件夾下面
- schemaName 數(shù)據(jù)庫連接,數(shù)據(jù)庫名稱
- dataSources 數(shù)據(jù)源
- rules 規(guī)則
- !SHARDING 分庫分表規(guī)則
- tables 表
- actualDataNodes 實際對于庫表
- databaseStrategy 分庫策略 none 不分庫分表
- defaultDatabaseStrategy 默認分庫策略
- defaultTableStrategy 默認分表策略
- defaultKeyGenerateStrategy 默認主鍵策略
- shardingAlgorithms 自定義分片算法
- keyGenerators 主鍵生成策略
config-encrypt.yaml
- resources文件夾,conf文件夾下面
- schemaName 數(shù)據(jù)庫連接,數(shù)據(jù)庫名稱
- dataSources 數(shù)據(jù)源
- rules 規(guī)則
- !ENCRYPT 數(shù)據(jù)加密
- encryptors 加密策略,可選擇AES或MD5,在下面具體字段可選則加密策略
- aes_encryptor,aes可以配置加鹽
- tables 表
- columns 字段s
- id_number 邏輯字段
- plainColumn 原字段
- cipherColumn 加密字段
- encryptorName 加密策略
- queryWithCipherColumn 查詢時是否使用加密字段
其他
- 可以下載源碼或者下載程序看看,里面的功能,都有配置文件案例,是注釋掉的,一款PostgreSQL的,一款MySQL的
- 目前就使用這2個功能,其他功能暫時沒研究,就不多說了
- 實際使用時,分庫分表和數(shù)據(jù)加密是一起使用的,所以只用了一個配置文件,都放在rules下面
- 后面我會把我的配置文件貼上去
使用情況
- 政務新媒體SaaS平臺暫時只使用分庫分表
- 數(shù)字鄉(xiāng)村SaaS平臺,使用了分庫分表和數(shù)據(jù)加密
- 分庫分表,主要做了基于租戶分庫,部分表,又根據(jù)某些業(yè)務字段做了分表
- 分表策略,默認分8個表寫法algorithm-expression: monitor_record_${media_id % 8}
- 我們基于租戶的哈希進行分庫,但對于某些租戶,又想指定數(shù)據(jù)庫,這就需要自定義分庫分表策略
- 要求分庫支持哈希和指定,寫了自定義分庫策略類,有一個靜態(tài)map,解析執(zhí)行SQL時,先從map里獲取,獲取不到,則根據(jù)哈希獲取
- 數(shù)據(jù)加密,主要是添加加密字段和對歷史數(shù)據(jù)處理
- 可以寫一個靜態(tài)方法,對已存在數(shù)據(jù)進行處理
總結
- sharding-proxy對于按照租戶分庫分表,以及數(shù)據(jù)加密,是完全支持的,足夠我們使用
- 使用起來很簡單,下載最新穩(wěn)定版安裝即可
- 如果沒有自定義分庫分表策略要求,只使用已有的策略,那只需要修改配置文件部署即可
- 如果需要自定義分庫分表策略,也不復雜,寫好類打包好,放入ext-lib下即可
- 配置文件部分示例
schemaName: digital_village dataSources: ds: url: jdbc:postgresql://192.168.1.xxx:5432/digital_village?currentSchema=public&serverTimezone=UTC&useSSL=false username: postgres password: xxxxxx connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 120 minPoolSize: 1 ds_0: url: jdbc:postgresql://192.168.1.xxx:5432/digital_village_0?currentSchema=public&serverTimezone=UTC&useSSL=false ds_1: url: jdbc:postgresql://192.168.1.xxx:5432/digital_village_1?currentSchema=public&serverTimezone=UTC&useSSL=false password: xxxxx rules: - !SHARDING tables: # 需要分庫的表,根據(jù)租戶id分庫 cms_basic_info: actualDataNodes: ds_${0..3}.cms_basic_info cms_column: actualDataNodes: ds_${0..3}.cms_column cms_content: actualDataNodes: ds_${0..3}.cms_content cms_content_text: actualDataNodes: ds_${0..3}.cms_content_text cms_menu_column_bind: actualDataNodes: ds_${0..3}.cms_menu_column_bind cms_message_board: actualDataNodes: ds_${0..3}.cms_message_board # 不需要分庫分表的表,全部存儲在 ds 數(shù)據(jù)源 auth_cfg_catalog_data_permission: actualDataNodes: ds.auth_cfg_catalog_data_permission databaseStrategy: none: auth_cfg_column_data_permission: actualDataNodes: ds.auth_cfg_column_data_permission databaseStrategy: # 默認分庫策略 defaultDatabaseStrategy: standard: shardingColumn: customer_id #分庫字段 shardingAlgorithmName: customer_id_inline #分庫規(guī)則: defaultTableStrategy: none: # 默認主鍵策略 defaultKeyGenerateStrategy: column: id keyGeneratorName: snowflake # 自定義分片算法 shardingAlgorithms: customer_id_inline: type: CLASS_BASED props: strategy: standard algorithmClassName: cn.lonsun.dv.DigitalVillageShardingAlgorithm # 主鍵生成策略 keyGenerators: snowflake: type: SNOWFLAKE worker-id: 123 - !ENCRYPT encryptors: aes_encryptor: type: AES aes-key-value: xxxwwaS213123SAD md5_encryptor: type: MD5 party_position: columns: mobile: plainColumn: mobile cipherColumn: mobile_cipher encryptorName: aes_encryptor village_population: id_number: plainColumn: id_number cipherColumn: id_number_cipher queryWithCipherColumn: true
到此這篇關于Sharding-Proxy分庫分表和數(shù)據(jù)加密的文章就介紹到這了,更多相關Sharding-Proxy分庫分表內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Ubuntu安裝JDK與IntelliJ?IDEA的詳細過程
APT是Linux系統(tǒng)上的包管理工具,能自動解決軟件包依賴關系并從遠程存儲庫中獲取安裝軟件包,這篇文章主要介紹了Ubuntu安裝JDK與IntelliJ?IDEA的過程,需要的朋友可以參考下2023-08-08詳解使用spring boot admin監(jiān)控spring cloud應用程序
本篇文章主要介紹了詳解使用spring boot admin監(jiān)控spring cloud應用程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11