SpringBoot配置druid数据源监控⾃定义⽇志监控StatLogger配
置logb。。。
1 配置druid数据源
springboot默认使⽤数据源Hikari,Druid(德鲁伊)是阿⾥巴巴开发的号称为监控⽽⽣的数据库连接池,在功能、性能、扩展性⽅⾯,都超过其他数据库连接池,同时加⼊了⽇志监控,可以很好的监控DB池连接和SQL的执⾏情况。
1.1 引⼊druid依赖:
<!-- mvnrepository/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
1.2 配置l
spring:
datasource:
username: root
password:
url: jdbc:mysql://localhost:3306/jdbc?serverTimezone=UTC&userUnicode=true&characterEncoding=utf-8
driver-class-name: sql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource #指定druid数据源
#druid 数据源专有配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
timeBetweenLogStatsMillis: 30000 #30s输出⼀次log
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: false #不缓存
#配置监控统计拦截的filters,stat:监控统计、slf4j:⽇志记录、wall:防御sql注⼊
filters: stat,wall,slf4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: false
connectionProperties: Sql=true;druid.stat.slowSqlMillis=500
注意:如果想要把druid的监控输⼊到⽇志中,timeBetweenLogStatsMillis必须要设置,useGlobalDtaSourceStat要设置为false。(不然会提⽰不⽀持)。配置timeBetweenLogStatsMillis>0之后,DruidDataSource会定期把监控数据输出到⽇志中。但是每次输出⽇志会导致清零(reset)连接池相关的计数器。
附上评论提的这个问题:
1.3 配置druid
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix="spring.datasource")
@Bean
public DataSource druidDataSource()
{
DruidDataSource dataSource = new DruidDataSource();
dataSource.setStatLogger(new MyStatLogger());
return dataSource;
}
//后台监控功能 druid
@Bean
/
/因为SpringBoot内置了servlet容器,所以没有l,替代⽅法就是注册ServletRegistrationBean
public ServletRegistrationBean statViewServlet()
{
ServletRegistrationBean<StatViewServlet> bean=new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
//后台需要有⼈登录监控
HashMap<String,String> initParameters=new HashMap<>();
//增加配置
initParameters.put("loginUsername","admin");
druid连接池配置详解initParameters.put("loginPassword","123456");
//允许谁能访问
initParameters.put("allow"," ");
bean.setInitParameters(initParameters);//设置初始化参数
return bean;
}
@Bean
//filter
public FilterRegistrationBean webStatFilter()
{
FilterRegistrationBean bean=new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
//可以过滤哪些请求呢
HashMap<String,String> initParameters=new HashMap<>();
/
/这些东西不进⾏统计
initParameters.put("exclusions","*.js,*.css,/druid/*");
bean.setInitParameters(initParameters);
return bean;
}
}
1.4 ⾃定义StatLogger
DruidDataSource是通过com.alibaba.druid.pool.DruidDataSourceStatLoggerImpl.DruidDataSourceStatLoggerImpl来实现输⼊监控数据到⽇志的,我们可以实现DruidDataSourceStatLogger,来⾃定义StatLogger,例如:
public class MyStatLogger implements DruidDataSourceStatLogger {
private static Log LOG = Log(DruidDataSourceStatLoggerImpl.class); private Log
logger = LOG;
public MyStatLogger(){
}
@Override
public void configFromProperties(Properties properties) {
String property = Property("druid.stat.loggerName");
if (property != null && property.length() > 0) {
setLoggerName(property);
}
}
public Log getLogger() {
return logger;
}
@Override
public void setLoggerName(String loggerName) {
logger = Log(loggerName);
}
@Override
public void setLogger(Log logger) {
if (logger == null) {
throw new IllegalArgumentException("logger can not be null");
}
this.logger = logger;
}
public boolean isLogEnable() {
return logger.isInfoEnabled();
}
public void log(String value) {
logger.info(value);
}
@Override
public void log(DruidDataSourceStatValue statValue) {
if (!isLogEnable()) {
return;
}
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("dbType", DbType());
map.put("name", Name());
map.put("activeCount", ActiveCount());
if (ActivePeak() > 0) {
map.put("activePeak", ActivePeak());
map.put("activePeakTime", ActivePeakTime());
}
map.put("poolingCount", PoolingCount());
if (PoolingPeak() > 0) {
map.put("poolingPeak", PoolingPeak());
map.put("poolingPeakTime", PoolingPeakTime());
}
map.put("connectCount", ConnectCount());
map.put("closeCount", CloseCount());
if (WaitThreadCount() > 0) {
map.put("waitThreadCount", WaitThreadCount());
}
if (NotEmptyWaitCount() > 0) {
map.put("notEmptyWaitCount", NotEmptyWaitCount());
}
if (NotEmptyWaitMillis() > 0) {
map.put("notEmptyWaitMillis", NotEmptyWaitMillis());
}
if (LogicConnectErrorCount() > 0) {
map.put("logicConnectErrorCount", LogicConnectErrorCount());
}
if (PhysicalConnectCount() > 0) {
map.put("physicalConnectCount", PhysicalConnectCount());
}
if (PhysicalCloseCount() > 0) {
map.put("physicalCloseCount", PhysicalCloseCount());
}
if (PhysicalConnectErrorCount() > 0) {
map.put("physicalConnectErrorCount", PhysicalConnectErrorCount());
}
if (ExecuteCount() > 0) {
map.put("executeCount", ExecuteCount());
}
if (ErrorCount() > 0) {
map.put("errorCount", ErrorCount());
}
if (CommitCount() > 0) {
map.put("commitCount", CommitCount());
}
if (RollbackCount() > 0) {
map.put("rollbackCount", RollbackCount());
}
if (PstmtCacheHitCount() > 0) {
map.put("pstmtCacheHitCount", PstmtCacheHitCount());
}
if (PstmtCacheMissCount() > 0) {
map.put("pstmtCacheMissCount", PstmtCacheMissCount());
}
if (StartTransactionCount() > 0) {
map.put("startTransactionCount", StartTransactionCount());
map.put("transactionHistogram", TransactionHistogram()));
}
if (ConnectCount() > 0) {
map.put("connectionHoldTimeHistogram", ConnectionHoldTimeHistogram())); }
if (ClobOpenCount() > 0) {
map.put("clobOpenCount", ClobOpenCount());
}
if (BlobOpenCount() > 0) {
map.put("blobOpenCount", BlobOpenCount());
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论