SpringCloud Executor,ExecutorService和ScheduledExecutorService

2023-12-02 11:39 更新

我們提供LazyTraceExecutorTraceableExecutorServiceTraceableScheduledExecutorService。每次提交,調(diào)用或計劃新任務(wù)時,這些實現(xiàn)都會創(chuàng)建spans。

以下示例顯示了使用CompletableFuture時如何將跟蹤信息傳遞給TraceableExecutorService

CompletableFuture<Long> completableFuture = CompletableFuture.supplyAsync(() -> {
	// perform some logic
	return 1_000_000L;
}, new TraceableExecutorService(beanFactory, executorService,
		// 'calculateTax' explicitly names the span - this param is optional
		"calculateTax"));

 Sleuth不適用于parallelStream()。如果要使跟蹤信息通過流傳播,則必須使用supplyAsync(…?)的方法,如前所示。

如果有beans實現(xiàn)了您想從跨度創(chuàng)建中排除的Executor接口,則可以使用spring.sleuth.async.ignored-beans屬性,在其中可以提供bean名稱的列表。

定制執(zhí)行者

有時,您需要設(shè)置AsyncExecutor的自定義實例。以下示例顯示如何設(shè)置這樣的自定義Executor

@Configuration
@EnableAutoConfiguration
@EnableAsync
// add the infrastructure role to ensure that the bean gets auto-proxied
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
static class CustomExecutorConfig extends AsyncConfigurerSupport {

	@Autowired
	BeanFactory beanFactory;

	@Override
	public Executor getAsyncExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		// CUSTOMIZE HERE
		executor.setCorePoolSize(7);
		executor.setMaxPoolSize(42);
		executor.setQueueCapacity(11);
		executor.setThreadNamePrefix("MyExecutor-");
		// DON'T FORGET TO INITIALIZE
		executor.initialize();
		return new LazyTraceExecutor(this.beanFactory, executor);
	}

}

 

為確保對配置進(jìn)行后期處理,請記住在@Configuration類上添加@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號