这篇文章给大家介绍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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。