zoukankan      html  css  js  c++  java
  • 第六章:(2)数据访问 之 整合 Druid 数据源

    一、整合 Druid 数据源

      1、导入依赖

            <!--  引入druid数据源  -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.8</version>
            </dependency>

      2、设置配置信息

    spring:
      datasource:
        username: root
        password: '123456'
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://192.168.1.6:3306/jdbc
        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

      3、测试

         这个时候已经使用的 Druid 数据源了。

        但是数据源的其他属性不能设置进去,因为创建的DataSource,只能设置与 DataSourceProperties 关联的属性,其他的属性不能设置到数据源中。

      4、实现自定义数据源

    @Configuration
    public class DruidConfig {
    
        @ConfigurationProperties(prefix = "spring.datasource")
        @Bean
        public DataSource druid() {
            return new DruidDataSource();
        }
    }

        创建一个自定义的数据源,并且绑定了配置文件中以 spring.datasource 的属性,这样属性就会被设置到配置的 druidDataSource 数据源中。

            

    二、使用 Druid 数据源监控

      1、配置Druid数据源及监控

    @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<String, String> initParams = new HashMap<>();
            initParams.put("loginUsername", "admin");
            initParams.put("loginPassword", "123456");
            initParams.put("allow", "");//默认就是允许所有访问
            initParams.put("deny", "192.168.15.21");
            bean.setInitParameters(initParams);
    
            return bean;
        }
    
        //2、配置一个web监控的filter
        @Bean
        public FilterRegistrationBean webStatFilter(){
            FilterRegistrationBean bean = new FilterRegistrationBean();
            bean.setFilter(new WebStatFilter());
            Map<String,String> initParams = new HashMap<>();
            //排除的请求
            initParams.put("exclusions","*.js,*.css,/druid/*");
    
            bean.setInitParameters(initParams);
            bean.setUrlPatterns(Arrays.asList("/*"));
    
            return bean;
        }
    }

       Servlet 更多的配置信息可以参考:StatViewServlet类 和 ResourceServlet 类。

       Filter 更多的配置信息可以参考:WebStatFilter 类。

      2、测试

      在浏览器中项目的目录下打开 druid 监控平台:http://localhost:8080/druid/index.html

      

       通过设置的账号密码进行登录。

      

       

  • 相关阅读:
    leetcode_697. 数组的度
    645. 错误的集合
    leetcode_448. 找到所有数组中消失的数字
    leetcode_628. 三个数的最大乘积
    leetcode_414. 第三大的数
    leetcode_495. 提莫攻击
    leetcode_485. 最大连续1的个数
    在 Mac、Linux、Windows 下Go交叉编译
    Goland基本操作
    etcd搭建及基本使用
  • 原文地址:https://www.cnblogs.com/niujifei/p/15705696.html
Copyright © 2011-2022 走看看