成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Sharding-Jdbc實(shí)現(xiàn)mysql分庫(kù)分表

go4it / 1792人閱讀

摘要:實(shí)現(xiàn)數(shù)據(jù)庫(kù)分庫(kù)分表可以自己實(shí)現(xiàn),也可以使用和實(shí)現(xiàn)。分布式數(shù)據(jù)庫(kù)的自增不是自增的。分布式數(shù)據(jù)庫(kù)分頁查詢需要使用插入時(shí)間實(shí)現(xiàn)。包含分庫(kù)分片和讀寫分離功能。

Sharding-Jdbc實(shí)現(xiàn)mysql分庫(kù)分表 簡(jiǎn)單介紹

數(shù)據(jù)庫(kù)分庫(kù)分表和讀寫分離區(qū)別,分庫(kù)分表是在多個(gè)庫(kù)建相同的表和同一個(gè)庫(kù)建不同的表,根據(jù)隨機(jī)或者哈希等方式查找實(shí)現(xiàn)。讀寫分離是為了解決數(shù)據(jù)庫(kù)的讀寫性能不足,使用主庫(kù)master進(jìn)行寫操作,從庫(kù)slave進(jìn)行讀操作,通過binglog實(shí)現(xiàn)主被庫(kù)數(shù)據(jù)的同步。
實(shí)現(xiàn)數(shù)據(jù)庫(kù)分庫(kù)分表可以自己實(shí)現(xiàn),也可以使用mycat和sharding-jdbc實(shí)現(xiàn)。

插播概念

(1)olap和oltp聯(lián)機(jī)事務(wù)處理OLTP(on-line transaction processing)、聯(lián)機(jī)分析處理OLAP(On-Line Analytical Processing)。OLTP是傳統(tǒng)的關(guān)系型數(shù)據(jù)庫(kù)的主要應(yīng)用,主要是基本的、日常的事務(wù)處理,例如銀行交易。OLAP是數(shù)據(jù)倉(cāng)庫(kù)系統(tǒng)的主要應(yīng)用,支持復(fù)雜的分析操作,側(cè)重決策支持,并且提供直觀易懂的查詢結(jié)果。
(2)分布式數(shù)據(jù)庫(kù)的自增ID不是自增的。分布式數(shù)據(jù)庫(kù)分頁查詢需要使用插入時(shí)間實(shí)現(xiàn)。
(3)explain命令,explain顯示了mysql如何使用索引來處理select語句以及連接表??梢詭椭x擇更好的索引和寫出更優(yōu)化的查詢語句。在select語句前面加上就可以。

Sharding-Jdbc介紹

sharding-jdbc是當(dāng)當(dāng)網(wǎng)開源的一款客戶端代理中間價(jià)。sharding-jdbc包含分庫(kù)分片和讀寫分離功能。對(duì)應(yīng)用的代碼沒有侵入型,幾乎沒有任何改動(dòng),兼容主流orm框架,主流數(shù)據(jù)庫(kù)連接池。目前屬于apache的孵化項(xiàng)目shardingSphere,發(fā)展迅猛。sharding-jdbc實(shí)現(xiàn)實(shí)現(xiàn)讀寫分離不能實(shí)現(xiàn)主從庫(kù)數(shù)據(jù)同步

Sharding-Jdbc使用(Spring boot)

(1)創(chuàng)建sharding-jdbc項(xiàng)目和數(shù)據(jù)庫(kù) ds_master_0,ds_master_1,ds_master_0_slave_0,ds_master_1_slave_0

create table order0
(
    id bigint(11) not null comment "主鍵ID" primary key,
    user_id  bigint(11) null comment "用戶ID",
    order_id bigint(11) null comment "訂單ID"
);
create table order1
(
    id bigint(11) not null comment "主鍵ID" primary key,
    user_id  bigint(11) null comment "用戶ID",
    order_id bigint(11) null comment "訂單ID"
);

(2)添加依賴

    
        io.shardingsphere
        sharding-jdbc-spring-boot-starter
        3.0.0
    

    
        com.alibaba
        druid-spring-boot-starter
        1.1.13
    
    
        mysql
        mysql-connector-java
        runtime
    
    
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        2.1.0
    

(3)配置文件

spring.application.name=sharding-jdbc

#mybatis
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.mapper-locations=classpath:mapper/*Mapper.xml

#當(dāng)注冊(cè)遇到相同名字是否允許被注冊(cè),在配置中心無效
spring.main.allow-bean-definition-overriding=true

#所有主從庫(kù)
sharding.jdbc.datasource.names=dsmaster0,dsmaster1,dsmaster0slave0,dsmaster1slave0

#dsmaster0
sharding.jdbc.datasource.dsmaster0.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.dsmaster0.driver-class-name=com.mysql.cj.jdbc.Driver
sharding.jdbc.datasource.dsmaster0.url=jdbc:mysql://ailijie.top:3306/ds_master_0?useSSL=false
sharding.jdbc.datasource.dsmaster0.username=root
sharding.jdbc.datasource.dsmaster0.password=

#slave for ds_master_0
sharding.jdbc.datasource.dsmaster0slave0.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.dsmaster0slave0.driver-class-name=com.mysql.cj.jdbc.Driver
sharding.jdbc.datasource.dsmaster0slave0.url=jdbc:mysql://ailijie.top:3306/ds_master_0_slave_0?useSSL=false
sharding.jdbc.datasource.dsmaster0slave0.username=root
sharding.jdbc.datasource.dsmaster0slave0.password=

#dsmaster1
sharding.jdbc.datasource.dsmaster1.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.dsmaster1.driver-class-name=com.mysql.cj.jdbc.Driver
sharding.jdbc.datasource.dsmaster1.url=jdbc:mysql://ailijie.top:3306/ds_master_1?useSSL=false
sharding.jdbc.datasource.dsmaster1.username=root
sharding.jdbc.datasource.dsmaster1.password=

#slave for ds_master_1
sharding.jdbc.datasource.dsmaster1slave0.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.dsmaster1slave0.driver-class-name=com.mysql.cj.jdbc.Driver
sharding.jdbc.datasource.dsmaster1slave0.url=jdbc:mysql://ailijie.top:3306/ds_master_1_slave_0?useSSL=false
sharding.jdbc.datasource.dsmaster1slave0.username=root
sharding.jdbc.datasource.dsmaster1slave0.password=

#分庫(kù)規(guī)則
sharding.jdbc.config.sharding.default-database-strategy.inline.sharding-column=user_id
sharding.jdbc.config.sharding.default-database-strategy.inline.algorithm-expression=dsmaster${user_id % 2}

#分表規(guī)則
sharding.jdbc.config.sharding.tables.order.actual-data-nodes=dsmaster${0..1}.order${0..1}
sharding.jdbc.config.sharding.tables.order.table-strategy.inline.shardingColumn=order_id
sharding.jdbc.config.sharding.tables.order.table-strategy.inline.algorithmExpression=order${order_id % 2}
#使用id作為分布式主鍵
sharding.jdbc.config.sharding.tables.order.key-generator-column-name=user_id

#邏輯主從庫(kù)名和實(shí)際主從庫(kù)映射關(guān)系
#sharding.jdbc.config.sharding.master-slave-rules.ds0.master-data-source-name=dsmaster0
#用逗號(hào)分隔
#sharding.jdbc.config.sharding.master-slave-rules.ds0.slave-data-source-names=dsmaster0
#sharding.jdbc.config.sharding.master-slave-rules.dsmaster1.masterDataSourceName=dsmaster1
#sharding.jdbc.config.sharding.master-slave-rules.dsmaster1.slaveDataSourceNames=dsmaster1slave0

(5)實(shí)體類 Order

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Order implements Serializable {
    private static final long serialVersionUID = 427226138907372838L;
    private Long id;

    private Integer userId;
    private Integer orderId;
}

(4)controller外部接口

@Slf4j
@RequestMapping("sharding")
@RestController
public class ShardingController {

   @Autowired
   private OrderMapper orderMapper;

   @RequestMapping
   public String helloShardin(){
      return "hello Sharding-jdbc";
   }

   @RequestMapping("insert")
   public void insert(@RequestParam Integer orderId, @RequestParam Integer userId) {
      Order order = Order.builder()
            .orderId(orderId).userId(userId).build();
      orderMapper.insert(order);
      Long id = order.getId();
      log.info("Generated Key--id:" + id);
   }

   @RequestMapping("queryAll")
   public void findAll() {
       List orders = orderMapper.queryAll();
       log.info("user:{}", orders);
       log.info("user:{}",orders.size());
   }

   @RequestMapping("getById")
   public void getById(@RequestParam Long id) {
      Order order = orderMapper.queryById(id);
      log.info("user:{}", order);
   }

   @RequestMapping("getByUserId")
   public void getByUserId(@RequestParam Long userId) {
      List orders = orderMapper.queryByUserId(userId);
      log.info("user:{}", orders);
   }

   @RequestMapping("deleteById")
   public void deleteById(@RequestParam Long id) {
        orderMapper.deleteById(id);
        log.info("user:{}", id);
   }
 }

使用sharding-jdbc沒有侵入性,不會(huì)影響業(yè)務(wù)代碼。
可以使用Springboot的配置文件規(guī)范進(jìn)行配置。

有問題,請(qǐng)留言!

個(gè)人博客地址 歡迎訪問

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/75650.html

相關(guān)文章

  • springboot實(shí)踐筆記之一:springboot+sharding-jdbc+mybatis全

    摘要:現(xiàn)在的分片策略是上海深圳分別建庫(kù),每個(gè)庫(kù)都存各自交易所的兩支股票的,且按照月分表。五配置分片策略數(shù)據(jù)庫(kù)分片策略在這個(gè)實(shí)例中,數(shù)據(jù)庫(kù)的分庫(kù)就是根據(jù)上海和深圳來分的,在中是單鍵分片。 由于當(dāng)當(dāng)發(fā)布了最新的Sharding-Sphere,所以本文已經(jīng)過時(shí),不日將推出新的版本 項(xiàng)目中遇到了分庫(kù)分表的問題,找到了shrding-jdbc,于是就搞了一個(gè)springboot+sharding-jd...

    Snailclimb 評(píng)論0 收藏0
  • database

    摘要:它是第一個(gè)把數(shù)據(jù)分布在全球范圍內(nèi)的系統(tǒng),并且支持外部一致性的分布式事務(wù)。目的是使得開發(fā)者閱讀之后,能對(duì)項(xiàng)目有一個(gè)初步了解,更好的參與進(jìn)入的開發(fā)中。深度探索數(shù)據(jù)庫(kù)并發(fā)控制技術(shù)并發(fā)控制技術(shù)是數(shù)據(jù)庫(kù)事務(wù)處理的核心技術(shù)。 存儲(chǔ)過程高級(jí)篇 講解了一些存儲(chǔ)過程的高級(jí)特性,包括 cursor、schema、控制語句、事務(wù)等。 數(shù)據(jù)庫(kù)索引與事務(wù)管理 本篇文章為對(duì)數(shù)據(jù)庫(kù)知識(shí)的查缺補(bǔ)漏,從索引,事務(wù)管理,...

    csRyan 評(píng)論0 收藏0
  • Spring Boot中整合Sharding-JDBC讀寫分離示例

    摘要:今天就給大家介紹下方式的使用,主要講解讀寫分離的配置,其余的后面再介紹。主要還是用提供的,配置如下配置內(nèi)容如下主數(shù)據(jù)源從數(shù)據(jù)源讀寫分離配置查詢時(shí)的負(fù)載均衡算法,目前有種算法,輪詢和隨機(jī),算法接口是。 在我《Spring Cloud微服務(wù)-全棧技術(shù)與案例解析》書中,第18章節(jié)分庫(kù)分表解決方案里有對(duì)Sharding-JDBC的使用進(jìn)行詳細(xì)的講解。 之前是通過XML方式來配置數(shù)據(jù)源,讀寫分離...

    kbyyd24 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<