component/sdkhelp wantedtype/enhancement
Repository metrics
- Stars
- (41,524 stars)
- PR merge metrics
- (Avg merge 7d 14h) (20 merged PRs in 30d)
Description
provider service method:
public CompletableFuture<Integer> newToken(NewTokenInfo tReq) {
TokenInfo tokenInfo = new TokenInfo(JsonUtils.toJson(tReq), 60);
// 可以设置回应附加信息
// RpcContext.getServerResponseContext().setAttachment("testInfo", JsonUtils.toJson(tokenInfo));
// 可以设置回应附加信息
// RpcContext.getServerContext().setAttachment("testInfo", JsonUtils.toJson(tokenInfo));
CompletableFuture<Integer> completableFuture = new CompletableFuture<>();
completableFuture = completableFuture.completeAsync(()->{
logger.debug("tokenInfo:" + JsonUtils.toJson(tokenInfo));
return 1;
}, Executors.newFixedThreadPool(1));
// 不可以设置回应附加信息
RpcContextAttachment serverResponseContext = RpcContext.getServerResponseContext();
// RpcContextAttachment serverResponseContext = RpcContext.getClientAttachment();
// RpcContextAttachment serverResponseContext = RpcContext.getServerContext();
// RpcServiceContext serverResponseContext = RpcContext.getServiceContext();
// RpcServiceContext serverResponseContext = RpcContext.getCurrentServiceContext();
// RpcContextAttachment serverResponseContext = RpcContext.getClientResponseContext();
// RpcContextAttachment serverResponseContext = RpcContext.getServerAttachment();
completableFuture = completableFuture.thenApply(ret->{
// 不可以设置回应附加信息
// RpcContextAttachment serverResponseContext = RpcContext.getServerResponseContext();
// RpcContextAttachment serverResponseContext = RpcContext.getClientAttachment();
// RpcContextAttachment serverResponseContext = RpcContext.getServerContext();
// RpcServiceContext serverResponseContext = RpcContext.getServiceContext();
// RpcServiceContext serverResponseContext = RpcContext.getCurrentServiceContext();
// RpcContextAttachment serverResponseContext = RpcContext.getClientResponseContext();
// RpcContextAttachment serverResponseContext = RpcContext.getServerAttachment();
serverResponseContext.setAttachment("testInfo", JsonUtils.toJson(tokenInfo));
return ret;
});
return completableFuture;
}
consumer :
NewTokenInfo tReq = new NewTokenInfo("testType", "testToken");
CompletableFuture<Integer> future = tokenRpcProxy.newToken(tReq);
CompletableFuture<Void> completableFuture = future.thenAccept(ret->{
logger.debug("newToken: " + ret + ", testInfo: " + RpcContext.getClientResponseContext().getAttachment("testInfo"));
logger.debug("newToken: " + ret + ", testInfo: " + RpcContext.getServerContext().getAttachment("testInfo"));
});
try {
completableFuture.get();
} catch (Exception e) {
throw new RuntimeException("未知异常", e);
}
provider的方法是异步执行,异步执行完后才能往RpcContext设置回应附加信息,这种情况回应附加信息没法传递到consumer这边