Argotto
Argotto

Reputation: 60

How to migrate a microservice that uses RESTFul to Spring Webflux reactive

Let's say I have a micro service with spring boot, with a PersonsRestController input like:

@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();

}

The implementation of personSaverService.getPublishItemListBy (...) looks something like this:

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

    try {

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

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

And finally the repository through Spring Jdbc makes a query to a database, Oracle db in this case.

How can I migrate this logic to Spring WebFlux?

I have tried changing only the returned list to a Flux in the controller:

  @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());
  }

but tried JMeter simulating multiple concurrent users (threads) and not seeing any improvement. I guess more needs to be done.

Thanks in advance.

Upvotes: 2

Views: 393

Answers (1)

Jo&#227;o Dias
Jo&#227;o Dias

Reputation: 17500

You will only get a real benefit from using a reactive stack if your complete application is reactive. In your case, the problem is the database, which does not support reactive programming and thus is still blocking.

There is a workaround for this (Spring Data R2DBC) that you will need to use so that you can fully benefit from using Spring WebFlux.

Upvotes: 3

Related Questions