這篇文章給大家介紹Sharding中怎么利用JDBC實現(xiàn)強(qiáng)制路由,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
創(chuàng)新互聯(lián)專注于鄱陽企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),商城系統(tǒng)網(wǎng)站開發(fā)。鄱陽網(wǎng)站建設(shè)公司,為鄱陽等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站策劃,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
package com.zero.sharding.shardingrule; import java.util.Collection; import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm; import org.apache.shardingsphere.api.sharding.hint.HintShardingValue; import com.google.common.collect.Sets; public class UserIdDataBaseHintAlgorithm implements HintShardingAlgorithm<Long> { @Override public Collection<String> doSharding(Collection<String> availableTargetNames, HintShardingValue<Long> shardingValue) { Collection<String> databaseSet = Sets.newConcurrentHashSet(); Collection<Long> orderIds = shardingValue.getValues(); for(Long orderId:orderIds){ if( orderId == 1l){ databaseSet.add("sharding1"); } } //TODO 根據(jù)傳進(jìn)來的分片值 路由到對應(yīng)的物理庫 return databaseSet; } }
package com.zero.sharding.shardingrule; import java.util.Collection; import java.util.List; import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm; import org.apache.shardingsphere.api.sharding.hint.HintShardingValue; import com.google.common.collect.Sets; public class OrderIdTableHintAlgorithm implements HintShardingAlgorithm<Long> { @Override public Collection<String> doSharding(Collection<String> availableTargetNames, HintShardingValue<Long> shardingValue) { Collection<String> tableSet = Sets.newConcurrentHashSet(); Collection<Long> orderIds = shardingValue.getValues(); for(Long orderId:orderIds){ if( orderId == 2l){ tableSet.add("t_order0"); } } //TODO 根據(jù)傳進(jìn)來的分片值 路由到對應(yīng)的物理表 return tableSet; } }
#數(shù)據(jù)源 spring.shardingsphere.datasource.names=sharding0,sharding1 #默認(rèn)數(shù)據(jù)源 spring.shardingsphere.sharding.default-data-source-name=sharding0 # 顯示sql spring.shardingsphere.props.sql.show=true #sharding0數(shù)據(jù)源配置 spring.shardingsphere.datasource.sharding0.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.sharding0.driver-class-name=com.MySQL.cj.jdbc.Driver spring.shardingsphere.datasource.sharding0.url=jdbc:mysql://**:3306/sharding0?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL spring.shardingsphere.datasource.sharding0.username=root spring.shardingsphere.datasource.sharding0.password=** #sharding1 數(shù)據(jù)源配置 spring.shardingsphere.datasource.sharding1.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.sharding1.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.sharding1.url=jdbc:mysql://**:3306/sharding1?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL spring.shardingsphere.datasource.sharding1.username=root spring.shardingsphere.datasource.sharding1.password=** #hint 強(qiáng)制路由 spring.shardingsphere.sharding.tables.t_order.database-strategy.hint.algorithm-class-name=com.zero.sharding.shardingrule.UserIdDataBaseHintAlgorithm spring.shardingsphere.sharding.tables.t_order.table-strategy.hint.algorithm-class-name=com.zero.sharding.shardingrule.OrderIdTableHintAlgorithm # 分布式主鍵 內(nèi)置的支持這三種 SNOWFLAKE/UUID/LEAF_SEGMENT spring.shardingsphere.sharding.tables.t_order.key-generator.column=id spring.shardingsphere.sharding.tables.t_order.key-generator.type=SNOWFLAKE
@Test public void testShardingRead(){ OrderExample ex = new OrderExample(); ex.createCriteria().andUserIdEqualTo(1l).andOrderIdEqualTo(1l); HintManager hintManager = HintManager.getInstance(); hintManager.addDatabaseShardingValue("t_order", 1l); hintManager.addTableShardingValue("t_order", 2l); List<Order> orders = orderMapper.selectByExample(ex); orders.stream().forEach(o->{ System.out.println("userId:"+o.getUserId() + " orderId:" + o.getOrderId()); }); hintManager.close(); }
關(guān)于Sharding中怎么利用JDBC實現(xiàn)強(qiáng)制路由就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
當(dāng)前文章:Sharding中怎么利用JDBC實現(xiàn)強(qiáng)制路由
網(wǎng)頁路徑:http://muchs.cn/article10/gdopdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、、網(wǎng)站制作、定制開發(fā)、App開發(fā)、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)