mirror of https://gitee.com/stylefeng/roses
parent
e73b2a4b70
commit
e6a5e4e9a7
|
@ -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", "系统配置表不存在该配置,配置名称:{},系统将使用默认配置"),
|
||||
|
||||
/**
|
||||
* 系统配置表获取值时,强转类型异常
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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";
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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>
|
||||
* 如果系统中没有配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<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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于配置Druid监控url统计
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue