springboot 整合Druid 连接池并开启监控GUI (多数据源)

本文介绍了如何在SpringBoot项目中整合Druid连接池,并实现多数据源配置以及开启监控GUI。通过添加相关Maven依赖、配置YML文件、Javaconfig配置类,详细展示了每个数据源的配置细节,包括连接池参数和监控设置。最后,启动项目后,可以通过指定URL访问Druid的监控页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot 整合Druid 链接池并开启监控GUI

场景:单项目多数据源情况下的配置
1.导入相关Maven依赖

		<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.20</version>
        </dependency>      

2.添加druid配置yml

spring:
  datasource:
    primary:
      url: 
      username: 
      password: 
      driver-class-name: 
      type: com.alibaba.druid.pool.DruidDataSource
      initialSize: 5 # 初始化大小,最小,最大
      maxActive: 100
      maxWait: 60000 # 配置获取连接等待超时的时间
      timeBetweenEvictionRunsMillis: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      minEvictableIdleTimeMillis: 300000 # 配置一个连接在池中最小生存的时间,单位是毫秒
      connection-timeout: 90000
      #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
      filters: stat,wall
      maxPoolPreparedStatementPerConnectionSize: 20
      useGlobalDataSourceStat: true
      connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
    secondary:
      url: 
      username: 
      password: 
      driver-class-name: com.ibm.db2.jcc.DB2Driver
      initialSize: 5 # 初始化大小,最小,最大
      maxActive: 100
      maxWait: 60000 # 配置获取连接等待超时的时间
      timeBetweenEvictionRunsMillis: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      minEvictableIdleTimeMillis: 300000 # 配置一个连接在池中最小生存的时间,单位是毫秒
      connection-timeout: 90000
      #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
      filters: stat,wall
      maxPoolPreparedStatementPerConnectionSize: 20
      useGlobalDataSourceStat: true
      connectionProperties: 

3.Java config 配置类(多个数据源就是多个)

@Configuration
@MapperScan(basePackages = "xxxx", sqlSessionFactoryRef = "secondarySqlSessionFactory")
public class SecondaryDataSourceConfig {

    @Bean(name = "secondaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public DataSource getSecondaryDateSource() {
        return DataSourceBuilder.create().type(DruidDataSource.class).build();
    }

    /**
     * 配置Druid的监控
     * 配置一个监控管理后台的Servlet
     * @return
     */
    @Bean
    public ServletRegistrationBean druidStatViewServlet(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");

        Map<String,String> initParam = new HashMap<String, String>();
        //允许登录的用户名
        initParam.put("loginUsername","admin");
        //允许登录的密码
        initParam.put("loginPassword","admin");
        //允许哪些ip地址访问,不写就是默认允许所有访问
        initParam.put("allow","");
        //拒绝访问ip名单
        //initParam.put("deny","192.169.1.1");
        //添加初始化参数
        bean.setInitParameters(initParam);
        return bean;
    }

    /**
     * 配置一个web监控的filter
     * @return
     */
    @Bean
    public FilterRegistrationBean druidWebStatFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());
        //拦截哪些请求
        bean.setUrlPatterns(Arrays.asList("/*"));

        Map<String,String> initParam = new HashMap<String, String>();
        //排除拦截哪些请求
        initParam.put("exclusions","*.js,*.css,/druid/*");
        bean.setInitParameters(initParam);
        return bean;
    }

    @Bean(name = "secondarySqlSessionFactory")
    public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/secondary/*.xml"));
        return bean.getObject();
    }
    @Bean("secondarySqlSessionTemplate")
    public SqlSessionTemplate secondarySqlSessionTemplate(
            @Qualifier("secondarySqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }

}

4.启动项目访问 http:ip:端口/druid 打开监控页面
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一名软件修理工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

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

抵扣说明:

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

余额充值