 So, I said my name is Sergei, so last week I was at the spring in Barcelona, actually that's my conference, which is the best, actually not the best only, but the only spring conference in Europe, right, and the biggest one. So Michael, one of the organizers of the Jack just asked me to deliver a talk here because I was here anyway for the Box Day. So, I didn't have anything ready to prepare, but at least we get to do a quick introduction to the new generation of spring applications. So how many of you use Spring Boot here? Spring Boot 2, some of you, Spring 5, none of you, weird, all right, so there have been a lot of efforts in the new releases of Spring, not only Spring 5, but also Spring Boot 2, which came out a couple of months ago, and most of the efforts have gone towards the reactive support in the core also, but in Spring Boot, but also in the entire portfolio. So that applies to spring security, spring data, spring cloud, spring integration and many other projects in the portfolio, right, because we're basically changing the programming model, right, so from an imperative programming style into a more declarative programming style, right, so we're going to see in the next slides, right, so what Spring 5 and Spring Boot 2 basically give us, right, as this new generation of applications. So going to lead you through a quick introduction on the new features, and then I'm going to try to start with the current model of programming, like traditional Spring MVC, and we're going to move it to a WebFlux model, which is what Spring 5 is introducing, right, if that's going to be in a demo. So we're going to basically move from the blocking kind of operations and basically models, right, because we have been using this model for a long time, right, so usually we had web applications with traditional server containers using the server APIs, right, interacting with data stores which were blocking, right, at the end of the day every single request got a new thread of execution, so we're basically having a bunch of threads, right, a thread pool that we shared, right, but this is not scaling anymore, right, so if we take a look at other languages like Node.js, Node.js had the concept of the event loop, and we basically had non-blocking operations and basically reusing the same threads for the different tasks that we had, so it was more an event-driven model, right, so it's not like a blocking model, but we're going to move into a non-blocking model, and this has a lot of benefits, right, so we need just a small thread pool, so we don't need a different thread for every single request, we can live with a small number of threads, and basically we'll be processing events on those threads, right, so we're not going to block on those threads, so the important thing here is to be non-blocking from end to end, so if you have a database which is blocking, if you're using JPA, if you're using a relational database and there's no asynchronous driver, well, you're going to block anyway, right, so actually we should be blocking from all the way down, so take a look at the current model, right, so the current model where we have the servlet API is basically based on a servlet container, so we use Tunket, we use Jetty, we use Undertow, we use any servlet container, on top of that we use the servlet API, right, so we all know a servlet API and we basically use frameworks that rely on the servlet API, and if you're using a spring, you probably use a spring MVC, maybe you're using Jersey or any other framework, but at the end of the day this is a blocking model, so we're going to move into a non-blocking or reactive model, so now we're not going to rely on the servlet API because it's actually a blocking, but we're going to use a container like NETI or any of the servlet 3.1 containers which support async operations already, so on top of that, I said we're not going to use the servlet API because it's blocking, but we're going to have some adapters which will use the reactive stream specification, so we're going to see that the project reactor which is part of the portfolio of a spring is an implementation of the reactive stream specification, so spring 5 introduces a new web framework which is called WebFlex, so it's important to understand that this is not replacing what we had, it's the spring MVC framework, so both models are valid options, it just depends on the use case, but we have both frameworks side by side, so we don't have to question whether we should use a spring MVC or a spring WebFlex, well both are options and depending on what you need you might be using one or another one, so if you're using a relational database which is blocking and you have blocking operations on the way down, so maybe you'll be using a spring MVC, if you have just non-blocking operations all the way down and you are using MongoDB or Redis or Cassandra and you have a non-blocking reactive driver, then you might be using a WebFlex framework, so we have actually these two different stacks that make sense depending on the use case that you have, so reactive spring came out not to be fast or faster, but to scale better, so if you take your traditional spring MVC application and try to migrate it to WebFlex, you'll see that you won't get any more performance or actually any more speed, so the speed will be the same, so the reactive support, the reactive web framework is not meant to be faster than a spring MVC, but more scalable than a spring MVC, so when you have slow consumers or devices like a mobile phone or application that might have a slow connection or you have slow producers as well, so that's basically where you're going to get more performance, so basically it's meant for scalability and not speed, so everything will be non-blocking as we have seen, and we're going to focus on events, so we're going to have events that we'll be subscribing on and we're going to react on those events, so that spring MVC is also an option, it's not that we have to use the WebFlex, the new framework that comes in spring five, so we have actually these two stacks, so spring five is using the project reactor underneath, that's one of the building blocks of a spring five, and now a spring boot as well, and every single project that uses all the reactive support, so the project reactor is basically an implementation of the reactive streams specification, and we have actually different implementations, it just happens that we have one in the portfolio set, this is not part of a spring, it's its own project, but the spring is using it underneath, so the two main types that the project reactor has are basically the mono and the flux, the mono specifies one single value, so that's saying well we have something right, and this something is defined as a type, parametrized, and the flux are basically zero or more values, so this is basically three more values, which is unbounded in time, so we don't know when they are going to come, but we are going to have a stream of values and we can react on those values, so all these types are going to be used everywhere in the portfolio, we can use them in the web layer, in the data access layer, and in many other projects like the spring cloud and so on in the spring portfolio, so we take a look at the WebFlex framework, which is a set, the new framework that the spring 5 introduces, which comes into flavors, so the first one is the annotation-based flavor and the other one is the functional flavor, so how many of you use the spring and vc again? Some of you? All right, so we are used to the traditional controllers, like the at controllers, rest controllers with the request mappings and so on, you don't have to relearn the framework, you can reuse the concepts that you basically know and use the reactive types on those controllers, so we are basically adopting the same programming model that we know, because well, even if this is a new programming style, we don't have to give away all our knowledge, so if you take a look at what happens underneath, the runtime is completely different, so we see that, well, we go asynchronous, non-blocking, compared to the blocking synchronous server API, but the programming style will be kind of the same, so if you want to get started with the WebFlex framework, maybe the annotation-based configuration might be the easiest way to do it, but if you are more a functional guy, we have also the functional support, so in here, we see an alternative to the controllers, which is basically a way to have control over how to map the request into a handler, so what we can do is to basically create the router functions and define that we have maybe, get request, and we pass this request to a handler, so we can inline the processing of this request, so you can define a lambda expression in there, or you can use method references that will go to a method to process those requests, so that's basically what we have seen, that's basically the WebFlex framework, so either the annotation-based or the more functional way to configure the WebPlayer, and well, if you take a look at the handlers, those handlers will return a mono, so we can return Flux as well, so we'll be using Flux and Mono at everywhere where we deal with reactive types, so we're going to see that in the demo. So Spring 5 also provides reactive WebSockets, so WebSockets have been supported in Spring 4 a long time since Spring 4, and actually we had support for raw WebSockets and also for sub-protocols like Stomp over WebSockets, so Spring 5 introduces their reactive support for WebSockets, we don't have a sub-protocol available for reactive WebSockets, but at least we can deal with the messages coming in, coming out, reactively, using Monos and Fluxes as well, so how many of you use WebSockets here? I've tried WebSockets, some of you. Another place where we have reactive support is Spring Data. Spring Data is the umbrella project to all the data access technologies, so it could be JPA, but also RedEase, Cassandra, Elasticsearch, Solar, so there are some community-driven projects and some other projects maintained by Pivotal. So the new Spring Data K release is basically having the reactive support, so that means that we'll have reactive templates, so the templates are these APIs like the Mongo template and RedEase template that have these one liner methods to basically interact with the database, and then we have also a way to create reactive repositories, so basically you can create the interfaces and get these instant repositories for reactive non-blocking repositories. So in terms of the reactive protocols, so currently we have MongoDB, Cassandra, RedEase, and Couchface, so of course JPA is blocking, so there's no way to have a reactive repository for JPA, so there are some works towards the JDBC asynchronous drivers, but that's, the work is ongoing, so there's no support for creating instant repositories for relational databases currently, but at least we'll have support for multiple non-SQL data stores. Spring Security is also another project that has embraced reactive, because if we take a look at how Spring Security worked previously, for a web request we usually had a servlet filter, so the servlet filter in accepted the request, it applied the authorization and authentication before delegating to our controllers, so now we don't have or we don't use the servlet API anymore, so that model is gone, we cannot use it, because we don't rely on the servlet API, so having or placing a servlet filter in before the controllers is not an option anymore, so Spring Security has done a lot of work to be able to integrate with a webflux framework, but also for the reactive method security, because previously the security context was stored as a third local variable, and we know that in a reactive environment we'll be reusing those threats, so we won't have a threat per request, so that's gone, that model is gone, so that means that what we had before, which was storing the security context in the threat local, is not a valid option anymore, so Spring Security is now using the context from the project reactor that actually works with a reactive environment, so Spring Security 5 can protect any request that is handled by webflux, any request which is reactive that goes into any method in your application, and also has some nice testing support, but if we take a look at the way to configure Spring Security in a reactive environment, that hasn't changed much from the configuration we had, it just happens that, well, again, we reuse the knowledge we have, but how everything works underneath completely changes, so this is kind of transparent to us, so internally we wouldn't use any cell filter, we wouldn't use any threat local, so everything has changed quite a lot. In terms of messaging, and especially when you build microservices, there's a project called the Spring Cloud Stream, so Spring Cloud Stream will help you deal with microservice communication by using any messaging broker, this could be Kafka, this could be RabbitMQ, but the main goal is that we have different microservices, and we kind of extract the messaging layer, and we send messages and consume messages from those microservices, basically abstracting from the broker we have in between. So, a Spring Cloud Stream basically uses Spring Integration underneath to provide this connectivity to different brokers, the set could be RabbitMQ or Kafka, and it also has our integration with the reactive support, so anyone used Spring Cloud Stream before? No? All right, so I'm not going to go in depth with that, but basically what we'll have is a channel, that's where we can get messages from, and this can be defined as a flux, so this is an unbound message stream, so basically consume messages as they come, and we can also send and publish messages to another channel that will be consumed maybe by another microservice. So Spring Boot Actuator has also had quite a lot of work in the Spring Boot too, anyone familiar with the Actuator? Some of you? So the Actuator exposes different endpoints, so you can know about the health of your application, or you know about the environment variables of your application, or the beans that are in the application context, and many other things. So now the Actuator works for Spring MVC, for web flags, or even with Jersey, so regardless of the web framework you use, the Actuator will give you all this information and metrics as well that will give you a lot of information at runtime. If you want to extend Spring Boot Actuator you can also do it easily, so in Spring Boot 2 there have been some annotations introduced so you can extend the Actuator endpoints and expose your own endpoints. One of the main changes in Spring Boot 2 and in the Actuator is that micrometer has been introduced, so micrometer is basically like SL4J, but for metrics, in SL4J we have different login frameworks, but we want FSAT on top of those frameworks that abstracts us from what we have underneath. So micrometer is basically the same thing, but for metrics. So we didn't care whether we used for myth use, or Netflix Atlas, or Influx, or Datadoc, so we want to have a consistent way or an abstraction on top of that, so we can use the same API, but just by changing the implementation we can actually use for myth use, or Atlas, or Influx, which is really nice. So previously in Spring Boot 1, if you use the Actuator metrics, we had kind of unidimensional metrics, so that means that, well, you could have the number of requests, for instance, but then if you wanted to drill down a little bit and you wanted to get only the GET request but not the POST request, you had to create yet another metric, so maybe HTTP request dot GET or dot POST. If you wanted to get the 404 errors in a GET request, you had to create yet another metric, which was HTTP dot request dot GET dot 404. So it was kind of unidimensional, so micrometer is basically giving you a multidimensional perspective on the metrics. So we're going to add tags to the metrics, so you can analyze these metrics by different tags. We're going to see an example in a second. Another thing that has been introduced, which is getting a lot of traction, is the Kotlin support. How many of you used Kotlin? Heard about it. So Kotlin has a lot of traction in the Android community. So Kotlin is an official language in Android, so maybe in Gradle as well, a little bit of traction. Spring 5 is also embracing Kotlin. So now when you go to the initializer, you can select Kotlin as a language. We have Java, we have Kotlin, we have Ruby, and it's basically a JVM language, but it's more concise, statically typed, and you have more elegant code, more concise. So I recommend you to check out Kotlin and the integration. So for now, we have Spring framework, which supports Kotlin, Spring Buddha as well, Spring data, and the reactor project. So set, when you go to the initializer, which is basically how we start a Spring project, you can select Kotlin as a language. So this will create already a Gradle or a Maven project with Kotlin. I'll take a look at some examples of basic things in Kotlin. So if you take a look at the main class, like a speaker. So we see that what we don't have to use or define the getters, setters, and this is actually more concise. So we define, this is like an immutable class. So we define the different properties of the class, like as ID, the name, the last name, and notice how the last name can be optional. So this is basically defined in a constructor with three parameters. And in order to avoid having more than one constructor, we are going to define one of these parameters as being optional. So this is a more concise way to define our classes. So take a look at the REST controller. So the REST controller, this is just a Spring MVC controller, basically defines the speaker repository as a dependency. And notice how we have these concise methods or these concise mappings, like the get mapping, where we define a function, which is the find all. And the type of the return of this function is basically taken from what the repository find all will return. So we don't have to explicitly define the type. So that's going to be inferred. And the same applies to the second mapping. So this is concise. We use the path variable notation, and we basically delegate to the repository find by name. HTTP 2. So using or enabling HTTP 2 in Spring Boot 2 is quite easy. So you just need to enable the property server.htp2 enabled in the application of properties. But that requires something else depending on the server container that we use. So for instance, on Tomcat, if you use Tomcat 9 with the JDK 9, everything works fine. Because the JDK 9 is what has the HTTP 2 support. But if you're using the JDK 8, then you'll have to use a library. So you can pass where the library is as a JVM argument. The same applies to Jetty. So it requires another library, which you have to pass. And in terms of undertow, everything will work with the JDK 8. So there's nothing to be configured. But if you need to use Tomcat, then either you have to use Tomcat 9 with JDK 9 or pass the library. But it's quite straightforward to enable the HTTP 2 support. All right. Any questions so far? No? So how much time do we have? Ten minutes. Ten minutes only. Okay. So I wanted to do the demo from scratch. But I think that we won't have time to do it in ten minutes. I have the same problem as before that this freezes my IntelliJ. I don't know why. It's actually the adapter which is freezing my IntelliJ. V.I., that would be an option, yes. It's kind of weird because I plug the adapter and then all of a sudden my IntelliJ freezes, but not my computer, right? So it's just IntelliJ freezing. So if you turn off the computer and try it off now, does it? Yeah, you can. Does it? Okay. Now it's working. This time and now my IntelliJ crashed. We'll have to focus a little bit. Hey, what's going there? Almost there. All right. So as we have like five minutes left only, I'm just going to show it real quick. So I'll try to build a reactive web application from end to end. So I have here my domain class which is a speaker. And if you're familiar with Spring Data, Spring Data is the umbrella project for a lot of different repository technologies. I'm going to use MongoDB in this case. So in JPA, we have the standard annotations to map our domain objects into the relational database. And Spring Data, as other data stores don't have these annotations, provides annotations so you can map those domain objects into the data source. As we are using MongoDB, we'll be using the add documentation. We're going to give an ID to this document which is going to be this string. All right. So now we'll create a repository. So this repository is a reactive repository. So we have reactive support for MongoDB. So if you're using Spring Data, you can use the reactive repositories. So we'll be extending from the reactive Mongo repository. So we take a look at the definition of the reactive repository. This is extending from the reactive sorting repository. And this is at the same time extending from the reactive repository and from the reactive repository itself. So we're going to inherit all the CRAT operations and sorting operations and all the Mongo operations for free. So these are called the instant repositories. So we don't need to implement anything. So we basically define an interface that extends one of the Spring Data MongoDB interfaces. And this will inherit all these methods. And at run time, there will be a proxy created that will have all the implemented methods. So you can just take this interface, inject it anywhere, and use any of the methods that have been created. But you can also define your own methods. And that's basically what we see here is that we create a find by name method. So we're basically getting a name and we're going to return a mono. So when you call this method, it will come back to you right away. So this is non-blocking. So we call it and it comes back to you right away. So eventually, this single value, because Mongo is a single value, will be available to you. We take a look at the other methods. In the interface, we see that some of them return mono, some of them return flux, depending on the operation. But basically, we're going to get single values or multiple values depending on what we are doing. All right. So we take a look at the controllers, the reactive controllers. So this is just a normal spring MVC controller, or it looks like a spring MVC controller. But we are using the reactive types instead of returns like objects or strings and so on. So we see, or we have seen that now our repository returns monos and flux. So we think we can take those monos and flux and send them directly upwards. Oops. I just messed around with that. Sorry about that. All right. So we can just take those monos and fluxes and send them directly to the MVC layer. So now we see how we return flux. When we want to get speakers, for instance, we call the find all from the repository, we return a flux and we return a mono when we find a speaker by name. So one of the main things about the project reactor, if you have used ARAC Java or ARAC JS, you'll be familiar with that. But one of the main things of project reactor is the composition of the different methods. So all the mono and fluxes can be composed. And we can apply operations on those monos and fluxes. So then I have an entry point. So that's basically the springboard application. And what I have here is an application runner that will run just before the application starts. So I'm basically creating a couple of speakers. I'm creating a flux with specific names of speakers, Sergey and Michael. I'm going to map those into a speaker class. So I'm going to create a new speaker out of these names. And then I'm going to pass them to the repository to store them. So notice that this is just a flux definition. So we are moving from the imperative programming style from when this happens then do this to a more declarative style. So I'm taking these names and just mapping them to speakers and then please store them. So this flux definition is not doing anything until we subscribe to it. So this is basically the definition. This is our publisher. This is basically what will create the speakers. But when we write this, nothing will happen until we subscribe. So the subscription is down here. But before doing the subscription, we want to make sure that no speaker is on MongoDB. So the first thing that we'll do is delete everything on the database. And this is also a known blocking operation. So that means that if we just do delete all and then right away insert, well, the delete all comes back to you right away. So we have to wait somehow. And blocking doesn't seem to be the right option. So what we're going to do is to compose this call. So we're going to delete all and then let's do something else. So this is basically saying, well, just complete this operation. And then let's do something else. And I'm going to pass what I just created here, which is this flux, this producer. So I'm going to get those names, map them to speakers, and basically store them into the database. All right. And then we have here the alternative way to declare our endpoints. So this is the functional web framework. So now instead of using a controller, what we're going to do is to define the router functions where we define the route for the speaker name. And then we define here a lambda expression. So this is basically what will process the request. So when we get this, then we're going to get a path variable, and we're going to create an OK response. So that means a 200 status code response with the body being what's returned by the fine name method on the repository. So if we start this just real quick, I have the port open somewhere. So if I have it somewhere, that means that this is running somewhere. All right. So when I go to localhost, 8080 slash speakers, I'm going to see both of them. Speakers, I think it's a speaker's name or speaker. Can't remember. OK. So I see one of them. And the last thing that I want to show you is the actuator. So when we hit slash actuator in spring boot, you'll see a bunch of endpoints that are exposed automatically for you. So you'll see endpoints that will tell you the different bins that you have in the application context. So if you want to see everything which is in the application context, they are all listed in here. You're going to see endpoints that will tell you the health of the application, for instance, whether it's up or not. And you can have more information here. Things like the environment variables that have been passed. So this is basically all the environment variables that you have coming from different property sources. And one of the interesting endpoints is this one here, the metrics endpoint. So set in a spring boot, we're going to use micrometer for the metrics. So when you click on the metrics endpoint, it will tell you different metrics that you have available. And I said these metrics will have a multi-dimensional approach. So let's take a look, for instance, to the J80p servlet request. So I'm going to take a look at this metric. And we see that we got like 16 requests. And we have some available tags. So these available tags will give us this multi-dimension that we require to query this metric. So what if I want to get, for instance, I don't know, let me just go back, the 404 request or a 200 request. So I can just use this tag to say I want to get the metric which is called HTTP server request, but could you please filter them by this tag? And we can actually filter by multiple tags. I can say I want to get the get request that ended up in a 404. And we can do that by passing in a parameter which is called tag, and then we pass key value pairs, status, 404. So now we see that we have three requests that ended up in a 404. So that's basically the exception that we got with this request. All right. So I think that we'll have to leave it here. So that was real quick demo. But at least takeaway would be that, well, in Spring 5, Spring Boot, a lot of efforts have been going towards the reactive non-blocking support. So we have all the reactive non-blocking types, which come from the project reactor available, not only in the core, but also on all the other projects in the portfolio, like spring data, spring integration, spring cloud stream, and many other projects in the portfolio. So you can have an end-to-end reactive pipeline. And that's the important thing to end up having this pipeline from end-to-end. So not having any of these questions which are blocking, because otherwise you won't benefit from the reactive support. Do you have any time for questions? Like if there are any questions? A couple of questions, maybe? So authentication is blocking, but I talk about spring security integrating with all the web flux and all the methods that use these monos and fluxes. Of course you want to authenticate and you want to basically authenticate before reaching the dispatcher service. So this process will be kind of upfront, actually before what's done afterwards. Yes. So that's gone. So that's basically what we had, but as we are reusing those threads in different requests, so the one thread per request doesn't apply anymore in web flux. Spring security is using the project reactor context which is basically the alternative to the thread local. And that's where you can get the security context from. That's from the context of the project reactor. Any other question here? So I think we'll leave it here. Thank you.