Apache ShardingSphere集成
Posted java_wxid
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Apache ShardingSphere集成相关的知识,希望对你有一定的参考价值。
文章目录
- 创建apache-shardingsphere-demo项目
- 修改pom.xml
- 修改ApacheShardingsphereDemoApplication
- 创建application.properties
- 创建application01.properties
- 创建application02.properties
- 创建application03.properties
- 创建application04.properties
- 创建Course
- 创建Dict
- 创建User
- 创建CourseMapper
- 创建DictMapper
- 创建UserMapper
- 创建MyComplexDSShardingAlgorithm
- 创建MyComplexTableShardingAlgorithm
- 创建MyHintTableShardingAlgorithm
- 创建MyPreciseDSShardingAlgorithm
- 创建MyPreciseTableShardingAlgorithm
- 创建MyRangeDSShardingAlgorithm
- 创建MyRangeTableShardingAlgorithm
- 创建course.sql
- 创建t_dict.sql
- 创建t_user.sql
- 修改ApacheShardingsphereDemoApplicationTests
- 校验Apache ShardingSphere是否正常工作
创建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模2加1
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模2加1
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.key-generator.column=dict_id
spring.shardingsphere.sharding.tables.t_dict.key-generator.type=SNOWFLAKE
spring.shardingsphere.props.sql.show = true
spring.main.allow-bean-definition-overriding=true
创建application03.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
spring.shardingsphere.sharding.tables.t_dict.actual-data-nodes=m1.t_dict_$->1..2
spring.shardingsphere.sharding.tables.t_dict.key-generator.column=dict_id
spring.shardingsphere.sharding.tables.t_dict.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.t_dict.key-generator.props.worker.id=1
spring.shardingsphere.sharding.tables.t_dict.table-strategy.inline.sharding-column=ustatus
spring.shardingsphere.sharding.tables.t_dict.table-strategy.inline.algorithm-expression=t_dict_$->ustatus.toInteger()%2+1
spring.shardingsphere.sharding.tables.user.actual-data-nodes=m1.t_user_$->1..2
spring.shardingsphere.sharding.tables.user.key-gene以上是关于Apache ShardingSphere集成的主要内容,如果未能解决你的问题,请参考以下文章
ShardingJdbc:Springboot集成ShardingSphere,单服务跨数据源时,简单实现事务管理
分布式数据库解决方案Apache ShardingSphere毕业成为顶级项目
Apache首个分布式数据库中间件项目ShardingSphere进入孵化器
京东数科主导的首个Apache基金会项目ShardingSphere进入孵化器