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

这篇文章给大家介绍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 {

	@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 根据传进来的分片值 路由到对应的物理库
		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 {

	@Override
	public Collection doSharding(Collection availableTargetNames,
			HintShardingValue shardingValue) {
		Collection tableSet = Sets.newConcurrentHashSet();
		Collection 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 orders = orderMapper.selectByExample(ex);
		orders.stream().forEach(o->{
			System.out.println("userId:"+o.getUserId() + " orderId:" + o.getOrderId());
		});
		
		hintManager.close();
	}

关于Sharding中怎么利用JDBC实现强制路由就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


文章名称:Sharding中怎么利用JDBC实现强制路由
本文路径:http://lszwz.com/article/gdopdo.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款