springboot專案整合druid,配置監控中心
阿新 • • 發佈:2019-01-29
druid算是經常用的資料連線池了,廢話不多說;
1,依賴(gradle)
// https://mvnrepository.com/artifact/com.alibaba/druid compile group: 'com.alibaba', name: 'druid', version: '1.1.6'
2,application.properties配置檔案
# Tomcat server.tomcat.max-threads=1000 server.tomcat.min-spare-threads=30 server.port=8081 ### mysql spring.datasource.type=com.alibaba.druid.pool.DruidDataSource3,druid配置類注意:配置類的位置一定要在啟動類包貨啟動類包的子包中才可以spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url= jdbc:mysql://127.0.0.1:3306/tacxdemo?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8 spring.datasource.username=root spring.datasource.password=root ## druid配置 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 spring.datasource.poolPreparedStatements=true spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 spring.datasource.filters=stat,wall,log4j spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 #JPA配置 spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update ##亂碼【配置 http: encoding: charset: UTF-8 force: true enabled: true
import com.alibaba.druid.pool.DruidDataSource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; /** * Created by EAJA on 2018/5/10 * 自動讀取 application.properties檔案中以spring.datasource開頭的資訊 * 將DataSource物件的實現類變為了DruidDataSource物件。 */ @Configuration public class DruidConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource druidDataSource() { System.out.println("druid datasource");//重啟檢視資料來源的載入 return new DruidDataSource(); } } |
4,監控中心的配置 4.1 過濾規則見註釋
import com.alibaba.druid.support.http.WebStatFilter; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; /** * Created by EAJA on 2018/5/10 * 配置druid過濾規則:監控所有的頁面,排除以下配置的檔案 */ @WebFilter(filterName="druidWebStatFilter",urlPatterns="/*", initParams={ @WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略資源 }) public class DruidStatFilter extends WebStatFilter { } |
4.2,配置登入賬號的白名單
import com.alibaba.druid.support.http.StatViewServlet; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; @WebServlet(urlPatterns = "/druid/*", initParams={ @WebInitParam(name="allow",value="192.168.1.20,127.0.0.1"),// IP白名單 (沒有配置或者為空,則允許所有訪問) @WebInitParam(name="deny",value="192.168.16.111"),// IP黑名單 (存在共同時,deny優先於allow) @WebInitParam(name="loginUsername",value="admin"),// 使用者名稱 @WebInitParam(name="loginPassword",value="admin"),// 密碼 @WebInitParam(name="resetEnable",value="false")// 禁用HTML頁面上的“Reset All”功能 }) public class DruidStatViewServlet extends StatViewServlet { private static final long serialVersionUID = 1L; }
4.3,別忘在啟動的main那添加註解
@SpringBootApplication
@ServletComponentScan //配置druid必須加的註解。filter和servlet、listener需要單獨進行註冊才能使用,spring boot裡面提供了該註解起到註冊作用
@EnableTransactionManagement //開啟事務
public class Application {
4.4,spring配置監控此時druid只能監控sql等,但是spring沒法監控。還需要配置切面
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 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 monitor setting --> <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor" /> <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype"> <property name="patterns"> <list> <value>com.www.demo.service.*</value> <value>com.www.demo.dao.*</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> |
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; /** * Created by EAJA on 2018/5/10 * 配置Spring監控 */ @Configuration @ImportResource(locations={"classpath:application-bean.xml"}) public class DruidStatInterceptor { } |
至此,所有的配置就完成;在你的sql監控介面登入進去會看到你的sql操作就恭喜你配置成功了。
有問題歡迎留言