From f744d564b47db6855dcd78e338605f734d655511 Mon Sep 17 00:00:00 2001
From: bunny <1319900154@qq.com>
Date: Mon, 29 Jul 2024 13:04:03 +0800
Subject: [PATCH] =?UTF-8?q?:clown=5Fface:=20feat(=E6=96=B0=E5=A2=9E):=20?=
=?UTF-8?q?=E8=AF=BB=E5=86=99=E5=88=86=E7=A6=BB=E5=AE=8C=E6=88=90?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
common/common-generator/pom.xml | 12 --
.../cn/bunny/common/generator/NewCodeGet.java | 17 +--
.../cn/bunny/common/generator/OldCodeGet.java | 57 --------
common/pom.xml | 2 +-
.../common/service/config/Knife4jConfig.java | 4 +-
.../bunny/common/service/utils/JwtHelper.java | 4 +-
dao/pom.xml | 2 +-
.../main/java/cn/bunny/entity/user/User.java | 2 +-
pom.xml | 2 +-
service/pom.xml | 2 +-
.../service/controller/IndexController.java | 21 ++-
.../service/controller/UserController.java | 2 +-
.../service/controller/WebController.java | 31 -----
.../cn/bunny/service/mapper/UserMapper.java | 2 +-
.../cn/bunny/service/service/UserService.java | 2 +-
.../service/service/impl/UserServiceImpl.java | 2 +-
service/src/main/resources/application.yml | 3 +-
service/src/main/resources/sharding.yaml | 54 --------
.../resources/sharding/sharding-default.yaml | 130 ++++++++++++++++++
.../resources/sharding/sharding-sharding.yaml | 36 +++++
.../service/impl/UserServiceImplTest.java | 15 ++
21 files changed, 225 insertions(+), 177 deletions(-)
delete mode 100644 common/common-generator/src/main/java/cn/bunny/common/generator/OldCodeGet.java
delete mode 100644 service/src/main/java/cn/bunny/service/controller/WebController.java
delete mode 100644 service/src/main/resources/sharding.yaml
create mode 100644 service/src/main/resources/sharding/sharding-default.yaml
create mode 100644 service/src/main/resources/sharding/sharding-sharding.yaml
diff --git a/common/common-generator/pom.xml b/common/common-generator/pom.xml
index 8a81ce5..6003e4d 100644
--- a/common/common-generator/pom.xml
+++ b/common/common-generator/pom.xml
@@ -34,17 +34,5 @@
velocity-engine-core
2.3
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/common/common-generator/src/main/java/cn/bunny/common/generator/NewCodeGet.java b/common/common-generator/src/main/java/cn/bunny/common/generator/NewCodeGet.java
index 9b2bfef..94bd594 100644
--- a/common/common-generator/src/main/java/cn/bunny/common/generator/NewCodeGet.java
+++ b/common/common-generator/src/main/java/cn/bunny/common/generator/NewCodeGet.java
@@ -10,16 +10,17 @@ import java.util.Collections;
public class NewCodeGet {
// 数据连接
- public static final String sqlHost = "jdbc:mysql://192.168.3.98:3306/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true";
+ // public static final String sqlHost = "jdbc:mysql://192.168.3.98:3306/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true";
+ public static final String sqlHost = "jdbc:mysql://192.168.3.21:3305/db_order?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true";
// 作者名称
- public static final String author = "Bunny";
+ public static final String author = "order";
// 公共路径
- public static final String outputDir = "D:\\Project\\demo\\sharding-sphere5\\service";
+ public static final String outputDir = "D:\\MyFolder\\demo\\sharding-sphere5\\service";
// 实体类名称
- public static final String entity = "Bunny";
+ public static final String entity = "order";
public static void main(String[] args) {
- Generation("t_userinfo");
+ Generation("t_order");
}
/**
@@ -40,7 +41,7 @@ public class NewCodeGet {
})
.packageConfig(builder -> {
builder.entity(entity)// 实体类包名
- // TODO 父包名。如果为空,将下面子包名必须写全部, 否则就只需写子包名
+ // 父包名。如果为空,将下面子包名必须写全部, 否则就只需写子包名
.parent("cn.bunny.service")
.controller("controller")// 控制层包名
.mapper("mapper")// mapper层包名
@@ -52,13 +53,13 @@ public class NewCodeGet {
.strategyConfig(builder -> {
// 设置要生成的表名
builder.addInclude(tableName)
- .addTablePrefix("t_")// TODO 设置表前缀过滤
+ .addTablePrefix("t_")// 设置表前缀过滤
.entityBuilder()
.enableLombok()
.enableChainModel()
.naming(NamingStrategy.underline_to_camel)// 数据表映射实体命名策略:默认下划线转驼峰underline_to_camel
.columnNaming(NamingStrategy.underline_to_camel)// 表字段映射实体属性命名规则:默认null,不指定按照naming执行
- .idType(IdType.AUTO)// TODO 添加全局主键类型
+ .idType(IdType.AUTO)// 添加全局主键类型
.formatFileName("%s")// 格式化实体名称,%s取消首字母I,
.mapperBuilder()
.mapperAnnotation(Mapper.class)// 开启mapper注解
diff --git a/common/common-generator/src/main/java/cn/bunny/common/generator/OldCodeGet.java b/common/common-generator/src/main/java/cn/bunny/common/generator/OldCodeGet.java
deleted file mode 100644
index d43819c..0000000
--- a/common/common-generator/src/main/java/cn/bunny/common/generator/OldCodeGet.java
+++ /dev/null
@@ -1,57 +0,0 @@
-package cn.bunny.common.generator;
-
-public class OldCodeGet {
- public static void main(String[] args) {
- // // 1、创建代码生成器
- // AutoGenerator mpg = new AutoGenerator();
- //
- // // 2、全局配置
- // // 全局配置
- // GlobalConfig gc = new GlobalConfig();
- // // TODO 需要修改路径名称
- // gc.setOutputDir("F:\\web项目\\Bunny-Cli\\Java\\java-template\\service" + "/src/main/java");
- // gc.setServiceName("%sService"); // 去掉Service接口的首字母I
- // gc.setAuthor("bunny");
- // gc.setOpen(false);
- // mpg.setGlobalConfig(gc);
- //
- // // 3、数据源配置
- // DataSourceConfig dsc = new DataSourceConfig();
- // // TODO 需要修改数据库
- // dsc.setUrl("jdbc:mysql://106.15.251.123:3305/guigu-oa?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true");
- // dsc.setDriverName("com.mysql.cj.jdbc.Driver");
- // dsc.setUsername("root");
- // dsc.setPassword("02120212");
- // dsc.setDbType(DbType.MYSQL);
- // mpg.setDataSource(dsc);
- //
- // // 4、包配置
- // PackageConfig pc = new PackageConfig();
- // pc.setParent("cn.bunny");
- // // TODO 需要修改模块名
- // pc.setModuleName("service");
- // pc.setController("controller");
- // pc.setService("service");
- // pc.setMapper("mapper");
- // mpg.setPackageInfo(pc);
- //
- // // 5、策略配置
- // StrategyConfig strategy = getStrategyConfig();
- // mpg.setStrategy(strategy);
- //
- // // 6、执行
- // mpg.execute();
- // }
- //
- // private static StrategyConfig getStrategyConfig() {
- // StrategyConfig strategy = new StrategyConfig();
- // // TODO 要生成的表
- // strategy.setInclude("sys_menu", "sys_role_menu");
- // strategy.setNaming(NamingStrategy.underline_to_camel);// 数据库表映射到实体的命名策略
- // strategy.setColumnNaming(NamingStrategy.underline_to_camel);// 数据库表字段映射到实体的命名策略
- // strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作
- // strategy.setRestControllerStyle(true); // restful api风格控制器
- // strategy.setControllerMappingHyphenStyle(true); // url中驼峰转连字符
- // return strategy;
- }
-}
diff --git a/common/pom.xml b/common/pom.xml
index 6b185ed..bcdf102 100644
--- a/common/pom.xml
+++ b/common/pom.xml
@@ -3,7 +3,7 @@
4.0.0
cn.bunny
- bunny-template
+ sharding-sphere5
0.0.1-SNAPSHOT
diff --git a/common/service-utils/src/main/java/cn/bunny/common/service/config/Knife4jConfig.java b/common/service-utils/src/main/java/cn/bunny/common/service/config/Knife4jConfig.java
index 2b1c77e..87c859f 100644
--- a/common/service-utils/src/main/java/cn/bunny/common/service/config/Knife4jConfig.java
+++ b/common/service-utils/src/main/java/cn/bunny/common/service/config/Knife4jConfig.java
@@ -16,11 +16,11 @@ public class Knife4jConfig {
@Bean
public OpenAPI openAPI() {
// 作者等信息
- Contact contact = new Contact().name("Bunny").email("1319900154@qq.com").url("http://z-bunny.com");
+ Contact contact = new Contact().name("order").email("1319900154@qq.com").url("http://z-bunny.com");
// 使用协议
License license = new License().name("MIT").url("http://MUT.com");
// 相关信息
- Info info = new Info().title("Bunny-Java-Template").description("Bunny的Java模板").version("v1.0.0").contact(contact).license(license).termsOfService("记得给我start");
+ Info info = new Info().title("order-Java-Template").description("Bunny的Java模板").version("v1.0.0").contact(contact).license(license).termsOfService("记得给我start");
return new OpenAPI().info(info).externalDocs(new ExternalDocumentation());
}
diff --git a/common/service-utils/src/main/java/cn/bunny/common/service/utils/JwtHelper.java b/common/service-utils/src/main/java/cn/bunny/common/service/utils/JwtHelper.java
index 07394bf..4c58d50 100644
--- a/common/service-utils/src/main/java/cn/bunny/common/service/utils/JwtHelper.java
+++ b/common/service-utils/src/main/java/cn/bunny/common/service/utils/JwtHelper.java
@@ -13,9 +13,9 @@ public class JwtHelper {
// 时间 按天 计算
private static final long tokenExpiration = 24 * 60 * 60 * 1000;
// JWT 的 秘钥
- private static final String tokenSignKey = "Bunny-Java-Template";
+ private static final String tokenSignKey = "order-Java-Template";
// 默认主题
- private static final String subject = "Bunny";
+ private static final String subject = "order";
// 默认时间
private static final Date time = new Date(System.currentTimeMillis() + tokenExpiration * 7);
diff --git a/dao/pom.xml b/dao/pom.xml
index dfcd89a..ce59a37 100644
--- a/dao/pom.xml
+++ b/dao/pom.xml
@@ -3,7 +3,7 @@
4.0.0
cn.bunny
- bunny-template
+ sharding-sphere5
0.0.1-SNAPSHOT
diff --git a/dao/src/main/java/cn/bunny/entity/user/User.java b/dao/src/main/java/cn/bunny/entity/user/User.java
index 6667317..7be04ed 100644
--- a/dao/src/main/java/cn/bunny/entity/user/User.java
+++ b/dao/src/main/java/cn/bunny/entity/user/User.java
@@ -16,7 +16,7 @@ import java.io.Serializable;
*
*
*
- * @author Bunny
+ * @author order
* @since 2024-07-28
*/
@Getter
diff --git a/pom.xml b/pom.xml
index e29a465..0baaf86 100644
--- a/pom.xml
+++ b/pom.xml
@@ -9,7 +9,7 @@
cn.bunny
- bunny-template
+ sharding-sphere5
0.0.1-SNAPSHOT
pom
bunny-template
diff --git a/service/pom.xml b/service/pom.xml
index 3e98048..466838d 100644
--- a/service/pom.xml
+++ b/service/pom.xml
@@ -3,7 +3,7 @@
4.0.0
cn.bunny
- bunny-template
+ sharding-sphere5
0.0.1-SNAPSHOT
diff --git a/service/src/main/java/cn/bunny/service/controller/IndexController.java b/service/src/main/java/cn/bunny/service/controller/IndexController.java
index d645557..0cad8ce 100644
--- a/service/src/main/java/cn/bunny/service/controller/IndexController.java
+++ b/service/src/main/java/cn/bunny/service/controller/IndexController.java
@@ -1,7 +1,13 @@
package cn.bunny.service.controller;
+import cn.bunny.service.service.LoginService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -10,9 +16,22 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/")
public class IndexController {
+ @Autowired
+ private LoginService loginService;
+
@Operation(summary = "访问首页", description = "访问首页")
@GetMapping("")
public String index() {
- return "欢迎访问 Bunny Java Template,欢迎去Gitee:https://gitee.com/BunnyBoss/java_single.git";
+ return "欢迎访问 order Java Template,欢迎去Gitee:https://gitee.com/BunnyBoss/java_single.git";
+ }
+
+ @Operation(summary = "生成验证码", description = "生成验证码")
+ @GetMapping("api/checkCode")
+ public ResponseEntity checkCode() {
+ HttpHeaders headers = new HttpHeaders();
+ headers.setContentType(MediaType.IMAGE_JPEG);
+
+ byte[] image = loginService.checkCode();
+ return new ResponseEntity<>(image, headers, HttpStatus.OK);
}
}
diff --git a/service/src/main/java/cn/bunny/service/controller/UserController.java b/service/src/main/java/cn/bunny/service/controller/UserController.java
index b4e7a60..cbd25cd 100644
--- a/service/src/main/java/cn/bunny/service/controller/UserController.java
+++ b/service/src/main/java/cn/bunny/service/controller/UserController.java
@@ -15,7 +15,7 @@ import org.springframework.web.bind.annotation.*;
* 前端控制器
*
*
- * @author Bunny
+ * @author order
* @since 2024-07-28
*/
@Tag(name = "用户表")
diff --git a/service/src/main/java/cn/bunny/service/controller/WebController.java b/service/src/main/java/cn/bunny/service/controller/WebController.java
deleted file mode 100644
index 88e4fb6..0000000
--- a/service/src/main/java/cn/bunny/service/controller/WebController.java
+++ /dev/null
@@ -1,31 +0,0 @@
-package cn.bunny.service.controller;
-
-import cn.bunny.service.service.LoginService;
-import io.swagger.v3.oas.annotations.Operation;
-import io.swagger.v3.oas.annotations.tags.Tag;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.MediaType;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
-
-@Tag(name = "web相关接口")
-@RestController
-@RequestMapping("/api")
-public class WebController {
- @Autowired
- private LoginService loginService;
-
- @Operation(summary = "生成验证码", description = "生成验证码")
- @GetMapping("checkCode")
- public ResponseEntity checkCode() {
- HttpHeaders headers = new HttpHeaders();
- headers.setContentType(MediaType.IMAGE_JPEG);
-
- byte[] image = loginService.checkCode();
- return new ResponseEntity(image, headers, HttpStatus.OK);
- }
-}
diff --git a/service/src/main/java/cn/bunny/service/mapper/UserMapper.java b/service/src/main/java/cn/bunny/service/mapper/UserMapper.java
index 2cadc19..db232b8 100644
--- a/service/src/main/java/cn/bunny/service/mapper/UserMapper.java
+++ b/service/src/main/java/cn/bunny/service/mapper/UserMapper.java
@@ -9,7 +9,7 @@ import org.apache.ibatis.annotations.Mapper;
* Mapper 接口
*
*
- * @author Bunny
+ * @author order
* @since 2024-07-28
*/
@Mapper
diff --git a/service/src/main/java/cn/bunny/service/service/UserService.java b/service/src/main/java/cn/bunny/service/service/UserService.java
index 0d361f6..432aeb5 100644
--- a/service/src/main/java/cn/bunny/service/service/UserService.java
+++ b/service/src/main/java/cn/bunny/service/service/UserService.java
@@ -10,7 +10,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
* 服务类
*
*
- * @author Bunny
+ * @author order
* @since 2024-07-28
*/
public interface UserService extends IService {
diff --git a/service/src/main/java/cn/bunny/service/service/impl/UserServiceImpl.java b/service/src/main/java/cn/bunny/service/service/impl/UserServiceImpl.java
index 3e4fc70..16b3f9b 100644
--- a/service/src/main/java/cn/bunny/service/service/impl/UserServiceImpl.java
+++ b/service/src/main/java/cn/bunny/service/service/impl/UserServiceImpl.java
@@ -13,7 +13,7 @@ import org.springframework.stereotype.Service;
* 服务实现类
*
*
- * @author Bunny
+ * @author order
* @since 2024-07-28
*/
@Service
diff --git a/service/src/main/resources/application.yml b/service/src/main/resources/application.yml
index e78efd5..2ac61eb 100644
--- a/service/src/main/resources/application.yml
+++ b/service/src/main/resources/application.yml
@@ -9,7 +9,8 @@ spring:
name: bunny-service
datasource:
driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
- url: jdbc:shardingsphere:classpath:sharding.yaml
+ # url: jdbc:shardingsphere:classpath:sharding/sharding-default.yaml
+ url: jdbc:shardingsphere:classpath:sharding/sharding-sharding.yaml
jackson:
date-format: yyyy-MM-dd HH:mm:ss
diff --git a/service/src/main/resources/sharding.yaml b/service/src/main/resources/sharding.yaml
deleted file mode 100644
index 7856e75..0000000
--- a/service/src/main/resources/sharding.yaml
+++ /dev/null
@@ -1,54 +0,0 @@
-mode:
- type: Standalone
- repository:
- type: JDBC
-dataSources:
- master:
- dataSourceClassName: com.zaxxer.hikari.HikariDataSource
- driverClassName: com.mysql.cj.jdbc.Driver
- jdbcUrl: jdbc:mysql://192.168.3.21:3306/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
- username: root
- password: "02120212"
- slave1:
- dataSourceClassName: com.zaxxer.hikari.HikariDataSource
- driverClassName: com.mysql.cj.jdbc.Driver
- jdbcUrl: jdbc:mysql://192.168.3.21:3307/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
- username: root
- password: "02120212"
- slave2:
- dataSourceClassName: com.zaxxer.hikari.HikariDataSource
- driverClassName: com.mysql.cj.jdbc.Driver
- jdbcUrl: jdbc:mysql://192.168.3.21:3308/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
- username: root
- password: "02120212"
-rules:
-- !SINGLE
- tables:
- - "*.*"
- defaultDataSource: master
-- !READWRITE_SPLITTING
- dataSources:
- readwrite_ds:
- writeDataSourceName: master
- readDataSourceNames:
- - slave1
- - slave2
- transactionalReadQueryStrategy: PRIMARY
- loadBalancerName: random
- loadBalancers:
- random:
- type: RANDOM
-props:
- sql-show: true
-
-sqlParser:
- sqlStatementCache:
- initialCapacity: 2000
- maximumSize: 65535
- parseTreeCache:
- initialCapacity: 128
- maximumSize: 1024
-
-sqlTranslator:
- type: Native
- useOriginalSQLWhenTranslatingFailed: true
diff --git a/service/src/main/resources/sharding/sharding-default.yaml b/service/src/main/resources/sharding/sharding-default.yaml
new file mode 100644
index 0000000..5248241
--- /dev/null
+++ b/service/src/main/resources/sharding/sharding-default.yaml
@@ -0,0 +1,130 @@
+mode:
+ type: Standalone
+ repository:
+ type: JDBC
+dataSources:
+ master:
+ dataSourceClassName: com.zaxxer.hikari.HikariDataSource
+ driverClassName: com.mysql.cj.jdbc.Driver
+ jdbcUrl: jdbc:mysql://192.168.3.21:3306/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
+ username: root
+ password: "02120212"
+ slave1:
+ dataSourceClassName: com.zaxxer.hikari.HikariDataSource
+ driverClassName: com.mysql.cj.jdbc.Driver
+ jdbcUrl: jdbc:mysql://192.168.3.21:3307/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
+ username: root
+ password: "02120212"
+ slave2:
+ dataSourceClassName: com.zaxxer.hikari.HikariDataSource
+ driverClassName: com.mysql.cj.jdbc.Driver
+ jdbcUrl: jdbc:mysql://192.168.3.21:3308/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
+ username: root
+ password: "02120212"
+rules:
+ - !SINGLE
+ # 匹配所有表
+ tables:
+ - "*.*"
+ # 默认数据库
+ defaultDataSource: master
+
+ # 数据分片配置
+ - !SHARDING
+ tables:
+ t_order:
+ actualDataNodes: ds_${0..1}.t_order_${0..1}
+ tableStrategy:
+ standard:
+ shardingColumn: order_id
+ shardingAlgorithmName: t_order_inline
+ keyGenerateStrategy:
+ column: order_id
+ keyGeneratorName: snowflake
+ auditStrategy:
+ auditorNames:
+ - sharding_key_required_auditor
+ allowHintDisable: true
+ t_order_item:
+ actualDataNodes: ds_${0..1}.t_order_item_${0..1}
+ tableStrategy:
+ standard:
+ shardingColumn: order_id
+ shardingAlgorithmName: t_order_item_inline
+ keyGenerateStrategy:
+ column: order_item_id
+ keyGeneratorName: snowflake
+ t_account:
+ actualDataNodes: ds_${0..1}.t_account_${0..1}
+ tableStrategy:
+ standard:
+ shardingAlgorithmName: t_account_inline
+ keyGenerateStrategy:
+ column: account_id
+ keyGeneratorName: snowflake
+ defaultShardingColumn: account_id
+ bindingTables:
+ - t_order,t_order_item
+ defaultDatabaseStrategy:
+ standard:
+ shardingColumn: user_id
+ shardingAlgorithmName: database_inline
+ defaultTableStrategy:
+ none:
+ shardingAlgorithms:
+ database_inline:
+ type: INLINE
+ props:
+ algorithm-expression: ds_${user_id % 2}
+ t_order_inline:
+ type: INLINE
+ props:
+ algorithm-expression: t_order_${order_id % 2}
+ t_order_item_inline:
+ type: INLINE
+ props:
+ algorithm-expression: t_order_item_${order_id % 2}
+ t_account_inline:
+ type: INLINE
+ props:
+ algorithm-expression: t_account_${account_id % 2}
+ keyGenerators:
+ snowflake:
+ type: SNOWFLAKE
+ auditors:
+ sharding_key_required_auditor:
+ type: DML_SHARDING_CONDITIONS
+
+ # 广播表
+ - !BROADCAST
+ tables:
+ - t_address
+
+ # 读写分离配置
+ - !READWRITE_SPLITTING
+ dataSources:
+ readwrite_ds:
+ writeDataSourceName: master
+ readDataSourceNames:
+ - slave1
+ - slave2
+ transactionalReadQueryStrategy: PRIMARY
+ # 负载均衡名称,对应下面loadBalancers中名称
+ loadBalancerName: ds_wight
+ loadBalancers:
+ # 随机负载均衡算法
+ ds_random:
+ type: RANDOM
+ # 轮询负载均衡算法
+ ds_round:
+ type: ROUND_ROBIN
+ # 权重负载均衡算法
+ ds_wight:
+ type: WEIGHT
+ # 配置数据库权重
+ props:
+ slave1: 1
+ slave2: 2
+# 打印SQL
+props:
+ sql-show: true
diff --git a/service/src/main/resources/sharding/sharding-sharding.yaml b/service/src/main/resources/sharding/sharding-sharding.yaml
new file mode 100644
index 0000000..92b95cb
--- /dev/null
+++ b/service/src/main/resources/sharding/sharding-sharding.yaml
@@ -0,0 +1,36 @@
+mode:
+ type: Standalone
+ repository:
+ type: JDBC
+dataSources:
+ server_user:
+ dataSourceClassName: com.zaxxer.hikari.HikariDataSource
+ driverClassName: com.mysql.cj.jdbc.Driver
+ jdbcUrl: jdbc:mysql://192.168.3.21:3304/db_user?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
+ username: root
+ password: "02120212"
+ server_order:
+ dataSourceClassName: com.zaxxer.hikari.HikariDataSource
+ driverClassName: com.mysql.cj.jdbc.Driver
+ jdbcUrl: jdbc:mysql://192.168.3.21:3305/db_order?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
+ username: root
+ password: "02120212"
+rules:
+ # - !SINGLE
+ # # 匹配所有表
+ # tables:
+ # - "*.*"
+ # # 默认数据库
+ # defaultDataSource: server_user
+
+ # 数据分片配置
+ - !SHARDING
+ tables:
+ server_user:
+ actualDataNodes: server_user.t_user
+ server_order:
+ actualDataNodes: server_order.t_order
+
+# 打印SQL
+props:
+ sql-show: true
diff --git a/service/src/test/java/cn/bunny/service/service/impl/UserServiceImplTest.java b/service/src/test/java/cn/bunny/service/service/impl/UserServiceImplTest.java
index 4826702..32f7795 100644
--- a/service/src/test/java/cn/bunny/service/service/impl/UserServiceImplTest.java
+++ b/service/src/test/java/cn/bunny/service/service/impl/UserServiceImplTest.java
@@ -26,14 +26,29 @@ class UserServiceImplTest {
@Test
@Transactional
void addTransactional() {
+ // 插入时会回滚,在测试状态下加上事务都会回滚
User user = new User();
user.setUname("哈哈哈");
userMapper.insert(user);
}
+ // 查询测试
@Test
void selectUseList() {
List users = userMapper.selectList(null);
users.forEach(System.out::println);
}
+
+ // 负载均衡测试
+ @Test
+ void selectUserAllLoadBalancers() {
+ List userList1 = userMapper.selectList(null);
+ System.out.println(userList1);
+ List userList2 = userMapper.selectList(null);
+ System.out.println(userList2);
+ List userList3 = userMapper.selectList(null);
+ System.out.println(userList3);
+ List userList4 = userMapper.selectList(null);
+ System.out.println(userList3);
+ }
}
\ No newline at end of file