Proficient in SpringBoot — Integration with Druid to monitor SQL execution

developlee

  • monitoring
  • SQL
  • The database
  • spring
  • configuration
  • logger
  • string
  • PUT
  • bean
  • alibaba
  • Druid
  • Boot

Spring Boot implements SQL monitoring with Druid

Cough, today we do something simple and practical, using Springboot to integrate Alibaba’s Driud database monitoring tool. Alibaba already offers the Spring-boot-Starter JAR package. Druid.version (); druid.version ();

<! Alibaba </groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid.version}</version> </dependency>Copy the code

Configuration information is necessary

Spring: # development environment configuration datasource: druid: url: JDBC: mysql: / / localhost: 3306 / test? allowMultiQueries:true&characterEncoding:UTF-8&zeroDateTimeBehavior:convertToNull username: root password: 123456 driver-class-name: com.mysql.jdbc.driver # Initial-size: 1 max-active: 20 min-idle: 1 max-wait: 10000 pool-prepared-statements: true max-open-prepared-statements: 20 validation-query: SELECT 1 FROM DUAL validation-query-timeout: 5000 test-on-borrow: false test-on-return: false test-while-idle: true time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 30000 max-evictable-idle-time-millis: 60000 removeAbandoned: true removeAbandonedTimeout: 1800 connectionProperties: druid.stat.mergeSql=true; druid.stat.slowSqlMillis=5000 max-pool-prepared-statement-per-connection-size: 20 filters: stat,wall #filters: # configure multiple English commas (statistics, SQL injection, log4j filtering) type: com. Alibaba. Druid. Pool. DruidDataSourceCopy the code

Next is focus, focus, focus…… @configuration Configuration class for the annotation

@Configuration public class DruidConfig { private static final Logger logger = LoggerFactory.getLogger(DruidConfig.class); @Autowired private AppConfig appConfig; @Value("${spring.datasource.druid.url}") private String dbUrl; @Value("${spring.datasource.druid.username}") private String username; @Value("${spring.datasource.druid.password}") private String password; @Value("${spring.datasource.druid.driver-class-name}") private String driverClassName; @Value("${spring.datasource.druid.initial-size}") private int initialSize; @Value("${spring.datasource.druid.min-idle}") private int minIdle; @Value("${spring.datasource.druid.max-active}") private int maxActive; @Value("${spring.datasource.druid.max-wait}") private int maxWait; @Value("${spring.datasource.druid.time-between-eviction-runs-millis}") private int timeBetweenEvictionRunsMillis; @Value("${spring.datasource.druid.min-evictable-idle-time-millis}") private int minEvictableIdleTimeMillis; @Value("${spring.datasource.druid.validation-query}") private String validationQuery; @Value("${spring.datasource.druid.test-while-idle}") private boolean testWhileIdle; @Value("${spring.datasource.druid.test-on-borrow}") private boolean testOnBorrow; @Value("${spring.datasource.druid.test-on-return}") private boolean testOnReturn; @Value("${spring.datasource.druid.pool-prepared-statements}") private boolean poolPreparedStatements; @Value("${spring.datasource.druid.max-pool-prepared-statement-per-connection-size}") private int maxPoolPreparedStatementPerConnectionSize; @Value("${spring.datasource.druid.filters}") private String filters; @Value("{spring.datasource.druid.connection-properties}") private String connectionProperties; @primary // In the same DataSource, DruidDataSource public DataSource DataSource() {DruidDataSource DataSource = new DruidDataSource(); datasource.setUrl(dbUrl); datasource.setUsername(username); datasource.setPassword(password); datasource.setDriverClassName(driverClassName); //configuration datasource.setInitialSize(initialSize); datasource.setMinIdle(minIdle); datasource.setMaxActive(maxActive); datasource.setMaxWait(maxWait); datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); datasource.setValidationQuery(validationQuery); datasource.setTestWhileIdle(testWhileIdle); datasource.setTestOnBorrow(testOnBorrow); datasource.setTestOnReturn(testOnReturn); datasource.setPoolPreparedStatements(poolPreparedStatements); datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); try { datasource.setFilters(filters); } catch (Exception e) { logger.error("druid configuration initialization filter", e); } datasource.setConnectionProperties(connectionProperties); return datasource; } @Bean public ServletRegistrationBean druidServlet() { //logger.info("init Druid Servlet Configuration "); ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(); servletRegistrationBean.setServlet(new StatViewServlet()); servletRegistrationBean.addUrlMappings("/druid/*"); Map<String, String> initParameters = new HashMap<>(); initParameters.put("loginUsername", appConfig.getDruidUserName()); / / user name initParameters. Put (" loginPassword ", appConfig. GetDruidPassword ()); / / password initParameters. Put (" resetEnable ", appConfig. GetDruidResetAll ()); // Disable the "Reset All" function on HTML pages initparameters. put("allow", ""); // IP whitelist (not configured or empty, all access allowed) // initparameters. put("deny", "192.168.20.38"); When there is a common / / IP blacklist (deny prior to allow) servletRegistrationBean. SetInitParameters (initParameters); return servletRegistrationBean; } @Bean public FilterRegistrationBean druidWebStatViewFilter() { FilterRegistrationBean registrationBean = new FilterRegistrationBean(new WebStatFilter()); registrationBean.addInitParameter("urlPatterns", "/*"); registrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"); return registrationBean; }}Copy the code

To clarify, the AppConfig class is a set of configuration items that I have customized. It has nothing to do with integrating Druid.

Localhost :8080/druid /druid /druid /druid /druid /druid /druid

Use the cloud habitat community APP, comfortable ~

For details, please click
Comments (0)

To:


Related articles

  • SpringBoot integrates with Alibaba Druid monitoring
  • Spring Boot…
  • Spring Boot 1.5.4 Integration with Druid
  • Druid database connection pooling is as simple as that
  • SpringBoot Development case configuration Alibaba Druid connection…
  • Springboot configures multi-data source GOOD
  • Spring – the boot | integrated general Mabatis paging…
  • Springboot1.5.9 + Mybatis +…
  • SpringBoot learning process
  • Spring Boot developed its own website from scratch

The net friend comment on