问题
I'm developing simple Spring Boot web application with embedded Jetty server.
I'd like to have some statistics on connection pool usage (i.e. how many threads, avg request time. avg queue wait time, queue size etc.)
I realized that Spring Boot configures Jetty with QueuedThreadPool
which has few basic metrics. Is there more sophisticated bean or module designated for statistics collecting in Spring Boot? How to enable it?
回答1:
Have you had a look at Spring boot actuator? It provides you with some production ready features like a health endpoint and metrics endpoint. Just have a look at the documentation https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html
回答2:
There is a MonitoredQueuedThreadPool implementation that collects stats. The code below enables it and exposes via JMX:
@Bean
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory(@Value("${server.port:8080}") final String port) {
JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory(Integer.valueOf(port));
//enable thread pool with stats
factory.setThreadPool(new MonitoredQueuedThreadPool());
//enable JMX
factory.addServerCustomizers(server -> {
MBeanContainer mbContainer=new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
server.addEventListener(mbContainer);
server.addBean(mbContainer);
server.addBean(Log.getLog());
});
return factory;
}
JMX bean is exposed under node:
It provides metrics:
来源:https://stackoverflow.com/questions/57406631/how-to-collect-and-get-connection-pool-statistics-using-jetty-and-spring-boot