这个 Async HystrixCommand 有什么问题?
Whats wrong with this Async HystrixCommand?
我需要不时发送通知,我异步执行此任务。我正在使用如下的 HystrixCommand 来执行一个不工作的异步 RestTemplate 调用:
@HystrixCommand
public Future<String> notify(final Query query) {
return new AsyncResult<String>() {
@Override
public String invoke() {
String result = null;
try {
ResponseEntity<HashMap> restExchange = restTemplate.exchange(url,
HttpMethod.POST,
new HttpEntity<String>(mapper.writeValueAsString(queryMap), httpHeaders),
HashMap.class);
LOGGER.info("Response code from " + url + " = " + restExchange.getStatusCodeValue());
result = ""+ restExchange.getStatusCodeValue();
} catch(Exception e) {
LOGGER.error("Exception while sending notification! Message = " + e.getMessage(), e);
}
return result;
}
};
}
这是我之前尝试做的(也没有用):
@HystrixCommand
public String notify(final Query query) {
new Thread(new Runnable() {
@Override
public void run() {
try {
ResponseEntity<HashMap> restExchange = restTemplate.exchange(url, HttpMethod.POST,
new HttpEntity<String>(mapper.writeValueAsString(queryMap), httpHeaders), HashMap.class);
LOGGER.info("Response code from " + url + " = " + restExchange.getStatusCodeValue());
} catch (Exception e) {
LOGGER.error("Exception while sending notification! Message = " + e.getMessage(), e);
}
}
}).start();
}
P.S:向标签添加侦探的原因是,在新线程中执行此操作不会传播 headers(baggage-*) 因此尝试此操作希望 Hystrix 命令能够执行技巧
使用 Runnable
时,您必须将它们包装在跟踪表示中。即 TraceRunnable
。它在文档中 - http://cloud.spring.io/spring-cloud-sleuth/spring-cloud-sleuth.html#_runnable_and_callable .
至于为什么 Hystrix 的东西不起作用 - 很可能与 https://github.com/spring-cloud/spring-cloud-sleuth/issues/612 有关。
是否从同一个 class 中的方法调用方法通知?如果是这种情况,请尝试直接从不同的 class 调用方法通知,其中通知方法的封闭 class 作为依赖项注入。
基本上,尝试这样做:
而不是这个:
我需要不时发送通知,我异步执行此任务。我正在使用如下的 HystrixCommand 来执行一个不工作的异步 RestTemplate 调用:
@HystrixCommand
public Future<String> notify(final Query query) {
return new AsyncResult<String>() {
@Override
public String invoke() {
String result = null;
try {
ResponseEntity<HashMap> restExchange = restTemplate.exchange(url,
HttpMethod.POST,
new HttpEntity<String>(mapper.writeValueAsString(queryMap), httpHeaders),
HashMap.class);
LOGGER.info("Response code from " + url + " = " + restExchange.getStatusCodeValue());
result = ""+ restExchange.getStatusCodeValue();
} catch(Exception e) {
LOGGER.error("Exception while sending notification! Message = " + e.getMessage(), e);
}
return result;
}
};
}
这是我之前尝试做的(也没有用):
@HystrixCommand
public String notify(final Query query) {
new Thread(new Runnable() {
@Override
public void run() {
try {
ResponseEntity<HashMap> restExchange = restTemplate.exchange(url, HttpMethod.POST,
new HttpEntity<String>(mapper.writeValueAsString(queryMap), httpHeaders), HashMap.class);
LOGGER.info("Response code from " + url + " = " + restExchange.getStatusCodeValue());
} catch (Exception e) {
LOGGER.error("Exception while sending notification! Message = " + e.getMessage(), e);
}
}
}).start();
}
P.S:向标签添加侦探的原因是,在新线程中执行此操作不会传播 headers(baggage-*) 因此尝试此操作希望 Hystrix 命令能够执行技巧
使用 Runnable
时,您必须将它们包装在跟踪表示中。即 TraceRunnable
。它在文档中 - http://cloud.spring.io/spring-cloud-sleuth/spring-cloud-sleuth.html#_runnable_and_callable .
至于为什么 Hystrix 的东西不起作用 - 很可能与 https://github.com/spring-cloud/spring-cloud-sleuth/issues/612 有关。
是否从同一个 class 中的方法调用方法通知?如果是这种情况,请尝试直接从不同的 class 调用方法通知,其中通知方法的封闭 class 作为依赖项注入。
基本上,尝试这样做:
而不是这个: