假设我有一个带有 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/