【发布时间】:2018-07-14 08:44:09
【问题描述】:
我有以下 Spring Boot 控制器:
@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();
}
}
和服务:
@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);
}
}
当我点击 /hello/cf 端点时,我得到一个响应“你好” 当我点击 /hello/hystrix 端点时,我收到 404 错误。
我能以这种方式从控制器返回 AsyncResult 吗?如果是这样,我做错了什么?
谢谢。
【问题讨论】:
-
确切的代码给了我
{ cancelled: false, done: true }。我没有看到 404
标签: spring-boot hystrix