SpringBoot整合Druid數(shù)據(jù)庫(kù)連接池的方法
一,Druid是什么?
Druid是Java語(yǔ)言中最好的數(shù)據(jù)庫(kù)連接池。Druid能夠提供強(qiáng)大的監(jiān)控和擴(kuò)展功能。
二, 在哪里下載druid
maven中央倉(cāng)庫(kù): http://central.maven.org/maven2/com/alibaba/druid/
三, 怎么獲取Druid的源碼
Druid是一個(gè)開(kāi)源項(xiàng)目,源碼托管在github上,源代碼倉(cāng)庫(kù)地址是 https://github.com/alibaba/druid。同時(shí)每次Druid發(fā)布正式版本和快照的時(shí)候,都會(huì)把源碼打包,你可以從上面的下載地址中找到相關(guān)版本的源碼
SpringBoot整合Druid數(shù)據(jù)庫(kù)連接池的方法。
項(xiàng)目配置
pom.xml
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!--自啟動(dòng)Druid管理后臺(tái)--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency>
application.yml
server: port: 8080 spring: datasource: username: root password: root url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 25 filters: stat,wall,slf4j connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 useGlobalDataSourceStat: true cache: type: redis redis: host: 127.0.0.1 port: 6379 password: pool: max-active: 100 max-idle: 10 max-wait: 100000 lettuce: shutdown-timeout: 0 timeout: 5000 database: 0 thymeleaf: cache: false; mybatis: mapper-locations: classpath:zhw.example.zhw.loginModule.loginDao/*.xml
配置JdbcConfig
package zhw.example.zhw.loginModule.config;
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.Collections;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class JdbcConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource dataSource(){
return new DruidDataSource();
}
/**
* 配置Druid監(jiān)控
*
* @return StatViewServlet
*/
@Bean
public ServletRegistrationBean servletRegistrationBean() {
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
Map<String, String> map = new HashMap<>();
//訪問(wèn)的用戶名密碼
map.put(StatViewServlet.PARAM_NAME_USERNAME, "root");
map.put(StatViewServlet.PARAM_NAME_PASSWORD, "root");
//允許訪問(wèn)的ip,默認(rèn)是所有ip
map.put(StatViewServlet.PARAM_NAME_ALLOW, "");
//禁止訪問(wèn)的ip
map.put(StatViewServlet.PARAM_NAME_DENY, "192.168.1.1");
bean.setInitParameters(map);
return bean;
}
/**
* 配置一個(gè)監(jiān)控的filter
*
* @return WebStatFilter
*/
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean<WebStatFilter> bean = new FilterRegistrationBean<>();
bean.setFilter(new WebStatFilter());
Map<String, String> map = new HashMap<>();
//移除這些監(jiān)聽(tīng)
map.put(WebStatFilter.PARAM_NAME_EXCLUSIONS, "*.js,*.css,/druid/*,*.gif,*.jpg,*.png");
bean.setInitParameters(map);
//攔截所有請(qǐng)求,全部都要走druid監(jiān)聽(tīng)
bean.setUrlPatterns(Collections.singletonList("/*"));
return bean;
}
}
測(cè)試配置url白名單
如果工程中配置了Apache Shiro,需要在配置類中添加白名單

監(jiān)控界面

到此這篇關(guān)于SpringBoot整合Druid數(shù)據(jù)庫(kù)連接池的方法的文章就介紹到這了,更多相關(guān)SpringBoot整合Druid數(shù)據(jù)庫(kù)連接池內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot中Druid連接池與多數(shù)據(jù)源切換的方法
- SpringBoot整合mybatis使用Druid做連接池的方式
- Springboot中加入druid連接池
- springboot2.0配置連接池(hikari、druid)的方法
- SpringBoot整合Druid實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接池和監(jiān)控
- springboot項(xiàng)目整合druid數(shù)據(jù)庫(kù)連接池的實(shí)現(xiàn)
- springboot集成druid連接池配置的方法
- springboot整合druid連接池的步驟
- SpringBoot使用 druid 連接池來(lái)優(yōu)化分頁(yè)語(yǔ)句
- 解決Spring Boot中Druid連接池“discard long time none received connection“警告
相關(guān)文章
java8中的Collectors.groupingBy用法詳解
這篇文章主要介紹了java8中的Collectors.groupingBy用法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
JavaEE開(kāi)發(fā)基于Eclipse的環(huán)境搭建以及Maven Web App的創(chuàng)建
本文主要介紹了如何在Eclipse中創(chuàng)建的Maven Project,本文是JavaEE開(kāi)發(fā)的開(kāi)篇,也是基礎(chǔ)。下面內(nèi)容主要包括了JDK1.8的安裝、JavaEE版本的Eclipse的安裝、Maven的安裝、Tomcat 9.0的配置、Eclipse上的M2Eclipse插件以及STS插件的安裝。2017-03-03
java+socket實(shí)現(xiàn)簡(jiǎn)易局域網(wǎng)聊天室
這篇文章主要為大家詳細(xì)介紹了java+socket實(shí)現(xiàn)簡(jiǎn)易局域網(wǎng)聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Spring的@Scheduled 如何動(dòng)態(tài)更新cron表達(dá)式
這篇文章主要介紹了Spring的@Scheduled 如何動(dòng)態(tài)更新cron表達(dá)式的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
idea編譯報(bào)錯(cuò)-代碼沒(méi)問(wèn)題IDEA編譯不通過(guò)的處理方案
這篇文章主要介紹了idea編譯報(bào)錯(cuò)-代碼沒(méi)問(wèn)題IDEA編譯不通過(guò)的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Java?jar打包成exe應(yīng)用程序的詳細(xì)步驟
本文主要介紹了Java?jar打包成exe應(yīng)用程序的詳細(xì)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
SpringCloud 2020-Ribbon負(fù)載均衡服務(wù)調(diào)用的實(shí)現(xiàn)
這篇文章主要介紹了SpringCloud 2020-Ribbon負(fù)載均衡服務(wù)調(diào)用的實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03

