In Microservices Engine (MSE) end-to-end canary release, canary traffic tags are passed through in call processes of requests. In some unsupported asynchronous tasks, the pass-through of canary traffic tags may be interrupted. As a result, canary traffic processing fails. By default, MSE allows the pass-through of tags for asynchronous tasks that are created by using the @Async annotation in Spring. MSE also allows you to customize asynchronous tasks to implement traffic tag pass-through by adding scan packages for asynchronous pass-through.
Prerequisites
The MSE probe V3.2.0 or later for public preview is used. If you want to update the MSE probe, join the MSE DingTalk group (ID: 43525005207).
To query the MSE probe version, run the following command:
cat /home/admin/.opt/ArmsAgent/versionMethod 1: Use the @Async annotation in Spring
By default, MSE allows you to use asynchronous tasks that are created by using the @Async annotation in Spring to perform asynchronous tag pass-through. MSE automatically enhances the following default executors and tasks in Spring:
Executors:
org.springframework.scheduling.concurrent.ConcurrentTaskExecutor
org.springframework.core.task.SimpleAsyncTaskExecutor
org.springframework.scheduling.quartz.SimpleThreadPoolTaskExecutor
org.springframework.core.task.support.TaskExecutorAdapter
org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler
org.springframework.jca.work.WorkManagerTaskExecutor
org.springframework.scheduling.commonj.WorkManagerTaskExecutor
Tasks:
org.springframework.aop.interceptor.AsyncExecutionInterceptor$1
org.springframework.aop.interceptor.AsyncExecutionInterceptor$$Lambda$
Method 2: Add a scan package for asynchronous pass-through
In Java applications, you can add environment variables or Java startup parameters to enable the capability of asynchronously passing through scan packages. This helps pass through traffic tags for asynchronous tasks. The Runnable, Callable, or Supplier interface that is involved automatically captures the context of the call process in the current thread when new objects are created. When threads are used in asynchronous mode, the context of the call process is used to implement end-to-end pass-through of the canary traffic tag.
-Dprofiler.thread.match.package="com.alibaba.mse.brightroar.console.service"To add multiple scan packages for asynchronous pass-through, separate the package names with commas (,).
For example, you can use a scan package for asynchronous pass-through to monitor the asynchronous task that is created by using the following sample code. The full name of the sample scan package for asynchronous pass-through is com.alibaba.mse.brightroar.console.service.
When you add the scan package for asynchronous pass-through to your application configurations, you can adjust the specified package name based on your business requirements. If you want to monitor a large number of asynchronous tasks, you can specify the prefix of the package name instead of the full package name. In this example, you can enter the full package name com.alibaba.mse.brightroar.console.service or the prefix com.alibaba.mse. If you enter the prefix, all scan packages for asynchronous pass-through in this directory are scanned. However, a short prefix may involve an excessively large number of scan packages for asynchronous pass-through, which results in performance deterioration. Proceed with caution when you specify the package name.
package com.alibaba.mse.brightroar.console.service;
@Service
public class NameService {
private ExecutorService es = Executors.newFixedThreadPool(5);
public void name() {
es.submit(new Runnable() {
@Override
public void run() {
System.out.println(System.currentTimeMillis()+ ": my name is john, " + Thread.currentThread().getId());
}
});
}
}