SpringBoot整合druid數據源及添加Druid監控頁面

不是不會,只是沒見過,代碼只是一種工具,首先要會用,應用中使用druid鏈接池,並添加監控css

  • 1.首先引入druid座標
<dependency>
    <groupId>com.alibaba</groupId>
     <artifactId>druid</artifactId>
     <version>1.0.11</version>
</dependency>
  • 2.添加druid配置參數

參考:spring

數據庫鏈接池優化配置(druid,dbcp,c3p0)sql

參數 默認值 解釋
initialSize 3 初始化配置
minIdle 3 最小鏈接數
maxActive 15 最大鏈接數
maxWait 5000 獲取鏈接超時時間(單位:ms)
timeBetweenEvictionRunsMillis 90000 鏈接有效性檢測時間(單位:ms)
testOnBorrow false 獲取鏈接檢測
testOnReturn false 歸還鏈接檢測
minEvictableIdleTimeMillis 1800000 最大空閒時間(單位ms)
testWhileIdle true 在獲取鏈接後,肯定是否要進行鏈接空間時間的檢查
  • 配置說明:

1:minEvictableIdleTimeMillis(最大空閒時間):默認爲30分鐘,配置裏面不進行設置。數據庫

2:testOnBorrow ,testOnReturn 默認爲關閉,能夠設置爲不配置。數組

3:testWhileIdle(在獲取鏈接後,肯定是否要進行鏈接空閒時間的檢查)。默認爲true。配置裏面再也不進行設置。app

  • 流程說明:

1:在第一次調用connection的時候,纔會進行 initialSize的初始化。ide

2:心跳檢測時間線程,會休眠timeBetweenEvictionRunsMillis時間,而後只對(沒有borrow的線程 減去 minIdle)的線程進行檢查,若是空閒時間大於minEvictableIdleTimeMillis則進行close。spring-boot

3:testWhileIdle必須設置爲true,在獲取到鏈接後,先檢查testOnBorrow,而後再斷定testwhileIdle,若是鏈接空閒時間大於timeBetweenEvictionRunsMillis,則會進行心跳檢測。工具

4:不須要配置validationQuery,若是不配置的狀況下會走ping命令,性能更高。post

5:鏈接保存在數組裏面,獲取鏈接的時候,獲取數組的最後一位。在imeBetweenEvictionRunsMillis時是從前日後進行檢查鏈接的有效性。

在applicatioin.properties中添加配置

druid.url=jdbc:postgresql://139.1X8.1.1X8:1XX0/account
druid.driver-class=org.postgresql.Driver
druid.username=root
druid.password=123
druid.initial-size=1
druid.min-idle=1
druid.max-active=20
druid.test-on-borrow=true
druid.timeBetweenEvictionRunsMillis=9000
  • 3.定義配置類,啓動讀取druid開頭的參數

driver-class有和driverClass是不同的,因此要引入,參數容錯座標

<!--配置命名容錯處理-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
@ConfigurationProperties(prefix = "druid")
public class DruidProperties {
    private String url;
    private String username;
    private String password;
    private String driverClass;
    private int maxActive;//最大鏈接數
    private int minIdle;//最小鏈接數
    private int initialSize;//初始化數量和
    private boolean testOnBorrow;
    private Long timeBetweenEvictionRunsMillis;//心跳
}
  • 4.注入
@Configuration
@EnableConfigurationProperties(DruidProperties.class)
@ConditionalOnClass(DruidDataSource.class)
@ConditionalOnProperty(prefix = "druid", name = "url")
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
public class DruidAutoConfiguration {

    @Autowired
    private DruidProperties properties;

    @Bean
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(properties.getUrl());
        dataSource.setUsername(properties.getUsername());
        dataSource.setPassword(properties.getPassword());
        dataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
        if (properties.getInitialSize() > 0) {
            dataSource.setInitialSize(properties.getInitialSize());
        }
        if (properties.getMinIdle() > 0) {
            dataSource.setMinIdle(properties.getMinIdle());
        }
        if (properties.getMaxActive() > 0) {
            dataSource.setMaxActive(properties.getMaxActive());
        }
        dataSource.setTestOnBorrow(properties.isTestOnBorrow());
        try {
            dataSource.init();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return dataSource;
    }
}
  • 5.添加攔截器,攔截器druid性能監控
/**
 * @Package: pterosaur.account.config.druid
 * @Description: 監控數據庫性能
 * @author: liuxin
 * @date: 17/4/21 上午11:23
 */
@SuppressWarnings("serial")
@WebServlet(urlPatterns = "/druid/*",
        initParams={
                @WebInitParam(name="allow",value="192.168.16.110,127.0.0.1"),// IP白名單 (沒有配置或者爲空,則容許全部訪問)
                @WebInitParam(name="deny",value="192.168.16.111"),// IP黑名單 (存在共同時,deny優先於allow)
                @WebInitParam(name="loginUsername",value="test"),// 用戶名
                @WebInitParam(name="loginPassword",value="test"),// 密碼
                @WebInitParam(name="resetEnable",value="false")// 禁用HTML頁面上的「Reset All」功能
        })
public class DruidStatViewServlet extends StatViewServlet{
}


/**
 * @Package: pterosaur.account.config.filter
 * @Description: 攔截druid監控請求
 * @author: liuxin
 * @date: 17/4/21 上午11:24
 */
@WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",
        initParams={
                @WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略資源
        })
public class DruidStatFilter extends WebStatFilter{
}
  • 6.最終要一步,啓動掃描Servlet
@SpringBootApplication
@MapperScan(basePackages = "pterosaur.account.mapper")
@EnableCaching
@ServletComponentScan  //這個
public class AccountApplication {

    public static void main(String[] args) {
        SpringApplication.run(AccountApplication.class, args);
    }
}
相關文章
相關標籤/搜索