栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

16-SpringBoot 整合Druid数据源

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

16-SpringBoot 整合Druid数据源

Druid是数据库连接池,它能够提供强大的监控和扩展功能。官方文档

Spring Boot整合第三方技术的两种方式:

  • 自定义
  • 找starter场景
1.自定义方式

使用自定义方式整合Druid

1.添加依赖

在pom.xml添加相关依赖

  
        
            org.springframework.boot
            spring-boot-starter-data-jdbc
        
        
            mysql
            mysql-connector-java
            5.1.6
        
        
            com.alibaba
            druid
            1.2.6
        
    
2.编写配置

application.properties

server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/db_springtest?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

配置Druid的监控页功能:

  • Druid内置提供了一个StatViewServlet用于展示Druid的统计信息。官方文档 - 配置StatViewServlet
    这个StatViewServlet的用途包括:
    • 提供监控信息展示的html页面
    • 提供监控信息的JSON API
  • Druid内置提供一个StatFilter,用于统计监控信息。官方文档 - 配置_StatFilter
  • WebStatFilter用于采集web-jdbc关联监控的数据,如SQL监控、URI监控。官方文档 - 配置WebStatFilter
  • Druid提供了WallFilter,它是基于SQL语义分析来实现防御SQL注入攻击的。官方文档 - 配置 wallfilter

DataSourceConfig

package com.limi.springboottest2.config;


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;

@Configuration
public class DataSourceConfig {

    @Bean
    @ConfigurationProperties("spring.datasource")//复用配置文件的数据源配置, 这样就不用写代码一个一个set了
    public DataSource dataSource() throws SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();

        //加入监控和防火墙功能功能
        druidDataSource.setFilters("stat,wall");
        return druidDataSource;
    }

    
    @Bean
    public ServletRegistrationBean statViewServlet(){
        StatViewServlet statViewServlet = new StatViewServlet();
        ServletRegistrationBean registrationBean =
                new ServletRegistrationBean<>(statViewServlet, "/druid
    @Bean
    public FilterRegistrationBean webStatFilter(){
        WebStatFilter webStatFilter = new WebStatFilter();

        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean<>(webStatFilter);
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");

        return filterRegistrationBean;
    }
}

3.测试

HelloController

package com.limi.springboottest2.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @ResponseBody
    @GetMapping("/sql")
    public String test1(){
        String sql = "select count(*) from t_user";
        Long i = jdbcTemplate.queryForObject(sql, Long.class);
        return i.toString();
    }
}

多次点击刷新进行访问

登录druid后台查看监控信息
账号admin, 密码123456


2.starter方式(推荐)

使用场景启动器整合druid, 效果和自定义方式一样, 但更加方便

1.添加依赖

在pom.xml添加相关依赖

        
        
            org.springframework.boot
            spring-boot-starter-data-jdbc
        
        
            mysql
            mysql-connector-java
            5.1.6
        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.17
        

pom.xml完整内容如下



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.7.0
         
    
    com.limi
    springboot-test2
    0.0.1-SNAPSHOT
    springboot-test2
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        

        
        
            org.springframework.boot
            spring-boot-starter-data-jdbc
        
        
            mysql
            mysql-connector-java
            5.1.6
        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.17
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            

            
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.springframework.boot
                            spring-boot-configuration-processor
                        
                    
                
            
        
    



2.编写配置

application.properties

server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/db_springtest?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#监控SpringBean
spring.datasource.druid.aop-patterns=com.limi.springboottest2.*  
# 底层开启功能,stat(sql监控),wall(防火墙)
spring.datasource.druid.filters=stat,wall

# 配置监控页功能
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=123456
spring.datasource.druid.stat-view-servlet.reset-enable=false

# 监控web
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions='*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'

# 对上面filters里面的stat的详细配置
spring.datasource.druid.filter.stat.slow-sql-millis=1000
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.enabled=true
spring.datasource.druid.filter.wall.enabled=true
spring.datasource.druid.filter.wall.config.drop-table-allow=false
3.测试

HelloController

package com.limi.springboottest2.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @ResponseBody
    @GetMapping("/sql")
    public String test1(){
        String sql = "select count(*) from t_user";
        Long i = jdbcTemplate.queryForObject(sql, Long.class);
        return i.toString();
    }
}

多次点击刷新进行访问

登录druid后台查看监控信息
账号admin, 密码123456


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/987749.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号