我有多个服务,其中一些使用 Hystrix 的 HystrixObservableCommand 来调用其他服务,而另一些使用 HystrixCommand。如何将 traceIds 从调用服务传递到 HystrixObservableCommand 中的 Observables,并且如果调用回退也让它们传递?
所有服务都使用 grpc-java。
我拥有的示例代码:
WorldCommand worldCommand = new WorldCommand(greeterRequest, worldServiceStub);
String messageFromWorldService = "";
String idFromWorldService = "";
try {
Greeter.GreeterReply greeterReply = worldCommand.construct().toBlocking().toFuture().get();
messageFromWorldService = greeterReply.getMessage();
idFromWorldService = greeterReply.getId();
logger.info("Response from WorldService -- {}, id = {}", messageFromWorldService, idFromWorldService);
} catch (StatusRuntimeException | InterruptedException | ExecutionException e) {
logger.warn("Exception when calling WorldService\n" + e);
}
WorldCommand.java
public class WorldCommand extends HystrixObservableCommand<Greeter.GreeterReply> {
private static final Logger logger = LoggerFactory.getLogger(WorldCommand.class.getName());
private final Greeter.GreeterRequest greeterRequest;
private final WorldServiceGrpc.WorldServiceStub worldServiceStub;
public WorldCommand(Greeter.GreeterRequest greeterRequest, WorldServiceGrpc.WorldServiceStub worldServiceStub) {
super(HystrixCommandGroupKey.Factory.asKey("WorldService"));
this.greeterRequest = greeterRequest;
this.worldServiceStub = worldServiceStub;
}
@Override
protected Observable<Greeter.GreeterReply> construct() {
Context context = Context.current();
return Observable.create(new Observable.OnSubscribe<Greeter.GreeterReply>() {
@Override
public void call(Subscriber<? super Greeter.GreeterReply> observer) {
logger.info("In WorldCommand");
if (!observer.isUnsubscribed()) {
//pass on the context, if you want only certain headers to pass on then create a new Context and attach it.
context.attach();
logger.info("In WorldCommand after attach");
worldServiceStub.greetWithHelloOrWorld(greeterRequest, new StreamObserver<Greeter.GreeterReply>() {
@Override
public void onNext(Greeter.GreeterReply greeterReply) {
logger.info("Response from WorldService -- {}, id = {}", greeterReply.getMessage(), greeterReply.getId());
observer.onNext(greeterReply);
observer.onCompleted();
}
@Override
public void onError(Throwable t) {
logger.info("Exception from WorldService -- {}", t);
}
@Override
public void onCompleted() {
}
});
}
}
} ).subscribeOn(Schedulers.io());
}
@Override
protected Observable<Greeter.GreeterReply> resumeWithFallback() {
logger.info("Response from fallback");
Greeter.GreeterReply greeterReply = Greeter.GreeterReply.newBuilder().setMessage("teammate").setId("-1").build();
return Observable.just(greeterReply);
}
我正在使用 Zipkin grpc tracking 和 MDCCurrentTraceContext 在日志中打印 traceId 和 spanId。
WorldCommand 中的两个日志条目都不会打印出 trace 和 span id,它们在 RxIoScheduler 线程上调用。
编辑
按照 Mike 的建议添加了 ConcurrencyStrategy。
public class CustomHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {
private static final Logger log = LoggerFactory.getLogger(CustomHystrixConcurrencyStrategy.class);
public <T> Callable<T> wrapCallable(Callable<T> callable){
log.info("In CustomHystrixConcurrencyStrategy: callable="+ callable.toString());
return new ContextCallable<>(callable);
}
}
HelloService 调用两个服务 World 和 Team。WorldCommand 是 HystrixObservableCommand,TeamCommand 是 HystrixCommand。
logger.info("In the HelloService:greetWithHelloWorld");
Greeter.GreeterRequest greeterRequest = Greeter.GreeterRequest.newBuilder().setId(request.getId()).build();
//Call WorldService
ManagedChannel worldChannel = getChannel("localhost:8081", "helloService-world-client");
//Async stub instead of blockingStub
WorldServiceGrpc.WorldServiceStub worldServiceStub = WorldServiceGrpc.newStub(worldChannel);
WorldCommand worldCommand = new WorldCommand(greeterRequest, worldServiceStub);
String messageFromWorldService = "";
String idFromWorldService = "";
try {
Greeter.GreeterReply greeterReply = worldCommand.construct().toBlocking().toFuture().get();
messageFromWorldService = greeterReply.getMessage();
idFromWorldService = greeterReply.getId();
logger.info("Response from WorldService -- {}, id = {}", messageFromWorldService, idFromWorldService);
} catch (StatusRuntimeException | InterruptedException | ExecutionException e) {
logger.warn("Exception when calling WorldService\n" + e);
}
//Call TeamService
ManagedChannel teamChannel = getChannel("localhost:8082", "helloService-team-client");
TeamServiceGrpc.TeamServiceBlockingStub teamServiceStub = TeamServiceGrpc.newBlockingStub(teamChannel);
TeamCommand teamCommand = new TeamCommand(greeterRequest, teamServiceStub);
String messageFromTeamService = "";
String idFromTeamService = "";
try {
Greeter.GreeterReply greeterReply = teamCommand.construct().toBlocking().toFuture().get();
messageFromTeamService = greeterReply.getMessage();
idFromTeamService = greeterReply.getId();
logger.info("Response from TeamService -- {}, id = {}", messageFromTeamService, idFromTeamService);
} catch (StatusRuntimeException | InterruptedException | ExecutionException e) {
logger.warn("Exception when calling TeamService\n" + e);
}
assert(idFromWorldService.equals(idFromTeamService));
Greeter.GreeterReply greeterReply = Greeter.GreeterReply.newBuilder().setMessage("Hello" + messageFromWorldService + " from " + messageFromTeamService).setId(idFromWorldService).build();
responseObserver.onNext(greeterReply);
responseObserver.onCompleted();
PreservableContext 类
public class PreservableContexts {
//private final TraceContext traceContext;
private static final Logger logger = LoggerFactory.getLogger(PreservableContexts.class.getName());
public PreservableContexts() {
logger.info("Creating new PreservableContexts");
//this.traceContext = TraceContextHolder.getContext();
}
public void set() {
// if (traceContext != null) {
//TraceContextHolder.setContext(traceContext);
// }
}
public void clear() {
//TraceContextHolder.clearContext();
}
PreservableContexts 和 CustomHystrixConcurrencyStrategy 中的日志永远不会被打印出来。我在启动 HelloServer 时正在注册 startegy。
HystrixConcurrencyStrategy strategy = new CustomHystrixConcurrencyStrategy();
HystrixPlugins.getInstance().registerConcurrencyStrategy(strategy);
context = HystrixRequestContext.initializeContext();
编辑 2
更新了 Observable 的设置方式:
ManagedChannel worldChannel = getChannel("localhost:8081", "helloService-world-client");
//Async stub instead of blockingStub
WorldServiceGrpc.WorldServiceStub worldServiceStub = WorldServiceGrpc.newStub(worldChannel);
WorldCommand worldCommand = new WorldCommand(greeterRequest, worldServiceStub);
//Call TeamService
ManagedChannel teamChannel = getChannel("localhost:8082", "helloService-team-client");
TeamServiceGrpc.TeamServiceStub teamServiceStub = TeamServiceGrpc.newStub(teamChannel);
//TeamServiceGrpc.TeamServiceBlockingStub teamServiceStub = TeamServiceGrpc.newBlockingStub(teamChannel);
TeamCommand teamCommand = new TeamCommand(greeterRequest, teamServiceStub);
try {
rx.Observable<Greeter.GreeterReply> worldReplyObservable = worldCommand.observe().subscribeOn(Schedulers.computation());
rx.Observable<Greeter.GreeterReply> teamReplyObservable = teamCommand.observe().subscribeOn(Schedulers.computation());
Observable.zip(worldReplyObservable, teamReplyObservable, new Func2<Greeter.GreeterReply, Greeter.GreeterReply, Object>() {
@Override
public Object call(Greeter.GreeterReply worldReply, Greeter.GreeterReply teamReply) {
String messageFromWorldService = worldReply.getMessage();
String idFromWorldService = worldReply.getId();
logger.info("Response from WorldService -- {}, id = {}", messageFromWorldService, idFromWorldService);
String messageFromTeamService = teamReply.getMessage();
String idFromTeamService = teamReply.getId();
logger.info("Response from TeamService -- {}, id = {}", messageFromTeamService, idFromTeamService);
assert(idFromWorldService.equals(idFromTeamService));
Greeter.GreeterReply greeterReply = Greeter.GreeterReply.newBuilder().setMessage("Hello" + messageFromWorldService + " from " + messageFromTeamService).setId(idFromWorldService).build();
logger.info("Final response=" + greeterReply.getMessage());
responseObserver.onNext(greeterReply);
responseObserver.onCompleted();
return null;
}
});
} catch (StatusRuntimeException e) {
logger.warn("Exception when calling WorldService and/or TeamService\n" + e);
}
我现在有一个奇怪的问题,对 TeamCommand 和 WorldCommand 的调用没有完成,因为这段代码永远不会执行:
Observable.zip(worldReplyObservable, teamReplyObservable, new Func2<Greeter.GreeterReply, Greeter.GreeterReply, Object>() {
@Override
public Object call(Greeter.GreeterReply worldReply, Greeter.GreeterReply teamReply) {
String messageFromWorldService = worldReply.getMessage();
此外,如果有回退,则 hystrix-timer 线程不再具有 MDC。