SpringBoot 2.0 整合 Druid 连接池

SpringBoot 2.0 整合 Druid 连接池

首先导入一个 Druid 和日志的 Maven 依赖

<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>druidartifactId>
    <version>1.1.9version>
dependency>

还要导入以下依赖, 否则会报下面的错, 因为 Druid 直接使用的 Log4j 作为日志系统中间没有使用日志中间层, SpringBoot2.0 使用的是 slf4j 作为统一的日志接口

<dependency>
    <groupId>org.slf4jgroupId>
    <artifactId>log4j-over-slf4jartifactId>
    <version>1.7.30version>
dependency>
***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'spring.datasource' to javax.sql.DataSource:

    Property: spring.datasource.filters
    Value: stat,wall,log4j
    Origin: class path resource [application.yml]:24:14
    Reason: org.apache.log4j.Logger

Action:

Update your application's configuration

定义数据源配置

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://10.0.0.11:3306/eesy
    driver-class-name: com.mysql.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

封装配置信息

@Configuration
public class DruidConfig {

    // 绑定配置文件中连接池配置, 并将配置实例化到 IOC 中
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid() {
        return new DruidDataSource();
    }

    // 配置 Druid 的监控
    @Bean
    public ServletRegistrationBean statViewServlet() {
        // 配置一个管理后台的 Servlet, 通过这个接口来访问 Druid 的监控
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        Map<String, String> init = new HashMap<>();
        // 配置登录 Druid 监控后台时的用户名
        init.put("loginUsername", "admin");
        // 配置登录 Druid 监控后台时的密码
        init.put("loginPassword", "admin");
        // 配置允许那些地址访问,当值为空时表示允许所有地址访问,值为 localhost 表示允许本机访问
        init.put("allow", "localhost");
        // 配置拒绝访问的地址
        init.put("deny", "10.0.0.11");
        // 添加初始化参数
        servletRegistrationBean.setInitParameters(init);

        return servletRegistrationBean;
    }

    // 配置一个 Web 监控的 Filter
    @Bean
    public FilterRegistrationBean webStatFilter() {
        FilterRegistrationBean filter = new FilterRegistrationBean();
        filter.setFilter(new WebStatFilter());
        Map<String, String> init = new HashMap<>();
        // 添加不拦截的请求
        init.put("exclusions", "*.js,*.css,/druid/");

        // 设置初始化参数
        filter.setInitParameters(init);
        // 添加拦截的请求
        filter.setUrlPatterns(Arrays.asList("/**"));
        return filter;
    }

}

你可能感兴趣的:(SpringBoot 2.0 整合 Druid 连接池)