springboot 2.0.5配置Druid连接池和web监控后台
生活随笔
收集整理的這篇文章主要介紹了
springboot 2.0.5配置Druid连接池和web监控后台
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
配置類:?
import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; 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 javax.sql.DataSource; import java.util.Arrays; import java.util.HashMap; import java.util.Map;/*** @ProjectName driver-service* @ClassName DruidConfig* @Desicription TODO* @Author Zhang Xueliang* @Date 2019/11/4 15:34* @Version 1.0**/ @Configurationpublic class DruidConfig {@ConfigurationProperties(prefix = "spring.datasource")@Beanpublic DataSource druid(){return new DruidDataSource();}/*** druid管理后臺servlet* @return*/@Beanpublic ServletRegistrationBean statViewServlet(){ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");Map<String,String> initParams = new HashMap<>();initParams.put("loginUsername","admin");initParams.put("loginPassword","fengyun2018");initParams.put("allow","");//為空表示允許所有訪問servletRegistrationBean.setInitParameters(initParams);return servletRegistrationBean;}/*** 配置druid web監控filter* @return*/@Beanpublic FilterRegistrationBean webStatFilter(){FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();filterRegistrationBean.setFilter(new WebStatFilter());Map<String,String> initParams = new HashMap<>();initParams.put("exclusions","*.js,*.css,/druid/*");filterRegistrationBean.setInitParameters(initParams);filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));return filterRegistrationBean;}}配置文件:?
spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driver# 連接池的配置信息# 初始化大小,最小,最大initialSize: 5minIdle: 5maxActive: 50# 配置獲取連接等待超時的時間maxWait: 60000# 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒timeBetweenEvictionRunsMillis: 60000# 配置一個連接在池中最小生存的時間,單位是毫秒minEvictableIdleTimeMillis: 300000validationQuery: SELECT 1 FROM DUALtestWhileIdle: truetestOnBorrow: falsetestOnReturn: false# 打開PSCache,并且指定每個連接上PSCache的大小poolPreparedStatements: truemaxPoolPreparedStatementPerConnectionSize: 20# 配置監控統計攔截的filters,去掉后監控界面sql無法統計,'wall'用于防火墻filters: stat,wall,logback# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000pom依賴:
<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.3</version></dependency>?
總結
以上是生活随笔為你收集整理的springboot 2.0.5配置Druid连接池和web监控后台的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springboot支持三种嵌入式ser
- 下一篇: spring boot日志框架体系剖析(