【db】整理druid连接池监控的配置

【config】找不到配置,修改提示改为warn级别
pull/3/head
fengshuonan 2021-01-10 13:43:42 +08:00
parent e73b2a4b70
commit e6a5e4e9a7
8 changed files with 267 additions and 26 deletions

View File

@ -24,7 +24,7 @@ public enum ConfigExceptionEnum implements AbstractExceptionEnum {
* <p>
* 使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", "系统配置表不存在该配置,配置名称:{},系统将使用默认配置"),
/**
*

View File

@ -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<SysConfigMapper, SysConfig
// 1.根据id获取常量
SysConfig sysConfig = this.querySysConfig(sysConfigParam);
if (sysConfig == null) {
String userTip = StrUtil.format(CONFIG_NOT_EXIST.getUserTip(), "id: " + sysConfigParam.getConfigId());
throw new ConfigException(CONFIG_NOT_EXIST, userTip);
String userTip = StrUtil.format(ConfigExceptionEnum.CONFIG_NOT_EXIST.getUserTip(), "id: " + sysConfigParam.getConfigId());
throw new ConfigException(ConfigExceptionEnum.CONFIG_NOT_EXIST, userTip);
}
// 2.不能删除系统参数
if (YesOrNotEnum.Y.getCode().equals(sysConfig.getSysFlag())) {
throw new ConfigException(CONFIG_SYS_CAN_NOT_DELETE);
throw new ConfigException(ConfigExceptionEnum.CONFIG_SYS_CAN_NOT_DELETE);
}
// 3.设置状态为已删除
@ -122,8 +119,8 @@ public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig
private SysConfig querySysConfig(SysConfigParam sysConfigParam) {
SysConfig sysConfig = this.getById(sysConfigParam.getConfigId());
if (ObjectUtil.isEmpty(sysConfig) || sysConfig.getDelFlag().equals(YesOrNotEnum.Y.getCode())) {
String userTip = StrUtil.format(CONFIG_NOT_EXIST.getUserTip(), "id: " + sysConfigParam.getConfigId());
throw new ConfigException(CONFIG_NOT_EXIST, userTip);
String userTip = StrUtil.format(ConfigExceptionEnum.CONFIG_NOT_EXIST.getUserTip(), "id: " + sysConfigParam.getConfigId());
throw new ConfigException(ConfigExceptionEnum.CONFIG_NOT_EXIST, userTip);
}
return sysConfig;
}

View File

@ -6,13 +6,12 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.config.api.ConfigApi;
import cn.stylefeng.roses.kernel.config.api.exception.ConfigException;
import cn.stylefeng.roses.kernel.config.api.exception.enums.ConfigExceptionEnum;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
import java.util.Set;
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.CONVERT_ERROR;
/**
*
@ -60,16 +59,16 @@ public class ConfigContainer implements ConfigApi {
public <T> T getConfigValue(String configCode, Class<T> 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> T getConfigValueNullable(String configCode, Class<T> 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;
}
}

View File

@ -38,6 +38,14 @@
<artifactId>druid</artifactId>
</dependency>
<!--config模块的api-->
<!--数据源配置要放到容器里-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>config-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>

View File

@ -18,4 +18,54 @@ public interface DbConstants {
*/
String DB_EXCEPTION_STEP_CODE = "02";
/**
* druidservleturl
*/
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 urlsession
*/
String DRUID_WEB_STAT_FILTER_SESSION_STAT_ENABLE = "false";
/**
* druid web urlsession
*/
String DRUID_WEB_STAT_FILTER_PRINCIPAL_SESSION_NAME = "";
/**
* druid web urlsession
*/
String DRUID_WEB_STAT_FILTER_SESSION_STAT_MAX_COUNT = "1000";
/**
* druid web urlcookie
*/
String DRUID_WEB_STAT_FILTER_PRINCIPAL_COOKIE_NAME = "";
/**
* druid web url urlsql
*/
String DRUID_WEB_STAT_FILTER_PROFILE_ENABLE = "true";
}

View File

@ -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 {
/**
* Druidurl
*
* @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 urlsession
*
* @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 urlsession
*
* @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 urlsession
*
* @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 urlcookie
*
* @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 urlurlsql
*
* @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);
}
}

View File

@ -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
*
* <p>
* DataSourceDruidDruid
*
* @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<StatViewServlet> statViewServletRegistrationBean() {
ServletRegistrationBean<StatViewServlet> 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;
}
/**
* Druidurl
*
* @author fengshuonan
* @date 2021/1/10 11:45
*/
@Bean
public FilterRegistrationBean<WebStatFilter> webStatFilterRegistrationBean() {
FilterRegistrationBean<WebStatFilter> 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;
}
}

View File

@ -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
cn.stylefeng.roses.kernel.db.starter.GunsDbInitListenerAutoConfiguration