资讯专栏INFORMATION COLUMN

SpringBoot使用Druid数据源

netmou / 2174人阅读

摘要:结构导包如果运行出错还需要导入数据源其他配置配置监控统计拦截的,去掉后监控界面无法统计,用于防火墙配置的监控配置一个管理后台的账号密码默认允许所有配置一个监控的打开输入账号密码,成功

结构

导包

    com.alibaba
    druid
    1.1.16


如果运行出错还需要导入

 
     log4j
     log4j
     1.2.17
 
 
application.yml
spring:
  datasource:
    username: blog
    password: 123456
    url: jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
    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
    #   配置监控统计拦截的filters,去掉后监控界面sql无法统计,"wall"用于防火墙
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
    
    
DruidConfig
@Configuration
public class DruidConfig {
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid(){
        return new DruidDataSource();
    }
    //配置Druid的监控
    //1. 配置一个管理后台的servlet
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean bean =  new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        Map initParams = new HashMap<>();

        initParams.put("loginUsername","admin");//账号
        initParams.put("loginPassword","12345");//密码
        initParams.put("allow","");//默认允许所有
        initParams.put("deny","192.168.123.22");

        bean.setInitParameters(initParams);
        return bean;
    }

    // 2. 配置一个监控的filter
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());

        Map initParams = new HashMap<>();
        initParams.put("exclusions","*.js,*.css,/druid/*");

        bean.setInitParameters(initParams);
        bean.setUrlPatterns(Arrays.asList("/*"));

        return bean;
    }
}

打开 http://localhost:8080/druid

输入账号密码,

成功

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/75324.html

相关文章

  • 单手撸了个springboot+mybatis+druid

    摘要:配置想想,我们需要哪些数据库要用到,数据库连接池要用到桥接器要用到,因此要仓库点我去仓库中找到搜索这些加进去。 本文旨在用最通俗的语言讲述最枯燥的基本知识 最近身边的程序员掀起了学习springboot的热潮,说什么学会了springboot在大街上就可以横着走、什么有了springboot妈妈再也不担心我的编程了、什么BAT都喜欢的框架...听得作者那个心痒痒的,于是找了个时间,下载...

    adie 评论0 收藏0
  • springboot系列】springboot整合独立模块Druid + mybatis-plus

    摘要:申请连接时执行检测连接是否有效,做了这个配置会降低性能。作者在版本中使用,通过监控界面发现有缓存命中率记录,该应该是支持。允许和不允许单条语句返回多个数据集取决于驱动需求使用列标签代替列名称。需要驱动器支持。将自动映射所有复杂的结果。 项目github地址:https://github.com/5-Ason/aso... 具体可看 ./db/db-mysql 模块 本文主要实现的是对...

    RobinTang 评论0 收藏0
  • springboot使用druid连接池,并配置据源监控

    摘要:首先肯定是引入的依赖然后将配置到项目中在中加入数据源的其他配置配置监控统计拦截的,去掉后监控界面无法统计,用于防火墙这里配置好之后,数据源是从默认的切换到了,可是的属性比如还未生效,因为在下没有对应的匹配项,所以还需要我们手动配置新建将所有 首先肯定是引入druid的依赖: com.alibaba druid 1.1.10 然后将druid配置到项目中: 在...

    MSchumi 评论0 收藏0

发表评论

0条评论

netmou

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<