In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about the three ways of configuring Druid in SpringBoot. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Record how to configure Alibaba Druid to monitor or view SQL status in the project using pure YML (application.yml or application.properties) files, Java code configuration Bean, and annotations:
1. Pure configuration file .yml or .properties
(1) pom.xml adds related dependencies
Org.springframework.boot spring-boot-starter-web com.alibaba druid-spring-boot-starter 1.1.10 org.springframework.boot spring-boot-starter-jdbc mysql Mysql-connector-java ${mysql-connector-java.version} log4j log4j 1.2.16 compile
(2) configure application.yml or application.properties file, this time the configuration file is application.yml
Spring: # data source configuration datasource: type: com.alibaba.druid.pool.DruidDataSource # MYSQL 5 driver: com.mysql.jdbc.Driver MYSQL 6 + driver: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=GMT%2B8 username: root password: 123456 # connection pool configuration druid: # initialization size, minimum Maximum initial-size: 5 min-idle: 5 max-active: 20 # configure the time to get connection wait timeout max-wait: 60000 # configure how often to detect idle connections that need to be closed Unit millisecond time-between-eviction-runs-millis: 60000 # configure the minimum lifetime of a connection in the pool min-evictable-idle-time-millis: 300000 validation-query: SELECT 1 FROM sys_user test-while-idle: true test-on-borrow: false test-on-return: false # Open PSCache And specify the size of the PSCache on each connection pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 20 # configure the Filter to be intercepted by monitoring statistics. After removing the monitoring interface, SQL cannot be counted. Wall is used for firewall filters: stat,wall,log4j # enable the mergeSql function through the connection-properties attribute Slow SQL record connection-properties: druid.stat.mergeSql\ = true Druid.stat.slowSqlMillis\ = 5000 # configure DruidStatFilter web-stat-filter: enabled: true url-pattern: / * exclusions: .js, * .gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/* # configure DruidStatViewServlet stat-view-servlet: url-pattern: / druid/* # IP whitelist, no configuration or empty Allow all access to the allow: 127.0.0.1 # IP blacklist. If the whitelist also exists, priority is given to using deny: 192.168.31.253 # disable the Reset All button in HTML reset-enable: false # login username / password login-username: root login-password: 123
(3) launch the SpringBoot project access address http://localhost:8080/druid/login.html to log in. If the following interface appears, the configuration will be successful.
2.Java code configuration Bean
(1) pom.xml dependence
Com.alibaba druid 1.1.10
(2) add Druid configuration to the configuration file application.yml
Other configurations for spring: datasource: type: com.alibaba.druid.pool.DruidDataSource # data sources initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true # configure filters for monitoring statistics interception After the monitoring interface is removed, the sql cannot be counted. 'wall' is used for firewalls filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true Druid.stat.slowSqlMillis=500
(3) add DruidConfig.java configuration file
Import javax.servlet.Filter;import javax.servlet.Servlet;import javax.sql.DataSource;import org.springframework.boot.context.properties.ConfigurationProperties;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 com.alibaba.druid.pool.DruidDataSource;import com.alibaba.druid.support.http.StatViewServlet;import com.alibaba.druid.support.http.WebStatFilter @ Configurationpublic class DruidConfig {@ Bean / / load all configuration items prefixed with spring.datasource into DataSource @ ConfigurationProperties (prefix = "spring.datasource") public DataSource druidDataSource () {return new DruidDataSource ();} @ Bean public ServletRegistrationBean druidServlet () {/ / configuration processing for druid monitoring ServletRegistrationBean srb = new ServletRegistrationBean (new StatViewServlet (), "/ druid/**") / / whitelist srb.addInitParameter ("allow", "127.0.0.1"); / blacklist srb.addInitParameter ("deny", "192.168.31.253"); / / username srb.addInitParameter ("loginUsername", "root"); / / password srb.addInitParameter ("loginPassword", "root") / / whether the data source srb.addInitParameter ("resetEnable", "false") can be reset; return srb;} @ Bean public FilterRegistrationBean filterRegistrationBean () {FilterRegistrationBean frb = new FilterRegistrationBean (); frb.setFilter (new WebStatFilter ()); / / all requests are monitored for processing frb.addUrlPatterns ("/ *") / / exclusion list frb.addInitParameter ("exclusions", "* .js,*.gif,*.jpg,*.css,/druid/*"); return frb;}}
(4) launch the SpringBoot project access address http://localhost:8080/druid/login.html to view and log in
3. Notes
(1) add Druid dependency to pom.xml file (same way 2)
(2) add information to the configuration file (same way 2)
(3) configure WebServlet
Import javax.servlet.annotation.WebInitParam;import javax.servlet.annotation.WebServlet;import com.alibaba.druid.support.http.StatViewServlet @ WebServlet (urlPatterns = "/ druid/*", initParams= {@ WebInitParam (name= "allow", value= "192.168.16.110127.0.0.1"), / / IP whitelist (no configuration or empty, then all access is allowed) @ WebInitParam (name= "deny", value= "192.168.16.111"), / / IP blacklist (if there is common Deny takes precedence over allow) @ WebInitParam (name= "loginUsername", value= "admin"), / / username @ WebInitParam (name= "loginPassword", value= "admin"), / / password @ WebInitParam (name= "resetEnable", value= "false") / / disable the "Reset All" function} on the HTML page) public class DruidServlet extends StatViewServlet {private static final long serialVersionUID = 1L }
(4) configure WebFilter
Import javax.servlet.annotation.WebFilter;import javax.servlet.annotation.WebInitParam;import com.alibaba.druid.support.http.WebStatFilter;@WebFilter (filterName= "druidWebStatFilter", urlPatterns= "/ *", initParams= {@ WebInitParam (name= "exclusions", value= "* .js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*") / / ignore resources}) public class DruidFilter extends WebStatFilter {}
(5) configure the startup file SpringBootApplication to scan the classes we configured.
Add @ ServletComponentScan annotation to scan
Import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletComponentScan;@SpringBootApplication@ServletComponentScan ("com.uy.servlet") public class SpringBootApplication {public static void main (String [] args) {SpringApplication.run (SpringBootApplication.class, args);}}
(6) launch the SpringBoot project access address http://localhost:8080/druid/login.html to view and log in
These are the three ways of configuring Druid for SpringBoot shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.