Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 从Spring引导控制器返回Hystrix AsyncResult_Spring Boot_Hystrix - Fatal编程技术网

Spring boot 从Spring引导控制器返回Hystrix AsyncResult

Spring boot 从Spring引导控制器返回Hystrix AsyncResult,spring-boot,hystrix,Spring Boot,Hystrix,我有以下Spring引导控制器: @Controller public class TestController { @Autowired private TestService service; @GetMapping(path="/hello") public ResponseEntity<String> handleGet() { return service.getResponse(); } @GetMap

我有以下Spring引导控制器:

@Controller
public class TestController {
    @Autowired
    private TestService service;


    @GetMapping(path="/hello")
    public ResponseEntity<String> handleGet() {
        return service.getResponse();
    }

    @GetMapping(path="/hello/hystrix")
    public Future<ResponseEntity<String>> handleGetAsync() {
        return service.getResponseAsync();
    }

    @GetMapping(path="/hello/cf")
    public Future<ResponseEntity<String>> handleGetCF() {
        return service.getResponseCF();
    }
}
当我点击/hello/cf端点时,我得到一个响应“hello” 当我点击/hello/hystrix端点时,我得到一个404错误

我能以这种方式从控制器返回AsyncResult吗?如果是,我做错了什么


谢谢。

您的服务类需要返回一个完整的未来


另外,除非您使用的是AspectJ,否则如果从同一个类中调用带有@HystrixCommand的方法,断路器将无法工作

精确的代码给了我
{cancelled:false,done:true}
。我看不到404
@Service
public class TestService {
    @HystrixCommand
    public ResponseEntity<String> getResponse() {
        ResponseEntity<String> response = ResponseEntity.status(HttpStatus.OK).body("Hello");
        return response;
    }

    @HystrixCommand
    public Future<ResponseEntity<String>> getResponseAsync() {
        return new AsyncResult<ResponseEntity<String>>() {
            @Override
            public ResponseEntity<String> invoke() {
                return getResponse();
            }
        };
    }

    public Future<ResponseEntity<String>> getResponseCF() {
        return CompletableFuture.supplyAsync(() -> getResponse());
    }
}
@EnableHystrix
@SpringBootApplication
@EnableAsync
public class HystrixApplication {
    public static void main(String[] args) {
        SpringApplication.run(HystrixApplication.class, args);
    }
}