3 Spring Boot 使用Druid连接池

Spring Boot 使用Druid连接池

      • druid功能
      • druid配置
        • druid Filter配置
        • druid 密码加密
        • druid SQL防注入
      • 案例
        • 配置文件
        • 完整代码

druid功能

详细的监控;ExceptionSorter,针对主流数据库的返回码都有支持
SQL防注入;内置加密配置;
纵多扩展点,方便定制

druid配置

  • 直接配置DruidDataSource
    通过代码形式写入各种属性

  • 通过druid-spring-boot-starter,项目通过Maven依赖jar包
    配置前缀 spring.datasource.druid.*

druid Filter配置

定制连接池操作的各种环节;可以继承FilterEventAdapter以便方便实现Filter;
META-INF/druid-filter.properties增加Filter配置

  • spring.datasource.druid.filters = stat,config,wall,log4j

druid 密码加密

  • spring.datasource.password = <加密密码>
  • spring.datasource.druid.filter.config.enabled = true
  • spring.datasource.druid.connection-properties = config.decrypt=true; config.decrypt.key=

druid SQL防注入

  • spring.datasource.druid.filter.wall.enabled = true
  • spring.datasource.druid.filter.wall.db-type = h2
  • spring.datasource.druid.filter.wall.config.delete-allow=false
  • spring.datasource.druid.filter.wall.config.drop-table-allow=false

案例

配置文件

  • META-INF/druid-filter.properties ,这里仅仅在建立连接时进行日志输出
//继承FilterEventAdapter以便方便实现Filter
druid.filters.conn= com.example.druid.ConnectionLogFilter
  • application.properties
spring.output.ansi.enabled=ALWAYS

spring.datasource.url=jdbc:h2:mem:foo
spring.datasource.username=sa
spring.datasource.password=n/z7PyA5cvcXvs8px8FVmBVpaRyNsvJb3X7YfS38DJrIg25EbZaZGvH4aHcnc97Om0islpCAPc3MqsGvsrxVJw==

spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.filters=conn,config,stat,slf4j

spring.datasource.druid.connection-properties=config.decrypt=true;config.decrypt.key=${public-key}
spring.datasource.druid.filter.config.enabled=true

spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.test-on-return=true
spring.datasource.druid.test-while-idle=true

public-key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALS8ng1XvgHrdOgm4pxrnUdt3sXtu/E8My9KzX8sXlz+mXRZQCop7NVQLne25pXHtZoDYuMh3bzoGj6v5HvvAQ8CAwEAAQ==

完整代码

  • ConnectionLogFilter.java
package com.example.druid;

import com.alibaba.druid.filter.FilterChain;
import com.alibaba.druid.filter.FilterEventAdapter;
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
import lombok.extern.slf4j.Slf4j;

import java.util.Properties;

@Slf4j
public class ConnectionLogFilter extends FilterEventAdapter {
    @Override
    public void connection_connectBefore(FilterChain chain, Properties info) {
       log.info("BEFORE CONNECTION !");
    }

    @Override
    public void connection_connectAfter(ConnectionProxy connection) {
        log.info("AFTER CONNECTION !");
    }
}

  • DruidApplication.java
package com.example.druid;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@SpringBootApplication
@Slf4j
public class DruidApplication implements CommandLineRunner {

	@Autowired
	private DataSource dataSource;
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public static void main(String[] args) {
		SpringApplication.run(DruidApplication.class, args);
	}

	@Override
	public void run(String... args) throws Exception {
		log.info(dataSource.toString());
	}
}

输出
3 Spring Boot 使用Druid连接池_第1张图片
本文是通过学习极客时间丁雪丰老师的 spring全家桶,做的学习笔记,有错误的地方,请网友提出,大家共同学习,后续陆续更新!扫描下方二维码,可以共同学习。

3 Spring Boot 使用Druid连接池_第2张图片

你可能感兴趣的:(#spring)