您好,登錄后才能下訂單哦!
小編給大家分享一下springboot項(xiàng)目中如何實(shí)現(xiàn)訪問(wèn)druid內(nèi)置監(jiān)控頁(yè)面,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.9</version> </dependency>
# 數(shù)據(jù)庫(kù)訪問(wèn)配置 # 主數(shù)據(jù)源,默認(rèn)的 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 # 下面為連接池的補(bǔ)充設(shè)置,應(yīng)用到上面所有數(shù)據(jù)源中 # 初始化大小,最小,最大 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 # 配置獲取連接等待超時(shí)的時(shí)間 spring.datasource.maxWait=60000 # 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒 spring.datasource.timeBetweenEvictionRunsMillis=60000 # 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.validationQuery=SELECT 1 FROM DUAL spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false # 打開(kāi)PSCache,并且指定每個(gè)連接上PSCache的大小 spring.datasource.poolPreparedStatements=true spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 # 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無(wú)法統(tǒng)計(jì),'wall'用于防火墻 spring.datasource.filters=stat,wall,log4j # 通過(guò)connectProperties屬性來(lái)打開(kāi)mergeSql功能;慢SQL記錄 spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 合并多個(gè)DruidDataSource的監(jiān)控?cái)?shù)據(jù) #spring.datasource.useGlobalDataSourceStat=true
訪問(wèn)頁(yè)面http://127.0.0.1:8001/druid/sql.html,就可以訪問(wèn)監(jiān)控頁(yè)面了,其中ip和端口號(hào)為項(xiàng)目的ip和端口號(hào)。
頁(yè)面效果如下:
只需要在springboot啟動(dòng)類(lèi)中加入@bean配置即可:
@Bean public ServletRegistrationBean<StatViewServlet> druidStatViewServlet() { ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*"); registrationBean.addInitParameter("allow", "127.0.0.1");// IP白名單 (沒(méi)有配置或者為空,則允許所有訪問(wèn)) registrationBean.addInitParameter("deny", "");// IP黑名單 (存在共同時(shí),deny優(yōu)先于allow) registrationBean.addInitParameter("loginUsername", "root"); registrationBean.addInitParameter("loginPassword", "1234"); registrationBean.addInitParameter("resetEnable", "false"); return registrationBean; }
Druid是由阿里巴巴團(tuán)隊(duì)開(kāi)發(fā)的,能夠提供強(qiáng)大的監(jiān)控和擴(kuò)展功能的數(shù)據(jù)庫(kù)連接池。github地址
本項(xiàng)目的項(xiàng)目框架為SpringBoot+Mybatis+Security,并將druid到項(xiàng)目中,作為一個(gè)輔助工具幫助提升項(xiàng)目的性能。
1、設(shè)置項(xiàng)目依賴
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency>
2、application.yml中配置Druid數(shù)據(jù)源,并設(shè)置監(jiān)控參數(shù)。YAML文件格式化校驗(yàn)(https://old.qqe2.com/jsontool/yaml.php)
spring: #####DruidDataSource配置##################### type: com.alibaba.druid.pool.DruidDataSource initialSize: 5 minIdle: 5 maxActive: 20 # 配置獲取連接等待超時(shí)的時(shí)間 maxWait: 60000 # 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false # 打開(kāi)PSCache,并且指定每個(gè)連接上PSCache的大小 poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 # 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無(wú)法統(tǒng)計(jì),'wall'用于防火墻 filters: stat,wall,slf4j # 通過(guò)connectProperties屬性來(lái)打開(kāi)mergeSql功能;慢SQL記錄 connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 #合并多個(gè)DruidDataSource的監(jiān)控?cái)?shù)據(jù) useGlobalDataSourceStat: true
3、設(shè)置druid訪問(wèn)頁(yè)面的用戶名和密碼,只需要在springboot啟動(dòng)類(lèi)中加入@bean配置即可:
/** * @description 注冊(cè)一個(gè)StatViewServlet,進(jìn)行druid監(jiān)控頁(yè)面配置 * @return servlet registration bean */ @Bean public ServletRegistrationBean druidStatViewServlet() { //先配置管理后臺(tái)的servLet,訪問(wèn)的入口為/druid/ ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean( new StatViewServlet(), "/druid/*"); // IP白名單 (沒(méi)有配置或者為空,則允許所有訪問(wèn)) servletRegistrationBean.addInitParameter("allow", "127.0.0.1"); // IP黑名單 (存在共同時(shí),deny優(yōu)先于allow) servletRegistrationBean.addInitParameter("deny", ""); servletRegistrationBean.addInitParameter("loginUsername", "admin"); servletRegistrationBean.addInitParameter("loginPassword", "sdb3309"); servletRegistrationBean.addInitParameter("resetEnable", "false"); return servletRegistrationBean; }
4、注冊(cè)一個(gè)過(guò)濾器,允許Druid監(jiān)控頁(yè)面的正常瀏覽
/** * @description 注冊(cè)一個(gè)過(guò)濾器,允許頁(yè)面正常瀏覽 * @return filter registration bean */ @Bean public FilterRegistrationBean druidStatFilter(){ FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean( new WebStatFilter()); // 添加過(guò)濾規(guī)則. filterRegistrationBean.addUrlPatterns("/*"); // 添加不需要忽略的格式信息. filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; }
5、啟動(dòng)項(xiàng)目后,訪問(wèn)頁(yè)面http://ip:port/druid/sql.html(其中ip和端口號(hào)為項(xiàng)目的ip和端口號(hào)),使用配置的用戶名與密碼登錄,就可以訪問(wèn)監(jiān)控頁(yè)面了。
開(kāi)啟慢sql監(jiān)控
SQL監(jiān)控中,有一項(xiàng)關(guān)于sql執(zhí)行最慢執(zhí)行時(shí)間的統(tǒng)計(jì),但是只有一個(gè)值,就是一條sql語(yǔ)句最慢的執(zhí)行時(shí)間記錄,其他執(zhí)行時(shí)間是看不到的,只能通過(guò)總時(shí)間來(lái)進(jìn)行粗略的估計(jì);一旦項(xiàng)目重啟,這些記錄就全部消失,因此需要制定相應(yīng)的日志輸出策略。
具體步驟:
1、配置慢sql及日志攔截器
/** * @description 配置慢sql攔截器 * @return */ @Bean(name = "statFilter") public StatFilter statFilter(){ StatFilter statFilter = new StatFilter(); //慢sql時(shí)間設(shè)置,即執(zhí)行時(shí)間大于200毫秒的都是慢sql statFilter.setSlowSqlMillis(30); statFilter.setLogSlowSql(true); statFilter.setMergeSql(true); return statFilter; } /** * @description 配置日志攔截器 * @return */ @Bean(name = "logFilter") public Slf4jLogFilter logFilter(){ Slf4jLogFilter slf4jLogFilter = new Slf4jLogFilter(); slf4jLogFilter.setDataSourceLogEnabled(true); slf4jLogFilter.setStatementExecutableSqlLogEnable(true); return slf4jLogFilter; }
2、修改application.yml,增加慢sql日志的輸出策略
logging: pattern: #配置日志格式 %d:日期 , %msg:日志信息 ,%n換行 console: "%d - %msg%n" #設(shè)置控制臺(tái)打印格式 file: "%d{yyyy/MM/dd-HH:mm} %-5level %logger- %msg%n" level: root: INFO org: springframework: security: ERROR web: ERROR hhu.yu: INFO file: name: YU.log max-history: 30
3、修改數(shù)據(jù)源配置,設(shè)置慢sql攔截器和sql攔截器
@Bean @Primary @Qualifier("mainDataSource") @ConfigurationProperties(prefix = "spring.datasource.main") DataSource mainConfig() throws SQLException{ DruidDataSource build = DruidDataSourceBuilder.create().build(); List<Filter> filters = new ArrayList<>(); filters.add(statFilter()); filters.add(logFilter()); build.setProxyFilters(filters); return build;
重啟系統(tǒng)后查看druid監(jiān)控后臺(tái),由于設(shè)置了慢sql的時(shí)間為大于200毫秒,所以執(zhí)行時(shí)間大于200毫秒的都會(huì)被紅色標(biāo)注。查看日志文件,可以看到日志文件中存在慢sql記錄的數(shù)據(jù)(包括慢sql以及這條sql語(yǔ)句執(zhí)行的時(shí)間)
開(kāi)啟Spring監(jiān)控
在監(jiān)控面板中開(kāi)啟spring監(jiān)控功能
@Configuration @EnableAspectJAutoProxy(proxyTargetClass = true) public class DruidAspectConfig { @Bean public DruidStatInterceptor druidStatInterceptor() { DruidStatInterceptor dsInterceptor = new DruidStatInterceptor(); return dsInterceptor; } @Bean @Scope("prototype") public JdkRegexpMethodPointcut druidStatPointcut() { JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut(); pointcut.setPatterns("com.qa.cloud.mapper.*","com.qa.cloud.service.*"); return pointcut; } @Bean public DefaultPointcutAdvisor druidStatAdvisor(DruidStatInterceptor druidStatInterceptor, JdkRegexpMethodPointcut druidStatPointcut) { DefaultPointcutAdvisor defaultPointAdvisor = new DefaultPointcutAdvisor(); defaultPointAdvisor.setPointcut(druidStatPointcut); defaultPointAdvisor.setAdvice(druidStatInterceptor); return defaultPointAdvisor; }
以上是“springboot項(xiàng)目中如何實(shí)現(xiàn)訪問(wèn)druid內(nèi)置監(jiān)控頁(yè)面”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。