1. 程式人生 > >SpringBoot 2.1.1.RELEASE 整合Druid

SpringBoot 2.1.1.RELEASE 整合Druid

SpringBoot 2.1.1.RELEASE 整合Druid
詳情:
http://www.qchcloud.cn/system/article/show/68
配置依賴:

mysql mysql-connector-java com.alibaba druid 1.1.4 配置applicaton.properties spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver spring.datasource.url = jdbc:mysql://localhost:3306/iot?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC spring.datasource.username = root spring.datasource.password = 1234 ### log4j配置 logging.config=classpath:log4j2.xml ### 資料來源類別 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 spring.datasource.validationQuery=SELECT 1 FROM DUAL 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 配置監控: @Configuration public class DruidConfiguration { @Bean public ServletRegistrationBean druidStatViewServle(){ //ServletRegistrationBean提供類的進行註冊. ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); //新增初始化引數:initParams //白名單: servletRegistrationBean.addInitParameter("allow","127.0.0.1"); //IP黑名單 (存在共同時,deny優先於allow) // 如果滿足deny的話提示:Sorry, you are not permitted to view this page. servletRegistrationBean.addInitParameter("deny","192.168.1.7"); //登入檢視資訊的賬號和密碼. servletRegistrationBean.addInitParameter("loginUsername","admin"); servletRegistrationBean.addInitParameter("loginPassword","123456"); //是否能夠重置資料. servletRegistrationBean.addInitParameter("resetEnable","false"); return servletRegistrationBean; } @Bean public FilterRegistrationBean druidStatFilter(){ FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); //新增過濾規則. filterRegistrationBean.addUrlPatterns("/*"); //新增不需要忽略的格式資訊. filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; } }