Spring Boot Actuator 监控线程池

在一个分布式系统中,一般都会有一个或多个固定的线程池来处理我们提交的任务。很多时候我们需要关注线程池的运行情况,根据情况来调整我们线程池参数。我们可以使用Actuator 很简单的完成这件事情。

 

pom.xml

	   <dependency>
	       <groupId>org.springframework.boot</groupId>
	       <artifactId>spring-boot-starter-actuator</artifactId>
	   </dependency>

 

 

自定义一个监控线程池的 HealthContributor

 


/**
 * @author lyy
 */
public class MyThreadPollHealthContributor implements HealthIndicator {
	
	private ThreadPoolExecutor executor;
	
	public MyThreadPollHealthContributor(ThreadPoolExecutor executor) {
		this.executor = executor;
	}

	@Override
	public Health health() {
		//核心线程数
		int corePoolSize = executor.getCorePoolSize();
		//活跃线程数
		int activeCount = executor.getActiveCount();
		//完成的任务数
		long completedTaskCount = executor.getCompletedTaskCount();
		//线程数历史高峰线
		int largestPoolSize = executor.getLargestPoolSize();
		//当前池中线程数
		int poolSize = executor.getPoolSize();
		BlockingQueue<Runnable> queue = executor.getQueue();
		//队列剩余空间数
		int remainingCapacity = queue.remainingCapacity();
		//队列中的任务
		int queueSize = queue.size();
		//最大线程数
		int maximumPoolSize = executor.getMaximumPoolSize();
		
		//如果当前活跃线程数 大于  80%的最大线程数,就认证是down
		double rate = BigDecimal.valueOf(activeCount)
				.divide(BigDecimal.valueOf(maximumPoolSize), 2, BigDecimal.ROUND_HALF_UP)
				.doubleValue();
		
		Map<String, Object> infoMap = new HashMap<String, Object>();
		infoMap.put("核心线程数",corePoolSize);
		infoMap.put("当前活跃线程数", activeCount);
		infoMap.put("线程峰值", largestPoolSize);
		infoMap.put("完成的任务数", completedTaskCount);
		infoMap.put("当前池中线程数", poolSize);
		infoMap.put("队列剩余大小", remainingCapacity);
		infoMap.put("当前队列中的任务数", queueSize);
		infoMap.put("设置最大线程数", maximumPoolSize);
		if(rate > 0.8) {
			return Health.down().withDetails(infoMap).build();
		}else {
			return Health.up().withDetails(infoMap).build();
		}

	}

 

 

我们可以监控多个对象,自定义一个  CompositeHealthContributor 

 


/**
 * @author lyy
 *  线程池
 */
public class ThreadPoolConfig {
	
	public static final ThreadPoolExecutor executor = 
			new ThreadPoolExecutor(5, 10, 1, TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(100));

}


/**
 * @author lyy
 */
@Component
public class MyCompositeHealthContributor implements CompositeHealthContributor {
	
	private Map<String, HealthContributor> map = new HashMap<String, HealthContributor>();

	@PostConstruct
	public void init() {
		MyThreadPollHealthContributor threadPool = new MyThreadPollHealthContributor(ThreadPoolConfig.executor);
		map.put("threadPoll", threadPool);
		
		addTask();
	}
	
	@Override
	public HealthContributor getContributor(String name) {
		return map.get(name);
	}

	@Override
	public Iterator<NamedContributor<HealthContributor>> iterator() {
		List<NamedContributor<HealthContributor>> contributors = new ArrayList<NamedContributor<HealthContributor>>();
		
		map.forEach((name,c) ->{
			contributors.add(NamedContributor.of(name, c));
		});
		
		return contributors.iterator();
	}
	
	
	/**
	 * 模拟添加任务
	 */
	public void addTask() {
		AtomicLong finishTaskNum = new AtomicLong();
		new Thread(() ->{

			while(true) {
				try {
					ThreadPoolConfig.executor.execute(() ->{
						try {
							TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(10,40));
							System.out.println("完成任务.."+ finishTaskNum.getAndIncrement());
						} catch (Exception e) {
							e.printStackTrace();
						}
					});
					TimeUnit.SECONDS.sleep(1);
				} catch (Exception e2) {
				}

				
			}
		}).start();

	}
	
}

 

application.yml 

management:
  endpoints:
    web:
      exposure:
        include: "*"
        
  endpoint:
    health:
      show-details: always

 

ok,启动项目 访问 http://localhost:8080/actuator/health   就可以看到线程池的相关信息了

 

 

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Spring Boot ActuatorSpring Boot提供的一组特性,可以帮助监控和管理Spring Boot应用程序。它提供了一组RESTful接口,可以提供有关应用程序运行状态的信息,如健康状态、内存使用情况、线程池状态等。此外,Actuator还提供了一些用于管理应用程序的功能,如重启和关闭应用程序。 ### 回答2: Spring Boot ActuatorSpring Boot的扩展模块,它提供了许多监控和管理Spring Boot应用程序的功能。它通过HTTP或JMX公开RESTful端点,可以实时监控Spring Boot应用的运行状态和度量指标,并提供管理资源的操作方法。 Spring Boot Actuator的功能包括: 1.健康状况检查:检查应用程序运行状况,并返回一个简单的指示器(UP / DOWN)。 2.指标收集:收集各种应用程序指标,如内存使用率、请求处理时间、CPU利用率等等。 3.环境和配置信息:查看应用程序配置参数、环境变量、JVM属性等等。 4.操作管理:管理应用程序运行时的常用操作,如:关闭应用程序、重新加载日志配置等等。 5.跟踪请求:实时监控应用程序收到的所有HTTP请求,包括请求的URL、状态码、IP地址、处理时间等等。 Spring Boot Actuator使用非常方便,只需在应用程序中添加依赖并配置相关的端点和配置即可。然后就可以通过HTTP或JMX接口实现控制和监控。此外,Spring Boot Actuator还支持自定义端点和指标收集器,可以根据具体应用程序的需求进行扩展和定制。 总之,Spring Boot Actuator是一款非常实用的Spring Boot扩展模块,可以实现对Spring Boot应用程序的实时监控和管理。它不仅能够帮助开发人员快速定位和解决问题,还能帮助运维人员有效管理应用程序,提高系统的可用性和稳定性。 ### 回答3: Spring Boot ActuatorSpring Boot的子项目之一,它提供了对生产环境中运行的Spring应用程序的直接管理和监控支持。Actuator可以暴露应用程序有关存活状态、运行指标、日志信息、环境细节和应用程序健康状况的关键信息。这些信息可以通过HTTP接口、JMX、SSH和telnet接口来获得。 Spring Boot Actuator包括几个组件: 1. 健康状况指示器(Health Indicator):用于提供应用程序健康状况信息,如应用程序在运行时所使用的数据库是否可用、磁盘是否有足够的空间、内存是否正常等。 2. 生命周期指示器(Lifecycle Indicator):提供应用程序生命周期的信息,如应用程序的启动时间、运行时间、关闭时间等。 3. 配置指示器(Configuration Indicator):提供应用程序的配置信息,如系统属性、环境变量、配置文件等。 4. 环境指示器(Environment Indicator):提供应用程序的环境信息,如Java版本、操作系统型、IP地址等。 5. 端点(Endpoint):提供了Actuator数据的访问入口,可以通过HTTP接口、JMX、SSH和telnet接口访问。 Actuator可以让应用程序在生产环境中更容易地进行监控和管理,同时提供了很多有用的信息来诊断和解决应用程序中的问题。因此,它是一个非常重要的工具,在使用Spring Boot构建应用程序时不可忽视。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值