Atlanta - Reactive Spring

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so I'm Kenny Bustani I'm a spring developer advocate at pivotal a little bit about myself from Silicon Valley mostly on an airplane I traveled around the world as Andrew said talking about spring spreading the good word of spring also while doing that I wrote a book with Josh long my coworker at pivotal who's also a spring developer advocate called cloud native Java which is all about building micro services using spring boots spring cloud and cloud foundry I highly recommend it okay so what are going to be talking about today we're going to be talking about reactive in spring five so we'll go over what's a reactive application so a little bit of an introduction kind of where we came from why we why we need this why you need this why it's important to microservices yep so why does it matter how do you do it in spring so I'll show you a little demo from scratch using start dot spring that IO will spin up a spring boot application and we'll create a reactive web front-end so what's changed why why do we need this what is what changes from the java api x' some examples and then you know no Tech Talk would be complete without a mention of microservices so we'll look at reactive microservices all right so reactive applications so what's different with reactive applications all right so they're fundamentally non-blocking and event-driven so what that means is that you're gonna have two models you'll have a publisher and you'll have a subscriber publisher is gonna send events and a subscriber is going to subscribe to them right and this creates a asynchronous control flow which means that I'm not gonna have threads be blocked while I'm making a downstream request so in micro services for example I have service a call service B call service C D E right and so what happens is that each time I make a request to a downstream micro service I'm keeping threads open right so if we have a lot of communication between these applications we're we're consuming a lot of resources keeping these threads open so it's not very it's not a very efficient means of managing resources so what's fundamentally different about reactive web applications is that they have a smaller number of threads and the flow control changes so if you've used nodejs or javascript hopefully now you're gonna notice that it will have callbacks right so you have these callbacks when you make a call to a MongoDB database you're gonna also provide a callback that will be called by the producer the publisher when it's done getting a subset of data and so that's the gist now where we came from so let's do a little bit of an overview of concurrency in the async API is in Java so here we have a basic use case return value and I have a user repository and I'm going to find a user by an ID right so pretty basic it throws an IO exception if something goes wrong when I contact the database I'm going to throw an IO exception now here's the usage so there's my repository interface and going to have an instance of it I will call that find bi ayyi and it returns back a user and I have to try a catch I have to catch the IO exception if something happens now with a future what changes is we don't need to throw a exception because that is likely going to happen in another thread so we won't be able to catch it and we're just going to add in their future and then we're going to provide the user object now I'm going to call same call back to that repository user repository dot find by ID and I'm going to assign that to a future variable which wraps a user object and now I'll call future get that's going to block and then when it's finished it's going to return back that user now the ugh here is that it's very ugly having to try catch all these exceptions so if you have a micro service and you're making a lot of HTTP calls using rest template with spring boot you might notice you have a ton of try catch statements it just becomes kind of ugly and inconvenient to read that all the time and it becomes inconvenient to try catch all the time so it's not really I guess that this the best syntactic sugar so here we're gonna return a value with completable future it's a bit different this is in the Java eight streams API so here we have repository dot find by ID we'll assign that to a future which is completable future and here we're that's where we're going to provide a callback right so in like like Java Script with a callback gonna call future when complete and we're gonna provide here a lambda expression with user and throwable so user is then going to be provided into that method and we're going to be able to subscribe to when that event comes in and then we'll be able to get that user and here we're gonna return a collection so I have my user repository I have my completable future and then inside that I have my collection my list of user and it's as a fine doll method so I'm gonna get all users in the database and I have my void which is going to allow me to I'm not gonna get anything back but I'm gonna subscribe to when it's complete or if an error own and so there's three fundamental things here right we have a single item we have a collection and we have a void so with completable future what's good about it is that it's fundamentally the right idea for usin async idea API right so that contract is actually quite good and so we want to use that there's some deficiencies there we'll go over so it also allows declarative composition of async logic I'm gonna keep C's lambda expressions readable right so we have much more readable code the problem is it's not ideal for collection return values or is it good for latency sensitive data sets so if I have a ton of items in my collection and I'm returning that all I have to read that entire collection I can't get them one by one which is much more efficient so it's not very good for large infinite data sets and by not good it won't work because you'll never get back that infinite data set so we can't do it so what if we returned a stream so in Java we have the streams API and so let's look at what that might look like so here I have a flow of data and let's say that I have an on next method right so I'm subscribing to a result and I'm calling on next I'm providing some function some call back and I'm gonna consume that data right so I'll have dot on next dot on next dot on next and then on error or on complete so I can't really subscribe to an error and then continue in this and that's something that we can do with reactive streams so a couple use cases here I guess I should go through all these so the success use case here I'm gonna use the on complete notification right so we can kind of think about this as notifications if anyone's a front-end developer in the room I started as a front-end developer and so if you're familiar with jQuery and JavaScript and event handling it actually is quite similar to what we're doing here right so our notifications are just going to be subscribers to these events so for success I'm gonna say I'm complete for error I'm gonna stay on error I'm gonna get that throwable exception and I'll be able to respond to it for match I'm gonna use on next and then on complete for no match shall also use on complete for a failure I'll use on error for that sorry we're going void to item so for user items match no match and failure and then finally for collections we have two matches here we'll call on next we'll get the user on next and then on completes for no match then just on complete and then failure on air pretty simple but we're going to keep going back to this pattern here which is void item collection so for the Java 8 stream things have gotten a lot better and in spring five we have support underneath the hood we finally support Java eight until we can our engineers in spring can actually use Java eight streams we haven't been able to do that until now they're very excited about it so it's a great example of the kind of api's we'd like again right we think it's a good contracts good model also it's a declarative composition of async logic per item meaning that we can stream results per item and get them back and then respond to them and then again lambdas keep things readable but it's built for collections and it's not active for a hot source of data so what that means is that if we have a micro service or to a client and then a micro service right it's a remote server and we call that with the streams API we can't get individual items from that HTTP connection right so it's not a hot source of data and it's not very good for latency sensitive data sequences because if we return collection we have to return everything okay so the good stuff reactive streams so this is the new stuff this is the stuff that you're going to be able to use in spring five you can use it today with project reactor which is another project that we have but what's reactive Spring is is really it brings in project for reactor into the Spring Framework internals and allows you to use flux and mono which is an item in a collection alright so metaphor time alright so we're in Atlanta let people in Atlanta like baseball I think I'm pretty sure I liked baseball when I was here I was the Braves fan still AM although I'm kind of a Giants fan now but here we have an example of a batting cage who both do batting cages is curious anyone great metaphor alright good job Kenny alright so here we have a reactive example I have a publisher which is my pitcher who's going to be throwing a stream of balls and then I have my batter who's going to hit those balls right so each one of these baseballs and it is an event right and so typically there's gonna be some time in between each one the batter has to be ready before the pitcher throws the ball right so this is an example of a publisher who's going to publish to a subscriber and the subscriber is going to respond to that event and hit the ball out of the park now that's reactive now what's not reactive is what most people are doing today which is this right so my subscriber is just going to sit there and it doesn't matter whether or not they're ready the balls are going to come anyways right so if you want to think about calling any HTTP server without reactive support is kind of like the server's not smart about how it's sending thing it's just going to send a constant stream of baseballs and your clients just going to have to be able to handle that right so reactive stream this allows the publisher subscriber model to communicate bidirectionally so if I'm a subscriber I can let that publisher know when I'm ready for more items right and I can do that remotely I can do that across the network which is really powerful and so we have some interfaces here for that our publisher has a subscribe method which provides a lambda which is going to be a subscriber and then our subscriber interface which is going to be of a type has on subscribe on next on air on complete we saw that earlier and there's this idea here about back pressure so back pressure is interesting right because what it means is that as a subscriber I can tell the publisher how I want to receive my data so if I want to receive it all at once I can certainly do that I don't want to do that I can receive them one by one or I can say I'm gonna receive them in a subset so maybe I'll see two items I'll download them I'll perform an action say I'm ready for more and I can define that from the subscriber side so Project reactor has been around for a little bit based on our extremes library for the jvm who's using Project reactor today just curious this is really cool stuff so one one guy good so this is really really cool stuff and it says a lot that the spring team is going to include this into Spring Framework because there's a lot of going a lot that needs to be overhauled for that right so you have two different models you have one that we've always been using right and that's the non reactive and then we have side-by-side and parallel all of the reactive libraries and so this is a huge undertaking with Spring 5 and so we're very excited about it and we want to provide that support so with project reactor it's going to provide you declarative operations on items that's very similar to the Java age stream so if you use Java 8 streams today projects reactor is going to provide you a very similar experience to that so going back to the idea of Lloyd item collection the types that we're going to use for this a flux is going to be a collection and a mono is going to be a single item so in a mono which is 0 or 1 this is a diagram here that explains how that works it's very boring but I'm going to walk through it so gonna have my operator here that's going to perform a data transformation right so I have my publisher on top that's gonna publish that single zero or one items and that's going to go to the operator and the operator is gonna make a transformation and it's gonna send it along to the subscriber now we can have a chain of these operators right with Java streams which means that I can have one operator that's maybe a filter and that's going to constrain a subset and then maybe I have a mapper which is going to turn that into a different item and then if something happens along the way we are going to at the very bottom that arrow represents time we will just replace that with an X meaning that it terminates that stream now for a flux a collection we're just going to be streaming a set of items one by one so here I have one that's gonna go to the operator and asynchronously there at the bottom our subscriber is going to it's going to subscribe to each one of these items that's coming through that pipe being transformed and then it's going to do something but the idea here is that I don't have to wait for each operator to complete on the entire collection it's going to be one by one so I'll have one operator who's going to send along to the next operator and this is all happening asynchronously we also have reactive repository support so we can actually stream things from a database that's very exciting with MongoDB we have reactive support they support a reactive driver which means that if I make a query to MongoDB I don't have to wait for everything to return I can use reactive streams as a subscriber to say I'm ready for more data and MongoDB will send it along so here in this example I have a basic user repository which is I have a mono user flux user and mono voice so I have that item collection and void now how we're gonna use this is I have a repository dot find all you're pretty familiar with that and that's not actually going to get all of those items right it's going to say this is what my pipeline looks like I'm gonna set up a subscriber model I'm then going to apply in operation asynchronously to each item in this stream and they're just gonna flow through this these operators right so I start with filter I'm going to filter a subset of users and then I'm going to map that and then I'm gonna log it out and then to actually kick this off we have to use the SUBSCRIBE method and the SUBSCRIBE method is going to start this chain right so until we call subscribe we're not going to it's not going to do anything it's really just a definition so the subscriber triggers the flow of data and then one by one they will filter and trickle down all the way to the log so by default we consume without back pressure which just means we're just going to get them one by one but what back pressure means is that we can tune this dial up and down we can adjust the volume to set the capacity which means here's my buffer size I'm going to add a used capacity operator which means that I'm going to apply each operator to two items return that so subscriber will get those two items and then go on to the next two so I can dial that up and down so here I have on subscribe I'm gonna request two on next Jason on next J and then I'll request another two and then Joe and John so with spring data spring data project so we have a bunch of spring data projects who's using spring data today great so we have a few projects in there that have reactive support today they're not GA yet but we have we have Redis I believe Cassandra there's a few others but what that looks like here is the only thing that's really changed is again we have mono and flux to return items in collection and then we're just appending reactive on to everything so if you're familiar with spring data we have this beautiful abstraction model which at the very bottom we have the base repository and then all of the different databases have their specific traits and so we have these abstractions that pile on top of it so if I'm using spring day 2 JPA I'm gonna get relational database specific traits if I'm using a MongoDB I'm gonna get document database traits so each so here we have the abstraction crud repository we're just adding on reactive repository and so each one of the vendors the database projects can then implement that and so again what changes here I'm just going to find by first name and last name I want to find one person I'll return back a mono person if I want to return back a collection I'll use flux person now what that looks like is in the frameworks we have end-to-end back pressure right so from the HTTP HTTP server to the web frameworks the controller to the repository and to end means that we're going to be able to stream from the database all the way to the front-end so as you're in the front-end you're actually going to be seeing one by one this stream of data coming in directly from the database right so this chain it's not going to hold these blocking threads it's gonna stream them all the way to the front-end so it's very very efficient we also have reactive web support so again if I if you want to have a web MVC controller that streams data from a repository you can certainly do that and I'll show you an example of that but what that looks like here is now in spring five you'll be able to return back a mono or a flux and you just get it out of the box right so if you return back a mono and your repository supports reactive streams it's just going to stream that to your client now here's the this is why we think it's so important why you're again and the spring team thinks it's so important is that this is the biggest overhaul and a very long time to Spring Framework and so what what we're seeing here is we have side by side components right so we had Spring web MVC and side by side we're going to support which is now called spring web flux which is the reactive version of spring web NBC right so we'll be able to use both of those now they're going to share a set of libraries but they are two separate implementations so we have our servlet servlet API for web MVC and then for web flux we have now reactive HTTP which is going to sit on top of our reactive application server which is either neti undertow servlet 3.1 but for the servlet API for spring web MVC we have just our basic servlet container so again it's parallel to spring web MVC so you can use your rest controllers with reactive streams in spring five and that it operates on reactive HTTP requests and responses so if you have that chain of micro-services now you're not going to have to wait for each microservice to return a response and collapse the call chain you can stream all the way to the front end even if you're going five deep it's gonna stream results all the way back it's really cool so here's an example of a non-blocking HTTP GET which means that we're just going to return a result we're not going to block the thread so how you would do it with Web MD see here I have a get user method and I'm just going to return a new user I'm not going to call to anything in block and now in the bottom here I have the reactive version of that which is going to return back a mono user and it's going to call a reactive for posit Ori and then we get an async non-blocking method now the reverse of that you can also post so I can post a so I can actually publish on post which is pretty cool so if I'm calling a downstream microservice and I'm adding a set of accounts to a collection I can stream those to that server and it's going to subscribe to those right so we have this model all across the board now this is probably the coolest thing I think that most people will get a kick out of is that we have native support for server sent events now what that means is if you're a front-end developer what you can do is you can call a spring boot application and you can stream to your browser results on demand so you can see an entirely different experience rather than trying to pull things you can keep a connection open and items will flow into the browser and you can see updates and so we're going to take a look at that in a little bit and then finally we have our web clients so if we're making calls from service to service on the backend typically use a rest template I think we're gonna have rest template support that wraps a reactive web client but this is how it works so you have your web client you'll perform in this case I think we're making a post describe your content type and then extract the response all right so let's do a demo so I'm going to fire up start spring that IO and we're going to create a reactive web application so the first thing you'll need to do is I'm going to generate a maven project with Java I'm gonna use spring boots version 2.0 m2 this so that has two this spring five in it so spring boot two is in milestone two and so if I go to my dependencies here I can choose web reactive web instead of regular web and I'm going to name this reactive spring and I'll go ahead and generate my project now I've loaded this loaded this up in my IDE already to make things smooth but here we have a basic spring boot application I added one comment here just to remind myself what I'm doing and so what we're gonna do here is we're going to create a rest controller with spring web flux which is the reactive version of spring MVC and we're going to stream some server sent events to a client and so the first thing I'm going to do here is I'm going to create my class reactive controller and I'm going to annotate it with rest controller and then I'm going to create a method in here public we're going to turn back case set so we'll use flux I'm going to stream a set of messages so that'll be a flux of a string so a collection of string items and we'll say string flux all right so I'm going to provide a request parameter and I'm going to call that what actually let's call this echo it's kind of like an echo and what do I want to echo now I'm going to return back here so this is where that stream API comes in I'm going to call flux and I'm going to generate a new flux I think I'm gonna use interval interval so interval I can create a stream this is really good for demos or if you want to maybe make a call every so often that's some fixed interval I can go here and specify the duration I'm gonna say let's do milliseconds every second would be 1000 all right so now I have a publisher that every second it's going to publish to a subscriber so the next thing I'm going to do here is I'm going to transform that so I'll use map and I'm just gonna return back my what and let's go ahead and hide in here the the long I think that'll be the millisecond okay so that's it so now I'm returning back a flux which is my stream which is continuous so it's going to continuously publish forever indefinitely a stream of strings and what I need to do here on top is I need to add in just like we would before I'm gonna add in a get mapping I'm gonna say path put that at the root and then I'm going to say that I want to send a server sent events and so I'll say produces a text stream media type and that'll be text event stream right so that type here if you go and see it's just text for slash event stream and that tell us the browser that it's going to keep that connection open and stream results all right so we're all set to spin this so I'm gonna go to my terminal here let's make that bigger so maven spring boot run that's how I'm gonna start the application it's gonna start up on port 8080 and we're up and running alright so now if I went from the browser it'll probably just hang because it's stream and I usually if you're consuming a stream you would want to use something like server sent events an event source in JavaScript and so you can actually have an asynchronous callback that will subscribe to items being published by the server but for I'm going to show you that a little bit but I'll just show you from the command line how that works so I can actually just make a curl request which is an HTTP GET and I'm just gonna go to my local host 8080 the root endpoints I do have to provide my what's and that'll be hello and yep and so there we go right so it's keeping up it's keeping that stream open and that server is just continuing to publish data right and so I can have this layered on top of layer with my web servers and a micro service architecture service a call service B and C and it's just gonna stream those all the way to the front-end I think that's pretty cool so I think it's really gonna change the way that we experience the web right so spring boot is one of the most popular if not the most popular framework for Java today so just think about how that's gonna change the user experience for everybody in the world I think it's pretty exciting alright so reactive micro services taking this to the next level I'm going to show you a sample application which is oh I guess we're going to go over micro services first who's heard of micro services yes all right so just a quick review here this is a micro service architecture of an online store that I put together for the cloud native Java book and this is a full sample application so I'm not an ivory tower architect I just I didn't just draw this I drew it and then built it and modified it at the very top I have my web front-end that's my online store web that it's my static JavaScript HTML and then I have my edge servicer which is my API gateway which is a spring boot application I'm going to call that and that's gonna expose me to all these back-end biker services so if I go to Lord slash catalog it's gonna route me to catalog return back a response right so at the very minimum what we have here without reactive is we have a call chain which is 2d so I will call from online store web to the edge service I'll keep that connection open that those threads open they'll block every time I make a downstream call now with reactive support I'm gonna just subscribe to events on the front end and it doesn't matter how far I go it's going to publish items one by one and I can subscribe to them from the front end so now I'm going to show you an example application of this of a Microsoft architecture a new one that I put together which is called spring cloud to do's it's much simpler I realized 10 microservices on a laptop was kind of expensive resource wise for most people so I simplified things and so let's go over the architecture here I call this a service block architecture which combines microservices with serverless functions so with spring cloud function another new project that we just booted up we can create Java server lists Java functions using spring boot and so in this architecture here I have a core service that's my micro service and it's able to publish events to service functions that's gonna write to a reactive MongoDB database and then I can subscribe to those from that core and I can I can see my lambda functions actually a publishing events live which is pretty cool also I have tasks here so to keep things durable every time an event gets created I'm gonna store that's in my rabbitmq message broker that way I'm not gonna lose any events and then I have my my sequel database in the backend alright so I'm gonna fire this up if you're interested in cloning it hits apps I think I have a slide here it's on github and I used to use docker because it made it easy to fire all these things up and then Dave sire told me like that's totally useless you're putting a container side of a container just to run containers so I got better at Bosch and created a bootstrapper so if this is the project and it has so it has a config server discovery service it has a core service and then functions and I'm going to fire all of those up all at once I have to make sure I okay so now it's just building and then one by one it's going to start up these JVM applications so first it's going to start up the so I'm running instead of on AWS lambda I'm going to run my metrics function locally the metrics functions responsible for collecting actuator metrics from all instances of a spring boot application so if you're familiar with actuator we got demo that earlier it's going to allow me to pull and get metrics from my instances and then I can put them in something like Prometheus and then I can create a visualization in grow fauna and so the problem there is that if you have a highly available system you have to pull each one of those instances and so I created a function that's just going to collect them from all of the instances and it's going to aggregate those and I'm going to see a reactive actuator dashboard which we can actually see those actuator metrics updating live so I think we're just about ready alright yep so Eureka is online let's go and see if everything is up and it is so this is my Eureka dashboard which is spring cloud discovery service and this is how I'm going to be able to find other services in my cluster and so I have my core application that's gonna be my back-end API and then I have my static content in my user interface and that'll be my front-end so I have a front-end container and a back-end container which is called a back-end for front-end or a BFF and then my two new core is going to be able to call the metrics function to aggregate those metrics and so if I go to my UI here just gonna bring up an error but I go to route endpoint I get my angularjs application and now I can start creating tasks so I can remember what to do but before I do that let's go ahead and pull up another browser window which is gonna be my actuator metrics I'll pull that here and because I'm using Zul I can actually proxy to the backend that back-end service from the front-end using forward slash API and that's going to proxy to whatever is in that core application and so the dashboard that I created for this is called aggregator dot HTML and so pull that up should be yeah so we see some metrics that have been reported and now I'm going to create a few items so let's see I like grocery store eggs bread expense reports travel slides alright so as I add these items each time I do that it's going to publish a new event to that back-end so we should be able to see this update let's try this again let's go localhost:8080 well I screwed something up the stuff always works when you're by yourself never works when you're up on stage never let's see what have you got here connection refused to rabbit I must have shut it down oh I started up on a different network success that looks good maybe I think maybe it works yeah alright so let's try that again alright so now we should be able to see these events get published to the backend it should update that user interface so eggs bread expense reports and yeah you can see them changing right yeah so if you look at that one here alright so I have another dashboard which is more compelling so I'm going to go to gauges which is a work in progress progress which has some gauges and we should be able to see then throttle so if I do things one of those gauges should hit oh there it goes super exciting right alright so that's about it it's gonna change the web it's gonna change the web alright so I think we're just about out of time so I'm actually gonna open it up for questions any questions cool we'll all be around today and tomorrow oh there's one yes I can provide them to you I I'll give them to Andrew or Taschen and they'll send them out to everyone yeah question the back you don't have to so if you don't support reactive maybe on your front end container then it's just going to get all of those items and then return them but yeah you would probably want to have that entire stack support reactive so you can stream things all the way to the front end now you can be declarative whether or not you want to block or if you want to stream and so that's important for some things right you want to make sure that you have an atomic transaction then you'll want to make sure that you don't stream question so you have an in-flight transaction and it goes down it's gonna you'll get an error published so basically what happens on the client side is that you'll get an error notification which will be a timeout or not available basically a500 no I won't return a 500 it just won't return anything but you'll get an error and that will stop the subscription and you'll handle that error yeah yeah so you can use a retry template and the retry you can use exponential back-off you can use circuit breakers circuit breakers with spring cloud are going to allow you to check whether or not a service is healthy and so if it gets a velocity of errors it's gonna swim flip a switch go to a backup and then when that service comes back online it'll call back to that service and get the response any other questions yes sorry say it again a cluster well so actually let's consider this a cluster right so we have many different applications and then many different instances of each application now if they're all speaking reactive spring and they're using reactive streams then we're going to get much better latency in our requests right just as a product of there's no blocking if I have these large call chains I have to wait until the furthest one completes before returns back upstream so we end up having to wait for each one of these requests to complete with streams we don't have to we open that that stream up and it's like a river and it flows all the way back to the original requester so you're going to get much better latency that answer your question question well ok we'll talk about it alright so I'm gonna go ahead and say thank you very much guys I appreciate your time I'll be around for questions thank you [Applause]
Info
Channel: SpringDeveloper
Views: 4,329
Rating: 4.8904109 out of 5
Keywords: Web Development (Interest), spring, pivotal, Web Application (Industry) Web Application Framework (Software Genre), Java (Programming Language), Spring Framework, Software Developer (Project Role), Java (Software), Weblogic, IBM WebSphere Application Server (Software), IBM WebSphere (Software), WildFly (Software), JBoss (Venture Funded Company), cloud foundry, spring boot, spring cloud
Id: SRSB96-W0OM
Channel Id: undefined
Length: 40min 28sec (2428 seconds)
Published: Wed Nov 01 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.