SpringBoot集成Druid配置 SpringBoot集成Druid配置(yaml版本配置文件)详解
沐北ah 人气:2maven 配置
<!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--配置了日志log4j--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
application.yaml 配置
spring: datasource: username: 填自己的 url: jdbc:mysql://localhost:3306/填自己的数据库?useUnicode=true&characterEncoding=UTF-8&useSSL=false driver-class-name: com.mysql.cj.jdbc.Driver password: "填自己的" #数字注意一定要有""包围 eg:"123456" type: com.alibaba.druid.pool.DruidDataSource # 下面为连接池的补充设置,应用到上面所有数据源中 # 初始化大小,最小,最大 initial-size: 5 min-idle: 5 max-active: 20 # 配置获取连接等待超时的时间 max-wait: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 time-between-eviction-runs-millis: 60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 min-evictable-idle-time-millis: 300000 validation-query: SELECT 1 FROM DUAL test-while-idle: true test-on-borrow: false test-on-return: false # 打开PSCache,并且指定每个连接上PSCache的大小 pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 20 # 配置监控统计拦截的 Filter,去掉后监控界面 SQL 无法统计,wall 用于防火墙 日志 log4j filters: stat,wall,log4j #导入了log4j use-global-data-source-stat: true # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
自定义配置
自定义配置 过滤器以及后台管理
package spring.mubei.config; import com.alibaba.druid.pool.xa.DruidXADataSource; 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.util.HashMap; /** * @author mubei * @date 2020/12/5 * @description druid 自定义配置 */ @Configuration public class DruidConfigration { @ConfigurationProperties(prefix="spring.datasource") @Bean public DataSource druidDataSource(){ return new DruidXADataSource(); } //后台监控:web.xml ServletRegistrationBean //因为SpringBoot内置了 servlet容器 所以没有web.xml 替代方法ServletRegistrationBean @Bean public ServletRegistrationBean StatViewServlet(){ ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*"); //后台需要有人登录 账号密码配置 HashMap<String, String> initParameters = new HashMap<>(); //登录的两个key是固定的 initParameters.put("loginPassword","020323"); initParameters.put("loginUsername","mubei"); //允许谁可以访问 //空全部人可以访问 initParameters.put("allow",""); //禁止某人访问 initParameters.put("mubei","端口"); //设置初始化参数 bean.setInitParameters(initParameters); return bean; } //filter过滤器 @Bean public FilterRegistrationBean b(){ FilterRegistrationBean bean = new FilterRegistrationBean(); bean.setFilter(new WebStatFilter()); //可以过滤哪些请求 HashMap<String, String> initParameters = new HashMap<>(); //不过滤 initParameters.put("exclusion","*.js,*.css,/druid/**"); bean.setInitParameters(initParameters); return bean; } }
加载全部内容