SpringBoot集成ShardingSphere(自动配置)

接上一篇,介绍下SpringBoot集成ShardingSphere,基于sharding-jdbc-spring-boot-starter依赖编写配置文件实现自动配置的集成方法,这种方法的优点是简单,缺点是不够灵活。

  • 新建项目并导入依赖


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.6.RELEASE
        
    
    com.hyc
    shard3-auto
    0.0.1-SNAPSHOT
    shard3-auto
    springboot集成shardingshpere3自动配置

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.1
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            io.shardingsphere
            sharding-jdbc-spring-boot-starter
            3.1.0
        
        
        
            org.apache.commons
            commons-lang3
            3.9
        
        
            cn.hutool
            hutool-all
            4.5.13
        
        
            com.alibaba
            fastjson
            1.2.58
        
        
            com.alibaba
            druid
            1.1.17
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


  • 其他步骤基本与上一篇类似,不再赘述,可以查看上篇:https://blog.csdn.net/hyc2zbj/article/details/94005745
  • 分片规则配置
sharding.jdbc.datasource.names=ds0,ds1

sharding.jdbc.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.ds0.url=jdbc:mysql://127.0.0.1:3306/test1?serverTimezone=GMT%2B8&useSSL=false
sharding.jdbc.datasource.ds0.username=root
sharding.jdbc.datasource.ds0.password=123456

sharding.jdbc.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.ds1.url=jdbc:mysql://127.0.0.1:3306/test2?serverTimezone=GMT%2B8&useSSL=false
sharding.jdbc.datasource.ds1.username=root
sharding.jdbc.datasource.ds1.password=123456

sharding.jdbc.config.sharding.tables.t_user.actual-data-nodes=ds$->{0..1}.t_user_$->{0..1}
sharding.jdbc.config.sharding.tables.t_user.table-strategy.inline.sharding-column=gender
sharding.jdbc.config.sharding.tables.t_user.table-strategy.inline.algorithm-expression=t_user_$->{gender == 1 ? 0 : 1}
sharding.jdbc.config.sharding.tables.t_user.key-generator-column-name=user_id
sharding.jdbc.config.sharding.tables.t_user.key-generator-class-name=com.hyc.keygen.SnowflakeShardingKeyGenerator

sharding.jdbc.config.sharding.tables.t_user_address.actual-data-nodes=ds$->{0..1}.t_user_address_$->{0..1}
sharding.jdbc.config.sharding.tables.t_user_address.table-strategy.inline.sharding-column=gender
sharding.jdbc.config.sharding.tables.t_user_address.table-strategy.inline.algorithm-expression=t_user_address_$->{gender == 1 ? 0 : 1}
sharding.jdbc.config.sharding.tables.t_user_address.key-generator-column-name=address_id
sharding.jdbc.config.sharding.tables.t_user_address.key-generator-class-name=com.hyc.keygen.SnowflakeShardingKeyGenerator

sharding.jdbc.config.sharding.tables.t_order.actual-data-nodes=ds$->{0..1}.t_order_$->{0..1}
sharding.jdbc.config.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id
sharding.jdbc.config.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order_$->{order_id % 2}
sharding.jdbc.config.sharding.tables.t_order.key-generator-column-name=order_id
sharding.jdbc.config.sharding.tables.t_order.key-generator-class-name=com.hyc.keygen.SnowflakeShardingKeyGenerator

sharding.jdbc.config.sharding.tables.t_order_item.actual-data-nodes=ds$->{0..1}.t_order_item_$->{0..1}
sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.sharding-column=order_id
sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.algorithm-expression=t_order_item_$->{order_id % 2}
sharding.jdbc.config.sharding.tables.t_order_item.key-generator-column-name=order_item_id
sharding.jdbc.config.sharding.tables.t_order_item.key-generator-class-name=com.hyc.keygen.SnowflakeShardingKeyGenerator

sharding.jdbc.config.sharding.binding-tables[0]=t_user,t_user_address
sharding.jdbc.config.sharding.binding-tables[1]=t_order,t_order_item

sharding.jdbc.config.sharding.broadcast-tables=t_product

sharding.jdbc.config.sharding.default-database-strategy.standard.sharding-column=user_id
sharding.jdbc.config.sharding.default-database-strategy.standard.precise-algorithm-class-name=com.hyc.dbstrategy.IdShardingAlgorithm

sharding.jdbc.config.props.sql.show=true

snow.work.id=1
snow.datacenter.id=2

#sql日志
logging.level.com.hyc.dao=debug

#actuator端口
management.server.port=9001
#开放所有页面节点  默认只开启了health、info两个节点
management.endpoints.web.exposure.include=*
#显示健康具体信息  默认不会显示详细信息
management.endpoint.health.show-details=always

mybatis.configuration.map-underscore-to-camel-case=true
mybatis.type-aliases-package=com.hyc.entity
mybatis.mapper-locations=classpath:mappers/*.xml

以上就配置完成了,测试结果和上一篇一样

你可能感兴趣的:(java,SpringBoot)