From e6a5e4e9a7c65f11f5f23ab379ddc4ca0728003c Mon Sep 17 00:00:00 2001 From: fengshuonan Date: Sun, 10 Jan 2021 13:43:42 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90db=E3=80=91=E6=95=B4=E7=90=86druid?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E6=B1=A0=E7=9B=91=E6=8E=A7=E7=9A=84=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=20=E3=80=90config=E3=80=91=E6=89=BE=E4=B8=8D=E5=88=B0?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=EF=BC=8C=E4=BF=AE=E6=94=B9=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E6=94=B9=E4=B8=BAwarn=E7=BA=A7=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/enums/ConfigExceptionEnum.java | 2 +- .../service/impl/SysConfigServiceImpl.java | 15 +- .../roses/kernel/config/ConfigContainer.java | 23 ++- kernel-d-db/db-api/pom.xml | 8 + .../kernel/db/api/constants/DbConstants.java | 50 +++++++ .../db/api/expander/DruidConfigExpander.java | 137 ++++++++++++++++++ .../GunsDataSourceAutoConfiguration.java | 54 ++++++- .../main/resources/META-INF/spring.factories | 4 +- 8 files changed, 267 insertions(+), 26 deletions(-) create mode 100644 kernel-d-db/db-api/src/main/java/cn/stylefeng/roses/kernel/db/api/expander/DruidConfigExpander.java diff --git a/kernel-d-config/config-api/src/main/java/cn/stylefeng/roses/kernel/config/api/exception/enums/ConfigExceptionEnum.java b/kernel-d-config/config-api/src/main/java/cn/stylefeng/roses/kernel/config/api/exception/enums/ConfigExceptionEnum.java index 273c2dfb8..84f7c6f0a 100644 --- a/kernel-d-config/config-api/src/main/java/cn/stylefeng/roses/kernel/config/api/exception/enums/ConfigExceptionEnum.java +++ b/kernel-d-config/config-api/src/main/java/cn/stylefeng/roses/kernel/config/api/exception/enums/ConfigExceptionEnum.java @@ -24,7 +24,7 @@ public enum ConfigExceptionEnum implements AbstractExceptionEnum { *

* 使用时候,用StrUtil.format()将配置名称带上 */ - CONFIG_NOT_EXIST(RuleConstants.BUSINESS_ERROR_TYPE_CODE + ConfigConstants.CONFIG_EXCEPTION_STEP_CODE + "02", "系统配置表不存在该配置,请检查该配置是否存在,配置名称:{}"), + CONFIG_NOT_EXIST(RuleConstants.BUSINESS_ERROR_TYPE_CODE + ConfigConstants.CONFIG_EXCEPTION_STEP_CODE + "02", "系统配置表不存在该配置,配置名称:{},系统将使用默认配置"), /** * 系统配置表获取值时,强转类型异常 diff --git a/kernel-d-config/config-business/src/main/java/cn/stylefeng/roses/kernel/config/modular/service/impl/SysConfigServiceImpl.java b/kernel-d-config/config-business/src/main/java/cn/stylefeng/roses/kernel/config/modular/service/impl/SysConfigServiceImpl.java index fdf782afa..094aef325 100644 --- a/kernel-d-config/config-business/src/main/java/cn/stylefeng/roses/kernel/config/modular/service/impl/SysConfigServiceImpl.java +++ b/kernel-d-config/config-business/src/main/java/cn/stylefeng/roses/kernel/config/modular/service/impl/SysConfigServiceImpl.java @@ -5,6 +5,7 @@ import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.stylefeng.roses.kernel.config.api.context.ConfigContext; import cn.stylefeng.roses.kernel.config.api.exception.ConfigException; +import cn.stylefeng.roses.kernel.config.api.exception.enums.ConfigExceptionEnum; import cn.stylefeng.roses.kernel.config.modular.entity.SysConfig; import cn.stylefeng.roses.kernel.config.modular.mapper.SysConfigMapper; import cn.stylefeng.roses.kernel.config.modular.param.SysConfigParam; @@ -22,10 +23,6 @@ import org.springframework.transaction.annotation.Transactional; import java.util.List; -import static cn.stylefeng.roses.kernel.config.api.exception.enums.ConfigExceptionEnum.CONFIG_NOT_EXIST; -import static cn.stylefeng.roses.kernel.config.api.exception.enums.ConfigExceptionEnum.CONFIG_SYS_CAN_NOT_DELETE; - - /** * 系统参数配置service接口实现类 * @@ -77,13 +74,13 @@ public class SysConfigServiceImpl extends ServiceImpl T getConfigValue(String configCode, Class clazz) throws ConfigException { String configValue = CONFIG_CONTAINER.getStr(configCode); if (ObjectUtil.isEmpty(configValue)) { - String format = StrUtil.format(CONFIG_NOT_EXIST.getUserTip(), configCode); - log.error(format); - throw new ConfigException(CONFIG_NOT_EXIST.getErrorCode(), format); + String format = StrUtil.format(ConfigExceptionEnum.CONFIG_NOT_EXIST.getUserTip(), configCode); + log.warn(format); + throw new ConfigException(ConfigExceptionEnum.CONFIG_NOT_EXIST.getErrorCode(), format); } else { try { return Convert.convert(clazz, configValue); } catch (Exception e) { - String format = StrUtil.format(CONVERT_ERROR.getUserTip(), configCode, configValue, clazz.toString()); - log.error(format); - throw new ConfigException(CONVERT_ERROR.getErrorCode(), format); + String format = StrUtil.format(ConfigExceptionEnum.CONVERT_ERROR.getUserTip(), configCode, configValue, clazz.toString()); + log.warn(format); + throw new ConfigException(ConfigExceptionEnum.CONVERT_ERROR.getErrorCode(), format); } } } @@ -78,15 +77,15 @@ public class ConfigContainer implements ConfigApi { public T getConfigValueNullable(String configCode, Class clazz) { String configValue = CONFIG_CONTAINER.getStr(configCode); if (ObjectUtil.isEmpty(configValue)) { - String format = StrUtil.format(CONFIG_NOT_EXIST.getUserTip(), configCode); - log.error(format); + String format = StrUtil.format(ConfigExceptionEnum.CONFIG_NOT_EXIST.getUserTip(), configCode); + log.warn(format); return null; } else { try { return Convert.convert(clazz, configValue); } catch (Exception e) { - String format = StrUtil.format(CONVERT_ERROR.getUserTip(), configCode, configValue, clazz.toString()); - log.error(format); + String format = StrUtil.format(ConfigExceptionEnum.CONVERT_ERROR.getUserTip(), configCode, configValue, clazz.toString()); + log.warn(format); return null; } } diff --git a/kernel-d-db/db-api/pom.xml b/kernel-d-db/db-api/pom.xml index edd0859e2..f1a25cd9d 100644 --- a/kernel-d-db/db-api/pom.xml +++ b/kernel-d-db/db-api/pom.xml @@ -38,6 +38,14 @@ druid + + + + cn.stylefeng.roses + config-api + 1.0.0 + + diff --git a/kernel-d-db/db-api/src/main/java/cn/stylefeng/roses/kernel/db/api/constants/DbConstants.java b/kernel-d-db/db-api/src/main/java/cn/stylefeng/roses/kernel/db/api/constants/DbConstants.java index 9d92cb0ac..b38be9212 100644 --- a/kernel-d-db/db-api/src/main/java/cn/stylefeng/roses/kernel/db/api/constants/DbConstants.java +++ b/kernel-d-db/db-api/src/main/java/cn/stylefeng/roses/kernel/db/api/constants/DbConstants.java @@ -18,4 +18,54 @@ public interface DbConstants { */ String DB_EXCEPTION_STEP_CODE = "02"; + /** + * druid默认servlet的映射url + */ + String DEFAULT_DRUID_URL_MAPPINGS = "/druid/*"; + + /** + * druid控制台账号 + */ + String DEFAULT_DRUID_ADMIN_ACCOUNT = "admin"; + + /** + * druid控制台的监控数据默认不能清空 + */ + String DEFAULT_DRUID_ADMIN_RESET_ENABLE = "false"; + + /** + * druid web url统计的拦截范围 + */ + String DRUID_WEB_STAT_FILTER_URL_PATTERN = "/*"; + + /** + * druid web url统计的排除拦截表达式 + */ + String DRUID_WEB_STAT_FILTER_EXCLUSIONS = "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"; + + /** + * druid web url统计的session统计开关 + */ + String DRUID_WEB_STAT_FILTER_SESSION_STAT_ENABLE = "false"; + + /** + * druid web url统计的session名称 + */ + String DRUID_WEB_STAT_FILTER_PRINCIPAL_SESSION_NAME = ""; + + /** + * druid web url统计的session最大监控数 + */ + String DRUID_WEB_STAT_FILTER_SESSION_STAT_MAX_COUNT = "1000"; + + /** + * druid web url统计的cookie名称 + */ + String DRUID_WEB_STAT_FILTER_PRINCIPAL_COOKIE_NAME = ""; + + /** + * druid web url统计的 是否开启监控单个url调用的sql列表 + */ + String DRUID_WEB_STAT_FILTER_PROFILE_ENABLE = "true"; + } diff --git a/kernel-d-db/db-api/src/main/java/cn/stylefeng/roses/kernel/db/api/expander/DruidConfigExpander.java b/kernel-d-db/db-api/src/main/java/cn/stylefeng/roses/kernel/db/api/expander/DruidConfigExpander.java new file mode 100644 index 000000000..5e9b14389 --- /dev/null +++ b/kernel-d-db/db-api/src/main/java/cn/stylefeng/roses/kernel/db/api/expander/DruidConfigExpander.java @@ -0,0 +1,137 @@ +package cn.stylefeng.roses.kernel.db.api.expander; + +import cn.hutool.core.util.RandomUtil; +import cn.stylefeng.roses.kernel.config.api.context.ConfigContext; +import cn.stylefeng.roses.kernel.db.api.constants.DbConstants; +import lombok.extern.slf4j.Slf4j; + +/** + * Druid数据源的一些配置 + * + * @author fengshuonan + * @date 2021/1/10 11:32 + */ +@Slf4j +public class DruidConfigExpander { + + /** + * Druid监控界面的url映射 + * + * @author fengshuonan + * @date 2021/1/10 11:32 + */ + public static String getDruidUrlMappings() { + return ConfigContext.me().getSysConfigValueWithDefault("SYS_DRUID_URL_MAPPINGS", String.class, DbConstants.DEFAULT_DRUID_URL_MAPPINGS); + } + + /** + * Druid控制台账号 + * + * @author fengshuonan + * @date 2021/1/10 11:32 + */ + public static String getDruidAdminAccount() { + return ConfigContext.me().getSysConfigValueWithDefault("SYS_DRUID_ACCOUNT", String.class, DbConstants.DEFAULT_DRUID_ADMIN_ACCOUNT); + } + + /** + * Druid控制台账号密码 + * + * @author fengshuonan + * @date 2021/1/10 11:34 + */ + public static String getDruidAdminPassword() { + String sysDruidPassword = ConfigContext.me().getConfigValueNullable("SYS_DRUID_PASSWORD", String.class); + + // 没配置就返回一个随机密码 + if (sysDruidPassword == null) { + String randomString = RandomUtil.randomString(20); + log.info("Druid密码未在系统配置表设置,Druid密码为:{}", randomString); + return randomString; + } else { + return sysDruidPassword; + } + } + + /** + * Druid控制台的监控数据是否可以重置清零 + * + * @return true-可以重置,false-不可以 + * @author fengshuonan + * @date 2021/1/10 11:34 + */ + public static String getDruidAdminResetFlag() { + return ConfigContext.me().getSysConfigValueWithDefault("SYS_DRUID_RESET_ENABLE", String.class, DbConstants.DEFAULT_DRUID_ADMIN_RESET_ENABLE); + } + + /** + * druid web url统计的拦截范围 + * + * @author fengshuonan + * @date 2021/1/10 11:34 + */ + public static String getDruidAdminWebStatFilterUrlPattern() { + return ConfigContext.me().getSysConfigValueWithDefault("SYS_DRUID_WEB_STAT_FILTER_URL_PATTERN", String.class, DbConstants.DRUID_WEB_STAT_FILTER_URL_PATTERN); + } + + /** + * druid web url统计的排除拦截表达式 + * + * @author fengshuonan + * @date 2021/1/10 11:34 + */ + public static String getDruidAdminWebStatFilterExclusions() { + return ConfigContext.me().getSysConfigValueWithDefault("SYS_DRUID_WEB_STAT_FILTER_EXCLUSIONS", String.class, DbConstants.DRUID_WEB_STAT_FILTER_EXCLUSIONS); + } + + /** + * druid web url统计的session统计开关 + * + * @author fengshuonan + * @date 2021/1/10 11:34 + */ + public static String getDruidAdminWebStatFilterSessionStatEnable() { + return ConfigContext.me().getSysConfigValueWithDefault("SYS_DRUID_WEB_STAT_FILTER_SESSION_STAT_ENABLE", String.class, DbConstants.DRUID_WEB_STAT_FILTER_SESSION_STAT_ENABLE); + } + + /** + * druid web url统计的session名称 + * + * @author fengshuonan + * @date 2021/1/10 11:34 + */ + public static String getDruidAdminWebStatFilterSessionName() { + return ConfigContext.me().getSysConfigValueWithDefault("SYS_DRUID_WEB_STAT_FILTER_PRINCIPAL_SESSION_NAME", String.class, DbConstants.DRUID_WEB_STAT_FILTER_PRINCIPAL_SESSION_NAME); + } + + /** + * druid web url统计的session最大监控数 + * + * @author fengshuonan + * @date 2021/1/10 11:34 + */ + public static String getDruidAdminWebStatFilterSessionStatMaxCount() { + return ConfigContext.me().getSysConfigValueWithDefault("SYS_DRUID_WEB_STAT_FILTER_SESSION_STAT_MAX_COUNT", String.class, DbConstants.DRUID_WEB_STAT_FILTER_SESSION_STAT_MAX_COUNT); + } + + /** + * druid web url统计的cookie名称 + * + * @author fengshuonan + * @date 2021/1/10 11:34 + */ + public static String getDruidAdminWebStatFilterPrincipalCookieName() { + return ConfigContext.me().getSysConfigValueWithDefault("SYS_DRUID_WEB_STAT_FILTER_PRINCIPAL_COOKIE_NAME", String.class, DbConstants.DRUID_WEB_STAT_FILTER_PRINCIPAL_COOKIE_NAME); + } + + /** + * druid web url统计的是否开启监控单个url调用的sql列表 + * + * @author fengshuonan + * @date 2021/1/10 11:34 + */ + public static String getDruidAdminWebStatFilterProfileEnable() { + return ConfigContext.me().getSysConfigValueWithDefault("SYS_DRUID_WEB_STAT_FILTER_PROFILE_ENABLE", String.class, DbConstants.DRUID_WEB_STAT_FILTER_PROFILE_ENABLE); + } + +} diff --git a/kernel-d-db/db-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/db/starter/GunsDataSourceAutoConfiguration.java b/kernel-d-db/db-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/db/starter/GunsDataSourceAutoConfiguration.java index cc97ee32f..292822a3b 100644 --- a/kernel-d-db/db-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/db/starter/GunsDataSourceAutoConfiguration.java +++ b/kernel-d-db/db-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/db/starter/GunsDataSourceAutoConfiguration.java @@ -1,11 +1,17 @@ package cn.stylefeng.roses.kernel.db.starter; +import cn.hutool.core.util.StrUtil; +import cn.stylefeng.roses.kernel.db.api.expander.DruidConfigExpander; import cn.stylefeng.roses.kernel.db.api.factory.DruidDatasourceFactory; import cn.stylefeng.roses.kernel.db.api.pojo.druid.DruidProperties; import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.support.http.StatViewServlet; +import com.alibaba.druid.support.http.WebStatFilter; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -13,7 +19,9 @@ import org.springframework.context.annotation.Import; import javax.sql.DataSource; /** - * 数据库连接和DAO框架的配置 + * 数据库连接池的配置 + *

+ * 如果系统中没有配DataSource,则系统默认加载Druid连接池,并开启Druid的监控 * * @author fengshuonan * @date 2020/11/30 22:24 @@ -21,6 +29,7 @@ import javax.sql.DataSource; @Configuration @Import(GunsDruidPropertiesAutoConfiguration.class) @AutoConfigureBefore(DataSourceAutoConfiguration.class) +@ConditionalOnMissingBean(DataSource.class) public class GunsDataSourceAutoConfiguration { /** @@ -30,9 +39,50 @@ public class GunsDataSourceAutoConfiguration { * @date 2020/11/30 22:37 */ @Bean(initMethod = "init") - @ConditionalOnMissingBean(DataSource.class) public DruidDataSource druidDataSource(DruidProperties druidProperties) { return DruidDatasourceFactory.createDruidDataSource(druidProperties); } + /** + * Druid监控界面的配置 + * + * @author fengshuonan + * @date 2021/1/10 11:29 + */ + @Bean + public ServletRegistrationBean statViewServletRegistrationBean() { + ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(); + registrationBean.setServlet(new StatViewServlet()); + registrationBean.addUrlMappings(DruidConfigExpander.getDruidUrlMappings()); + registrationBean.addInitParameter("loginUsername", DruidConfigExpander.getDruidAdminAccount()); + registrationBean.addInitParameter("loginPassword", DruidConfigExpander.getDruidAdminPassword()); + registrationBean.addInitParameter("resetEnable", DruidConfigExpander.getDruidAdminResetFlag()); + return registrationBean; + } + + /** + * 用于配置Druid监控url统计 + * + * @author fengshuonan + * @date 2021/1/10 11:45 + */ + @Bean + public FilterRegistrationBean webStatFilterRegistrationBean() { + FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); + WebStatFilter filter = new WebStatFilter(); + registrationBean.setFilter(filter); + registrationBean.addUrlPatterns(DruidConfigExpander.getDruidAdminWebStatFilterUrlPattern()); + registrationBean.addInitParameter("exclusions", DruidConfigExpander.getDruidAdminWebStatFilterExclusions()); + registrationBean.addInitParameter("sessionStatEnable", DruidConfigExpander.getDruidAdminWebStatFilterSessionStatEnable()); + registrationBean.addInitParameter("sessionStatMaxCount", DruidConfigExpander.getDruidAdminWebStatFilterSessionStatMaxCount()); + if (StrUtil.isNotBlank(DruidConfigExpander.getDruidAdminWebStatFilterSessionName())) { + registrationBean.addInitParameter("principalSessionName", DruidConfigExpander.getDruidAdminWebStatFilterSessionName()); + } + if (StrUtil.isNotBlank(DruidConfigExpander.getDruidAdminWebStatFilterPrincipalCookieName())) { + registrationBean.addInitParameter("principalCookieName", DruidConfigExpander.getDruidAdminWebStatFilterPrincipalCookieName()); + } + registrationBean.addInitParameter("profileEnable", DruidConfigExpander.getDruidAdminWebStatFilterProfileEnable()); + return registrationBean; + } + } diff --git a/kernel-d-db/db-spring-boot-starter/src/main/resources/META-INF/spring.factories b/kernel-d-db/db-spring-boot-starter/src/main/resources/META-INF/spring.factories index 3c869b4cb..b252d308b 100644 --- a/kernel-d-db/db-spring-boot-starter/src/main/resources/META-INF/spring.factories +++ b/kernel-d-db/db-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -1,5 +1,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ cn.stylefeng.roses.kernel.db.starter.GunsDataSourceAutoConfiguration,\ - cn.stylefeng.roses.kernel.db.starter.GunsDataSourceAutoConfiguration,\ + cn.stylefeng.roses.kernel.db.starter.GunsDruidPropertiesAutoConfiguration,\ cn.stylefeng.roses.kernel.db.starter.GunsMyBatisPlusAutoConfiguration,\ - cn.stylefeng.roses.kernel.db.starter.GunsDbInitListenerAutoConfiguration \ No newline at end of file + cn.stylefeng.roses.kernel.db.starter.GunsDbInitListenerAutoConfiguration