For springboot data Sources please refer to my previous post: https://www.cnblogs.com/yueshutong/p/9409295.html
One: Druid Introduction 1. What is Druid?
Druid is the best database connection pool in the Java language. The Druid provides powerful monitoring and extension capabilities.
2. Where to download Druid
- Official version Download:
Maven Central Warehouse: Http://central.maven.org/maven2/com/alibaba/druid/
3. How to get Druid source code
Druid is an open source project, source is hosted on GitHub, source code warehouse address is https://github.com/alibaba/druid. At the same time each Druid release of the official version and snapshot, will be the source packaging, you can find the relevant version of the source code from the above
II: Springboot Integration
Start by importing dependencies on your Springboot project
1. Import dependencies
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version></dependency><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope></dependency>
2. Configure the data source and connection pool
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 type: com.alibaba.druid.pool.DruidDataSource# 数据源其他配置 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# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
3. Inject DataSource
@Configurationpublic class Druidconfig {@ConfigurationProperties (prefix = "Spring.datasource") @Bean public Data Source Druid () {return new Druiddatasource (); }//Configure Druid Monitoring//1, configure a management background servlet @Bean public Servletregistrationbean Statviewservlet () {//configuration parameter Reference R Esourceservlet class Servletregistrationbean Bean = new Servletregistrationbean (new Statviewservlet (), "/druid/*"); map<string, string> initparams = new hashmap<> (); Initparams.put ("Loginusername", "admin"); Initparams.put ("Loginpassword", "123456"); Initparams.put ("Allow", "");//default is to permit all access to Initparams.put ("Deny", "192.168.15.21");//Reject who Bean.setinitparameter S (initparams); return bean; }//2, configure a Web monitoring filter @Bean public Filterregistrationbean webstatfilter () {Filterregistrationbean Bean = New Filterregistrationbean (); Bean.setfilter (New Webstatfilter ()); map<string, string> initparams = new Hashmap<> (); Initparams.put ("Exclusions", "*.js,*.css,/druid/*"); Bean.setinitparameters (InitParams); Bean.seturlpatterns (Arrays.aslist ("/*")); return bean; }}
4. Launch the App
Visit Http://localhost:8080/druid to experience the powerful druid!
Springboot Consolidating Druid data sources