springboot集成Druid

Druid是java平台上一个非常优秀的连接池,是阿里巴巴开源平台上的一个项目,整个项目由数据库连接池、插件框架和SQL解析器组成,能够提供强大的监控和扩展功能。
创建maven项目并引入依赖

<parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.4.1.RELEASEversion>
    parent>

    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.0.26version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <optional>trueoptional>
        dependency>
    dependencies>

创建application.properties配置数据源相关信息

#数据库设置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
#--------------------------
# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒 
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小 
spring.datasource.poolPreparedStatements=true
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

配置Filter

@WebFilter(filterName = "druidWebStatFilter", urlPatterns = "/*", initParams = {
        @WebInitParam(name = "exclusions", value = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略资源拦截
})
public class DruidStatFilter extends WebStatFilter {

}

配置Servlet

@WebServlet(urlPatterns = "/druid/*", initParams = { @WebInitParam(name = "allow", value = ""), // IP白名单(没有配置或者为空,则允许所有访问)
        @WebInitParam(name = "deny", value = ""), // IP黑名单 (存在共同时,deny优先于allow)
        @WebInitParam(name = "loginUsername", value = "admin"), // 用户名
        @WebInitParam(name = "loginPassword", value = "123"), // 密码
        @WebInitParam(name = "resetEnable", value = "false")// 禁用HTML页面上的“Reset
                                                            // All”功能
})
public class DruidStatViewServlet extends StatViewServlet {
    private static final long serialVersionUID = 1L;

}

因为spring boot集成druid无法设置druid datasource属性,所以要配置数据源,这里相关的参数会自动赋值到datasource里。

@Configuration //等价 与XML中配置beans
public class DruidDataSourceConfiguration {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource(){
        DataSource druidDataSource = new DruidDataSource();
        return druidDataSource;
    }
}

创建springboot启动类

@SpringBootApplication //集成@Configuration,@EnableAutoConfiguration,@ComponentScan三个注解
@ServletComponentScan //使的spring能够扫描到我们自己编写的servlet和filter。
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

启动项目,访问 localhost:8080/druid,输入账号:admin,密码:123(DruidStatViewServlet 里可自定义配置),即可查看druid提供的监控拦截等信息。

你可能感兴趣的:(springboot,springboot,druid,数据库连接池,监控)