Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use JDBC to realize forced routing in Sharding

Shulou Source: shulou.com Published: 2022-06-02 06:01:07 09月29日 Update

This article introduces how to use JDBC to achieve mandatory routing in Sharding, the content is very detailed, interested friends can use for reference, I hope it can be helpful to you.

Sublibrary policy 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 {@ Override public Collection doSharding (Collection availableTargetNames, HintShardingValue shardingValue) {Collection databaseSet = Sets.newConcurrentHashSet () Collection orderIds = shardingValue.getValues (); for (Long orderId:orderIds) {if (orderId = = 1l) {databaseSet.add ("sharding1") }} / / TODO is routed to the corresponding physical library return databaseSet;}} according to the passed shard value 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 {@ Override public Collection doSharding (Collection availableTargetNames, HintShardingValue shardingValue) {Collection tableSet = Sets.newConcurrentHashSet (); Collection orderIds = shardingValue.getValues () For (Long orderId:orderIds) {if (orderId = = 21) {tableSet.add ("t_order0");}} / / TODO is routed to the corresponding physical table return tableSet according to the passed shard value }} configure # data source spring.shardingsphere.datasource.names=sharding0 Sharding1# default data source spring.shardingsphere.sharding.default-data-source-name=sharding0# display sqlspring.shardingsphere.props.sql.show=true#sharding0 data source configuration spring.shardingsphere.datasource.sharding0.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.sharding0.driver-class-name=com.mysql.cj.jdbc.Driverspring.shardingsphere.datasource.sharding0.url=jdbc:mysql://**:3306/sharding0?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULLspring.shardingsphere.datasource.sharding0 .username = rootspring.shardingsphere.datasource.sharding0.password=**#sharding1 data source configuration spring.shardingsphere.datasource.sharding1.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.sharding1.driver-class-name=com.mysql.cj.jdbc.Driverspring.shardingsphere.datasource.sharding1.url=jdbc:mysql://**:3306/sharding1?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULLspring.shardingsphere.datasource.sharding1.username=rootspring.shardingsphere.datasource.sharding1.password=**#hint forced routing spring.shardingsphere .sharding .tables.t _ order.database-strategy.hint.algorithm-class-name=com.zero.sharding.shardingrule.UserIdDataBaseHintAlgorithmspring.shardingsphere.sharding.tables.t_order.table-strategy.hint.algorithm-class-name=com.zero.sharding.shardingrule.OrderIdTableHintAlgorithm# distributed primary key built-in support for these three SNOWFLAKE/UUID/LEAF_SEGMENTspring.shardingsphere.sharding.tables.t_order.key-generator.column=idspring.shardingsphere.sharding.tables.t_order.key-generator.type=SNOWFLAKE usage examples @ Test public void testShardingRead () {OrderExample ex = new OrderExample () Ex.createCriteria (). AndUserIdEqualTo (11) .andOrderIdEqualTo (11); HintManager hintManager = HintManager.getInstance (); hintManager.addDatabaseShardingValue ("t_order", 11); hintManager.addTableShardingValue ("t_order", 21); List orders = orderMapper.selectByExample (ex) Orders.stream () .forEach (o-> {System.out.println ("userId:" + o.getUserId () + "orderId:" + o.getOrderId ();}); hintManager.close () } about how to use JDBC to achieve mandatory routing in Sharding is shared here. I hope the above content can be helpful to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Routing data data source enforcement configuration content more physics policy help good interest distributed sub-library sub-table buddy partner article knowledge example Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Redmi MySQL Huawei Microsoft