不是不會(huì)鹃觉,只是沒(méi)見(jiàn)過(guò)厢钧,代碼只是一種工具,首先要會(huì)用掏呼,應(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ù)庫(kù)連接池優(yōu)化配置(druid,dbcp,c3p0)
參數(shù) | 默認(rèn)值 | 解釋 |
---|---|---|
initialSize | 3 | 初始化配置 |
minIdle | 3 | 最小連接數(shù) |
maxActive | 15 | 最大連接數(shù) |
maxWait | 5000 | 獲取連接超時(shí)時(shí)間(單位:ms) |
timeBetweenEvictionRunsMillis | 90000 | 連接有效性檢測(cè)時(shí)間(單位:ms) |
testOnBorrow | false | 獲取連接檢測(cè) |
testOnReturn | false | 歸還連接檢測(cè) |
minEvictableIdleTimeMillis | 1800000 | 最大空閑時(shí)間(單位ms) |
testWhileIdle | true | 在獲取連接后,確定是否要進(jìn)行連接空間時(shí)間的檢查 |
- 配置說(shuō)明:
1:minEvictableIdleTimeMillis(最大空閑時(shí)間):默認(rèn)為30分鐘憎夷,配置里面不進(jìn)行設(shè)置莽鸿。
2:testOnBorrow ,testOnReturn 默認(rèn)為關(guān)閉,可以設(shè)置為不配置拾给。
3:testWhileIdle(在獲取連接后祥得,確定是否要進(jìn)行連接空閑時(shí)間的檢查)。默認(rèn)為true蒋得。配置里面不再進(jìn)行設(shè)置级及。
- 流程說(shuō)明:
1:在第一次調(diào)用connection的時(shí)候,才會(huì)進(jìn)行 initialSize的初始化额衙。
2:心跳檢測(cè)時(shí)間線程饮焦,會(huì)休眠timeBetweenEvictionRunsMillis時(shí)間,然后只對(duì)(沒(méi)有borrow的線程 減去 minIdle)的線程進(jìn)行檢查窍侧,如果空閑時(shí)間大于minEvictableIdleTimeMillis則進(jìn)行close县踢。
3:testWhileIdle必須設(shè)置為true,在獲取到連接后伟件,先檢查testOnBorrow硼啤,然后再判定testwhileIdle,如果連接空閑時(shí)間大于timeBetweenEvictionRunsMillis斧账,則會(huì)進(jìn)行心跳檢測(cè)谴返。
4:不需要配置validationQuery,如果不配置的情況下會(huì)走ping命令咧织,性能更高嗓袱。
5:連接保存在數(shù)組里面,獲取連接的時(shí)候习绢,獲取數(shù)組的最后一位索抓。在imeBetweenEvictionRunsMillis時(shí)是從前往后進(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.定義配置類(lèi)毯炮,啟動(dòng)讀取druid開(kāi)頭的參數(shù)
driver-class有和driverClass是不一樣的逼肯,所以要引入,參數(shù)容錯(cuò)坐標(biāo)
<!--配置命名容錯(cuò)處理-->
<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)控?cái)?shù)據(jù)庫(kù)性能
* @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白名單 (沒(méi)有配置或者為空篮幢,則允許所有訪問(wèn))
@WebInitParam(name="deny",value="192.168.16.111"),// IP黑名單 (存在共同時(shí),deny優(yōu)先于allow)
@WebInitParam(name="loginUsername",value="test"),// 用戶(hù)名
@WebInitParam(name="loginPassword",value="test"),// 密碼
@WebInitParam(name="resetEnable",value="false")// 禁用HTML頁(yè)面上的“Reset All”功能
})
public class DruidStatViewServlet extends StatViewServlet{
}
/**
* @Package: pterosaur.account.config.filter
* @Description: 攔截druid監(jiān)控請(qǐng)求
* @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.最終要一步为迈,啟動(dòng)掃描Servlet
@SpringBootApplication
@MapperScan(basePackages = "pterosaur.account.mapper")
@EnableCaching
@ServletComponentScan //這個(gè)
public class AccountApplication {
public static void main(String[] args) {
SpringApplication.run(AccountApplication.class, args);
}
}