DataSource配置
一、JDBC
Jar依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
数据源的相关参数配置:DataSourceProperties
application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/arch1 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=123456
默认是用org.apache.tomcat.jdbc.pool.DataSource作为数据源
二、Druid DataSource
引入Jar依赖:
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency>
application-dev.properties
spring.datasource.url=jdbc:mysql://localhost:3306/arch1 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root spring.datasource.password=123456 ########################## druid配置 ########################## spring.datasource.type=com.alibaba.druid.pool.DruidDataSource # 初始化大小,最小,最大 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 # 配置获取连接等待超时的时间 spring.datasource.maxWait=60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 spring.datasource.timeBetweenEvictionRunsMillis=60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 spring.datasource.minEvictableIdleTimeMillis=300000 # 校验SQL,Oracle配置 spring.datasource.validationQuery=SELECT 1 FROM DUAL,如果不配validationQuery项,则下面三项配置无用 spring.datasource.validationQuery=SELECT 'x' spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false # 打开PSCache,并且指定每个连接上PSCache的大小 spring.datasource.poolPreparedStatements=true spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 spring.datasource.filters=stat,wall,log4j # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 合并多个DruidDataSource的监控数据 spring.datasource.useGlobalDataSourceStat=true
为了使spring.datasource.initialSize、spring.datasource.minIdle等属性生效,可以使用ConfigurationProperties(prefix="spring.datasource")引入
配置类DruidConfig
1 @Configuration 2 public class DruidConfig { 3 4 @Bean 5 @ConfigurationProperties(prefix="spring.datasource") 6 public DataSource druidDataSource(){ 7 return new DruidDataSource(); 8 } 9 10 @Bean 11 public ServletRegistrationBean startViewServlet(){ 12 ServletRegistrationBean druidServlet =new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); 13 14 Map<String,String> initParams =new HashMap<String,String>(); 15 initParams.put("loginUsername", "root"); 16 initParams.put("loginPassword", "root"); 17 18 initParams.put("allow","");//默认就是允许 19 initParams.put("deny","192.168.15.21"); 20 21 druidServlet.setInitParameters(initParams); 22 23 return druidServlet; 24 } 25 26 @Bean 27 public FilterRegistrationBean webStatFilter(){ 28 FilterRegistrationBean filter =new FilterRegistrationBean(); 29 filter.setFilter(new WebStatFilter()); 30 31 Map<String,String> initParams = new HashMap<>(); 32 initParams.put("exclusions","*.js,*.css,/druid/*"); 33 filter.setInitParameters(initParams); 34 filter.setUrlPatterns(Arrays.asList("/*")); 35 36 return filter; 37 } 38 39 40 41 }
监控测试: