我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

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实现强制路由
URL分享:http://shouzuofang.com/article/gdopdo.html

其他资讯