Apache ShardingSphere集成

Posted java_wxid

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Apache ShardingSphere集成相关的知识,希望对你有一定的参考价值。

主项目链接:https://gitee.com/java_wxid/java_wxid
项目架构及博文总结:

项目模块:
前期规划,实现部分

java_wxid   
├── demo        // 演示模块
│     └── 模块名称:spring-boot-demo模块                   //Spring Boot快速构建应用(已实现并有博文总结)
│     └── 模块名称:spring-cloud-alibaba-nacos-demo模块    //Spring Cloud Alibaba Nacos集成(已实现并有博文总结)
│     └── 模块名称:spring-cloud-open-feign-demo模块       //Spring Cloud Open Feign集成(已实现并有博文总结)
│     └── 模块名称:spring-cloud-hystrix-demo模块          //Spring Cloud Hystrix集成(已实现并有博文总结)
│     └── 模块名称:spring-cloud-ribbon-demo模块           //Spring Cloud Ribbon集成(已实现并有博文总结)
│     └── 模块名称:spring-cloud-gateway-demo模块          //Spring Cloud Gateway集成(已实现并有博文总结)
│     └── 模块名称:spring-cloud-security-oauth2-demo模块  //Spring Cloud Security Oauth2集成(已实现并有博文总结)
│     └── 模块名称:spring-cloud-stream-demo模块           //Spring Cloud Stream集成(框架已搭建,待校验,待博文总结)
│     └── 模块名称:spring-cloud-skywalking-demo模块       //Spring Cloud Skywalking集成(框架已搭建,待校验,待博文总结)
│     └── 模块名称:spring-cloud-alibaba-seata-demo模块    //Spring Cloud Alibaba Seata集成(已实现并有博文总结)
│     └── 模块名称:spring-cloud-alibaba-sentinel-demo模块 //Spring Cloud Alibaba Sentinel集成(框架已搭建,待校验,待博文总结)
│     └── 模块名称:apache-shardingsphere-demo模块         //Apache ShardingSphere集成(已实现并有博文总结)
│     └── 模块名称:apache-mybatis-demo模块                //Apache Mybatis集成(已实现并有博文总结)
│     └── 模块名称:redis-demo模块                         //Redis集成(已实现并有博文总结)
│     └── 模块名称:elasticsearch-demo模块                 //ElasticSearch集成(框架已搭建,待校验,待博文总结)
│     └── 模块名称:mongodb-demo模块                       //MongoDB集成(已实现并有博文总结)
│     └── 模块名称:xxl-job-demo模块                       //xxl-job集成(框架已搭建,待校验,待博文总结)
│     └── 模块名称:apache-spark-demo模块                  //Apache Spark集成
│     └── 模块名称:etl-hdfs-hive-hbase-demo模块           //ETL、HDFS、Hive、Hbase集成
│     └── 模块名称:ddd-design-mode-demo模块               //DDD、设计模式集成
│     └── 模块名称:netty-demo模块                         //Netty集成
│     └── 模块名称:vue-demo模块                           //前端vue集成
├── document        // 文档
│     └── README.ch.md                      //中文文档
│     └── README.en.md                      //英文文档

系列文章:前期主要以demo项目为主,快速集成各种微服务相关的技术,帮助大家可以快速集成到自己的项目中,节约开发时间。后期则会以企业实战项目为主,致力于解决高并发、高可用、高性能、高可靠、海量数据等疑难问题,同时记录工作中遇到的技术难点、有技术亮点,项目会结合博客、视频辅助讲解,帮助大家查询是否遇到过同类型的问题。
提示:系列文章还未全部完成,后续的文章,会慢慢补充进去的。

文章目录

创建apache-shardingsphere-demo项目

项目代码:https://gitee.com/java_wxid/java_wxid/tree/master/demo/apache-shardingsphere-demo
项目结构如下(示例):

修改pom.xml

代码如下(示例):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>apache-shardingsphere-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>apache-shardingsphere-demo</name>
    <description>Demo project for Spring Boot</description>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.4.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.4.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.23</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.22</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

修改ApacheShardingsphereDemoApplication

代码如下(示例):

package com.example.apacheshardingspheredemo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.example.apacheshardingspheredemo.mapper")
@SpringBootApplication
public class ApacheShardingsphereDemoApplication 

    public static void main(String[] args) 
        SpringApplication.run(ApacheShardingsphereDemoApplication.class, args);
    



创建application.properties

真正运行的配置
代码如下(示例):

#配置数据源
spring.shardingsphere.datasource.names=m1

spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://110.42.239.246:3306/coursedb?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=591e242ca29b9c37
#course是逻辑表名,actual-data-nodes是真实表分布,也就是sharding里面的逻辑表course对应的是m0库中course_1和course_2
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m1.course_$->1..2
#主键生成策略,cid作为主键
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
#使用雪花算法生成主键
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE
#雪花算法需要有一个参数worker.id,这个是可选的
spring.shardingsphere.sharding.tables.course.key-generator.props.worker.id=1
#表策略:选择inline依赖策略,sharding-column分片键cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid
#表策略:algorithm-expression分片算法cid模21
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->cid%2+1
#其他运行属性
spring.shardingsphere.props.sql.show = true
spring.main.allow-bean-definition-overriding=true
创建application01.properties

代码如下(示例):

#配置数据源
spring.shardingsphere.datasource.names=m1

spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://110.42.239.246:3306/coursedb?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=591e242ca29b9c37
#course是逻辑表名,actual-data-nodes是真实表分布,也就是sharding里面的逻辑表course对应的是m0库中course_1和course_2
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m1.course_$->1..2
#主键生成策略,cid作为主键
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
#使用雪花算法生成主键
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE
#雪花算法需要有一个参数worker.id,这个是可选的
spring.shardingsphere.sharding.tables.course.key-generator.props.worker.id=1
#表策略:选择inline依赖策略,sharding-column分片键cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid
#表策略:algorithm-expression分片算法cid模21
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->cid%2+1
#其他运行属性
spring.shardingsphere.props.sql.show = true
spring.main.allow-bean-definition-overriding=true


创建application02.properties

代码如下(示例):

#配置多个数据源
spring.shardingsphere.datasource.names=m1,m2

spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://110.42.239.246:3306/coursedb?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=591e242ca29b9c37


spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m2.url=jdbc:mysql://110.42.239.246:3306/coursedb2?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m2.username=root
spring.shardingsphere.datasource.m2.password=591e242ca29b9c37

#真实表分布,分库,分表
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m$->1..2.course_$->1..2

spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.course.key-generator.props.worker.id=1
#inline分片策略
#spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid
#spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->cid%2+1

#spring.shardingsphere.sharding.tables.course.database-strategy.inline.sharding-column=cid
#spring.shardingsphere.sharding.tables.course.database-strategy.inline.algorithm-expression=m$->cid%2+1
#standard标准分片策略
#spring.shardingsphere.sharding.tables.course.table-strategy.standard.sharding-column=cid
#spring.shardingsphere.sharding.tables.course.table-strategy.standard.precise-algorithm-class-name=com.roy.shardingDemo.algorithem.MyPreciseTableShardingAlgorithm
#spring.shardingsphere.sharding.tables.course.table-strategy.standard.range-algorithm-class-name=com.roy.shardingDemo.algorithem.MyRangeTableShardingAlgorithm

spring.shardingsphere.sharding.tables.course.database-strategy.standard.sharding-column=cid
spring.shardingsphere.sharding.tables.course.database-strategy.standard.precise-algorithm-class-name=com.roy.shardingDemo.algorithem.MyPreciseDSShardingAlgorithm
spring.shardingsphere.sharding.tables.course.database-strategy.standard.range-algorithm-class-name=com.roy.shardingDemo.algorithem.MyRangeDSShardingAlgorithm
#complex复杂分片策略
#spring.shardingsphere.sharding.tables.course.table-strategy.complex.sharding-columns= cid, user_id
#spring.shardingsphere.sharding.tables.course.table-strategy.complex.algorithm-class-name=com.roy.shardingDemo.algorithem.MyComplexTableShardingAlgorithm
#
#spring.shardingsphere.sharding.tables.course.database-strategy.complex.sharding-columns=cid, user_id
#spring.shardingsphere.sharding.tables.course.database-strategy.complex.algorithm-class-name=com.roy.shardingDemo.algorithem.MyComplexDSShardingAlgorithm
#hint强制路由策略
spring.shardingsphere.sharding.tables.course.table-strategy.hint.algorithm-class-name=com.roy.shardingDemo.algorithem.MyHintTableShardingAlgorithm
#广播表配置
spring.shardingsphere.sharding.broadcast-tables=t_dict
spring.shardingsphere.sharding.tables.t_dict.k

以上是关于Apache ShardingSphere集成的主要内容,如果未能解决你的问题,请参考以下文章

ShardingJdbc:Springboot集成ShardingSphere,单服务跨数据源时,简单实现事务管理

分布式数据库解决方案Apache ShardingSphere毕业成为顶级项目

Apache首个分布式数据库中间件项目ShardingSphere进入孵化器

京东数科主导的首个Apache基金会项目ShardingSphere进入孵化器

暑期 2021 参与社区——Apache ShardingSphere

深度剖析Apache Shardingsphere对分布式事务的支持