Asynchronous 这个异步HystrixCommand有什么问题?

Asynchronous 这个异步HystrixCommand有什么问题?,asynchronous,spring-cloud,spring-cloud-netflix,hystrix,spring-cloud-sleuth,Asynchronous,Spring Cloud,Spring Cloud Netflix,Hystrix,Spring Cloud Sleuth,我需要不时发送通知,我异步执行此任务。我正在使用HystrixCommand执行一个异步RestTemplate调用,该调用不起作用: @HystrixCommand public Future<String> notify(final Query query) { return new AsyncResult<String>() { @Override public String invoke()

我需要不时发送通知,我异步执行此任务。我正在使用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
公共未来通知(最终查询){
返回新的AsyncResult(){
@凌驾
公共字符串调用(){
字符串结果=null;
试一试{
ResponseEntity restExchange=restTemplate.exchange(url,
HttpMethod.POST,
新的HttpEntity(mapper.writeValueAsString(queryMap),httpHeaders),
HashMap.class);
LOGGER.info(“来自“+url+”=“+restExchange.getStatusCodeValue()”的响应代码);
结果=”“+restExchange.getStatusCodeValue();
}捕获(例外e){
LOGGER.error(“发送通知时出现异常!Message=“+e.getMessage(),e”);
}
返回结果;
}
};
}
这就是我之前试图做的(也不起作用):

@HystrixCommand
公共字符串通知(最终查询){
新线程(newrunnable()){
@凌驾
公开募捐{
试一试{
ResponseEntity restExchange=restemplate.exchange(url,HttpMethod.POST,
新的HttpEntity(mapper.writeValueAsString(queryMap)、httpHeaders、HashMap.class);
LOGGER.info(“来自“+url+”=“+restExchange.getStatusCodeValue()”的响应代码);
}捕获(例外e){
LOGGER.error(“发送通知时出现异常!Message=“+e.getMessage(),e”);
}
}
}).start();
}  

另外:将sleuth添加到标记中的原因是,在新线程中执行此操作不会传播头文件(baggage-*),因此尝试此操作时,希望Hystrix命令能够做到这一点,当使用
Runnable
时,您必须将它们包装在跟踪表示中。i、 e.
tracerunable
。它在文档中-


至于为什么Hystrix的东西不起作用——很可能与此有关。

方法notify是从同一类中的方法调用的吗?如果是这种情况,请尝试直接从另一个类调用notify方法,其中notify方法的封闭类作为依赖项注入

基本上,尝试这样做:

而不是这个:


你能粘贴你最初想要做的事情吗?我想你可能用错了未来,你不需要显式地执行它吗?在Future方面没有太多经验,通常使用CompletableFuture。@MarcinGrzejszczak我将整个方法包装在一个runnable中,并在适当的位置调用start()。从未工作过,我将用前面的代码编辑这个问题,同时,您是否看到我可以做些什么来执行异步HystrixCommand,该控件甚至没有进入invoke方法。您知道它必须包装在
TraceRunnable
?我还假设RestTemplate是一个bean?这个例子非常依赖于线程,如果不调试它,我就不能说太多。可能与?@marcun确认问题不是因为sleuth,我从服务中删除了sleuth依赖项,但是控件没有进入AsyncResult的“invoke”方法。
@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();
    }