当前位置:   article > 正文

数据库慢sql日志监控等_druid.stat.slowsqlmillis

druid.stat.slowsqlmillis

yml配置: 

  1. datasource:
  2. type: com.alibaba.druid.pool.DruidDataSource
  3. driver-class-name: com.mysql.cj.jdbc.Driver
  4. # mysql连接 本地
  5. url: jdbc:mysql://${MYSQL_HOST:127.0.0.1}:${MYSQL_PORT:3306}/${MYSQL_DATABASE:xxxx}?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&tcpKeepAlive=true&serverTimezone=CTT
  6. # mysql账号
  7. username: ${MYSQL_USERNAME:root}
  8. # mysql密码
  9. password: ${MYSQL_PASSWORD:1234}
  10. # 服务器密码
  11. #druid 数据源专有配置
  12. initialSize: 5
  13. minIdle: 5
  14. maxActive: 20
  15. maxWait: 60000
  16. timeBetweenEvictionRunsMillis: 60000
  17. minEvictableIdleTimeMillis: 300000
  18. validationQuery: SELECT 1 FROM DUAL
  19. testWhileIdle: true
  20. testOnBorrow: false
  21. testOnReturn: false
  22. poolPreparedStatements: true
  23. #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
  24. #如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority
  25. #则导入 log4j 依赖即可,Maven 地址: https://mvnrepository.com/artifact/log4j/log4j
  26. filters: stat,wall,slf4j
  27. maxPoolPreparedStatementPerConnectionSize: 20
  28. connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

代码配置:

  1. package com.yd.mental.hospital.server.config;
  2. import com.alibaba.druid.pool.DruidDataSource;
  3. import com.alibaba.druid.support.http.StatViewServlet;
  4. import com.alibaba.druid.support.http.WebStatFilter;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  7. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Primary;
  11. import javax.sql.DataSource;
  12. import java.sql.SQLException;
  13. /**
  14. * @creed: 阿里连接池后台 http://localhost:9002/druid/index.html 端口号记得换
  15. * @date 2020/2/25 10:29
  16. */
  17. @Configuration
  18. public class DruidConfig {
  19. @Value("${spring.datasource.url}")
  20. private String dbUrl;
  21. @Value("${spring.datasource.username}")
  22. private String username;
  23. @Value("${spring.datasource.password}")
  24. private String password;
  25. @Value("${spring.datasource.driver-class-name}")
  26. private String driverClassName;
  27. @Value("${spring.datasource.initialSize}")
  28. private int initialSize;
  29. @Value("${spring.datasource.minIdle}")
  30. private int minIdle;
  31. @Value("${spring.datasource.maxActive}")
  32. private int maxActive;
  33. @Value("${spring.datasource.maxWait}")
  34. private int maxWait;
  35. @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
  36. private int timeBetweenEvictionRunsMillis;
  37. @Value("${spring.datasource.minEvictableIdleTimeMillis}")
  38. private int minEvictableIdleTimeMillis;
  39. @Value("${spring.datasource.validationQuery}")
  40. private String validationQuery;
  41. @Value("${spring.datasource.testWhileIdle}")
  42. private boolean testWhileIdle;
  43. @Value("${spring.datasource.testOnBorrow}")
  44. private boolean testOnBorrow;
  45. @Value("${spring.datasource.testOnReturn}")
  46. private boolean testOnReturn;
  47. @Value("${spring.datasource.poolPreparedStatements}")
  48. private boolean poolPreparedStatements;
  49. @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
  50. private int maxPoolPreparedStatementPerConnectionSize;
  51. @Value("${spring.datasource.filters}")
  52. private String filters;
  53. @Value("{spring.datasource.connectionProperties}")
  54. private String connectionProperties;
  55. @Bean //声明其为Bean实例
  56. @Primary //在同样的DataSource中,首先使用被标注的DataSource
  57. public DataSource dataSource() {
  58. DruidDataSource datasource = new DruidDataSource();
  59. datasource.setUrl(this.dbUrl);
  60. datasource.setUsername(username);
  61. datasource.setPassword(password);
  62. datasource.setDriverClassName(driverClassName);
  63. //configuration
  64. datasource.setInitialSize(initialSize);
  65. datasource.setMinIdle(minIdle);
  66. datasource.setMaxActive(maxActive);
  67. datasource.setMaxWait(maxWait);
  68. datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
  69. datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
  70. datasource.setValidationQuery(validationQuery);
  71. datasource.setTestWhileIdle(testWhileIdle);
  72. datasource.setTestOnBorrow(testOnBorrow);
  73. datasource.setTestOnReturn(testOnReturn);
  74. datasource.setPoolPreparedStatements(poolPreparedStatements);
  75. datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
  76. try {
  77. datasource.setFilters(filters);
  78. } catch (SQLException e) {
  79. System.err.println("druid configuration initialization filter: " + e);
  80. }
  81. datasource.setConnectionProperties(connectionProperties);
  82. return datasource;
  83. }
  84. @Bean
  85. public ServletRegistrationBean statViewServlet() {
  86. //创建servlet注册实体
  87. ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
  88. //设置ip白名单
  89. servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
  90. //设置ip黑名单
  91. servletRegistrationBean.addInitParameter("deny", "192.168.0.2");
  92. //设置控制台管理用户__登录用户名和密码
  93. servletRegistrationBean.addInitParameter("loginUsername", "druid");
  94. servletRegistrationBean.addInitParameter("loginPassword", "123456");
  95. //是否可以重置数据
  96. servletRegistrationBean.addInitParameter("resetEnable", "false");
  97. return servletRegistrationBean;
  98. }
  99. @Bean
  100. public FilterRegistrationBean statFilter() {
  101. //创建过滤器
  102. FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
  103. //设置过滤器过滤路径
  104. filterRegistrationBean.addUrlPatterns("/*");
  105. //忽略过滤的形式
  106. filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.png,*.css,*.ico,/druid/*");
  107. return filterRegistrationBean;
  108. }
  109. }

数据库连接池

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.11</version>
</dependency>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/517423
推荐阅读
相关标签
  

闽ICP备14008679号