Skip to content

Commit

Permalink
service-staff add sharding-jdbc
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhiQinIsZhen committed Dec 2, 2024
1 parent ff85ead commit 35f6bf0
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 29 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
![Gateway Version](https://img.shields.io/badge/Gateway-4.1.3-brightgreen)
![jjwt Version](https://img.shields.io/badge/jjwt-0.12.6-brightgreen)
![Mybatis-plus Version](https://img.shields.io/badge/MybatisPlus-3.5.9-brightgreen)
![Sharding-jdbc Version](https://img.shields.io/badge/ShardingJdbc-5.5.1-brightgreen)
![Swagger Version](https://img.shields.io/badge/knife4j-4.5.0-brightgreen)
![Elasticjob Version](https://img.shields.io/badge/elasticjob-3.0.4-brightgreen)

Expand Down Expand Up @@ -47,6 +48,9 @@

1. `cloud-service-auth`: 认证资源服务,基于SpringSecurity以及jwt
2. `cloud-service-staff`: 员工信息服务
```text
1.1 使用shardingsphere-jdbc对登录登出日志进行分表
```
## 开源共建
1. 如有问题可以提交[issue](https://github.com/ZhiQinIsZhen/cloud-springboot3/issues)
Expand Down
5 changes: 5 additions & 0 deletions cloud-common/cloud-common-dao/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-jsqlparser</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.liyz.cloud</groupId>
<artifactId>cloud-common-exception</artifactId>
Expand Down
21 changes: 16 additions & 5 deletions cloud-dependencies-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springboot.version>3.3.4</springboot.version>
<spring-cloud.version>2023.0.3</spring-cloud.version>
<springboot.version>3.3.6</springboot.version>
<spring-cloud.version>2023.0.4</spring-cloud.version>
<hutool.version>5.8.32</hutool.version>
<google.guava.version>33.3.1-jre</google.guava.version>
<knife4j.version>4.5.0</knife4j.version>
<mybatis.plus.version>3.5.8</mybatis.plus.version>
<redisson.version>3.37.0</redisson.version>
<mybatis.plus.version>3.5.9</mybatis.plus.version>
<redisson.version>3.39.0</redisson.version>
<io.jwt.version>0.12.6</io.jwt.version>
<elasticjob.version>3.0.4</elasticjob.version>
<shardingsphere.version>5.5.1</shardingsphere.version>
<excel.version>4.0.3</excel.version>
<springboot.admin.version>3.3.4</springboot.admin.version>
<springboot.admin.version>3.3.6</springboot.admin.version>
<rocketmq.version>2.3.1</rocketmq.version>
<kafka.version>3.2.4</kafka.version>

Expand Down Expand Up @@ -75,6 +76,11 @@
<artifactId>mybatis-plus-annotation</artifactId>
<version>${mybatis.plus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-jsqlparser</artifactId>
<version>${mybatis.plus.version}</version>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
Expand Down Expand Up @@ -105,6 +111,11 @@
<artifactId>elasticjob-lite-spring-boot-starter</artifactId>
<version>${elasticjob.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc</artifactId>
<version>${shardingsphere.version}</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions cloud-service/cloud-service-auth/auth-dao/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-jsqlparser</artifactId>
</dependency>
<dependency>
<groupId>com.liyz.cloud</groupId>
<artifactId>cloud-common-exception</artifactId>
Expand Down
8 changes: 8 additions & 0 deletions cloud-service/cloud-service-staff/staff-dao/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-jsqlparser</artifactId>
</dependency>
<dependency>
<groupId>com.liyz.cloud</groupId>
<artifactId>cloud-common-exception</artifactId>
Expand All @@ -32,6 +36,10 @@
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ spring:
enabled: false
# datasource
datasource:
url: jdbc:mysql://localhost:3306/staff3?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
type: com.zaxxer.hikari.HikariDataSource
username: root
password: root
url: jdbc:shardingsphere:classpath:sharding.yaml
driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
# hikari
hikari:
minimum-idle: 2
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
databaseName: staff2

dataSources:
staff_db_0:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/staff2?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
username: root
password: root

rules:
- !SINGLE
tables:
- "staff_db_0.*"
- !SHARDING
tables:
staff_login_log:
actualDataNodes: staff_db_0.staff_login_log_$->{0..1}
databaseStrategy:
none:
tableStrategy:
standard:
shardingColumn: staff_id
shardingAlgorithmName: login_inline
keyGenerateStrategy:
column: id
keyGeneratorName: snowflake
staff_logout_log:
actualDataNodes: staff_db_0.staff_logout_log_$->{0..1}
databaseStrategy:
none:
tableStrategy:
standard:
shardingColumn: staff_id
shardingAlgorithmName: logout_inline
keyGenerateStrategy:
column: id
keyGeneratorName: snowflake
keyGenerators:
snowflake:
type: SNOWFLAKE
props:
worker-id: 123
# 分片算法配置
shardingAlgorithms:
login_inline:
type: INLINE
props:
algorithm-expression: staff_login_log_$->{staff_id % 2}
logout_inline:
type: INLINE
props:
algorithm-expression: staff_logout_log_$->{staff_id % 2}

props:
sql-show: true
75 changes: 55 additions & 20 deletions cloud-service/cloud-service-staff/user.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE `user_auth_email` (
`user_id` bigint(20) unsigned NOT NULL COMMENT '用户ID',
CREATE TABLE `staff_auth_email` (
`staff_id` bigint(20) unsigned NOT NULL COMMENT '员工ID',
`email` varchar(128) NOT NULL COMMENT '邮箱',
`password` varchar(256) NOT NULL COMMENT '密码',
`create_user` bigint(20) NOT NULL COMMENT '创建者',
Expand All @@ -8,12 +8,12 @@ CREATE TABLE `user_auth_email` (
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`is_deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除 | 0、未删除 1、已删除',
`version` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '版本号',
PRIMARY KEY (`user_id`) USING BTREE,
PRIMARY KEY (`staff_id`) USING BTREE,
UNIQUE KEY `uniq_email` (`email`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户邮箱认证表';
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='员工邮箱认证表';

CREATE TABLE `user_auth_mobile` (
`user_id` bigint(20) unsigned NOT NULL COMMENT '用户ID',
CREATE TABLE `staff_auth_mobile` (
`staff_id` bigint(20) unsigned NOT NULL COMMENT '员工ID',
`mobile` varchar(11) NOT NULL COMMENT '邮箱',
`password` varchar(256) NOT NULL COMMENT '密码',
`create_user` bigint(20) NOT NULL COMMENT '创建者',
Expand All @@ -22,12 +22,12 @@ CREATE TABLE `user_auth_mobile` (
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`is_deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除 | 0、未删除 1、已删除',
`version` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '版本号',
PRIMARY KEY (`user_id`) USING BTREE,
PRIMARY KEY (`staff_id`) USING BTREE,
UNIQUE KEY `uniq_mobile` (`mobile`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户手机认证表';
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='员工手机认证表';

CREATE TABLE `user_info` (
`user_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户ID',
CREATE TABLE `staff_info` (
`staff_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '员工ID',
`real_name` varchar(128) DEFAULT NULL COMMENT '真实名称',
`nick_name` varchar(128) NOT NULL COMMENT '昵称',
`mobile` varchar(11) DEFAULT NULL COMMENT '手机号码',
Expand All @@ -40,12 +40,12 @@ CREATE TABLE `user_info` (
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`is_deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除 | 0、未删除 1、已删除',
`version` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '版本号',
PRIMARY KEY (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户基础信息表';
PRIMARY KEY (`staff_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='员工基础信息表';

CREATE TABLE `user_login_log` (
CREATE TABLE `staff_login_log_0` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增ID',
`user_id` bigint(20) unsigned NOT NULL COMMENT '用户ID',
`staff_id` bigint(20) unsigned NOT NULL COMMENT '员工ID',
`login_type` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '登录方式:1:手机;2:邮箱',
`device` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '登录设备:1:手机;2:web',
`login_time` timestamp NOT NULL COMMENT '登录时间',
Expand All @@ -57,12 +57,29 @@ CREATE TABLE `user_login_log` (
`is_deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除 | 0、未删除 1、已删除',
`version` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '版本号',
PRIMARY KEY (`id`) USING BTREE,
KEY `idx_user_id` (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户登录日志表';
KEY `idx_staff_id` (`staff_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='员工登录日志表-0';

CREATE TABLE `user_logout_log` (
CREATE TABLE `staff_login_log_1` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增ID',
`staff_id` bigint(20) unsigned NOT NULL COMMENT '员工ID',
`login_type` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '登录方式:1:手机;2:邮箱',
`device` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '登录设备:1:手机;2:web',
`login_time` timestamp NOT NULL COMMENT '登录时间',
`ip` varchar(256) DEFAULT NULL COMMENT '登录IP地址',
`create_user` bigint(20) NOT NULL COMMENT '创建者',
`update_user` bigint(20) NOT NULL COMMENT '更新者',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`is_deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除 | 0、未删除 1、已删除',
`version` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '版本号',
PRIMARY KEY (`id`) USING BTREE,
KEY `idx_staff_id` (`staff_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='员工登录日志表-1';

CREATE TABLE `staff_logout_log_0` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增ID',
`user_id` bigint(20) unsigned NOT NULL COMMENT '用户ID',
`staff_id` bigint(20) unsigned NOT NULL COMMENT '员工ID',
`logout_type` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '登出方式:1:手机;2:邮箱',
`device` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '登录设备:1:手机;2:web',
`logout_time` timestamp NOT NULL COMMENT '登录时间',
Expand All @@ -74,5 +91,23 @@ CREATE TABLE `user_logout_log` (
`is_deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除 | 0、未删除 1、已删除',
`version` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '版本号',
PRIMARY KEY (`id`) USING BTREE,
KEY `idx_user_id` (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户登出日志表';
KEY `idx_staff_id` (`staff_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='员工登出日志表-0';


CREATE TABLE `staff_logout_log_1` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增ID',
`staff_id` bigint(20) unsigned NOT NULL COMMENT '员工ID',
`logout_type` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '登出方式:1:手机;2:邮箱',
`device` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '登录设备:1:手机;2:web',
`logout_time` timestamp NOT NULL COMMENT '登录时间',
`ip` varchar(256) DEFAULT NULL COMMENT '登录IP地址',
`create_user` bigint(20) NOT NULL COMMENT '创建者',
`update_user` bigint(20) NOT NULL COMMENT '更新者',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`is_deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除 | 0、未删除 1、已删除',
`version` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '版本号',
PRIMARY KEY (`id`) USING BTREE,
KEY `idx_staff_id` (`staff_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='员工登出日志表-1';

0 comments on commit 35f6bf0

Please sign in to comment.