Spring boot 从Spring引导控制器返回Hystrix AsyncResult
我有以下Spring引导控制器: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
@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);
}
}