【问题标题】:Returing Hystrix AsyncResult from Spring Boot Controller从 Spring Boot 控制器返回 Hystrix AsyncResult
【发布时间】: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


【解决方案1】:

您的服务类需要返回 CompletableFuture。

此外,除非您使用 AspectJ,否则如果从同一类中调用带有 @HystrixCommand 的方法,则断路器将不起作用。

【讨论】:

    猜你喜欢
    • 2016-12-31
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 2019-01-31
    相关资源
    最近更新 更多