原创

Skywalking跨线程跟踪方法

一、引入apm-toolkit-trace组件

<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-trace</artifactId>
<version>${skywalking.version}</version>
<!--${skywalking.version}   项目中使用skywalking版本,公司现在暂时统一使用8.1.0 ,2021-11-26--->
</dependency>

二、 使用方法

1. 在项目中使用 @TraceCrossThread注解

// usage 1, for Callable<T>
@TraceCrossThread
public static class MyCallable<String> implements Callable<String> {
@Override
public String call() throws Exception {
return null;
}
}
...
ExecutorService executorService = Executors.newFixedThreadPool(1);
executorService.submit(new MyCallable());

// usage 2, for Supplier<T>
@TraceCrossThread
public class MySupplier<String> implements Supplier<String> {
@Override
public String get() {
return null;
}
}
...
CompletableFuture.supplyAsync(new MySupplier<String>());

2. 使用CallableWrapper.of()或者SupplierWrapper.of()

// usage 1, for CallableWrapper.of()
ExecutorService executorService = Executors.newFixedThreadPool(1);
executorService.submit(CallableWrapper.of(new Callable<String>() {
@Override public String call() throws Exception {
return null;
}
}));
// or
ExecutorService executorService = Executors.newFixedThreadPool(1);
executorService.execute(RunnableWrapper.of(new Runnable() {
@Override public void run() {
//your code
}
}));

// usage 2, for SupplierWrapper.of()
CompletableFuture.supplyAsync(SupplierWrapper.of(()->{
return "SupplierWrapper";
})).thenAccept(System.out::println);

//usage 3, for SupplierWrapper.of()
CompletableFuture.supplyAsync(SupplierWrapper.of(() -> {
return "SupplierWrapper";
})).thenAcceptAsync(ConsumerWrapper.of(c -> {
// your code visit(url)
System.out.println("ConsumerWrapper");
}));
// or
CompletableFuture.supplyAsync(SupplierWrapper.of(() -> {
return "SupplierWrapper";
})).thenApplyAsync(FunctionWrapper.of(f -> {
// your code visit(url)
return "FunctionWrapper";
}));

三、项目实例

 @Override
public RestResponse<MemberCardAccountRespDto> queryByMemberId(Long memberId) {
CompletableFuture<RestResponse<MemberCardAccountRespDto>> future = CompletableFuture.supplyAsync(SupplierWrapper.of(() -> {
//查询会员账户信息
return memberCardQueryApi.queryMemberAccountEoByMemberId(memberId);

})).thenCombine(CompletableFuture.supplyAsync(SupplierWrapper.of(() -> {
//查询会员档案信息
return ListUtil.getFirst(memberStandardQueryApi.queryMemberInfoByMemberIds(Arrays.asList(memberId)).getData());

})), (memberCardAccountResult, memberInfoDto) -> {

if (memberCardAccountResult == null || !"0".equals(((RestResponse<MemberCardAccountRespDto>) memberCardAccountResult).getResultCode())
|| null == ((RestResponse<MemberCardAccountRespDto>) memberCardAccountResult).getData()) {
return memberCardAccountResult;
}

if (null != memberInfoDto) {
((RestResponse<MemberCardAccountRespDto>) memberCardAccountResult).getData().setMemberName(((MemberInfoRespDto) memberInfoDto).getRealName());
}
return memberCardAccountResult;
});

try {
return future.get(5, TimeUnit.SECONDS);
} catch (Exception e) {
logger.error("查询出现异常:", e);
}

return RestResponse.FAIL;
}

正文到此结束
本文目录