From 254674bb4060d4169e0a80ffa2b3519f6439df87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johnny=20Miller=20=28=E9=94=BA=E4=BF=8A=29?= Date: Mon, 19 Jul 2021 09:46:30 +0800 Subject: [PATCH] perf($MyBatisPlus): data source loading improvements --- api-gateway/pom.xml | 2 +- auth-center/auto-run-windows.cmd | 2 +- auth-center/pom.xml | 2 +- .../user/service/impl/UserServiceImpl.java | 23 ++--- .../application-development-docker.yml | 4 +- .../application-development-local.yml | 9 +- .../main/resources/application-production.yml | 4 +- .../src/main/resources/application-stage.yml | 4 +- .../src/main/resources/application-test.yml | 4 +- .../src/main/resources/application.yml | 29 ++++--- common/pom.xml | 2 +- maf-mis/pom.xml | 2 +- .../application-development-docker.yml | 4 +- .../application-development-local.yml | 4 +- .../main/resources/application-production.yml | 4 +- .../src/main/resources/application-stage.yml | 4 +- .../src/main/resources/application-test.yml | 4 +- maf-mis/src/main/resources/application.yml | 29 ++++--- oss-center/pom.xml | 2 +- .../application-development-docker.yml | 4 +- .../application-development-local.yml | 4 +- .../main/resources/application-production.yml | 4 +- .../src/main/resources/application-stage.yml | 4 +- .../src/main/resources/application-test.yml | 4 +- oss-center/src/main/resources/application.yml | 29 ++++--- pom.xml | 2 +- reactive-spring-cloud-starter/pom.xml | 2 +- spring-boot-admin/pom.xml | 2 +- spring-cloud-starter/pom.xml | 2 +- .../MafAutoConfiguration.java | 7 +- .../configuration/JwtConfiguration.java | 18 ++-- .../database/DataSourceConfiguration.java | 51 ++++++++---- .../database/DataSourceContextHolder.java | 58 ------------- .../database/DataSourceEnum.java | 4 +- .../DynamicDataSourceInterceptor.java | 83 ------------------- .../database/MyBatisPlusConfiguration.java | 7 +- ...riteIsolationDynamicRoutingDataSource.java | 15 ---- universal-ui/pom.xml | 2 +- 38 files changed, 151 insertions(+), 289 deletions(-) delete mode 100644 spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/DataSourceContextHolder.java delete mode 100644 spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/DynamicDataSourceInterceptor.java delete mode 100644 spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/ReadWriteIsolationDynamicRoutingDataSource.java diff --git a/api-gateway/pom.xml b/api-gateway/pom.xml index ebba2a63..ec0a5992 100644 --- a/api-gateway/pom.xml +++ b/api-gateway/pom.xml @@ -10,7 +10,7 @@ com.jmsoftware.maf muscle-and-fitness-server - 0.0.2 + 0.0.3-SNAPSHOT diff --git a/auth-center/auto-run-windows.cmd b/auth-center/auto-run-windows.cmd index 2b4a7fc1..965c8ee2 100644 --- a/auth-center/auto-run-windows.cmd +++ b/auth-center/auto-run-windows.cmd @@ -19,7 +19,7 @@ SET skipMavenBuild=true SET minimalJavaMajorVersion=11 SET javaExe=C:\Users\Johnny\.sdkman\candidates\java\11.0.10.hs-adpt\bin\java.exe SET mavenActiveProfile="development-local" -SET javaParameter=-Xms256m -Xmx256m -Dfile.encoding=UTF-8 -Dspring.cloud.consul.host=localhost +SET javaParameter=-Xms256m -Xmx256m -Dfile.encoding=UTF-8 -Dspring.cloud.consul.host=localhost -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:2345 GOTO:MAIN diff --git a/auth-center/pom.xml b/auth-center/pom.xml index a859a01c..20fe4100 100644 --- a/auth-center/pom.xml +++ b/auth-center/pom.xml @@ -10,7 +10,7 @@ com.jmsoftware.maf muscle-and-fitness-server - 0.0.2 + 0.0.3-SNAPSHOT diff --git a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/service/impl/UserServiceImpl.java b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/service/impl/UserServiceImpl.java index 93de41bd..5b0f96f8 100644 --- a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/service/impl/UserServiceImpl.java +++ b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/service/impl/UserServiceImpl.java @@ -66,11 +66,11 @@ public class UserServiceImpl extends ServiceImpl implements Us @Override public GetUserByLoginTokenResponse getUserByLoginToken(@NotBlank String loginToken) { - val key = String.format(mafProjectProperty.getProjectParentArtifactId() - + UserRedisKey.GET_USER_BY_LOGIN_TOKEN.getKeyInfixFormat(), loginToken); - val hasKey = redisTemplate.hasKey(key); + val key = String.format(String.format("%s%s", this.mafProjectProperty.getProjectParentArtifactId(), + UserRedisKey.GET_USER_BY_LOGIN_TOKEN.getKeyInfixFormat()), loginToken); + val hasKey = this.redisTemplate.hasKey(key); if (BooleanUtil.isTrue(hasKey)) { - return JSONUtil.toBean(redisTemplate.opsForValue().get(key), GetUserByLoginTokenResponse.class); + return JSONUtil.toBean(this.redisTemplate.opsForValue().get(key), GetUserByLoginTokenResponse.class); } val wrapper = Wrappers.lambdaQuery(User.class); wrapper.and(queryWrapper -> queryWrapper.eq(User::getUsername, loginToken) @@ -84,7 +84,8 @@ public GetUserByLoginTokenResponse getUserByLoginToken(@NotBlank String loginTok } val response = new GetUserByLoginTokenResponse(); BeanUtil.copyProperties(userPersistence, response); - redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(response), RandomUtil.randomLong(1, 7), TimeUnit.DAYS); + this.redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(response), RandomUtil.randomLong(1, 7), + TimeUnit.DAYS); return response; } @@ -94,11 +95,11 @@ public SignupResponse saveUserForSignup(@Valid SignupPayload payload) { val user = new User(); user.setUsername(payload.getUsername()); user.setEmail(payload.getEmail()); - user.setPassword(bCryptPasswordEncoder.encode(payload.getPassword())); + user.setPassword(this.bCryptPasswordEncoder.encode(payload.getPassword())); user.setStatus(UserStatus.ENABLED.getValue()); this.save(user); log.warn("Saved user for signup, going to assign guest role to user. {}", user); - userRoleService.assignRoleByRoleName(user, mafConfiguration.getGuestUserRole()); + this.userRoleService.assignRoleByRoleName(user, this.mafConfiguration.getGuestUserRole()); val response = new SignupResponse(); response.setUserId(user.getId()); return response; @@ -111,20 +112,20 @@ public LoginResponse login(@Valid LoginPayload payload) throws SecurityException throw new SecurityException(HttpStatus.UNAUTHORIZED); } log.info("User login: {}", user); - val matched = bCryptPasswordEncoder.matches(payload.getPassword(), user.getPassword()); + val matched = this.bCryptPasswordEncoder.matches(payload.getPassword(), user.getPassword()); if (!matched) { throw new SecurityException(HttpStatus.UNAUTHORIZED); } - val jwt = jwtService.createJwt(payload.getRememberMe(), user.getId(), user.getUsername(), null, null); + val jwt = this.jwtService.createJwt(payload.getRememberMe(), user.getId(), user.getUsername(), null, null); val response = new LoginResponse(); - response.setGreeting(messageSource.getMessage(("greeting"), null, LocaleContextHolder.getLocale())); + response.setGreeting(this.messageSource.getMessage(("greeting"), null, LocaleContextHolder.getLocale())); response.setJwt(jwt); return response; } @Override public boolean logout(HttpServletRequest request) throws SecurityException { - jwtService.invalidateJwt(request); + this.jwtService.invalidateJwt(request); return true; } diff --git a/auth-center/src/main/resources/application-development-docker.yml b/auth-center/src/main/resources/application-development-docker.yml index 15797f14..ba09cc4d 100644 --- a/auth-center/src/main/resources/application-development-docker.yml +++ b/auth-center/src/main/resources/application-development-docker.yml @@ -6,12 +6,12 @@ spring: datasource: dynamic: datasource: - master: + master_1: url: jdbc:mysql://maf-mysql-server-master:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_rw password: maf@mysql driver-class-name: com.mysql.cj.jdbc.Driver - slave1: + slave_1: url: jdbc:mysql://maf-mysql-server-slave:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_r password: maf@mysql diff --git a/auth-center/src/main/resources/application-development-local.yml b/auth-center/src/main/resources/application-development-local.yml index 6ad4c92e..b8cd35cb 100644 --- a/auth-center/src/main/resources/application-development-local.yml +++ b/auth-center/src/main/resources/application-development-local.yml @@ -6,12 +6,12 @@ spring: datasource: dynamic: datasource: - master: + master_1: url: jdbc:mysql://localhost:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_rw password: maf@mysql driver-class-name: com.mysql.cj.jdbc.Driver - slave1: + slave_1: url: jdbc:mysql://localhost:3307/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_r password: maf@mysql @@ -31,12 +31,11 @@ spring: username: maf_rabbitmq_su password: maf@rabbitmq +# Configure logging level for the environment logging: - # Configure logging level for SFTP/JSCH level: - com.jcraft.jsch: INFO + com.baomidou: INFO maf: configuration: - web-request-log-enabled: true swagger-enabled: true diff --git a/auth-center/src/main/resources/application-production.yml b/auth-center/src/main/resources/application-production.yml index db47a645..8b86fb39 100644 --- a/auth-center/src/main/resources/application-production.yml +++ b/auth-center/src/main/resources/application-production.yml @@ -6,12 +6,12 @@ spring: datasource: dynamic: datasource: - master: + master_1: url: jdbc:mysql://maf-mysql-server-master:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_rw password: maf@mysql driver-class-name: com.mysql.cj.jdbc.Driver - slave1: + slave_1: url: jdbc:mysql://maf-mysql-server-slave:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_r password: maf@mysql diff --git a/auth-center/src/main/resources/application-stage.yml b/auth-center/src/main/resources/application-stage.yml index db47a645..8b86fb39 100644 --- a/auth-center/src/main/resources/application-stage.yml +++ b/auth-center/src/main/resources/application-stage.yml @@ -6,12 +6,12 @@ spring: datasource: dynamic: datasource: - master: + master_1: url: jdbc:mysql://maf-mysql-server-master:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_rw password: maf@mysql driver-class-name: com.mysql.cj.jdbc.Driver - slave1: + slave_1: url: jdbc:mysql://maf-mysql-server-slave:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_r password: maf@mysql diff --git a/auth-center/src/main/resources/application-test.yml b/auth-center/src/main/resources/application-test.yml index 9c992a1b..674636f5 100644 --- a/auth-center/src/main/resources/application-test.yml +++ b/auth-center/src/main/resources/application-test.yml @@ -6,12 +6,12 @@ spring: datasource: dynamic: datasource: - master: + master_1: url: jdbc:mysql://maf-mysql-server-master:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_rw password: maf@mysql driver-class-name: com.mysql.cj.jdbc.Driver - slave1: + slave_1: url: jdbc:mysql://maf-mysql-server-slave:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_r password: maf@mysql diff --git a/auth-center/src/main/resources/application.yml b/auth-center/src/main/resources/application.yml index 42050999..1435323f 100644 --- a/auth-center/src/main/resources/application.yml +++ b/auth-center/src/main/resources/application.yml @@ -41,21 +41,24 @@ spring: health-check-critical-timeout: 15s datasource: type: com.alibaba.druid.pool.DruidDataSource + dynamic: + primary: master_1 + druid: + initial-size: 5 + max-active: 20 + min-idle: 2 + max-wait: 60000 + min-evictable-idle-time-millis: 600000 + max-evictable-idle-time-millis: 900000 + validation-query: SELECT 1 + test-while-idle: true + test-on-borrow: false + test-on-return: false + pool-prepared-statements: true + max-pool-prepared-statement-per-connection-size: 20 + keep-alive: true druid: driver-class-name: com.mysql.cj.jdbc.Driver - initial-size: 5 - minIdle: 10 - max-active: 20 - max-wait: 60000 - min-evictable-idle-time-millis: 600000 - max-evictable-idle-time-millis: 900000 - validation-query: SELECT 1 - test-while-idle: true - test-on-borrow: false - test-on-return: false - pool-prepared-statements: true - max-open-prepared-statements: 20 - keep-alive: true filters: stat,wall,log4j2 filter: stat: diff --git a/common/pom.xml b/common/pom.xml index 915a40b9..82a1442f 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -10,7 +10,7 @@ com.jmsoftware.maf muscle-and-fitness-server - 0.0.2 + 0.0.3-SNAPSHOT diff --git a/maf-mis/pom.xml b/maf-mis/pom.xml index 574d373f..d6b64e19 100644 --- a/maf-mis/pom.xml +++ b/maf-mis/pom.xml @@ -10,7 +10,7 @@ com.jmsoftware.maf muscle-and-fitness-server - 0.0.2 + 0.0.3-SNAPSHOT diff --git a/maf-mis/src/main/resources/application-development-docker.yml b/maf-mis/src/main/resources/application-development-docker.yml index 15797f14..ba09cc4d 100644 --- a/maf-mis/src/main/resources/application-development-docker.yml +++ b/maf-mis/src/main/resources/application-development-docker.yml @@ -6,12 +6,12 @@ spring: datasource: dynamic: datasource: - master: + master_1: url: jdbc:mysql://maf-mysql-server-master:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_rw password: maf@mysql driver-class-name: com.mysql.cj.jdbc.Driver - slave1: + slave_1: url: jdbc:mysql://maf-mysql-server-slave:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_r password: maf@mysql diff --git a/maf-mis/src/main/resources/application-development-local.yml b/maf-mis/src/main/resources/application-development-local.yml index 19f727d3..e6846a13 100644 --- a/maf-mis/src/main/resources/application-development-local.yml +++ b/maf-mis/src/main/resources/application-development-local.yml @@ -6,12 +6,12 @@ spring: datasource: dynamic: datasource: - master: + master_1: url: jdbc:mysql://localhost:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_rw password: maf@mysql driver-class-name: com.mysql.cj.jdbc.Driver - slave1: + slave_1: url: jdbc:mysql://localhost:3307/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_r password: maf@mysql diff --git a/maf-mis/src/main/resources/application-production.yml b/maf-mis/src/main/resources/application-production.yml index db47a645..8b86fb39 100644 --- a/maf-mis/src/main/resources/application-production.yml +++ b/maf-mis/src/main/resources/application-production.yml @@ -6,12 +6,12 @@ spring: datasource: dynamic: datasource: - master: + master_1: url: jdbc:mysql://maf-mysql-server-master:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_rw password: maf@mysql driver-class-name: com.mysql.cj.jdbc.Driver - slave1: + slave_1: url: jdbc:mysql://maf-mysql-server-slave:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_r password: maf@mysql diff --git a/maf-mis/src/main/resources/application-stage.yml b/maf-mis/src/main/resources/application-stage.yml index db47a645..8b86fb39 100644 --- a/maf-mis/src/main/resources/application-stage.yml +++ b/maf-mis/src/main/resources/application-stage.yml @@ -6,12 +6,12 @@ spring: datasource: dynamic: datasource: - master: + master_1: url: jdbc:mysql://maf-mysql-server-master:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_rw password: maf@mysql driver-class-name: com.mysql.cj.jdbc.Driver - slave1: + slave_1: url: jdbc:mysql://maf-mysql-server-slave:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_r password: maf@mysql diff --git a/maf-mis/src/main/resources/application-test.yml b/maf-mis/src/main/resources/application-test.yml index 9c992a1b..674636f5 100644 --- a/maf-mis/src/main/resources/application-test.yml +++ b/maf-mis/src/main/resources/application-test.yml @@ -6,12 +6,12 @@ spring: datasource: dynamic: datasource: - master: + master_1: url: jdbc:mysql://maf-mysql-server-master:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_rw password: maf@mysql driver-class-name: com.mysql.cj.jdbc.Driver - slave1: + slave_1: url: jdbc:mysql://maf-mysql-server-slave:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_r password: maf@mysql diff --git a/maf-mis/src/main/resources/application.yml b/maf-mis/src/main/resources/application.yml index afeb5833..28e2e4bd 100644 --- a/maf-mis/src/main/resources/application.yml +++ b/maf-mis/src/main/resources/application.yml @@ -43,21 +43,24 @@ spring: health-check-critical-timeout: 15s datasource: type: com.alibaba.druid.pool.DruidDataSource + dynamic: + primary: master_1 + druid: + initial-size: 5 + max-active: 20 + min-idle: 2 + max-wait: 60000 + min-evictable-idle-time-millis: 600000 + max-evictable-idle-time-millis: 900000 + validation-query: SELECT 1 + test-while-idle: true + test-on-borrow: false + test-on-return: false + pool-prepared-statements: true + max-pool-prepared-statement-per-connection-size: 20 + keep-alive: true druid: driver-class-name: com.mysql.cj.jdbc.Driver - initial-size: 5 - minIdle: 10 - max-active: 20 - max-wait: 60000 - min-evictable-idle-time-millis: 600000 - max-evictable-idle-time-millis: 900000 - validation-query: SELECT 1 - test-while-idle: true - test-on-borrow: false - test-on-return: false - pool-prepared-statements: true - max-open-prepared-statements: 20 - keep-alive: true filters: stat,wall,log4j2 filter: stat: diff --git a/oss-center/pom.xml b/oss-center/pom.xml index 180b9633..ceb43177 100644 --- a/oss-center/pom.xml +++ b/oss-center/pom.xml @@ -10,7 +10,7 @@ com.jmsoftware.maf muscle-and-fitness-server - 0.0.2 + 0.0.3-SNAPSHOT diff --git a/oss-center/src/main/resources/application-development-docker.yml b/oss-center/src/main/resources/application-development-docker.yml index 5b321c55..0658c666 100644 --- a/oss-center/src/main/resources/application-development-docker.yml +++ b/oss-center/src/main/resources/application-development-docker.yml @@ -6,12 +6,12 @@ spring: datasource: dynamic: datasource: - master: + master_1: url: jdbc:mysql://maf-mysql-server-master:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_rw password: maf@mysql driver-class-name: com.mysql.cj.jdbc.Driver - slave1: + slave_1: url: jdbc:mysql://maf-mysql-server-slave:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_r password: maf@mysql diff --git a/oss-center/src/main/resources/application-development-local.yml b/oss-center/src/main/resources/application-development-local.yml index 15551814..885c359c 100644 --- a/oss-center/src/main/resources/application-development-local.yml +++ b/oss-center/src/main/resources/application-development-local.yml @@ -6,12 +6,12 @@ spring: datasource: dynamic: datasource: - master: + master_1: url: jdbc:mysql://localhost:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_rw password: maf@mysql driver-class-name: com.mysql.cj.jdbc.Driver - slave1: + slave_1: url: jdbc:mysql://localhost:3307/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_r password: maf@mysql diff --git a/oss-center/src/main/resources/application-production.yml b/oss-center/src/main/resources/application-production.yml index 8698e8e5..4dbf071e 100644 --- a/oss-center/src/main/resources/application-production.yml +++ b/oss-center/src/main/resources/application-production.yml @@ -6,12 +6,12 @@ spring: datasource: dynamic: datasource: - master: + master_1: url: jdbc:mysql://maf-mysql-server-master:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_rw password: maf@mysql driver-class-name: com.mysql.cj.jdbc.Driver - slave1: + slave_1: url: jdbc:mysql://maf-mysql-server-slave:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_r password: maf@mysql diff --git a/oss-center/src/main/resources/application-stage.yml b/oss-center/src/main/resources/application-stage.yml index 8698e8e5..4dbf071e 100644 --- a/oss-center/src/main/resources/application-stage.yml +++ b/oss-center/src/main/resources/application-stage.yml @@ -6,12 +6,12 @@ spring: datasource: dynamic: datasource: - master: + master_1: url: jdbc:mysql://maf-mysql-server-master:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_rw password: maf@mysql driver-class-name: com.mysql.cj.jdbc.Driver - slave1: + slave_1: url: jdbc:mysql://maf-mysql-server-slave:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_r password: maf@mysql diff --git a/oss-center/src/main/resources/application-test.yml b/oss-center/src/main/resources/application-test.yml index c25e4c0b..c7cbca72 100644 --- a/oss-center/src/main/resources/application-test.yml +++ b/oss-center/src/main/resources/application-test.yml @@ -6,12 +6,12 @@ spring: datasource: dynamic: datasource: - master: + master_1: url: jdbc:mysql://maf-mysql-server-master:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_rw password: maf@mysql driver-class-name: com.mysql.cj.jdbc.Driver - slave1: + slave_1: url: jdbc:mysql://maf-mysql-server-slave:3306/muscle_and_fitness?useSSL=true&useUnicode=true username: maf_mysql_r password: maf@mysql diff --git a/oss-center/src/main/resources/application.yml b/oss-center/src/main/resources/application.yml index 8a1ed499..c40b7aa8 100644 --- a/oss-center/src/main/resources/application.yml +++ b/oss-center/src/main/resources/application.yml @@ -41,21 +41,24 @@ spring: health-check-critical-timeout: 15s datasource: type: com.alibaba.druid.pool.DruidDataSource + dynamic: + primary: master_1 + druid: + initial-size: 5 + max-active: 20 + min-idle: 2 + max-wait: 60000 + min-evictable-idle-time-millis: 600000 + max-evictable-idle-time-millis: 900000 + validation-query: SELECT 1 + test-while-idle: true + test-on-borrow: false + test-on-return: false + pool-prepared-statements: true + max-pool-prepared-statement-per-connection-size: 20 + keep-alive: true druid: driver-class-name: com.mysql.cj.jdbc.Driver - initial-size: 5 - minIdle: 10 - max-active: 20 - max-wait: 60000 - min-evictable-idle-time-millis: 600000 - max-evictable-idle-time-millis: 900000 - validation-query: SELECT 1 - test-while-idle: true - test-on-borrow: false - test-on-return: false - pool-prepared-statements: true - max-open-prepared-statements: 20 - keep-alive: true filters: stat,wall,log4j2 filter: stat: diff --git a/pom.xml b/pom.xml index 11b6f4dd..a1f22ab8 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.jmsoftware.maf muscle-and-fitness-server - 0.0.2 + 0.0.3-SNAPSHOT Muscle and Fitness Server Muscle and Fitness Server (M&F, maf), a Spring Cloud microservice based, back-end server for managing data of muscle and fitness. diff --git a/reactive-spring-cloud-starter/pom.xml b/reactive-spring-cloud-starter/pom.xml index 9f7a53d0..0a84e4c4 100644 --- a/reactive-spring-cloud-starter/pom.xml +++ b/reactive-spring-cloud-starter/pom.xml @@ -10,7 +10,7 @@ com.jmsoftware.maf muscle-and-fitness-server - 0.0.2 + 0.0.3-SNAPSHOT diff --git a/spring-boot-admin/pom.xml b/spring-boot-admin/pom.xml index 70f2f1cc..9b38743f 100644 --- a/spring-boot-admin/pom.xml +++ b/spring-boot-admin/pom.xml @@ -10,7 +10,7 @@ com.jmsoftware.maf muscle-and-fitness-server - 0.0.2 + 0.0.3-SNAPSHOT diff --git a/spring-cloud-starter/pom.xml b/spring-cloud-starter/pom.xml index 0cc516aa..56530137 100644 --- a/spring-cloud-starter/pom.xml +++ b/spring-cloud-starter/pom.xml @@ -10,7 +10,7 @@ com.jmsoftware.maf muscle-and-fitness-server - 0.0.2 + 0.0.3-SNAPSHOT diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/MafAutoConfiguration.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/MafAutoConfiguration.java index 75860611..bde6479f 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/MafAutoConfiguration.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/MafAutoConfiguration.java @@ -52,6 +52,7 @@ @EnableConfigurationProperties({ MafConfiguration.class, MafProjectProperty.class, + JwtConfiguration.class, ExcelImportConfiguration.class }) @Import({ @@ -156,10 +157,4 @@ public CommonController commonController(CommonService commonService) { log.warn("Initial bean: '{}'", CommonController.class.getSimpleName()); return new CommonController(commonService); } - - @Bean - public JwtConfiguration jwtConfiguration(MafProjectProperty mafProjectProperty) { - log.warn("Initial bean: '{}'", JwtConfiguration.class.getSimpleName()); - return new JwtConfiguration(mafProjectProperty); - } } diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/configuration/JwtConfiguration.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/configuration/JwtConfiguration.java index f595a15b..ea2708e1 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/configuration/JwtConfiguration.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/configuration/JwtConfiguration.java @@ -32,16 +32,6 @@ public class JwtConfiguration { */ @Setter(AccessLevel.NONE) private String jwtRedisKeyPrefix; - - public JwtConfiguration(MafProjectProperty mafProjectProperty) { - this.signingKey = String.format("%s@%s", mafProjectProperty.getProjectParentArtifactId(), - mafProjectProperty.getVersion()); - log.info("Initiated JWT signing key: {}. The specified key byte array is {} bits", this.signingKey, - this.signingKey.getBytes(StandardCharsets.UTF_8).length * 8); - jwtRedisKeyPrefix = String.format("%s:jwt:", mafProjectProperty.getProjectParentArtifactId()); - log.warn("Initiated 'jwtRedisKeyPrefix': {}", jwtRedisKeyPrefix); - } - /** * JWT signing key. Pattern: [project-parent-artifact-id]@[version] */ @@ -57,4 +47,12 @@ public JwtConfiguration(MafProjectProperty mafProjectProperty) { */ @NotNull private Long ttlForRememberMe = 7 * 86400000L; + public JwtConfiguration(MafProjectProperty mafProjectProperty) { + this.signingKey = String.format("%s:%s@%s", mafProjectProperty.getProjectParentArtifactId(), + mafProjectProperty.getProjectArtifactId(), mafProjectProperty.getVersion()); + log.info("Initiated JWT signing key: {}. The specified key byte array is {} bits", this.signingKey, + this.signingKey.getBytes(StandardCharsets.UTF_8).length * 8); + this.jwtRedisKeyPrefix = String.format("%s:jwt:", mafProjectProperty.getProjectParentArtifactId()); + log.warn("Initiated 'jwtRedisKeyPrefix': {}", this.jwtRedisKeyPrefix); + } } diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/DataSourceConfiguration.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/DataSourceConfiguration.java index 48099cdd..39cb1642 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/DataSourceConfiguration.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/DataSourceConfiguration.java @@ -1,20 +1,27 @@ package com.jmsoftware.maf.springcloudstarter.database; +import cn.hutool.db.ds.DataSourceWrapper; +import com.alibaba.druid.util.JdbcUtils; import com.baomidou.dynamic.datasource.DynamicRoutingDataSource; +import com.baomidou.dynamic.datasource.plugin.MasterSlaveAutoRoutingPlugin; +import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceAutoConfiguration; +import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties; import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import lombok.val; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.quartz.QuartzDataSource; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Primary; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import javax.sql.DataSource; -import java.util.HashMap; /** * Description: DataSourceConfiguration, change description here. @@ -23,34 +30,42 @@ **/ @Slf4j @ConditionalOnClass({MybatisPlusAutoConfiguration.class}) -@AutoConfigureBefore({MybatisPlusAutoConfiguration.class}) +@EnableConfigurationProperties(DynamicDataSourceProperties.class) +@AutoConfigureBefore({DynamicDataSourceAutoConfiguration.class}) public class DataSourceConfiguration { + @Bean @QuartzDataSource - @Bean("quartzDataSource") @ConditionalOnProperty(prefix = "spring.quartz", name = "job-store-type", havingValue = "jdbc") public DataSource quartzDataSource(DynamicRoutingDataSource dynamicRoutingDataSource) { - log.warn("Initial bean: quartzDataSource"); + log.info("Getting quartzDataSource from 'DynamicRoutingDataSource'"); return dynamicRoutingDataSource.getDataSource(DataSourceEnum.QUARTZ.getDataSourceName()); } + /** + * Primary data source. Had to configure DynamicRoutingDataSource as primary. Otherwise + * MasterSlaveAutoRoutingPlugin will not be able to be injected datasource correctly. + * + * @param dynamicRoutingDataSource the dynamic routing data source + * @return the data source + * @see MasterSlaveAutoRoutingPlugin + */ @Bean @Primary - public ReadWriteIsolationDynamicRoutingDataSource dynamicDataSource(DynamicRoutingDataSource dynamicRoutingDataSource) { - val targetDataSources = new HashMap<>(4); - targetDataSources.put(DataSourceEnum.MASTER, - dynamicRoutingDataSource.getDataSource(DataSourceEnum.MASTER.getDataSourceName())); - targetDataSources.put(DataSourceEnum.SLAVE1, - dynamicRoutingDataSource.getDataSource(DataSourceEnum.SLAVE1.getDataSourceName())); - val dynamicDataSource = new ReadWriteIsolationDynamicRoutingDataSource(); - dynamicDataSource.setDefaultTargetDataSource(dynamicRoutingDataSource.getDataSource("master")); - dynamicDataSource.setTargetDataSources(targetDataSources); - log.warn("Set 'masterDataSource' and 'slave1DataSource' as {}", - ReadWriteIsolationDynamicRoutingDataSource.class.getSimpleName()); - return dynamicDataSource; + @SneakyThrows + public DataSource primaryDataSource(DynamicRoutingDataSource dynamicRoutingDataSource, + DynamicDataSourceProperties dynamicDataSourceProperties) { + val jdbcUrl = dynamicDataSourceProperties + .getDatasource() + .get(dynamicDataSourceProperties.getPrimary()) + .getUrl(); + val driverClassName = JdbcUtils.getDriverClassName(jdbcUrl); + log.info("Wrapping 'DynamicRoutingDataSource' as 'primaryDataSource', jdbcUrl: {}, driverClassName: {}", + jdbcUrl, driverClassName); + return DataSourceWrapper.wrap(dynamicRoutingDataSource, driverClassName); } @Bean - public PlatformTransactionManager platformTransactionManager(ReadWriteIsolationDynamicRoutingDataSource readWriteIsolationDynamicRoutingDataSource) { - return new DataSourceTransactionManager(readWriteIsolationDynamicRoutingDataSource); + public PlatformTransactionManager platformTransactionManager(@Qualifier("primaryDataSource") DataSource primaryDataSource) { + return new DataSourceTransactionManager(primaryDataSource); } } diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/DataSourceContextHolder.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/DataSourceContextHolder.java deleted file mode 100644 index cd3f1955..00000000 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/DataSourceContextHolder.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.jmsoftware.maf.springcloudstarter.database; - -import lombok.extern.slf4j.Slf4j; -import lombok.val; - -import java.util.Optional; -import java.util.concurrent.atomic.AtomicInteger; - -/** - * Description: DatabaseContextHolder, thread-safe - * - * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 6/27/2021 12:07 AM - **/ -@Slf4j -public final class DataSourceContextHolder { - protected static final ThreadLocal CONTEXT_HOLDER = - ThreadLocal.withInitial(() -> DataSourceEnum.MASTER); - private static final AtomicInteger COUNTER = new AtomicInteger(-1); - private static final int MAX_COUNT = 9999; - - private DataSourceContextHolder() { - } - - public static DataSourceEnum get() { - return Optional.ofNullable(CONTEXT_HOLDER.get()).orElse(DataSourceEnum.MASTER); - } - - private static void set(DataSourceEnum dbType) { - CONTEXT_HOLDER.set(dbType); - } - - public static void master() { - set(DataSourceEnum.MASTER); - if (log.isDebugEnabled()) { - log.debug("Current data source -> {}", DataSourceEnum.MASTER); - } - } - - public static void slave() { - // Simple load-balance for more slave clusters, assumed we got 2 replicas - val index = COUNTER.getAndIncrement() % 2; - if (COUNTER.get() > MAX_COUNT) { - COUNTER.set(-1); - } - // if replica data sources are more then 1, the data source needs load balance by index - set(DataSourceEnum.SLAVE1); - if (log.isDebugEnabled()) { - log.debug("Current data source -> {}, index: {}", DataSourceEnum.MASTER, index); - } - } - - public static void clear() { - CONTEXT_HOLDER.remove(); - if (log.isDebugEnabled()) { - log.debug("Cleared CONTEXT_HOLDER"); - } - } -} diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/DataSourceEnum.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/DataSourceEnum.java index 8422fdd9..3c62c08f 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/DataSourceEnum.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/DataSourceEnum.java @@ -16,11 +16,11 @@ public enum DataSourceEnum { /** * Master */ - MASTER("master"), + MASTER_1("master_1"), /** * Slave 1 */ - SLAVE1("slave1"), + SLAVE_1("slave_1"), /** * Quartz */ diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/DynamicDataSourceInterceptor.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/DynamicDataSourceInterceptor.java deleted file mode 100644 index 7bbbe455..00000000 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/DynamicDataSourceInterceptor.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.jmsoftware.maf.springcloudstarter.database; - -import lombok.extern.slf4j.Slf4j; -import lombok.val; -import org.apache.ibatis.executor.Executor; -import org.apache.ibatis.executor.keygen.SelectKeyGenerator; -import org.apache.ibatis.mapping.MappedStatement; -import org.apache.ibatis.mapping.SqlCommandType; -import org.apache.ibatis.plugin.*; -import org.apache.ibatis.session.ResultHandler; -import org.apache.ibatis.session.RowBounds; -import org.springframework.transaction.support.TransactionSynchronizationManager; - -import java.util.Objects; - -/** - * Description: DynamicDataSourceInterceptor, change description here. - * - * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 6/27/2021 12:26 AM - **/ -@Slf4j -@Intercepts({ - @Signature( - type = Executor.class, - method = "update", - args = {MappedStatement.class, Object.class} - ), - @Signature( - type = Executor.class, - method = "query", - args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class - }) -}) -public class DynamicDataSourceInterceptor implements Interceptor { - @Override - public Object intercept(Invocation invocation) throws Throwable { - // Check if the transaction synchronization is active. It it was, would use MASTER data source - val actualTransactionActive = TransactionSynchronizationManager.isActualTransactionActive(); - // MyBatis CRUD args - val args = invocation.getArgs(); - // MappedStatement SqlCommandType - val mappedStatement = (MappedStatement) args[0]; - // Default using master - DataSourceContextHolder.master(); - if (!actualTransactionActive) { - // SqlCommandType.SELECT, UNKNOWN|INSERT|UPDATE|DELETE|SELECT|FLUSH - if (mappedStatement.getSqlCommandType().equals(SqlCommandType.SELECT)) { - // If it's the SQL return primary key as result - if (mappedStatement.getId().contains(SelectKeyGenerator.SELECT_KEY_SUFFIX)) { - log.warn("Calling ID-return SQL, {}", SelectKeyGenerator.SELECT_KEY_SUFFIX); - DataSourceContextHolder.master(); - } else { - if (Objects.deepEquals(SqlCommandType.SELECT, mappedStatement.getSqlCommandType())) { - DataSourceContextHolder.slave(); - } else { - DataSourceContextHolder.master(); - } - } - } - } else { - DataSourceContextHolder.master(); - } - log.warn("SQL statement [{}], SqlCommandType: [{}], using 🐬 [{}] data source", mappedStatement.getId(), - mappedStatement.getSqlCommandType().name(), DataSourceContextHolder.get()); - Object result; - try { - result = invocation.proceed(); - } finally { - DataSourceContextHolder.clear(); - } - return result; - } - - @Override - public Object plugin(Object target) { - if (target instanceof Executor) { - return Plugin.wrap(target, this); - } else { - return target; - } - } -} - diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/MyBatisPlusConfiguration.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/MyBatisPlusConfiguration.java index 27ebbdc0..09c4bfb5 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/MyBatisPlusConfiguration.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/MyBatisPlusConfiguration.java @@ -1,5 +1,6 @@ package com.jmsoftware.maf.springcloudstarter.database; +import com.baomidou.dynamic.datasource.plugin.MasterSlaveAutoRoutingPlugin; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; @@ -63,9 +64,9 @@ public Interceptor mybatisPlusInterceptor(PaginationInnerInterceptor paginationI @Bean @Order(2) - public Interceptor dynamicDataSourceInterceptor() { - log.warn("Initial bean: '{}'", DynamicDataSourceInterceptor.class.getSimpleName()); - return new DynamicDataSourceInterceptor(); + public Interceptor masterSlaveAutoRoutingPlugin() { + log.warn("Initial bean: '{}'", MasterSlaveAutoRoutingPlugin.class.getSimpleName()); + return new MasterSlaveAutoRoutingPlugin(); } @Bean diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/ReadWriteIsolationDynamicRoutingDataSource.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/ReadWriteIsolationDynamicRoutingDataSource.java deleted file mode 100644 index 7f0854c4..00000000 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/database/ReadWriteIsolationDynamicRoutingDataSource.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.jmsoftware.maf.springcloudstarter.database; - -import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; - -/** - * Description: ReadWriteIsolationDynamicRoutingDataSource, change description here. - * - * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 6/27/2021 12:06 AM - **/ -public class ReadWriteIsolationDynamicRoutingDataSource extends AbstractRoutingDataSource { - @Override - protected Object determineCurrentLookupKey() { - return DataSourceContextHolder.get(); - } -} diff --git a/universal-ui/pom.xml b/universal-ui/pom.xml index acd11658..e22d095e 100644 --- a/universal-ui/pom.xml +++ b/universal-ui/pom.xml @@ -11,7 +11,7 @@ muscle-and-fitness-server com.jmsoftware.maf - 0.0.2 + 0.0.3-SNAPSHOT