SpringBoot使用Druid数据源的配置方法

网友投稿 442 2023-02-07


SpringBoot使用Druid数据源的配置方法

Druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0、DBCPyJEDxY、PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB连接池(据说是目前最好的连接池)

一、依赖

为了测试,使用jdbcTemplate

org.springframework.boot

spring-boot-starter-jdbc

com.alibaba

druid

1.0.26

mysql

mysql-connector-java

runtime

二、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.testWhhttp://ileIdle=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> hello() {

List> 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数据源,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:vue组件实现弹出框点击显示隐藏效果
下一篇:父组件中vuex方法更新state子组件不能及时更新并渲染的完美解决方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~