mysqldatasource.url_DataSource配置
⼀、JDBC
Jar依赖:
org.springframework.boot
spring-boot-starter-jdbc
mysql
mysql-connector-java
数据源的相关参数配置:DataSourceProperties
application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/arch1
spring.datasource.sql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
默认是⽤at.jdbc.pool.DataSource作为数据源
⼆、Druid DataSource
引⼊Jar依赖:
com.alibaba
druid
1.1.10
application-dev.properties
spring.datasource.url=jdbc:mysql://localhost:3306/arch1
spring.datasource.sql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456>>>>># druid配置 >>>>>#
pe=com.alibaba.druid.pool.DruidDataSource
# 初始化⼤⼩,最⼩,最⼤
spring.datasource.initialSize=5spring.datasource.minIdle=5spring.datasource.maxActive=20# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000# 配置间隔多久才进⾏⼀次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000# 配置⼀个连接在池中最⼩⽣存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000# 校验SQL,Oracle配置 spring.datasource.validationQuery=SELECT 1FROM DUAL,如果不配validationQuery项,则下⾯三项配置⽆⽤
spring.datasource.validationQuery=SELECT
'x'stWhileIdle=stOnBorrow=stOnReturn=false# 打开PSCache,并且指定每个连接上PSCache的⼤⼩
spring.datasource.poolPreparedStatements=truespring.datasource.maxPoolPreparedStatementPerConnectionSize=20# 配置监控统计拦截的filters,去掉后监控界⾯sql⽆法统计,'wall'⽤于防⽕墙
spring.datasource.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
tionProperties=Sql=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 classDruidConfig {3
4 @Bean
5 @ConfigurationProperties(prefix="spring.datasource")
6 publicDataSource druidDataSource(){
7 return newDruidDataSource();
8 }9
10 @Bean11 publicServletRegistrationBean startViewServlet(){12 ServletRegistrationBean druidServlet =new ServletRegistrationBean(new StatViewServlet(),"/druid/*");13
14 Map initParams =new HashMap();15 initParams.put("loginUsername", "root");16 initParams.put("loginPassword", "root");17
18 initParams.put("allow","");//默认就是允许
mysql下载后为啥localhost打不开19 initParams.put("deny","192.168.15.21");20
21 druidServlet.setInitParameters(initParams);22
23 returndruidServlet;24 }25
26 @Bean27 publicFilterRegistrationBean webStatFilter(){28 FilterRegistrationBean filter =newFilterRegistrationBean();29 filter.setFilter(newWebStatFilter());30
31 Map initParams = new HashMap<>();32 initParams.put("exclusions","*.js,*.css,/druid/*");33
filter.setInitParameters(initParams);34 filter.setUrlPatterns(Arrays.asList("/*"));35
36 returnfilter;37 }38
39
40
41 }
监控测试:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论