最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot 2.0 整合sharding-jdbc中間件實(shí)現(xiàn)數(shù)據(jù)分庫分表

 更新時間:2019年06月04日 09:40:19   作者:知了一笑  
這篇文章主要介紹了SpringBoot 2.0 整合sharding-jdbc中間件,實(shí)現(xiàn)數(shù)據(jù)分庫分表,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下

一、水平分割

1、水平分庫
1)、概念:
 以字段為依據(jù),按照一定策略,將一個庫中的數(shù)據(jù)拆分到多個庫中。
2)、結(jié)果
 每個庫的結(jié)構(gòu)都一樣;數(shù)據(jù)都不一樣;
 所有庫的并集是全量數(shù)據(jù);
2、水平分表
1)、概念
 以字段為依據(jù),按照一定策略,將一個表中的數(shù)據(jù)拆分到多個表中。
2)、結(jié)果
 每個表的結(jié)構(gòu)都一樣;數(shù)據(jù)都不一樣;
 所有表的并集是全量數(shù)據(jù);

二、Shard-jdbc 中間件

1、架構(gòu)圖


2、特點(diǎn)

1)、Sharding-JDBC直接封裝JDBC API,舊代碼遷移成本幾乎為零。
2)、適用于任何基于Java的ORM框架,如Hibernate、Mybatis等 。
3)、可基于任何第三方的數(shù)據(jù)庫連接池,如DBCP、C3P0、 BoneCP、Druid等。
4)、以jar包形式提供服務(wù),無proxy代理層,無需額外部署,無其他依賴。
5)、分片策略靈活,可支持等號、between、in等多維度分片,也可支持多分片鍵。
6)、SQL解析功能完善,支持聚合、分組、排序、limit、or等查詢。

三、項(xiàng)目演示

1、項(xiàng)目結(jié)構(gòu)

springboot     2.0 版本
druid          1.1.13 版本
sharding-jdbc  3.1 版本

2、數(shù)據(jù)庫配置

一臺基礎(chǔ)庫映射(shard_one)
兩臺庫做分庫分表(shard_two,shard_three)。
表使用:table_one,table_two

3、核心代碼塊

數(shù)據(jù)源配置文件

spring:
 datasource:
  # 數(shù)據(jù)源:shard_one
  dataOne:
   type: com.alibaba.druid.pool.DruidDataSource
   druid:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/shard_one?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=false
    username: root
    password: 123
    initial-size: 10
    max-active: 100
    min-idle: 10
    max-wait: 60000
    pool-prepared-statements: true
    max-pool-prepared-statement-per-connection-size: 20
    time-between-eviction-runs-millis: 60000
    min-evictable-idle-time-millis: 300000
    max-evictable-idle-time-millis: 60000
    validation-query: SELECT 1 FROM DUAL
    # validation-query-timeout: 5000
    test-on-borrow: false
    test-on-return: false
    test-while-idle: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
  # 數(shù)據(jù)源:shard_two
  dataTwo:
   type: com.alibaba.druid.pool.DruidDataSource
   druid:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/shard_two?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=false
    username: root
    password: 123
    initial-size: 10
    max-active: 100
    min-idle: 10
    max-wait: 60000
    pool-prepared-statements: true
    max-pool-prepared-statement-per-connection-size: 20
    time-between-eviction-runs-millis: 60000
    min-evictable-idle-time-millis: 300000
    max-evictable-idle-time-millis: 60000
    validation-query: SELECT 1 FROM DUAL
    # validation-query-timeout: 5000
    test-on-borrow: false
    test-on-return: false
    test-while-idle: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
  # 數(shù)據(jù)源:shard_three
  dataThree:
   type: com.alibaba.druid.pool.DruidDataSource
   druid:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/shard_three?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=false
    username: root
    password: 123
    initial-size: 10
    max-active: 100
    min-idle: 10
    max-wait: 60000
    pool-prepared-statements: true
    max-pool-prepared-statement-per-connection-size: 20
    time-between-eviction-runs-millis: 60000
    min-evictable-idle-time-millis: 300000
    max-evictable-idle-time-millis: 60000
    validation-query: SELECT 1 FROM DUAL
    # validation-query-timeout: 5000
    test-on-borrow: false
    test-on-return: false
    test-while-idle: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

數(shù)據(jù)庫分庫策略

/**
 * 數(shù)據(jù)庫映射計(jì)算
 */
public class DataSourceAlg implements PreciseShardingAlgorithm<String> {

  private static Logger LOG = LoggerFactory.getLogger(DataSourceAlg.class);
  @Override
  public String doSharding(Collection<String> names, PreciseShardingValue<String> value) {
    LOG.debug("分庫算法參數(shù) {},{}",names,value);
    int hash = HashUtil.rsHash(String.valueOf(value.getValue()));
    return "ds_" + ((hash % 2) + 2) ;
  }
}

數(shù)據(jù)表1分表策略

/**
 * 分表算法
 */
public class TableOneAlg implements PreciseShardingAlgorithm<String> {
  private static Logger LOG = LoggerFactory.getLogger(TableOneAlg.class);
  /**
   * 該表每個庫分5張表
   */
  @Override
  public String doSharding(Collection<String> names, PreciseShardingValue<String> value) {
    LOG.debug("分表算法參數(shù) {},{}",names,value);
    int hash = HashUtil.rsHash(String.valueOf(value.getValue()));
    return "table_one_" + (hash % 5+1);
  }
}

數(shù)據(jù)表2分表策略

/**
 * 分表算法
 */
public class TableTwoAlg implements PreciseShardingAlgorithm<String> {
  private static Logger LOG = LoggerFactory.getLogger(TableTwoAlg.class);
  /**
   * 該表每個庫分5張表
   */
  @Override
  public String doSharding(Collection<String> names, PreciseShardingValue<String> value) {
    LOG.debug("分表算法參數(shù) {},{}",names,value);
    int hash = HashUtil.rsHash(String.valueOf(value.getValue()));
    return "table_two_" + (hash % 5+1);
  }
}

數(shù)據(jù)源集成配置

/**
 * 數(shù)據(jù)庫分庫分表配置
 */
@Configuration
public class ShardJdbcConfig {
  // 省略了 druid 配置,源碼中有
  /**
   * Shard-JDBC 分庫配置
   */
  @Bean
  public DataSource dataSource (@Autowired DruidDataSource dataOneSource,
                 @Autowired DruidDataSource dataTwoSource,
                 @Autowired DruidDataSource dataThreeSource) throws Exception {
    ShardingRuleConfiguration shardJdbcConfig = new ShardingRuleConfiguration();
    shardJdbcConfig.getTableRuleConfigs().add(getTableRule01());
    shardJdbcConfig.getTableRuleConfigs().add(getTableRule02());
    shardJdbcConfig.setDefaultDataSourceName("ds_0");
    Map<String,DataSource> dataMap = new LinkedHashMap<>() ;
    dataMap.put("ds_0",dataOneSource) ;
    dataMap.put("ds_2",dataTwoSource) ;
    dataMap.put("ds_3",dataThreeSource) ;
    Properties prop = new Properties();
    return ShardingDataSourceFactory.createDataSource(dataMap, shardJdbcConfig, new HashMap<>(), prop);
  }

  /**
   * Shard-JDBC 分表配置
   */
  private static TableRuleConfiguration getTableRule01() {
    TableRuleConfiguration result = new TableRuleConfiguration();
    result.setLogicTable("table_one");
    result.setActualDataNodes("ds_${2..3}.table_one_${1..5}");
    result.setDatabaseShardingStrategyConfig(new StandardShardingStrategyConfiguration("phone", new DataSourceAlg()));
    result.setTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("phone", new TableOneAlg()));
    return result;
  }
  private static TableRuleConfiguration getTableRule02() {
    TableRuleConfiguration result = new TableRuleConfiguration();
    result.setLogicTable("table_two");
    result.setActualDataNodes("ds_${2..3}.table_two_${1..5}");
    result.setDatabaseShardingStrategyConfig(new StandardShardingStrategyConfiguration("phone", new DataSourceAlg()));
    result.setTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("phone", new TableTwoAlg()));
    return result;
  }
}

測試代碼執(zhí)行流程

@RestController
public class ShardController {
  @Resource
  private ShardService shardService ;
  /**
   * 1、建表流程
   */
  @RequestMapping("/createTable")
  public String createTable (){
    shardService.createTable();
    return "success" ;
  }
  /**
   * 2、生成表 table_one 數(shù)據(jù)
   */
  @RequestMapping("/insertOne")
  public String insertOne (){
    shardService.insertOne();
    return "SUCCESS" ;
  }
  /**
   * 3、生成表 table_two 數(shù)據(jù)
   */
  @RequestMapping("/insertTwo")
  public String insertTwo (){
    shardService.insertTwo();
    return "SUCCESS" ;
  }
  /**
   * 4、查詢表 table_one 數(shù)據(jù)
   */
  @RequestMapping("/selectOneByPhone/{phone}")
  public TableOne selectOneByPhone (@PathVariable("phone") String phone){
    return shardService.selectOneByPhone(phone);
  }
  /**
   * 5、查詢表 table_one 數(shù)據(jù)
   */
  @RequestMapping("/selectTwoByPhone/{phone}")
  public TableTwo selectTwoByPhone (@PathVariable("phone") String phone){
    return shardService.selectTwoByPhone(phone);
  }
}

四、項(xiàng)目源碼

GitHub:知了一笑

https://github.com/cicadasmile/middle-ware-parent

總結(jié)

以上所述是小編給大家介紹的SpringBoot 2.0 整合sharding-jdbc中間件實(shí)現(xiàn)數(shù)據(jù)分庫分表,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

  • Spring?Boot配置文件的語法規(guī)則詳解(properties和yml)

    Spring?Boot配置文件的語法規(guī)則詳解(properties和yml)

    這篇文章主要介紹了Spring?Boot配置文件的語法規(guī)則,主要介紹兩種配置文件的語法和格式,properties和yml,對于配置文件也有獨(dú)立的文件夾存放,主要用來存放一些需要經(jīng)過變動的數(shù)據(jù)(變量值),感興趣的朋友跟隨小編一起看看吧
    2024-07-07
  • mybatis中xml之trim屬性說明

    mybatis中xml之trim屬性說明

    這篇文章主要介紹了mybatis中xml之trim屬性說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java Mybatis框架增刪查改與核心配置詳解流程與用法

    Java Mybatis框架增刪查改與核心配置詳解流程與用法

    MyBatis 是一款優(yōu)秀的持久層框架,它支持自定義 SQL、存儲過程以及高級映射。MyBatis 免除了幾乎所有的 JDBC 代碼以及設(shè)置參數(shù)和獲取結(jié)果集的工作。MyBatis 可以通過簡單的 XML 或注解來配置和映射原始類型、接口和 Java POJO為數(shù)據(jù)庫中的記錄
    2021-10-10
  • 淺談JVM之使用JFR解決內(nèi)存泄露

    淺談JVM之使用JFR解決內(nèi)存泄露

    內(nèi)存泄露的主要原因就是java中的對象生命周期有長有短。如果長生命周期的對象引用了短生命周期的對象,就有可能造成事實(shí)上的內(nèi)存泄露。本文將介紹JVM之使用JFR解決內(nèi)存泄露。
    2021-06-06
  • 利用Thumbnailator輕松實(shí)現(xiàn)圖片縮放、旋轉(zhuǎn)與加水印

    利用Thumbnailator輕松實(shí)現(xiàn)圖片縮放、旋轉(zhuǎn)與加水印

    java開發(fā)中經(jīng)常遇到對圖片的處理,JDK中也提供了對應(yīng)的工具類,不過處理起來很麻煩,Thumbnailator是一個優(yōu)秀的圖片處理的開源Java類庫,處理效果遠(yuǎn)比Java API的好,這篇文章主要介紹了利用Thumbnailator如何輕松的實(shí)現(xiàn)圖片縮放、旋轉(zhuǎn)與加水印,需要的朋友可以參考下
    2017-01-01
  • Java 中 ObjectMapper用法(一個簡單 Demo 講清楚)

    Java 中 ObjectMapper用法(一個簡單 Demo 講清楚)

    ObjectMapper是Jackson庫中的一個主要類,它負(fù)責(zé)將Java對象轉(zhuǎn)換為 JSON格式(序列化),或?qū)SON數(shù)據(jù)轉(zhuǎn)換為Java 對象(反序列化),本文給大家介紹Java 中 ObjectMapper用法,感興趣的朋友跟隨小編一起看看吧
    2026-01-01
  • SpringBoot?2.7.18?集成?Mybatis?Plus?+?Druid的實(shí)例詳解

    SpringBoot?2.7.18?集成?Mybatis?Plus?+?Druid的實(shí)例詳解

    Mybatis和MybatisPlus都是流行的持久層框架,MybatisPlus在Mybatis基礎(chǔ)上增加了更多便捷的功能,如自動CRUD、分頁插件等,文章還提到了Entity、Mapper、Service、Controller等組件的基本使用方法,為開發(fā)者提供了一套完整的集成方案
    2024-10-10
  • java基于netty NIO的簡單聊天室的實(shí)現(xiàn)

    java基于netty NIO的簡單聊天室的實(shí)現(xiàn)

    這篇文章主要介紹了java基于netty NIO的簡單聊天室的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 如何通過SpringBoot實(shí)現(xiàn)商城秒殺系統(tǒng)

    如何通過SpringBoot實(shí)現(xiàn)商城秒殺系統(tǒng)

    這篇文章主要介紹了如何通過SpringBoot實(shí)現(xiàn)商城秒殺系統(tǒng),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Zookeeper中如何解決zookeeper.out文件輸出位置問題

    Zookeeper中如何解決zookeeper.out文件輸出位置問題

    這篇文章主要介紹了Zookeeper中如何解決zookeeper.out文件輸出位置問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04

最新評論

平昌县| 广德县| 兴化市| 泉州市| 博乐市| 莎车县| 方正县| 时尚| 烟台市| 沙雅县| 广宗县| 浙江省| 那坡县| 曲麻莱县| 博湖县| 温泉县| 田东县| 东阿县| 汉阴县| 扎兰屯市| 石首市| 华安县| 水富县| 桐梓县| 扎赉特旗| 克东县| 云浮市| 绥中县| 昌平区| 广昌县| 尚义县| 太康县| 枣阳市| 新安县| 定边县| 怀来县| 莲花县| 惠州市| 凤山县| 建昌县| 上杭县|