sharding-jdbc 数据库连接池配置(有点小坑),读写分离,分表策略,主键自增

 

  • 常规spring boot druid 数据库连接配置
spring:
  datasource:
    name: druidDataSource
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8
      username: root
      password: 123456
      filters: stat,wall,log4j,config
      max-active: 100
      initial-size: 1
      max-wait: 60000
      min-idle: 1
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      validation-query: select 'x'
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      pool-prepared-statements: true
      max-open-prepared-statements: 50
      max-pool-prepared-statement-per-connection-size: 20
  • sharding-jdbc 数据库连接池 ,直接配置,不需增加父节点druid

 

spring:
  #shardingsphere
  shardingsphere:
    datasource:
      #主库
      m0:
        driver-class-name: com.mysql.cj.jdbc.Driver
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://192.168.88.22:3306/test?characterEncoding=utf8&serverTimezone=Asia/Shanghai&connectTimeout=15000&socketTimeout=8000&autoReconnect=true&useSSL=false&failOverReadOnly=false
        username: root
        password: root
        maximum-pool-size: 50
        pool-name: m0-pool
      # 从库
      s0:
        driver-class-name: com.mysql.cj.jdbc.Driver
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://192.168.88.22:3306/test?characterEncoding=utf8&serverTimezone=Asia/Shanghai&autoReconnect=true&useSSL=false
        username: root
        password: root
        initial-size: 20
        max-active: 200
        max-wait: 25000
        min-evictable-idle-time-millis: 1814400
        min-idle: 20
        test-on-borrow: true
        test-on-return: true
        test-while-idle: true
        validation-query: SELECT 1
        validation-query-timeout: 2000
        time-between-eviction-runs-millis: 1814400
        time-between-connect-error-millis: 60000
        pool-prepared-statements: true
        max-pool-prepared-statement-per-connection-size: 20


      names: m0,s0
    props:
      sql:
        #打印sql
        show: true
      max:
        connections:
          size:
            per:
              # 开启内存模式
              query: 50
    sharding:
      master-slave-rules:
        # 读写分离
        ds0:
          master-data-source-name: m0
          slave-data-source-names[0]: s0 # 从库

      # 分表策略
      tables:
        test_account_log:
          table-strategy:
            standard:
              precise-algorithm-class-name: com.test.algorithem.DatePreciseShardingAlgorithm
              range-algorithm-class-name: com.test.algorithem.DateRangeShardingAlgorithm
              shardingColumn: create_time # 分表键

          actual-data-nodes: ds0.test_account_log_$->{2020..2021}_$->{01..12}
          #主键策略
          key-generator:
            column: id
            props:
              worker:
                id: 1
            type: SNOWFLAKE
       

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值