SpringBoot-Druid数据源使用配置(三)

·  阅读 92

SpringBoot-Druid连接池的使用配置(三)

1.导入外部数据源

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.6</version>
        </dependency>
复制代码

添加类型type: com.alibaba.druid.pool.DruidDataSource

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
复制代码

运行测试,确认使用了Druid

image-20210508201308402

2.配置Druid

目前有两种方式!

方法一

导入starter依赖后直接启动应用,访问 http://localhost:8080/druid即可, 自动跳到index界面http://localhost:8080/druid/index.html

image-20210508202643426

依赖:

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
复制代码

这种方式,方便快捷,你只需要在application配置文件中写配置即可

提示:如果没有包,application.properties/application.yml 中将不会出现关于druid的提示

方法二

如果你导入的是Druid包

 	<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.6</version>
        </dependency>
复制代码

那你要编写配置类

用了这个注解@ConfigurationProperties(prefix = "spring.datasource")要导入以下依赖

	<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
复制代码

他会提示爆红!

它的意思是“Spring Boot配置注解执行器没有配置”,配置注解执行器的好处是什么。

配置注解执行器配置完成后,当执行类中已经定义了对象和该对象的字段后,在配置文件中对该类赋值时,便会非常方便的弹出提示信息。

创建配置类DruidConfig

package com.gip.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
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;

@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
public class DruidConfig {
    @Bean
    public DataSource druid(){
        return new DruidDataSource();
    }
    @Bean
    public ServletRegistrationBean druidServlet(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        bean.addInitParameter("allow","127.0.0.1");
        bean.addInitParameter("loginUsername","admin");
        bean.addInitParameter("loginPassword","123456");
        return bean;
    }

}

复制代码

这种方式需要我们手动的写上一个servlet以及filter,*注意:*这里必须要手动的注入一个DruidDataSource而且指定去读取文件,不然它是不会初始化数据源的 [这么个问题搞了半个多小时]

好了,现在druid的已经整合完毕啦。

3.遇到错误

image-20210508205729876

运行提示url没有设置?

这里就很奇怪,个人去看了看配置文件字段

没写错!路径

image-20210508205816572

问题在这!

image-20210508205956069

注解@ConfigurationProperties(prefix = "xxxx")位置标注错误,应该放在对应的bean上,而不是类上

对了记得导入log4j的依赖包

    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionoProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
复制代码

一定加入配置!!!!!!!!

不然监控不到sql

image-20210508214827915

image-20210508214837503

刚运行时候打开这个会显示这样

image-20210508214902274

这时候你去发个,或者走个请求连接就行,后面刷新就有显示了

image-20210508214938084

完整配置

package com.gip.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;

@Configuration

public class DruidConfig {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druid() {
        return new DruidDataSource();
    }

    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        bean.addInitParameter("allow", "");
//        bean.addInitParameter("loginUsername", "admin");
//        bean.addInitParameter("loginPassword", "123456");
        return bean;
    }

    //filter
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

}

复制代码
分类:
后端
标签: