您好,登錄后才能下訂單哦!
這篇文章給大家介紹Sharding中怎么利用JDBC實現強制路由,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
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 根據傳進來的分片值 路由到對應的物理庫 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 根據傳進來的分片值 路由到對應的物理表 return tableSet; } }
#數據源 spring.shardingsphere.datasource.names=sharding0,sharding1 #默認數據源 spring.shardingsphere.sharding.default-data-source-name=sharding0 # 顯示sql spring.shardingsphere.props.sql.show=true #sharding0數據源配置 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 數據源配置 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 強制路由 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 # 分布式主鍵 內置的支持這三種 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(); }
關于Sharding中怎么利用JDBC實現強制路由就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。