Spring Tips: @Controllers: RSocket

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi spring fans welcome to another installment of spring tips this week after a wonderful thanksgiving holiday here in the united states we're going to continue our tour of all things at controller this time deep diving into the r socket protocol and its integration in spring now to be fair i've already done another much more exhaustive look at our socket before so i'm going to encourage you to please check that out after you've watched this one consider this a quick refresher on all things r socket but if you want the goods you want to really get deep into the weeds then you gotta check out that original video let's get started all right now let's build a r socket endpoint our socket of course is a binary protocol that was created by engineers at facebook the point of our socket is to get around some of the limitations of http 1.x and 2.x and grpc and the like it is payload agnostic you can send binary data you can send xml you can send json you can send whatever you like it's well supported in spring boot all you need to do is to tell spring boot starter to create an r socket server so i'll do so here in port 8181 and then you can create a controller thusly and the support here will look more like the support in the websocket example than it does in the http example but it's still a controller so we're going to say greeting r socket controller and we're going to create a record here greeting string name uh and you know we can actually create a greetings request and greetings response all right now we've got a greetings let's say it's a greed kings plural we don't know who knows we can't be sure all right we're going to create an end point here using the message mapping annotation and the end point will be greetings and we're going to return a single value okay a greetings response given a greetings request so we'll say return new greetings response hello request dot name and that's it that's a simple value now in our socket you have different message exchange models so in this case i'm sending a single value in i'm getting a single value back that's request response if i were to send in multiple values in or just a single value in and get us multiple values back that would be streaming if i were to send in multiple values concurrent with the multiple values being sent back that is to say it's like a chat or there's constantly a stream of new values and constantly a stream of of uh outgoing values that's called channel if i were to do a single value in but no response at all where the client neither accepts uh or expects a response that's called fire and forget and you can imagine this is very useful to be able to tell the client just send the data and then disengage you don't have to keep the socket open there's nothing coming that's that's a performance optimization and it's one of those things where it seems so obvious when you think about it but we don't have that another a lot of other protocols so here we go here's our simple greetings endpoint let's try that out there's a great client i like to use called rsc tcp localhost you can just do brews brew install i think it's maki mocking m-a-k-i-n-g forward rsc i'm pretty sure it's something like that it's it's written by one of my uh all-time favorite people my colleague toshiyaki maki he's amazing all right so tcp a181 the route will be greetings and the data that we're sending in well it's going to be a json payload we're going to convert it on root so the name will be jane okay so there's my single value back all right good now what about what if we want to send a request with a parameter instead so instead of having this whole object i'll just have something called the destination variable name right okay so now i've got i'm going to generate a stream replace it with a lambda and this stream also delay so i'm going to delay it by one second i'll just take 10 let's say and voila so this would be an example of a streaming interaction see when it says um see i tried to make the request and it's saying hey you're trying to do a request response which is the default for for rsc but i need to actually specify what exchange model i want so i want to stream and there you go a new value every second for 10 seconds okay thus far i've just everything's been working assuming that the the name is capital letters but uh obviously i need to validate that so we'll say assert is true name dot car at zero character dot is upper case and we'll say the name should start with a capital letter okay very good and if something goes wrong well we can use a our old friend the message exception handler so string handle exception exception exception return a message so i'm going to say var message equals there's been a problem nested exception utils get most specific cause return message error okay there we are so let's go ahead and see what that does okay so capital letters no more so we'll say john lower case j and that throws an exception i got the string back instead of uh whatever response i wanted right it's actually been communicated to the client in a way that i can control there's been a problem right this is same as we've seen before and you can even put this in a controller advice just like before well that was a short and sweet look at our socket controllers and the integration specifically with the add controller component model obviously we've only just begun to scratch the surface here we focused on it uh in terms of the at controller component model and the integration therein but we didn't really focus on the protocol as a whole or the wider possibilities like building conversational and bi-directional flows uh using our socket we didn't look at how to use spring integration to build reactive r socket messaging flows we didn't use the spring cloud stream our socket binder we didn't look at how to use spring security to secure our socket uh we didn't look at the r socket requester which you can use to make requests uh to other socket services uh and more broadly we didn't even look at reactive programming really uh here and how that interacts with the binary protocol so there's a lot to to cover and again i want to encourage you to just check out that original video well that does it for this week but obviously there's more to be covered i just mentioned all the possible expansion points in the spring ecosystem itself but beyond that our socket is a part of the larger reactive foundation which in turn has an r socket routing broker that's worth looking at as well and our socket is being taken up by again companies like facebook companies like alibaba and so on so these large organizations are using our socket at scale and they're building amazing things as well the future is bright for our socket and i hope you'll check it out
Info
Channel: SpringDeveloper
Views: 6,295
Rating: undefined 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: d4HAqS_VfkQ
Channel Id: undefined
Length: 8min 37sec (517 seconds)
Published: Wed Dec 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.