Reactor netty tutorial


  • Getting Started With RSocket Part 1
  • Set a Timeout in Spring 5 Webflux WebClient
  • Tutorial: Reactive Spring Boot Part 2 – A REST Client for Reactive Streams
  • Getting started with Project Reactor
  • Spring Cloud Hoxton.SR9 gateway and reactor-netty 0.9.0.RELEASE is Incompatible [How to Solve]
  • Getting Started With RSocket Part 1

    Trisha Gee This second step shows how to create a Java client that will connect to an endpoint that emits a stream of server-sent events. The original inspiration was a 70 minute live demo, which I have split into a series of shorter videos with an accompanying blog post, explaining each of the steps more slowly and in more detail.

    This blog post contains a video showing the process step-by-step and a textual walk-through adapted from the transcript of the video for those who prefer a written format.

    This tutorial is a series of steps during which we will build a full Spring Boot application featuring a Kotlin back end, a Java client and a JavaFX user interface. This client will be used in later sections of the tutorial.

    This project will have multiple modules in, so start by selecting empty project from the choices on the left of the New Project Wizard. Call the project stock-client and press Finish. Add a new module here, this will be a Spring Boot module so choose Spring Initializr on the left. Keep the defaults of creating a Maven project with Java as the language.

    We can optionally change the default package structure if we wish. Use Spring Boot 2. Select the Spring Reactive Web starter and Lombok too. If given the option, enable auto-import on Maven so when we make changes to the pom. Create an instance of WebClientStockClient in order to test it. This method takes a String that represents the symbol of the stock we want the prices for.

    Using this, we only need to define the properties of this class using fields, the getters, setters, equals, hashCode, and toString methods are all provided by Lombok.

    Store the returned Flux in a prices local variable. Add an assertion that this is not null. Add an assertion that if we take five prices from the flux, we have more than one price. We want to use a WebClient to connect to the service. Create this as a field, and add a constructor parameter so that Spring automatically wires this in for us.

    Remove the stub code from prices for i. Call retrieve. We need to say how to turn the response of this call into a Flux of some type, so we use bodyToFlux and give it our data class, StockPrice.

    We can also define what to do when specific Exceptions are thrown. As an example, we can say that when we see an IOException we want to log it. We can use the Log4j2 annotation from Lombok to give us access to the log, and log an error.

    This is not the most robust way to handle errors, this simply shows that we can consider Exceptions as a first class concern in our reactive streams. Log4j2; import org. WebClient; import reactor. Flux; import java. IOException; import java. We now need to give our client a WebClient. Create a WebClient field in the test. Go back to StockServiceApplication from the first tutorial and run it. We should see the test go green at this point.

    Assertions; import org. Test; import org. However we have successfully used an integration test to drive the API and functionality of our stock prices client. This client connects to an endpoint that emits server-sent events and returns a Flux of StockPrice objects that could be consumed by another service.

    Set a Timeout in Spring 5 Webflux WebClient

    At that time project Spring WebFlux has been under active development, and now after official release of Spring 5 it is worth to take a look on the current version of it.

    The source code is as usual available on GitHub in sample-spring-cloud-webflux repository. Step 1. Building reactive application using Spring WebFlux To enable library Spring WebFlux for the project we should include starter spring-boot-starter-webflux to the dependencies.

    It includes some dependent libraries like Reactor or Netty server. The only difference is in type of returned objects. Instead of single object we return instance of class Mono, and instead of list we return instance of class Flux.

    Integrate an application with database using Spring Data Reactive Mongo The implementation of integration between application and database is also very simple.

    First, we need to include starter spring-boot-starter-data-mongodb-reactive to the project dependencies. The next step is to declare entity with ORM mappings. The following class is also returned as reponse by AccountController. It also allows to define methods with names, which are automatically mapped to queries.

    The only difference in comparison with standard Spring Data JPA repositories is in method signatures. The objects are wrapped by Mono and Flux. To enable discovery client we should first include starter spring-cloud-starter-netflix-eureka-client to the project dependencies. Of cource, we may run more than instance of every service.

    I would not like to go into the details of running application with embedded Eureka server. Eureka server is available as discovery-service module. Step 4. Inter-service communication between reactive microservices with WebClient An inter-service communication is realized by the WebClient from Spring WebFlux project.

    So, the first step is to declare a client builder bean with LoadBalanced annotation. Bean LoadBalanced public WebClient. It returns object Mono , while the WebClient returns Flux. Now, our main goal is to merge those to publishers and return single Mono object with the list of accounts taken from Flux without blocking the stream. The following fragment of code illustrates how I used WebClient to communicate with other microservice, and then merge the response and result from repository to single Mono object.

    Autowired private WebClient. It is built on top of Spring WebFlux, and thanks to that we may use it as a gateway to our sample system based on reactive microservices. Similar to Spring WebFlux applications it is ran on embedded Netty server.

    To enable it for Spring Boot application just include the following dependency to your project. To disable registration you may set property eureka. To enable it we should set property spring. Now, the last thing that should be done is the configuration of the routes.

    Spring Cloud Gateway provides two types of components that may be configured inside routes: filters and predicates. Predicates are used for matching HTTP requests with route, while filters can be used to modify requests and responses before or after sending the downstream request.

    It enables service discovery location, and defines two routes based on entries in service registry. We have two microservices account-service, customer-service that use MongoDB as a database. URL of account-service is taken from Eureka.

    The whole sample system is hidden behind gateway, which is available under address localhost Now, the first step is to run MongoDB on Docker container. After executing the following command Mongo is available under address Eureka is available under its default address localhost The same rule applies to our sample microservices.

    However, account-service needs to be multiplied in two instances, so you need to override default HTTP port when running second instance using -Dserver. Finally, after running gateway-service we may add some test data. It forward the request to customer-service, and then customer-service calls enpoint exposed by account-service using reactive WebClient. The result is visible below. Conclusion Since Spring 5 and Spring Boot 2.

    The main goal of this article is to show you how to use Spring WebFlux together with Spring Cloud projects in order to provide such a mechanisms like service discovery, load balancing or API gateway for reactive microservices build on top of Spring Boot.

    Before Spring 5 the lack of support for reactive microservices was one of the drawback of Spring framework, but now with Spring WebFlux it is no longer the case.

    Tutorial: Reactive Spring Boot Part 2 – A REST Client for Reactive Streams

    Technically, the Reactor is a fourth-generation reactive library, based on the Reactive Streams specification, for building non-blocking applications on the JVM. The reactor also supports non-blocking inter-process communication with the reactor-netty project. Reactor Netty is suited for microservices architecture. Who uses the Reactor? What is Reactive Programming? Reactive programming is a different way of handling data and events. Code becomes cleaner and more maintainable when you use reactive programming.

    In reactive programming, you consider everything as asynchronous data streams or async event streams. These streams could be UI-events, tweets, video streams, data coming from sockets, and so on. Imagine, you have Observers assigned to these streams which can react to every event. Then there are magic functions to Filter, Combine, or even to report an error when it happens. As you know, data can be of any forms, a stream of data, a stream of events, data from REST API, data from WebSockets, the user events such as mouse clicks, key inputs and so on.

    In Reactive Programming, this is what is known as Observable sequences. A function can subscribe to these Observables to receive asynchronous data whenever one arrives and they are Subscribers. Now, our main goal is to merge those to publishers and return single Mono object with the list of accounts taken from Flux without blocking the stream.

    The following fragment of code illustrates how I used WebClient to communicate with other microservice, and then merge the response and result from repository to single Mono object. Autowired private WebClient. It is built on top of Spring WebFlux, and thanks to that we may use it as a gateway to our sample system based on reactive microservices. Similar to Spring WebFlux applications it is ran on embedded Netty server.

    To enable it for Spring Boot application just include the following dependency to your project. To disable registration you may set property eureka. To enable it we should set property spring.

    Now, the last thing that should be done is the configuration of the routes. Spring Cloud Gateway provides two types of components that may be configured inside routes: filters and predicates. Predicates are used for matching HTTP requests with route, while filters can be used to modify requests and responses before or after sending the downstream request.

    It enables service discovery location, and defines two routes based on entries in service registry. We have two microservices account-service, customer-service that use MongoDB as a database. URL of account-service is taken from Eureka. The whole sample system is hidden behind gateway, which is available under address localhost Now, the first step is to run MongoDB on Docker container. After the introduction, you will learn how to use RSocket in combination with Spring Boot.

    RSocket is a communication protocol which embraces the Reactive principles. This means that RSocket uses asynchronuous communication.

    Getting started with Project Reactor

    It is also suited for push notifications. When using HTTP for example, there will be a need for polling in order to check whether new messages are available.

    This causes unnessary network load. RSocket provides a solution for this. In the next sections, you will find examples for each communication model: the server side, client side and a unit test.

    The source code being used in this post is of course available at GitHub.

    Spring Cloud Hoxton.SR9 gateway and reactor-netty 0.9.0.RELEASE is Incompatible [How to Solve]

    Reference Documentation Before getting started, it is useful to know where some interesting documentation can be found. During writing this blog, it appeared that reference documentation, examples, etc.

    This list should give you a flying start when taking your first steps with RSocket. All information about the protocol, the specification, implementations can be found at the official RSocket website. The Spring Boot reference section for the RSocket protocol. Ben Wilcock has written some awesome blogs about the RSocket protocol. The complete list can be found at GitHub.


    Reactor netty tutorial