Spring Web Flux Framework can be used with its latest Release Candidate Release 5.0.0.RC3 if you want to try it out. You just need to add the Spring Milestone Repository in your gradle or maven file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<groupId>lk.techtalks</groupId> | |
<artifactId>spring-webflux-test</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-context</artifactId> | |
<version>5.0.0.RC3</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-webflux</artifactId> | |
<version>5.0.0.RC3</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
<version>2.0.0.M3</version> | |
</dependency> | |
</dependencies> | |
<repositories> | |
<repository> | |
<id>spring-milestones</id> | |
<name>Spring Milestones</name> | |
<url>https://repo.spring.io/libs-milestone</url> | |
<snapshots> | |
<enabled>false</enabled> | |
</snapshots> | |
</repository> | |
</repositories> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Service | |
public class PersonService { | |
private final List<Person> PEOPLE = new ArrayList<>(); | |
{ | |
PEOPLE.add(new Person("Shazin")); | |
PEOPLE.add(new Person("Shahim")); | |
PEOPLE.add(new Person("Shuhail")); | |
PEOPLE.add(new Person("Shuhaib")); | |
PEOPLE.add(new Person("Athiyab")); | |
PEOPLE.add(new Person("Athif")); | |
} | |
public List<Person> getPeople() { | |
try { | |
Thread.sleep(new Random(System.currentTimeMillis()).nextInt(1000)); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
return Collections.unmodifiableList(PEOPLE); | |
} | |
} |
- >
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestController | |
@RequestMapping("/people") | |
public class PeopleController { | |
@Autowired | |
private PersonService blockingService; | |
@GetMapping | |
public Flux<Person> getPeople() { | |
return Flux.fromIterable(blockingService.getPeople()); | |
} | |
@GetMapping("/blocking") | |
public List<Person> getPeopleBlocking() { | |
return blockingService.getPeople(); | |
} | |
} |
Spring Web MVC results were
And Spring Web Flux results were
If you compare the 99th Percentile, Max and Mean response times of the both results you can see Spring Web Flux is considerably faster without any code changes to improve performance. This is a promising sign. The numbers can be improved even more if we use the Netty Reactive Servers instead of conventional Tomcat servers in my understanding.
No comments:
Post a Comment