不是不会,只是没见过,代码只是一种工具,首先要会用,应用中使用druid链接池,并添加监控css
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.11</version> </dependency>
参考: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
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;//心跳 }
@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; } }
/** * @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{ }
@SpringBootApplication @MapperScan(basePackages = "pterosaur.account.mapper") @EnableCaching @ServletComponentScan //这个 public class AccountApplication { public static void main(String[] args) { SpringApplication.run(AccountApplication.class, args); } }