您好,登錄后才能下訂單哦!
Druid是阿里巴巴開源平臺上一個數據庫連接池實現,它結合了C3P0、DBCP、PROXOOL等DB池的優點,同時加入了日志監控,可以很好的監控DB池連接和SQL的執行情況,可以說是針對監控而生的DB連接池(據說是目前最好的連接池)
一、依賴
為了測試,使用jdbcTemplate
<!-- jdbcTemplate --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- druid數據庫連接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.26</version> </dependency> <!-- mysql connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
二、Druid配置
druid.properties #數據庫設置 spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/uu_core?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=true spring.datasource.username=root spring.datasource.password=root #-------------------------- # 下面為連接池的補充設置,應用到上面所有數據源中 # 初始化大小,最小,最大 spring.datasource.initialSize=5 spring.datasource.minIdle=1 spring.datasource.maxActive=50 # 配置獲取連接等待超時的時間 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=false #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
三、實例化Druid Datasource
package cn.aduu.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 org.springframework.context.annotation.PropertySource; import javax.sql.DataSource; /** * @author zh * @ClassName cn.aduu.config.DruidConfiguration * @Description */ @Configuration @PropertySource(value = "classpath:druid.properties") public class DruidConfiguration { @Bean(destroyMethod = "close", initMethod = "init") @ConfigurationProperties(prefix = "spring.datasource") public DataSource druidDataSource() { DruidDataSource druidDataSource = new DruidDataSource(); return druidDataSource; } /** * 注冊一個StatViewServlet * @return */ @Bean public ServletRegistrationBean druidStatViewServlet(){ //org.springframework.boot.context.embedded.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.73"); //登錄查看信息的賬號密碼. servletRegistrationBean.addInitParameter("loginUsername","admin"); servletRegistrationBean.addInitParameter("loginPassword","123456"); //是否能夠重置數據. servletRegistrationBean.addInitParameter("resetEnable","false"); return servletRegistrationBean; } /** * 注冊一個:filterRegistrationBean * @return */ @Bean public FilterRegistrationBean druidStatFilter(){ FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); //添加過濾規則. filterRegistrationBean.addUrlPatterns("/*"); //添加不需要忽略的格式信息. filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; } }
四、監控
訪問 http://localhost:8080/druid, 使用上面配置的賬號密碼。
五、測試
@RestController public class HelloController{ private static final Logger logger = LoggerFactory.getLogger(HelloController.class); @Autowired private JdbcTemplate jdbcTemplate; @RequestMapping("hello") public List<Map<String, Object>> hello() { List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT user,password FROM mysql.user ", new Object[]{}); return list; } }
訪問 localhost:8080/hello
[ { "user": "root", "password": "*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B" }, { "user": "root", "password": "*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B" } ]
總結
以上所述是小編給大家介紹的SpringBoot使用Druid數據源,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。