Sharding中怎么利用JDBC实现强制路由

发布时间:2021-07-30 13:46:23 作者:Leah
来源:亿速云 阅读:860

这篇文章给大家介绍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实现强制路由就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. 利用Sharding-Jdbc组件实现分表
  2. Sharding JDBC分表怎么配置

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

sharding jdbc

上一篇:DataTables如何设置横向滚动条

下一篇:php中的子命名空间是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》