SpringBoot整合druid數(shù)據(jù)源及添加Druid監(jiān)控頁面

不是不會,只是沒見過,代碼只是一種工具,首先要會用,應(yīng)用中使用druid連接池,并添加監(jiān)控

  • 1.首先引入druid坐標(biāo)
<dependency>
    <groupId>com.alibaba</groupId>
     <artifactId>druid</artifactId>
     <version>1.0.11</version>
</dependency>
  • 2.添加druid配置參數(shù)

參考:

數(shù)據(jù)庫連接池優(yōu)化配置(druid,dbcp,c3p0)

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

1:minEvictableIdleTimeMillis(最大空閑時間):默認(rèn)為30分鐘,配置里面不進(jìn)行設(shè)置。

2:testOnBorrow ,testOnReturn 默認(rèn)為關(guān)閉,可以設(shè)置為不配置。

3:testWhileIdle(在獲取連接后,確定是否要進(jìn)行連接空閑時間的檢查)。默認(rèn)為true。配置里面不再進(jìn)行設(shè)置。

  • 流程說明:

1:在第一次調(diào)用connection的時候,才會進(jìn)行 initialSize的初始化。

2:心跳檢測時間線程,會休眠timeBetweenEvictionRunsMillis時間,然后只對(沒有borrow的線程 減去 minIdle)的線程進(jìn)行檢查,如果空閑時間大于minEvictableIdleTimeMillis則進(jìn)行close。

3:testWhileIdle必須設(shè)置為true,在獲取到連接后,先檢查testOnBorrow,然后再判定testwhileIdle,如果連接空閑時間大于timeBetweenEvictionRunsMillis,則會進(jìn)行心跳檢測。

4:不需要配置validationQuery,如果不配置的情況下會走ping命令,性能更高。

5:連接保存在數(shù)組里面,獲取連接的時候,獲取數(shù)組的最后一位。在imeBetweenEvictionRunsMillis時是從前往后進(jìn)行檢查連接的有效性。

在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開頭的參數(shù)

driver-class有和driverClass是不一樣的,所以要引入,參數(shù)容錯坐標(biāo)

  <!--配置命名容錯處理-->
        <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;//最大連接數(shù)
    private int minIdle;//最小連接數(shù)
    private int initialSize;//初始化數(shù)量和
    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性能監(jiān)控
/**
 * @Package: pterosaur.account.config.druid
 * @Description: 監(jiān)控數(shù)據(jù)庫性能
 * @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優(yōu)先于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監(jiān)控請求
 * @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);
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容