微服务数据库连接池监控:paascloud-master中Druid配置详解

微服务数据库连接池监控:paascloud-master中Druid配置详解

【免费下载链接】paascloud-master spring cloud + vue + oAuth2.0全家桶实战,前后端分离模拟商城,完整的购物流程、后端运营平台,可以实现快速搭建企业级微服务项目。支持微信登录等三方登录。 【免费下载链接】paascloud-master 项目地址: https://gitcode.com/gh_mirrors/pa/paascloud-master

你是否还在为微服务数据库连接池的性能问题烦恼?连接泄露、资源耗尽、监控盲区是否让你焦头烂额?本文将详细介绍如何在paascloud-master项目中配置Druid连接池并实现全方位监控,读完你将掌握:

  • Druid连接池在微服务中的配置方法
  • 监控面板的启用与安全控制
  • 连接池性能优化的关键参数
  • 实战案例与问题排查技巧

项目结构与 Druid 模块定位

paascloud-master作为Spring Cloud微服务架构,数据库连接池配置主要分布在服务提供者模块。典型的数据源配置位于各provider子项目的application.yml中,如:

核心配置类通常集中在公共模块,如:

基础数据源配置

YAML 配置示例

在各服务提供者的application.yml中,基础数据源配置结构如下:

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/paascloud_uac?useUnicode=true&characterEncoding=utf-8&useSSL=false
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      # 初始化连接池大小
      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

关键参数说明

参数名含义推荐值风险提示
initial-size初始化连接数5-10过大导致启动慢
max-active最大连接数20-50超过数据库最大连接数会抛异常
max-wait获取连接超时时间60000ms过短易触发超时异常
time-between-eviction-runs-millis连接检测周期60000ms过小影响性能

监控面板配置与启用

配置监控 Servlet

在配置类中添加Druid监控Servlet(通常位于公共配置模块):

@Configuration
public class DruidConfig {
    @Bean
    public ServletRegistrationBean<StatViewServlet> druidStatViewServlet() {
        ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>();
        registrationBean.setServlet(new StatViewServlet());
        registrationBean.addUrlMappings("/druid/*");
        
        // 监控面板登录账号密码
        Map<String, String> initParameters = new HashMap<>();
        initParameters.put("loginUsername", "admin");
        initParameters.put("loginPassword", "admin123");
        // 允许访问的IP,默认所有
        initParameters.put("allow", "");
        registrationBean.setInitParameters(initParameters);
        return registrationBean;
    }
}

配置 WebStatFilter

添加过滤器用于采集web-jdbc关联监控数据:

@Bean
public FilterRegistrationBean<WebStatFilter> druidWebStatFilter() {
    FilterRegistrationBean<WebStatFilter> registrationBean = new FilterRegistrationBean<>();
    registrationBean.setFilter(new WebStatFilter());
    // 过滤所有请求
    registrationBean.addUrlPatterns("/*");
    // 排除静态资源
    registrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
    return registrationBean;
}

访问监控面板

启动服务后,通过以下地址访问监控面板: http://服务IP:端口/druid/index.html

例如用户权限服务的监控地址:http://localhost:8000/druid/index.html

性能监控与优化

关键监控指标

监控面板主要关注以下指标:

  • 数据源页面:查看连接池当前状态
  • SQL监控:慢SQL记录与执行频率分析
  • 访问URL监控:Web应用与数据库交互统计
  • Spring监控:Spring Bean与方法调用统计

优化建议

  1. 连接池大小调整: 根据服务并发量调整max-active,通常设置为 CPU核心数 * 2 + 有效磁盘数

  2. 超时设置

    druid:
      # 获取连接超时时间
      max-wait: 60000
      # 连接超时回收时间
      min-evictable-idle-time-millis: 300000
      # 检测空闲连接的间隔时间
      time-between-eviction-runs-millis: 60000
    
  3. SQL防火墙启用

    druid:
      filters: stat,wall
    

安全控制最佳实践

IP访问限制

通过allow参数配置允许访问监控面板的IP段:

// 只允许内网IP访问
initParameters.put("allow", "192.168.1.,127.0.0.1");
// 禁止特定IP
initParameters.put("deny", "10.0.0.1");

密码加密存储

生产环境中避免明文存储密码,使用Druid提供的加密功能:

  1. 生成加密密码:

    java -cp druid-1.2.8.jar com.alibaba.druid.filter.config.ConfigTools your_password
    
  2. 配置加密参数:

    spring:
      datasource:
        password: 加密后的密码
        druid:
          connection-properties: config.decrypt=true;config.decrypt.key=公钥
          filters: config
    

常见问题排查

连接泄露排查

当监控面板显示removeAbandonedCount持续增长时,表示存在连接泄露。可通过以下配置开启泄露检测:

druid:
  remove-abandoned: true
  remove-abandoned-timeout: 300
  log-abandoned: true

慢SQL优化

在SQL监控页面,点击"执行时间"排序,重点优化执行时间长的SQL:

  1. 添加合适索引
  2. 优化JOIN查询
  3. 分页查询优化

总结与扩展

通过本文配置,可实现微服务数据库连接池的全方位监控与管理。生产环境建议:

  1. 所有服务统一配置Druid监控
  2. 集成Prometheus+Grafana实现监控数据持久化
  3. 配置告警机制(如连接数过高、慢SQL频发)

更多高级配置可参考:

通过合理配置与持续监控,可显著提升微服务架构的数据库访问性能与稳定性。

【免费下载链接】paascloud-master spring cloud + vue + oAuth2.0全家桶实战,前后端分离模拟商城,完整的购物流程、后端运营平台,可以实现快速搭建企业级微服务项目。支持微信登录等三方登录。 【免费下载链接】paascloud-master 项目地址: https://gitcode.com/gh_mirrors/pa/paascloud-master

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值