一、添加依賴
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、網(wǎng)絡(luò)空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、克拉瑪依網(wǎng)站維護(hù)、網(wǎng)站推廣。
<!--mybatis-開始--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>MySQL</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId >druid</artifactId> <version>1.0.26</version> </dependency> <!--mybatis-結(jié)束-->
二、配置application.properties
##################################mysql數(shù)據(jù)源配置################################## spring.datasource.url=jdbc:mysql://localhost/db_test?useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver #druid連接池 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
注意:其實(shí)到這里就可以正常使用了,但是如果想使用druid里的諸多屬性,則需要添加支持類
三、添加druid的支持類
package com.example.demo.utils.configuration.druid; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DatabaseDriver; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.alibaba.druid.pool.DruidDataSource; /** * druid的支持類 * @Author: 我愛大金子 * @Description: druid的支持類 * @Date: Created in 12:48 2017/6/20 */ @Configuration public class DruidConfiguration { @ConditionalOnClass(DruidDataSource.class) @ConditionalOnProperty(name = "spring.datasource.type", havingValue = "com.alibaba.druid.pool.DruidDataSource", matchIfMissing = true) static class Druid extends DruidConfiguration { @Bean @ConfigurationProperties("spring.datasource.druid") public DruidDataSource dataSource(DataSourceProperties properties) { DruidDataSource druidDataSource = (DruidDataSource) properties.initializeDataSourceBuilder().type(DruidDataSource.class).build(); DatabaseDriver databaseDriver = DatabaseDriver.fromJdbcUrl(properties.determineUrl()); String validationQuery = databaseDriver.getValidationQuery(); if (validationQuery != null) { druidDataSource.setValidationQuery(validationQuery); } return druidDataSource; } } }
配置application.properties:
##################################mysql數(shù)據(jù)源配置################################## spring.datasource.url=jdbc:mysql://localhost/db_test?useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #初始化連接大小 spring.datasource.druid.initial-size=8 #最小空閑連接數(shù) spring.datasource.druid.min-idle=5 #最大連接數(shù) spring.datasource.druid.max-active=10 #查詢超時(shí)時(shí)間 spring.datasource.druid.query-timeout=6000 #事務(wù)查詢超時(shí)時(shí)間 spring.datasource.druid.transaction-query-timeout=6000 #關(guān)閉空閑連接超時(shí)時(shí)間 spring.datasource.druid.remove-abandoned-timeout=1800
四、監(jiān)控
4.1、配置servlet
package com.example.demo.utils.configuration.druid; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import com.alibaba.druid.support.http.StatViewServlet; /** * 監(jiān)控-配置servlet * @Author: 我愛大金子 * @Description: 監(jiān)控-servlet * @Date: Created in 12:50 2017/6/20 */ @WebServlet(urlPatterns = { "/druid/*" }, initParams = { @WebInitParam(name = "loginUsername", value = "liuy"), @WebInitParam(name = "loginPassword", value = "123456") }) public class DruidStatViewServlet extends StatViewServlet { private static final long serialVersionUID = 1L; }
4.2、配置filter
package com.example.demo.utils.configuration.druid; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; import com.alibaba.druid.support.http.WebStatFilter; /** * 監(jiān)控-配置filter * @Author: 我愛大金子 * @Description: 描述 * @Date: Created in 12:51 2017/6/20 */ @WebFilter(filterName = "druidWebStatFilter", urlPatterns = "/*", initParams = { @WebInitParam(name = "exclusions", value = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*") }) public class DruidWebStatFilter extends WebStatFilter { }
測(cè)試:訪問http://localhost:9090/druid/index.html
注意:此時(shí)還沒有SQL監(jiān)控與spring監(jiān)控。
4.3、配置sql監(jiān)控與spring監(jiān)控
在application.properties文件中配置sql監(jiān)控
#sql監(jiān)控配置:filter類名:stat,config,encoding,logging spring.datasource.druid.filters=stat
spring監(jiān)控配置
第一步:新建一個(gè)配置文件,如:druid-bean.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 配置_Druid和Spring關(guān)聯(lián)監(jiān)控配置 --> <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"></bean> <!-- 方法名正則匹配攔截配置 --> <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype"> <property name="patterns"> <list> <value>com.example.demo.mapper.*</value> </list> </property> </bean> <aop:config proxy-target-class="true"> <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" /> </aop:config> </beans>
第二步:在啟動(dòng)類上加載druid-bean.xml文件
package com.example.demo; import com.example.demo.utils.filter.CustomFilter; import com.example.demo.utils.listener.CustomListener; import com.example.demo.utils.servlet.CustomServlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.*; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ImportResource; import javax.servlet.DispatcherType; import javax.servlet.ServletContext; import javax.servlet.ServletException; import java.util.EnumSet; @ServletComponentScan @SpringBootApplication @ImportResource(locations = { "classpath:druid-bean.xml" }) public class SpringbootDemo27Application { public static void main(String[] args) { SpringApplication.run(SpringbootDemo27Application.class, args); } }
重啟服務(wù),報(bào)異常:
解決:添加aop依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
重啟服務(wù)
另一種實(shí)現(xiàn)方式
package com.liuy.demo.configuration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DatabaseDriver; 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 com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; @Configuration public class DruidConfiguration { @ConditionalOnClass(DruidDataSource.class) @ConditionalOnProperty(name = "spring.datasource.type", havingValue = "com.alibaba.druid.pool.DruidDataSource", matchIfMissing = true) static class Druid extends DruidConfiguration { @Bean @ConfigurationProperties("spring.datasource.druid") public DruidDataSource dataSource(DataSourceProperties properties) { DruidDataSource druidDataSource = (DruidDataSource)properties.initializeDataSourceBuilder().type(DruidDataSource.class).build(); DatabaseDriver databaseDriver = DatabaseDriver.fromJdbcUrl(properties.determineUrl()); String validationQuery = databaseDriver.getValidationQuery(); if (validationQuery != null) { druidDataSource.setValidationQuery(validationQuery); } return druidDataSource; } } @Bean public ServletRegistrationBean statViewServlet() { ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); // IP白名單 servletRegistrationBean.addInitParameter("allow", "127.0.0.1,192.168.1.83"); // IP黑名單(優(yōu)先級(jí)高于白名單) servletRegistrationBean.addInitParameter("deny", "192.168.1.100"); // 控制臺(tái)管理用戶 servletRegistrationBean.addInitParameter("loginUsername", "liuy"); servletRegistrationBean.addInitParameter("loginPassword", "123456"); // 是否能夠重置數(shù)據(jù) servletRegistrationBean.addInitParameter("resetEnable", "false"); return servletRegistrationBean; } @Bean public FilterRegistrationBean statFilter() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); // 添加過濾規(guī)則 filterRegistrationBean.addUrlPatterns("/*"); // 忽略過濾的格式 filterRegistrationBean.addInitParameter("exclusions", "*.js,*.css,*.jpg,*.ico,/druid/*"); return filterRegistrationBean; } }
application.properties:
spring.profiles.active=dev server.port=9090 ##################################mysql數(shù)據(jù)源配置################################## spring.datasource.url=jdbc:mysql://localhost/db_test?useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver # druid連接池 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource # 初始化大小,最小,最大 spring.datasource.druid.initial-size=5 spring.datasource.druid.minIdle=5 spring.datasource.druid.maxActive=20 # 獲取連接等待超時(shí)的時(shí)間(毫秒) spring.datasource.druid.max-wait=60000 # 間隔多久進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接(毫秒) spring.datasource.druid.time-between-eviction-runs-millis=60000 # 一個(gè)連接在池中的最小生存時(shí)間(毫秒) spring.datasource.druid.min-evictable-idle-time-millis=300000 spring.datasource.druid.validation-query=SELECT 1 FROM DUAL spring.datasource.druid.test-while-idle=true spring.datasource.druid.test-on-borrow=false spring.datasource.druid.test-on-return=false # 打開PSCache,并且指定每個(gè)連接上PSCache的大小 spring.datasource.druid.pool-prepared-statements=true spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20 # 查詢超時(shí)時(shí)間 spring.datasource.druid.query-timeout=6000 # 事務(wù)查詢超時(shí)時(shí)間 spring.datasource.druid.transaction-query-timeout=6000 # 關(guān)閉空閑連接超時(shí)時(shí)間 spring.datasource.druid.remove-abandoned-timeout=1800 # sql監(jiān)控配置:filter類名:stat,config,encoding,logging spring.datasource.druid.filters=stat # 通過connect-properties屬性來打開mergesql功能;慢查詢記錄 spring.datasource.druid.connect-properties.druid.stat.meergeSql=true; spring.datasource.druid.connect-properties.druid.stat.slowSqlMillis=5000 # 合并多個(gè)druidDataSource的監(jiān)控?cái)?shù)據(jù) #spring.datasource.druid.use-global-data-source-stat=true ##################################mybatis基于xml集成################################## #掃包 mybatis.mapper-locations: classpath:mybatis/*.xml #別名 mybatis.type-aliases-package: com.liuy.demo.pojo
文章題目:springBoot(12):集成Druid
文章URL:http://jinyejixie.com/article46/pshpeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、App設(shè)計(jì)、標(biāo)簽優(yōu)化、品牌網(wǎng)站設(shè)計(jì)、自適應(yīng)網(wǎng)站、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)