springboot 整合Druid

Druid连接池监控配置

1) 引入依赖

 <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>

2) 添加配置项

  datasource:
    url: jdbc:mysql://127.0.0.1:3306/mq?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: kkk
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxAwait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIldle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    filters:
      commons-log.connection-logger-name: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

3) 加入java 配置来读取Druid配置

package com.jd.ng.shiro.config.druidConfig;

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;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: husToy.Wang
 * @Date: 2019/7/26 9:49
 * @Version 1.0
 */
@Configuration
public class DruidPoolConfig {

    /**
     * 配置数据库连接池
     *
     * @return
     */
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource dataSource() {
        return new DruidDataSource();
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean2() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        Map<String, String> params = new HashMap<>();
        params.put("allow", "");
        params.put("loginUsername", "admin");
        params.put("loginPassword", "admin");
        // 设置初始化参数
        servletRegistrationBean.setInitParameters(params);
        return servletRegistrationBean;
    }


    @Bean
    public FilterRegistrationBean filterRegistrationBean2() {
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());
        Map<String,String> params = new HashMap<>();
        params.put("exclusions","*.js,*.css,*,/druid/*");
        bean.setInitParameters(params);
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }

}

4) 测试数据源

package com.jd.ng.shiro.redis;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@RunWith(SpringRunner.class)
@SpringBootTest
public class NgShiroRedisApplicationTests {


    @Autowired
    DataSource dataSource;

    @Autowired
    RedisTemplate redisTemplate;

    private static final Logger logger = LoggerFactory.getLogger(NgShiroRedisApplicationTests.class);

    @Test
    public void testDruidPool() throws SQLException {

        Connection connection = dataSource.getConnection();

        System.out.println(dataSource);


    }

}

5) 浏览器查看监控

http://test.jd.com:8083/druid/sql.html
posted @ 2019-07-26 13:35  雷神约  阅读(134)  评论(0编辑  收藏  举报