91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Sharding中怎么利用JDBC實現強制路由

發布時間:2021-07-30 13:46:23 來源:億速云 閱讀:862 作者:Leah 欄目:大數據

這篇文章給大家介紹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實現強制路由就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

商都县| 资溪县| 武威市| 来安县| 乌海市| 呈贡县| 新津县| 唐海县| 区。| 珲春市| 长白| 勃利县| 汕头市| 庆安县| 益阳市| 资中县| 蓬溪县| 麻栗坡县| 阜新| 科技| 榆社县| 太谷县| 耿马| 永仁县| 左贡县| 临西县| 双城市| 徐水县| 丽江市| 兴安县| 大连市| 西充县| 桦南县| 景洪市| 平阴县| 应用必备| 丰都县| 乌兰浩特市| 旬阳县| 简阳市| 湘西|