spring-boot configuration integration druid

Reference
https://blog.csdn.net/zhaoyachao123/article/details/78413467

https://segmentfault.com/a/1190000013997259

Preparation: Druid Alibaba open performance, versatility connection pool, the main configuration parameters are as follows:

 

Configuration The default value Explanation
name   Configuring this attribute significance is that if multiple data sources exist to monitor when you can distinguish by area name. If no, will generate a name, the format is:. "DataSource-" + System.identityHashCode (this) This property is additionally arranged at least in version 1.0.5 is inactive, an error will be forcibly set name. Details - click here.
url   Url connection to the database, different databases are not the same. For example: MySQL: jdbc: MySQL: //127.0.0.1: 3306 / druid2
the Oracle: jdbc: the Oracle: Thin: @ 127.0.0.1: 1521: ocnauto
username   User name to connect the database
password   Password for the database connection. If you do not want to write code directly in the configuration file, you can use ConfigFilter. The details look here
driverClassName   The url can be equipped with an automatic recognition which may be unworthy, if not based on the configuration druid automatically identify dbType url, then select the appropriate driverClassName
initialSize 0 Establishing the number of physical connection initialization. Initialization occurs when the display call the init method, or the first getConnection
maxActive 8 The maximum number of connection pools
maxIdle 8 It is no longer used, equipped with no effect
minIdle   The minimum number of connection pool
maxWait   Acquires the maximum waiting time is connected, in milliseconds. After configuring maxWait, enabled by default fair locks, concurrent efficiency will decline, if necessary, by using the configuration useUnfairLock property to true unfair lock.
poolPreparedStatements false Whether the cache preparedStatement, which is PSCache. PSCache support database cursors huge performance boost, for example oracle. Under the proposed closure of mysql.
maxPoolPreparedStatementPerConnectionSize -1 To enable PSCache, must be greater than zero configuration, when greater than 0, poolPreparedStatements triggered automatically changed to true. In the Druid, the PSCache not exist too much memory for an Oracle issue, this value can be configured larger, such as 100
validationQuery   For detecting whether a valid connection sql, a query is required, common select 'x'. If validationQuery is null, testOnBorrow, testOnReturn, testWhileIdle will not work.
validationQueryTimeout   Unit: second detecting whether a valid connection timeout. void setQueryTimeout level calls jdbc Statement object (int seconds) Method
testOnBorrow true Execution validationQuery detect whether the connection is valid when applying for connection, made this configuration reduces performance
testOnReturn false Returned connection is valid execution validationQuery detect a connection, made this configuration can degrade performance.
testWhileIdle false Recommended configuration is true, does not affect performance, and ensure safety. Detecting when the application connection, if the idle time is greater than timeBetweenEvictionRunsMillis, performs detection validationQuery connection is valid.
keepAlive false (1.0.28)     连接池中的minIdle数量以内的连接,空闲时间超过minEvictableIdleTimeMillis,则会执行keepAlive操作。
timeBetweenEvictionRunsMillis 1分钟(1.0.14) 有两个含义:
1) Destroy线程会检测连接的间隔时间,如果连接空闲时间大于等于minEvictableIdleTimeMillis则关闭物理连接。
2) testWhileIdle的判断依据,详细看testWhileIdle属性的说明
numTestsPerEvictionRun 30分钟(1.0.14) 不再使用,一个DruidDataSource只支持一个EvictionRun
minEvictableIdleTimeMillis   连接保持空闲而不被驱逐的最小时间
connectionInitSqls   物理连接初始化的时候执行的sql
exceptionSorter 根据dbType自动识别 当数据库抛出一些不可恢复的异常时,抛弃连接
filters   属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有:
监控统计用的filter:stat
日志用的filter:log4j
防御sql注入的filter:wall
proxyFilters   类型是List<com.alibaba.druid.filter.Filter>,如果同时配置了filters和proxyFilters,是组合关系,并非替换关系

springboot 中整合druid 需要添加druid jar 这里spring-boot 的版本是1.5.7

添加druid maven 配置:


  
  
  1. <dependency>
  2. <groupId>com.alibaba </groupId>
  3. <artifactId>druid </artifactId>
  4. <version>1.1.5 </version>
  5. </dependency>
Class configuration as follows: wherein uses @Primary annotations , annotation used in the spring, often used @Autowired, default according to the type Type automatically injected. However, some special circumstances, for the same interface, there may be several different implementation class, and the default will only take one case @Primary role came out. Here is a simple example of the use.


  
  
  1. package com.zyc.config;
  2. import java.sql.SQLException;
  3. import javax.sql.DataSource;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  6. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.context.annotation.Primary;
  10. import com.alibaba.druid.pool.DruidDataSource;
  11. import com.alibaba.druid.support.http.StatViewServlet;
  12. import com.alibaba.druid.support.http.WebStatFilter;
  13. @Configuration
  14. public class DruidConfig {
  15. @Value( "${spring.datasource.url}")
  16. private String dbUrl;
  17. @Value( "${spring.datasource.username}")
  18. private String username;
  19. @Value( "${spring.datasource.password}")
  20. private String password;
  21. @Value( "${spring.datasource.driver-class-name}")
  22. private String driverClassName;
  23. @Value( "${spring.datasource.initialSize}")
  24. private int initialSize;
  25. @Value( "${spring.datasource.minIdle}")
  26. private int minIdle;
  27. @Value( "${spring.datasource.maxActive}")
  28. private int maxActive;
  29. @Value( "${spring.datasource.maxWait}")
  30. private int maxWait;
  31. @Value( "${spring.datasource.timeBetweenEvictionRunsMillis}")
  32. private int timeBetweenEvictionRunsMillis;
  33. @Value( "${spring.datasource.minEvictableIdleTimeMillis}")
  34. private int minEvictableIdleTimeMillis;
  35. @Value( "${spring.datasource.validationQuery}")
  36. private String validationQuery;
  37. @Value( "${spring.datasource.testWhileIdle}")
  38. private boolean testWhileIdle;
  39. @Value( "${spring.datasource.testOnBorrow}")
  40. private boolean testOnBorrow;
  41. @Value( "${spring.datasource.testOnReturn}")
  42. private boolean testOnReturn;
  43. @Value( "${spring.datasource.poolPreparedStatements}")
  44. private boolean poolPreparedStatements;
  45. @Value( "${spring.datasource.filters}")
  46. private String filters;
  47. @Value( "${spring.datasource.logSlowSql}")
  48. private String logSlowSql;
  49. @Bean
  50. @Primary
  51. public DataSource dataSource(){
  52. //@Primary 注解作用是当程序选择dataSource时选择被注解的这个
  53. DruidDataSource datasource = new DruidDataSource();
  54. datasource.setUrl(dbUrl);
  55. datasource.setUsername(username);
  56. datasource.setPassword(password);
  57. datasource.setDriverClassName(driverClassName);
  58. datasource.setInitialSize(initialSize);
  59. datasource.setMinIdle(minIdle);
  60. datasource.setMaxActive(maxActive);
  61. datasource.setMaxWait(maxWait);
  62. datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
  63. datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
  64. datasource.setValidationQuery(validationQuery);
  65. datasource.setTestWhileIdle(testWhileIdle);
  66. datasource.setTestOnBorrow(testOnBorrow);
  67. datasource.setTestOnReturn(testOnReturn);
  68. datasource.setPoolPreparedStatements(poolPreparedStatements);
  69. try {
  70. datasource.setFilters(filters);
  71. } catch (SQLException e) {
  72. // TODO Auto-generated catch block
  73. e.printStackTrace();
  74. }
  75. return datasource;
  76. }
  77. @Bean
  78. public ServletRegistrationBean druidServlet() {
  79. ServletRegistrationBean reg = new ServletRegistrationBean();
  80. reg.setServlet( new StatViewServlet());
  81. reg.addUrlMappings( "/druid/*");
  82. reg.addInitParameter( "loginUsername", username);
  83. reg.addInitParameter( "loginPassword", password);
  84. reg.addInitParameter( "logSlowSql", logSlowSql);
  85. return reg;
  86. }
  87. @Bean
  88. public FilterRegistrationBean filterRegistrationBean() {
  89. FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
  90. filterRegistrationBean.setFilter ( new WebStatFilter());
  91. filterRegistrationBean.addUrlPatterns( "/*");
  92. filterRegistrationBean.addInitParameter ( "exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
  93. filterRegistrationBean.addInitParameter ( "profileEnable", "true");
  94. return filterRegistrationBean;
  95. }
  96. }
application.properties configuration is as follows:


  
  
  1. spring.datasource.type = com.alibaba.druid.pool.DruidDataSource
  2. spring.datasource.url = jdbc:oracle:thin:@ 127.0.0.1/orcl
  3. spring.datasource.username = root
  4. spring.datasource.password = 123456
  5. spring.datasource.driver- class-name = oracle.jdbc.driver.OracleDriver
  6. spring.datasource.filters = stat,wall,log4j
  7. spring.datasource.maxActive = 20
  8. spring.datasource.initialSize = 1
  9. spring.datasource.maxWait = 60000
  10. spring.datasource.minIdle = 1
  11. spring.datasource.timeBetweenEvictionRunsMillis = 60000
  12. spring.datasource.minEvictableIdleTimeMillis = 300000
  13. spring.datasource.validationQuery = select 'x'
  14. spring.datasource.testWhileIdle = true
  15. spring.datasource.testOnBorrow = false
  16. spring.datasource.testOnReturn = false
  17. spring.datasource.poolPreparedStatements = true
  18. spring.datasource.maxOpenPreparedStatements = 20
  19. spring.datasource.logSlowSql= true
spring.datasource.type 是spring-boot 指定datasource类型的属性不是druid的属性,使用时需要注意,个别版本的spring-boot 不支持spring.datasource.type这个属性建议使用1.4.1版本以上的spring-boot

启动项目访问http://ip:port/druid/login.html

Guess you like

Origin blog.csdn.net/beyondxiaohu15/article/details/83347195