springboot配置druid数据源及druid的日志监控

代码如下:

  1. pom.xml中导入对应的jar包

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

        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.17version>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>
    dependencies>
  1. 切换数据源;Spring Boot 2.0 以上默认使用 com.zaxxer.hikari.HikariDataSource 数据源,可以通过 spring.datasource.type 指定数据源
spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver
#
    type: com.alibaba.druid.pool.DruidDataSource

    #Spring Boot 默认是不注入这些属性值的,需要自己绑定
    #druid 数据源专有配置
    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,stat:监控统计、log4j:日志记录、wall:防御sql注入
    #如果允许时报错  java.lang.ClassNotFoundException: org.apache.log4j.Priority
    #则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
  1. 由于spring默认不是注入druid配置属性,所以编写配置类手动绑定DruidDataSource 全局配置文件中的参数,日志监控也需要配置访问路径等,代码如下:
@Configuration
public class MyConfig {
//-----------------------------------绑定配置文件-----------------------------------------
    @Bean//添加到spring容器中,只有在spring容器中,才能使用spring强大的功能,如:配置绑定
    @ConfigurationProperties(prefix = "spring.datasource")//绑定配置文件 spring,datasource开头的数据
    public DruidDataSource druidDataSource(){
        return new DruidDataSource();
    }
//--------------------------以下为 druid日志监控配置(死配置)-----------------------------------------
	 /**
     * StatViewServlet 的父类 ResourceServlet中存在配置参数
     * ServletRegistrationBean 的父类 DynamicRegistrationBean中存在设置初始化参数的方法
     * @return
     */
    @Bean 
    public ServletRegistrationBean statViewServlet(){//控制器注册bean  传入 统计视图控制器、访问路径
        ServletRegistrationBean<Servlet> servletRegistrationBean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
           
        Map<String, String> initParameters = new HashMap<String,String>();
        initParameters.put("loginUsername","admin");
        initParameters.put("loginPassword","123123");
        //allow:允许哪个ip访问
        //deny:禁止哪个ip访问
        initParameters.put("allow","127.0.0.1"); 
        servletRegistrationBean.setInitParameters(initParameters);
        return servletRegistrationBean;
    }
}
  1. 启动服务 访问 http://localhost:8080/druid
    springboot配置druid数据源及druid的日志监控_第1张图片
    springboot配置druid数据源及druid的日志监控_第2张图片

你可能感兴趣的:(java)