SpringBoot+HikariCP+Dropwizard-Metrics统计连接池使用情况
背景,HikariCP是Java目前使用最广的连接池工具类,SpringBoot默认也是用这个,现在想获取连接池使用情况。
这里假设SpringBoot已集成HikariCP
1.pom.xml加上Dropwizard-Metrics配置
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-healthchecks</artifactId>
</dependency>
2在应用启动的时候连接池注册统计接口
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Slf4jReporter;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
public class ApplicationRunner implements ApplicationRunner {
private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationRunner.class);
@Autowired
private DataSource dataSource;
@Override
public void run(ApplicationArguments args) throws Exception {
try {
// see detail https://github.com/brettwooldridge/HikariCP/wiki/Dropwizard-Metrics
// 连接池注册统计接口
MetricRegistry metricRegistry = new MetricRegistry();
if(dataSource instanceof HikariDataSource) {
((HikariDataSource) dataSource).setMetricRegistry(metricRegistry);
}
// 定时打印连接池使用情况
Slf4jReporter reporter = Slf4jReporter.forRegistry(metricRegistry).build();
reporter.start(1, TimeUnit.MINUTES);
} catch (Exception e) {
String msg = "服务启动异常";
LOGGER.error(msg, e);
throw new IllegalStateException(msg, e);
}
}
}
3 提供http请求获取连接池使用情况
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
/**
* 远程使用的RestTemplatebean,与服务间调用区分开来
*/
@Configuration
public class RestTemplateConfig {
@Bean("remoteRestTemplate")
public RestTemplate remoteRestTemplate() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(180000);
factory.setConnectTimeout(8000);
return new RestTemplate(factory);
}
}
import com.codahale.metrics.*;
import com.netflix.appinfo.InstanceInfo;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.sql.DataSource;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
@RestController
@RequestMapping("hikariCpStatsController")
public class HikariCpStatsController {
private static final String SERVICE_NAME ="xiaoniu";
@Autowired
private DataSource dataSource;
@Autowired
private DiscoveryClient discoveryClient;
@Qualifier("remoteRestTemplate")
@Autowired
private RestTemplate restTemplate;
@GetMapping("poolStatsHa")
public TreeMap<String, Object> poolStatsHa() {
MetricRegistry metricRegistry = null;
if (dataSource instanceof HikariDataSource) {
metricRegistry = (MetricRegistry) ((HikariDataSource) dataSource).getMetricRegistry();
}
if (metricRegistry == null)
return null;
TreeMap<String, Object> dataAll = new TreeMap<>();
// 节点的信息
List<ServiceInstance> instances = discoveryClient.getInstances(SERVICE_NAME);
for (ServiceInstance instance : instances) {
InstanceInfo instanceInfo = ((EurekaDiscoveryClient.EurekaServiceInstance) instance).getInstanceInfo();
TreeMap data = restTemplate.getForObject(instance.getUri().toString() + "/hikariCpStatsController/poolStats", TreeMap.class);
dataAll.put(instanceInfo.getInstanceId(), data);
}
return dataAll;
}
@GetMapping("poolStats")
public TreeMap<String, Object> poolStats() {
MetricRegistry metricRegistry = null;
if (dataSource instanceof HikariDataSource) {
metricRegistry = (MetricRegistry) ((HikariDataSource) dataSource).getMetricRegistry();
}
if (metricRegistry == null)
return null;
TreeMap<String, Object> data = new TreeMap<>();
SortedMap<String, Gauge> gauges = metricRegistry.getGauges();
for (Map.Entry<String, Gauge> gaugeEntry : gauges.entrySet()) {
String key = gaugeEntry.getKey();
Gauge value = gaugeEntry.getValue();
data.put(key, value.getValue());
}
SortedMap<String, Timer> timers = metricRegistry.getTimers();
for (Map.Entry<String, Timer> timerEntry : timers.entrySet()) {
String key = timerEntry.getKey();
Timer value = timerEntry.getValue();
data.put(key, "获取连接时99%线程等待的纳秒=" + value.getSnapshot().get99thPercentile());
}
SortedMap<String, Meter> meters = metricRegistry.getMeters();
for (Map.Entry<String, Meter> meterEntry : meters.entrySet()) {
String key = meterEntry.getKey();
Meter value = meterEntry.getValue();
data.put(key, "count=" + value.getCount());
}
SortedMap<String, Histogram> histograms = metricRegistry.getHistograms();
for (Map.Entry<String, Histogram> histogramEntry : histograms.entrySet()) {
String key = histogramEntry.getKey();
Histogram value = histogramEntry.getValue();
data.put(key, "99%连接线程使用的毫秒=" + value.getSnapshot().get99thPercentile());
}
return data;
}
}
在此大功告成
参考// see detail https://github.com/brettwooldridge/HikariCP/wiki/Dropwizard-Metrics
分享到:
相关推荐
SpringBoot1.4之后不再支持velocity,导致spring-boot-starter-velocity无法使用,本demo是SpringBoot1.5.x与velocity2集成demo,代码是基于...并升级了springboot到目前最新的1.5.10
在本文中,我们将深入探讨如何将SpringBoot 1.5.10.RELEASE与RocketMQ 4.3.1集成,构建一个支持多个消费者监听的消息服务示例。RocketMQ是阿里巴巴开源的一款分布式消息中间件,它具有高吞吐量、低延迟、高可用性和...
Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、...
赠送jar包:swagger-annotations-1.5.10.jar; 赠送原API文档:swagger-annotations-1.5.10-javadoc.jar; 赠送源代码:swagger-annotations-1.5.10-sources.jar; 赠送Maven依赖信息文件:swagger-annotations-...
赠送jar包:swagger-annotations-1.5.10.jar; 赠送原API文档:swagger-annotations-1.5.10-javadoc.jar; 赠送源代码:swagger-annotations-1.5.10-sources.jar; 赠送Maven依赖信息文件:swagger-annotations-...
v1.5.10是该软件的一个版本,它提供了多国语言支持,使得全球用户都能方便地使用。在这个特定的压缩包`gallery-1.5.10.zip`中,很可能包含了Menalto Gallery的所有组件和资源,如安装文件、语言包、配置文件以及可能...
【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、...【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。
基于SpringBoot + Mybatis + Redis + RabbitMQ秒杀系统 开发工具 想法2017.2 开发环境 JDK 玛文 MySQL的 弹簧靴 Redis 兔子MQ 1.8 3.2.2 5.7 1.5.10发布 3.2 4倍 项目启动说明 1,启动前,请配置application....
这是那个spring-boot-1.5.10.RELEASE.jar,boot的核心jar包,导进去就能用,okok
sqlmapproject-sqlmap-1.5.10-10-ge9d7a77.zip
springboot 1.5.10 cas-server 5.2.2 cas-client 3.5.0 视频演示 CAS 5.1.2 基本的演示,说明和安装过程:https://v.qq.com/x/page/j063197nnmj.html CAS 5.2.x 单点登录 —— 实现单点登录演示:...
离线安装包,亲测可用
java运行依赖jar包
1.会员卡系统(充值、支付) 2.商家手机处理订单(接单、送货) 3.短信模板消息、订单动态、订单打印、主题切换
资源来自pypi官网。 资源全名:moesifdjango-1.5.10-py2.py3-none-any.whl