Spring Tips: the Spring Web Flux Reactive Client

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi spring band in this week's installment spring tips we're going to look at the we're going to continue our look at the nice features in spring five and particularly features around the new spring webflix module which is the reactive web tier that sits adjacent to Spring NBC but works on us and supports a reactive runtime right it's based on project reactor and some surfaces that it that API and then abstraction in that way of thinking about things always to to the component model that use now some of it is very similar to what you may use in spring MVC and what you might be associated or might be a use to in spring MVC but something that's different because of course the nature of the paradigm is different so one thing that is different and I think for the better is is how we can consume services right what is what an analogue do we have for the rest template in spring webflix and similarly how do we test you know from the client side perspective services so in order to demonstrate that we're going to build a a reactive application and the application will take advantage of the spring web list module which of course is provided by spring 5 which is do as a G anything by the summer so for now we'll suffice ourselves you know satisfy ourselves to use the spring WA flex module as support and spring boot to that of snapshot so I'll bring in the reactive web support and I'll hit generate and in our application we're going to build a simple domain and I'm very much Imperial domain I don't want to spend too long on the actual service I just want asked something with which we can you know play and experiment right so here's our new service and I always forgive this I'm going to bring in Lombok because I do have a simple entity that I want to I want to create that you know it's tedious or why I write the code by hand so say Lombok okay now Lombok is a compile time annotation processor it's going to make it trivial for me to you know build an entity that we can work with I don't really need I'm going to just create the client and the service in the same project naturally in most examples they would be in separate projects but what I want to demonstrate is the point and not particularly how to spin up a new project on the extraneous Lancer here so I'm going to have a service well let's go to base project called reactive alright we'll have a new service here and a new client okay and in the service we want to create a reactive service application so just a typical spring good application spring complete application and we're going to make this a rest controller and we'll provide a public static void main string arguments a callback array I method I'm going to say spring application run reactive service application back class args good now there's our there's our end point there's our best physical rest controller so far so good when you swing to you know to move around to talk about so let's build an event what is the event all just any kind of DTO right we don't care too much about the domain so shut the data you can have a field private long ID and it'll have a date because why not alright so there we go there's our three fields and we should probably build an all argument constructor very good now the first end point will just return a single event by its ID right so it'll be a mano that is to say a potentially infinite infinitely stream or infinitely valued container object that in this case only has one right so it's going to contain a simulant event by ID and the path variable will encode the ID and we're not going to spend too much time on this I would say minded that just we're just going to hard code it a new bit right so I don't even have an I don't even have a database connected to the classification because it's really not what we care about this time right which wants something to work with so that's what these cases how to get one particular ID that's called a mano it's got a zero or one value and then the next case is we're going to revisit our service and event discussion from a previous video and we'll create an in point that just returns records every second right so events and it's going to be a flux it's going to be a container that returns a potentially infinite number of values right so a flux of events and we don't want to generate actual events that are infinite right I don't want to call a database that I want to line entropy so I'll just create a continuous steady stream sort of based on the scheduler right every second I want have a new flux I wanna have a new event rather so to do that I need to build a couple of events so build a couple of fluxes one for the events themselves right and this will just be easy because I'll build a scheme I react a Java 8 stream and in order to do that I can use a stream back create method here generate rather and the generate method takes a supplier and the supplier just expects me to return the value so I can just hard code the current time and the date and there we go that's my first one and then the next one we want is a duration flux right so I'm going to say give us write flex but interval duration dot of seconds and there we go that's our flux that will emit a new value whenever they you know and the second is path and we're going to merge these together so I'm going to say it zip them together event flux and the duration flux and what we want to do is to map each one map you know map the resulting flux which we can have it's gonna be a two bolt and have a two parameters so it's a tuple of two basically we're going to map the tuple T git T to no sorry T one and that we return it fine so that's our value there now this could be a method reference a little cleaner I think right there we go very good so those are our two server-side events or two server-side endpoints rather if we run this we cook we can confirm that everything's working by using the command line here so let's see curl HTTP localhost 8080 full slash event four slash let's just say one okay and now let's just try all of them there we go so I like as I make requests I get a new value every second sort of like a heartbeat or something like that right so now I want to build a client okay plus not sound reactive client application what was that is something already run let's kill that and run this Oh because intelligence confused because I obliterate the first client in here so we'll just create it again run this one alright let's try it again okay so that is working right heartbeat every second and event 4/22 right so that's working right now in this case we have two different two endpoints those are up and running let's build a client let's build a client to actually talk to our service at the exercise for example this service and event extreme so this is actually something that you couldn't really easily do with the rest template before right so it's nice that we can do this because we have a client that is able to deal with reactive sort of payloads in this case a particularly long or for that perhaps never-ending payloads so we'll say reactive client application and because I've taken the lazy path here to keep it simple I've got to remember to change the port on which we start this application otherwise it's going to start on the same port as the service since they're on the same node and they're saving they're sharing the same auto configuration so we'll create a new spring boot application okay reactive client application by class args and we say that's the Builder spring application builder okay property is the most pass in a single property here server dot port equals 80 81 okay at the post or the default which is a you know 80 which is what these service itself is running on now let this we have a ample room to build a client so we need the spring one flex client type it's called the web client and you can initialize one rather an empty one like this or one that's pre you know primed and pointing to a particular service endpoint so that's what we'll do here and then we can use that in our command line runner so we can say command line Runner Demerol look at inject our client accordingly and what I want to do is I want to call that endpoint at a service an event endpoint and see the results now again think about what we had to do before with the rest of it the rest of the rest template would get the payload and then pass it to a converter well in this case see the pail is infinite so we need to be a little a little bit more intelligent about how we handle the client how we handle the results and thankfully that's mostly taken care of for us right if we call an endpoint that returns a million records bring the web put the web client won't want flinch right so we can say client dot get dot URI I'm going to say then we want to get the event in point we can say that the that we're accepting a particular type of data right so media type pick stream media type text event stream okay and then we'll say that when I make the actual quest so go ahead and issue the request and then from there we have to take the response they CR the client response and tell the work client how we want to convert it you know how which what kind of a body we expect to come back so what we want is a flux of event client our class and again I'm sharing the same event from the service implementation here but a you know in a proper system you'd have two different types you might even have a client-side representation add EPO or whatever and you'd use that accordingly so now I'm going to take the results I'm going to consumables the results I've got now events coming in and all I'm going to do is I'm going to write out I'm going to acknowledge the fact that I've received an event science on easy B T and this naturally lends itself to a to a mess Griffin okay now of course it's worth noting that we could have done a lot of other things here you know we could have applied any of the operators that you expect from flex and so on we could have specified a lot of the convenience headers and cookies and things like that that we want in the request so we have the ability to really change you know do interesting things with the request itself and the client itself is not just limited to get you know the lead in the head and all that stuff you can also contribute to a filter right if you want and that returns that returns a web client so you can contribute a filter that gets then gets applied to also some it calls that's an ideal place for example to insert things like authentication and you know Jesus encoding or whatever you need to let you can do that there so let's go ahead and run this web client example now this is now going to run a port 8081 and if everything goes right we should see on the console here the events being delivered from the service invalid source type did I go along so string args all right now we're cooking so there's our heartbeat value you know every second we see a new value so that's working fine that's that certainly it's working as we expected and that would continue at infinitum it would never run out of memory right it's doing the right thing it's going to load a certain amount you know in a page it will render it and then preload and and so on but it doesn't load at all as I'm trying to - wait - the way in to try and convert it right okay so those are reactive client sometimes I don't want actually use a client just a call to another service center of this when I test my service as though our client I want to confirm that services working in terms of the way that client would deal with it right so this is a pseudo integration test and this is one of the very loans features about Thea spring MVC modules that you have the ability to to sort of mock a client side perspective right as you call that service so I'm going to create a test here and we'll demonstrate the web test client that's new in the latest milestone of spring five okay so we'll use the usual spring runner-up beretta so spring winter and we're going to build ourselves a web test client okay so private web test client this dot web test client equals web test client stock and we have a couple of options here we can bind our web test client to actual server this is different from the the mock MVC thing before right before you were always talking to the application context itself you can do the same thing here you can talk to the spring web flex machinery in the same application context it'll you know in the process and route everything through the same components but you're not actually connecting to a socket is sending a request in this case you can make your bind to a server and that might be you know maybe might be very useful they're trying to do integration tests in another project maybe we've got a sort of a longer suite of tests that bloom through actual endpoints right it can bind to an application context bind to a particular controller so you can actually instantiate the controller here the M the reactive service replication and pass that in there as well and then of course if you know but if you've been following what we've been talking about in the spring and the spring v eccentric spring tips and you know that there are functional reactive endpoints and vente and the centerpiece of that API is a router function so you can pass that in here and that actually has a you know that describes a a catalog of different endpoints and routing so let's see let's try this one this is new so we'll do that well saying it that we want to use a base URL or we can use a builder factory but we'll just use the base URL of localhost 8080 okay we'll build that and with that it's not hard to call a particular endpoint so we can say this but what test client touch get looking very cylinder familiar here right this is very similar to what we did only use the web client itself and we want to call an endpoint by URL so here we can just say events let's say 42 and then we can say that we want to accept Jason all right so we can say application JSON and we can say that we want to actually then issue the request and we can expect a status to be you know is okay it'll be at 200 in this case we expect and then we can expect while we can assert all sorts of those things there as well like the type of payload that we should get back in the body of the response and the presence or non presence of a particular header etcetera but in this case I think we have enough to go so let's go ahead and see what that looks like right so and this very good our test is green everything's happy of course we should confirm the negative by you know seeing if we are if we try and confirm that it's a arrowing out by returning a 500 but it should fail right so let's go ahead and start that up and there we go so our tests are blazing fast we're not waiting for a whole server to start up or in this case we've already got it running but we wouldn't need to if we use the application context for example and we get the ability to do sudo integration tests we can do a lot of these very very quickly right now Springwood flux itself of course is very very fast so in this installment we've looked at a how to consume services using the web client and the web test client we looked at how this is similar to the rest template into the mock MVC sort of apparatus that you may be familiar with and spring MVC proper and of course that those components are still there right and it's also worth noting that you use the web client with you know none you can use it with traditional sort of non reactive services there's no reason you couldn't just use the web client in lieu of the rest template for example right either way you you have your choice so with that thanks very much for watching the we'll see you next
Info
Channel: SpringDeveloper
Views: 39,211
Rating: 4.8720932 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, spring webflux, reactive
Id: leZdgr-O4LE
Channel Id: undefined
Length: 18min 51sec (1131 seconds)
Published: Tue Mar 14 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.