基于springboot2 框架整合(2):druid数据源整合

前言

项目中使用了很多现成的框架,都是项目经理、架构师带来的,从来没有自己整合过!这次决定自己从零开始整合一次,以学习巩固。过程中参考很多开源框架的思路,工具类等,若有侵权,请速速联系,一定妥善处理

一:简介

druid是alibaba开源的数据库连接池,号称为监控而生,github地址:https://github.com/alibaba/druid

二:依赖

在pom.xml中添加以下依赖

        <!--druid数据源-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>

三:配置

数据源配置一般要分开发环境和生产环境,我们先创建开发环境的application-dev.yml

application.yml内容如下

server:
  #端口
  port: 8080

spring:
  # 环境 dev|test|prod
  profiles:
    active: dev

application-dev.yml内容如下

spring:
  datasource:
    druid:
      driver-class-name: com.mysql.jdbc.Driver
      # 基本属性 url、user、password
      url: jdbc:mysql://192.168.33.33:3306/frame?useUnicode=true&characterEncoding=utf-8&useSSL=false
      username: root
      password: 123456
      # 配置初始化大小、最小、最大 (通常来说,只需要修改initialSize、minIdle、maxActive)
      initial-size: 1
      min-idle: 1
      max-active: 20
      # 配置获取连接等待超时的时间
      max-wait: 60000
      # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      time-between-eviction-runs-millis:  60000
      # 配置一个连接在池中最小生存的时间,单位是毫秒
      min-evictable-idle-time-millis: 300000
      validation-query: select 'x'
      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
      # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
      filters: stat,wall,slf4j
      # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
      connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      #useGlobalDataSourceStat: true # 合并多个DruidDataSource的监控数据

在创建core和modules目录

DruidConfig.java内容如下

package org.itachi.frame.core.config.db;

import org.springframework.context.annotation.Configuration;


/**
 * Druid 数据源配置。主要的已经自动配置,如有特殊配置,可能写会在这里
 *
 * @author itachi
 * @date 2018-10-05 13:09
 */
@Configuration
public class DruidConfig {

}

DruidStatController.java内容如下

package org.itachi.frame.core.config.db;

import com.alibaba.druid.stat.DruidStatManagerFacade;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Druid 数据源web监控。
 *
 * @author itachi
 * @date 2018-10-05 12:55
 */
@RestController
public class DruidStatController {

    @GetMapping(value = "/druid/stat")
    public Object druidStat() {
        // DruidStatManagerFacade#getDataSourceStatDataList 该方法可以获取所有数据源的监控数据,
        // 除此之外 DruidStatManagerFacade 还提供了一些其他方法,你可以按需选择使用。
        return DruidStatManagerFacade.getInstance().getDataSourceStatDataList();
    }
}

 四:测试

启动项目,可以看到,数据源已经ok了

访问监控页面:http://localhost:8080/druid/datasource.html

关于更多druid的更多配置请参见github:https://github.com/alibaba/druid

猜你喜欢

转载自my.oschina.net/u/2935623/blog/2222958