java - 如何将使用 RESTFul 的微服务迁移到 Spring Webflux 响应式

标签 java spring-boot rest microservices spring-webflux

假设我有一个带有 Spring Boot 的微服务,其 PersonsRestController 输入如下:

@GetMapping(value = "/world/{countryId}/persons", produces = APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<PersonSaverItem> getPublishPersonsByCountry(@PathVariable("countryId") Integer countryId,
                                                                                                @RequestParam(value = "state", required = false) PublishItemState state,
                                                                                                @RequestParam(value = "limit", defaultValue = "50", required = false) int limit,
                                                                                                @RequestParam(value = "offset", defaultValue = "0", required = false) int offset) {

    PersonSaverList personSaverList = this.personSaverService.getPublishItemListBy(countryId, state, limit, offset);

    return personSaverList.getpersonSaverList();

}

personSaverService.getPublishItemListBy (...) 的实现看起来像这样:

@Override
public PersonSaverList getPublishItemListBy(Integer countryId, PublishItemState state, int limit, int offset) {

    try {

        return this.personSaverRepository.getPublishItemsBycountryId(countryId, state, limit, offset);

    } catch (Exception e) {
      ...
    }
}

最后,存储库通过 Spring Jdbc 对数据库(本例中为 Oracle db)进行查询。

如何将此逻辑迁移到 Spring WebFlux?

我尝试仅将返回的列表更改为 Controller 中的 Flux:

  @GetMapping(value = "/world/{countryId}/persons", produces = APPLICATION_JSON_UTF8_VALUE)
  public Flux<PersonSaverItem> getPublishPersonsByCountry(@PathVariable("countryId") Integer 
      countryId,
  @RequestParam(value = "state", required = false) PublishItemState state,
  @RequestParam(value = "limit", defaultValue = "50", required = false) int limit,
  @RequestParam(value = "offset", defaultValue = "0", required = false) int offset) {

       PersonSaverList personSaverList = this.personSaverService.getPublishItemListBy(countryId, state, limit, offset);

       return Flux.fromIterable(personSaverList.getpersonSaverList());
  }

但尝试使用 JMeter 模拟多个并发用户(线程),但没有看到任何改进。 我想还需要做更多的事情。

提前致谢。

最佳答案

只有当您的整个应用程序是响应式的时,您才会从使用响应式堆栈中获得真正的好处。就您而言,问题在于数据库,它不支持响应式(Reactive)编程,因此仍然处于阻塞状态。

您需要使用一个解决方法( Spring Data R2DBC ),以便您可以从使用 Spring WebFlux 中充分受益。

关于java - 如何将使用 RESTFul 的微服务迁移到 Spring Webflux 响应式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69722993/

相关文章:

java - 无法在 Spring Boot mTLS 中读取客户端的 X509 证书

logging - 如何以及在哪里存储部署在 CF 或 Heroku 中的 Spring Boot 应用程序中的日志文件?

java - 401 而不是 403 与 Spring Boot 2

java - 函数调用后的大括号是什么?

java - 更改生成的映射器类的位置

java - 如何制作一个像网络中那样的特定组合框?

java - 将 ArrayList 添加到类中 - 它有什么作用?

java - 在 ResponseEntity 主体中返回 null(Spring Boot RESTful)

wcf - 如何使用 WCF REST 读取自定义 HTTP 状态代码?

javascript - 将 Polymer 3 路由更改为不同的 html 页面