The RSocket Revolution

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] hello everybody thank you so much for joining us today i know that there's a lot of places you could be right now but you chose to be here uh with us and i appreciate that now we don't have a lot of time so let's dig right into it we're going to talk about is reactive programming with r socket now you've probably hopefully heard about rsocket our socket is a protocol it's a binary protocol that was developed by engineers at netflix who then went to facebook uh the protocol serves to support reactive stream semantics on the web uh on the open internet uh and the benefit of that is that you get things like back pressure you get things like uh you know you get operations minded features uh like metadata you get um lots of things that you want when you build production grade services uh it's also payload agnostic so it's binary data for both the payload and the headers it is a it is a very flexible uh protocol in that it supports a number of different message exchange patterns request response fire and forget you know fire things like that and there are a number of different bindings for it because it is a network protocol so you can use it from java from javascript from c plus plus et cetera now there are in the very beginning there were several different implementations one for java that was based on reactor now reactor of course is what supports the reactive apis in the spring ecosystem and so my friends that's what we're going to look at today is we're going to build a service today using the r socket support using lumbok and using spring security so we're going to go ahead and build a new project here at start.spring that i owe we're going to use springboot 2.3x it doesn't matter which version of 2.3 but 2.3 is very nice because it has among other things it has a new support for building or for for uh building more cloud native applications so uh easy uh docker image generation based on spring boot 2.3 easy features for kubernetes things like availability endpoints and things like gracefully degra graceful shutdown of services that are running inside of a containerized environment so that in-flight transactions get turned uh can get finished and then they're given a chance to con to uh to shut down gracefully all sorts of very useful features one of my favorite things that's uh now a possibility is to use spring boot 2.3 with growl vm growl vm is a ahead of time compiler actually it's just a it's a just-in-time compiler it's a c1 replacement basically for open jdk or other standard jdks like that but you can use one mechanism one feature in uh growl to support ahead of time compilation and that ahead of time compiler is called the gravim native image builder and the result is much lighter weight builds and much faster startup time so here's some traditional spring boot applications and i don't have enough time to show you all these things so please go on the internet and see my videos look for josh long and spring and growl but these are apache tomcat and spring data jpa with with uh hibernate very very slow kind of technology right so i go to target native image and then i just run the native image and you can see that it starts up in 0.3 seconds actually that's pretty slow there you go 0.169 right pretty good we can go to the mango example like this cd mongodb cd target native image and here you can run mongodb and the mongodb example starts up in uh you know about one set one tenth of a second 0.1 seconds and then of course you can go back to the reactive one now the reactive one supports sql based data access as well as a web server a reactive web server as well so target native image and then reactive and control c okay take two so that's 0.089 right pretty good and we can go here localhost customers oops there okay so there's our end point uh ctrl c this to stop it so yeah lots of reasons to use springboot 2.3 but that's not what we're here to do so let's ignore that for now and build our application so what we're going to do is we're going to build a new service using these dependencies i'll generate it and we'll open this up in our ide here okay so the first thing that we want to do is to comment out the dependencies that we don't need so i'll go down here and i'll just comment out the spring security dependencies for the moment we'll come back to them later okay so i'm going to re-import the build then i'll go to service service application and here i'm going to build a simple controller now i'm going to build a spring and spring uh you know messaging controller the controller will be a greetings controller okay a greeting controller and it in turn will return a see a stream of responses and requests based on requests so given a request i'll return a message okay and of course in languages like kotlin and scala they have convenient ways to turn these java objects into things with properties and getters and setters and two strings and so on but in java we don't really have that so you know until java 14 records arrive so i'll use this instead i'm going to create a simple endpoint that we're going to map to an r socket route now routes are not supported in the raw r socket protocol but they're easy to build here with um with a spring support so i'm going to build an endpoint that takes a single request in and returns many responses out a flux takes zero or more records i could also do just one record that's a mono right and one of the nice things that's really convenient about our socket is it supports many different message exchange patterns so fire and forget request response it supports request stream and it supports channel okay so a fire and forget looks like this right no response request response looks like this where you have a single value to come in or this this is the same thing mono right and a request stream is a single value to come in and multiple values to go back out and channel is multiple values coming in and multiple values going out so lots of different message exchange patterns remember http only supports request response there's no other way to do it you have to the client has to initiate it the other thing that's nice about our socket is that as you'll see the service starts up and it stays up and then the client starts up and it stays up and they stay connected right so once these things are connected they no longer need to negotiate the constant you know network connection and a handshake and all that we can just make requests and not worry about resetting up re-establishing all that information okay so let's now i'm going to do a request stream example here and what i'm going to do is i'm going to create a reactive stream using a java 8 stream so i'll say generate using a supplier and i'll say greeting response and we'll say new greetings response and i'll say knee how request dot get name at instant dot now okay now that's going to be an infinite stream and what i want to do is i want to turn that into a reactive stream so i'll just pass it into there and i also want to slow it down so we can see what's happening so i'll use delay elements to stagger the results to slow them down just a little bit okay here we go so there's my my greetings endpoint now i want to talk to this endpoint but well first we must start it up and in spring boot if you want to start up an r socket endpoint you just use the r socket port property okay so here is the the property i'm going to use 888 that's a nice good luck number start that up here and now i can talk to it uh using a very handy utility on github by makissan it's called rsc it was built by my friend and colleague toshiyaki maki he's a colleague at vmware and he built it as a cli to support our socket it's kind of like curl for our socket and it's very convenient so let's use that here to make a request to our service so we'll say rsc tcp now this is important because r socket speaks you can run r socket on top of many different protocols it can be tcp it can be aeron or websockets or whatever here we're saying that we're expecting not a request response but a request stream so that's this we want the route to be greetings we want to log the output and then debug the reactive calls and the data that we send will be we'll convert from this right so uh data name um josh let's say okay now of course i need to escape the quotes here all right and if i do that you can see i'm debugging the results so i can see the message frames and the payload itself and i can see the reactive uh reactor callbacks the on next methods and i can see the payloads knee how josh every second okay forever so uh that works right that's easy i can prove that it's working this way but i want to build a client to actually talk to that service so i'll go back to start.springrail and we'll build a client okay to talk to it as well and we'll generate that and we'll open this up and the client is going to talk to the downstream service so we'll create a simple bean a component actually here it's better this way a bean application listener application ready event okay return arguments and application ready event thank you and in order to make this in order to talk to the service we need an r socket requester now the r socket requester you need to construct it and point it to the service at startup time so here i'll tell it to find localhost 8888 using this builder okay and we can configure all sorts of things about the application with this builder but for now that's okay okay we'll leave it like this so our socket requested that builder and here i'll say that i want to uh to use my r socket requester all right like so i'll say client dot route greetings and the data that i want will be i'm going to send a new greetings request in but of course i don't have the type called greetings request do i so i need to copy and paste this type from over here so i'll go here and i'll take the dto the entities and we'll go back here and return it okay so greetings request and the name um you know it could be anything right all right there's our name and the data that we expect to come back will be of type greetings response so if i assign this to a stream then i get a publisher of greetings response right and of course i can log out the results system out print line all right good there we go there's our simple client the only thing to keep in mind is that if i start this program i'm not starting a our socket service and so my r socket service my our socket client will start and then immediately stop because there's no daemon thread keeping the java application open so i'll just read in a single character here before i run the client okay let's run it okay so there you go you can see here it's saying nihao alibaba every second forever it's reactive right now this wasn't so bad right i was able to uh i was able to point it to the route i can even do route variables right i can do that that kind of thing i could point it to the route and then i could uh easily just you know send data in and then i could easily get a stream back and so it maps very nicely it's very easy to build a client for services if you want to build a java object but we on the spring team have started uh something experimental maybe it'll be useful to you uh you can try it out it's called spring retro socket and it's a fan like r socket client very simple um and it's not ga okay it's early it's not yet ready for production but very simple you can create an r socket requester at enable r socket client define the r socket client interface and then you're done right you can actually map the interface methods to endpoints on the service and it'll automatically give you a working object an instance that will actually make those calls for you it could be very convenient i think but i would welcome your feedback and your pull requests and your ideas and so on now we have a simple service here and i'm using the rsocket client that's one way to do this right i have an infinite stream here let's limit this to just just a few okay 10 records let's say so that we can see what's happening in the next demo so i'm going to now restart this and sometimes by the way i noticed that you have to like force this you have to click it and then force it to restart i don't know why sometimes intellij doesn't kill the process so what we're going to do now is we're going to take a look at using our socket from spring integration and the reason that we want to do this is because uh it's very common that we have non-reactive services with which we need to integrate but it would be very nice to then bridge that non-reactive code with reactive r socket based code and spring integration gives us a very nice component model for integrating things that don't want to talk to each other right now keep in mind you can already achieve some of this if you're willing to manipulate schedulers and and so on at the lower level of reactor but it's just nice to have spring integration because spring integration gives you a pipes and filters component model so i'm going to build a integration application i'll hit generate and we'll go to the downloads directory integration.zip open this up and when i open this up i'm going to start writing some code using spring integration now spring integration has a component model based on the ideas from you know the amazing book by bobby wolf and gregor hope right this is uh gregor open great book um and i recommend you read the book it's called it's it's a patterns of enterprise application integration and in the book they talk about many different patterns uh to support integration how do you take one service and system and make it talk to another so there's a lot of different things you could use here a lot of different ways you could do this but if you write your code using messaging components then it becomes very natural to string together or recompose these messaging frameworks right so i'm going to bring in two modules spring integration file and spring integration r socket these give me components that i can use to talk to the outside world to talk to external systems like the file system and like our socket there are many different components but in spring integration you have inbound adapters and outbound adapters outbound adapters take spring framework message and turn it into uh and turn it into um you know something in the real world right uh inbound adapters take the real world events and turn it into a message okay if once you have the message in the system then you can do things with a message you can split the message you can route the message you can transform the message you can enrich the message you can wire tap the message you can uh what else can you do you can aggregate the message right these are all patterns from the emit from the book sometimes it's not enough to use just uni one direction adapter sometimes you want to take a real world message send it to spring integration and then send a response out back to the real world this is a in-bound gateway and the opposite is true as well you can say maybe i want spring integration to generate a request to the real world and then the real world will send back a response to spring integration this is called an outbound gateway okay so these are uh request and response basically whereas this is just send a message or receive a message just one direction and then in between you can do things like this right so lots of different possibilities lots of different patterns these these components are reusable if you do your job correctly with spring integration then a lot of what you will do is just taking existing things and using them to build solutions so let's build a simple integration that supports file data piped to a r socket endpoint piped to output okay we're going to just compose these things in this way so create a spring bean an integration flow our socket flow i'm going to say integration flows dot from and what we're going to do is we're going to create a file inbound adapter equals files dot inbound adapter var file or folder equals new file and we're going to inject spring i'm going to have spring inject the home directory here home okay we'll say home desktop and create a new file and it'll be in okay now the inbound directory is uh you know the inbound adapter will monitor this directory it'll make sure that if there's a new file we are given an event okay so to monitor that uh we wanted to create the directory uh and that's it i guess that's that's all i care about so we can then use that um file inbound adapter right uh there you go we can use that and we can then do things with it right we can do all sorts of things with it but the problem is that file systems they don't have any way to tell us what they want and so we must configure some way to ask over time what the file system is seeing so we can configure a poller okay and so we say pm.metadata fixed rate and here what i'm saying is i want to scan the file system every second and as soon as there is data then i will take that data and i will i will process it what i'm going to do with that is i'm going to take that that data that comes in it's a file right it's a java io file so i'll create a new file to string transformer and then from this string i'll create a a name right i'll assume it's a name so new greetings greeting request but the problem of course is we have no greetings request on the class path so let's go back to our code and take these two types as dtos and we'll paste them in here so greetings requests now name dot tostring all right and then from this all right sorry i don't want to do a trim not two string from there i'm going to then call the r socket endpoint right so var r socket equals our sockets outbound gateway okay and so the end point is called greetings just like we saw before the interaction model is called request stream the client r socket connector is a bean that we must define that tells spring and tells our socket how to connect our application to the service now we could also provide an r socket requester but this is very convenient so we'll do this right because it gives spring a little bit more information so new client our socket connector local host 8888 clc and we also need to provide the r socket strategies so our socket strategies all right there's this strategies return clc good so there's this now we're going to inject this bean here connector and we configure that as so and the other thing that we need uh when we configure the r socket outbound gateway is the expected response type right we want to know that it's a greetings response so there's the entire thing okay pretty consistent so this is file inbound adapter out r socket or yeah r socket outbound gateway and here are some of the components right transformers handlers etc now we're going to send this to our socket so this is going to send this message which is a greetings request to the rsocket gateway when the response comes back i will i will get if i write this code honestly what i will get is i will get a publisher of greeting response like this well i don't want a publisher right i i don't want this i want just this not this so what i want to do is to split it okay but the problem is that when i split it uh i'm also subscribing and that and i'm subscribing to a reactive response and so i want to make sure that all the components after split understand back pressure so i will create a message channel like so uh reactive message channel return message channels dot flux dot get okay so there is this and we say channel reactive message channel okay now this code this code is now different i'm not going to use flux here i'll just use greetings response okay and i can just log it out so i'll just say system out new message greetings response dot tostring and in spring integration when you return null that tells spring negation to stop processing okay so otherwise we're done we have done everything we need to do so let's run this code okay so now we go to the file system cd desktop in echo world or you know yeah let's just do spring name and then here you can see it says knee house spring every second going on and on and on okay so that's another kind of client another way to work with it there's also an inbound gateway so you can act as a service and this is actually everything we have done so far is one direction the data comes in we process it and we send a response but our socket is bi-directional and this is one of the really interesting powers of our socket is i can do things bi-directionally so let's do that let's actually change our example a little bit in this client i want to also have a controller okay so i'll create a health controller and the controller will have a message mapping endpoint health class client health state private boolean healthy okay now this will just be information that we will serve for whoever wants it so that they can know if our our client is healthy okay uh and if they're not healthy then we can stop serving the service can stop serving data to us right that's very useful so here i'll just create a you know a simple stream as before stream dot generate new supplier new client health state and we're we're just going to create some arbitrary sort of random healthiness health status so greater than point so we're going to be okay if math.random is greater than 0.2 right but you know that means that sometimes we're not going to be okay it'll be false so and then of course we're going to stagger this so this goes here dot delay elements duration of seconds one okay and what i want to do is i want to make sure that when somebody connects to this service so sorry to this client when the client connects to the service the service can ask the client hey are you is your service okay is your telemetry okay if so here's some data are you okay here's some data are you okay here's some data but if it becomes sick if it becomes unable to continue processing the results then we want to make sure that we stop bothering to send data that it cannot work with right so this is a very simple example but you know it's pretty easy to see how useful this be so what we're going to do is we're going to create a socket acceptor okay and we're going to use our socket message handler dot responder our socket strategies and we're going to inject the health controller as well strategies health controller okay now this socket acceptor we can configure on the r socket requester itself right so acceptor right like so oops it's a acceptor i think builder dot so there we go okay and we'll inject our socket acceptor here okay good so that's all that we need to do on the client side now on the service side i want to change this code so that when the client first connects and makes this request we can ask the client how healthy it is and we're going to do that by injecting a reference to the client r socket connection okay so there's this and we can use that we can say okay let's clean this up a little bit here var out okay that's a reactive stream but the data that is coming in is from the client our socket connection from the health endpoint and we're going to expect a stream of client health state.class but of course we have to go get this type from the client so here it is okay good so there's this okay now what we want to do is we want to say okay return out but only only when there is an unhealthy only when the data is healthy when the client says that it's healthy so we're going to filter chs we're going to keep it if it's not healthy so if it's healthy then this is empty right if it is unhealthy we have one element right we have a client hell you know client health state uh healthy equals false right so that will only happen once as soon as that happens not empty right as soon as it's not empty then this out will stop producing data okay so let's restart this service okay and we can go to the client and restart that okay and now so you can see on the left we've got data and it stopped right at some point it got to an unhealthy response and just stop producing data so this is very powerful now a very common question that people ask is can i do security well of course you can uh and so what we're going to do is we're going to use spring security to lock down our service i have those dependencies here i'll go ahead and just reinstall them okay there we go so i'm going to bring in spring boot starter security and all of this and i have to answer a few questions a few concerns i have to create a security configuration class security configuration and the first bean that i need is authentication and i'll just create a user jaylon password roles user.build okay so there's my authentication and i want to also care about authorization so authorization limits how much access i have right so r socket security and i'm going to say return security dot build i want to lock down everything so i'll say ap dot any exchange dot must be authenticated i want to use simple authentication but you could use jwt if you wanted as well right that's another option i won't use it here but you could okay so that locks down everything but i want to change my example a little bit so that instead of getting the name from a greetings request we get it from a authenticated principle so it'll be the same basically but the user will be authenticated will use that information to determine who we should greet in the response okay so we say authenticated principle and here user dot map ud ud get user name dot map user name new greetings request and then flat map mini uh gr this dot great i'm gonna pass in the client r socket connection gr okay uh let's see oh this there you go okay so that's i'm just reusing the existing endpoints uh and we're just gonna take this one out we don't want that one publicly accessible anymore okay so now we have the same basic functionality except that there's no greetings request anymore right we're going to have just the current authenticated user the thing is this annotation doesn't work unless you configure it in spring security so i'm going to create a a r socket message handler and var rmh equals new r socket message handler rmh.get new authentication principle and then we have to set the strategies as well so i'll say r socket strategies like that return rmh all right that's it that's the entirety of our service let's restart this okay and on the client we must connect to that service as well now uh is it okay yep it's up and running there's the service here's the client we need to change the client to be authenticated now and so i'll do this easily by just using my you know hard-coded username and password okay so credentials username jlong pw and the mime type so in in our socket you have this concept of metadata and it's like headers but each header has a mime type a content type so i need to specify that we want to use the well-known mime type there once i have done this then i can configure the authentication a couple of places i can do it as part of the setup information here or i can do it in the request itself so obviously this no longer needs to be there i can do money.empty and i can do dot setup i'm sorry dot metadata this dot mime type okay the only other thing that i need to do is i need to teach spring i need to teach r socket how to encode um the the the metadata for security so our socket strategies customizer strategies strategies dot encoder new simple authentication encoder okay so there's the client let's go ahead and restart it and there you go it says it says jaylon all right thank you everybody uh for watching this i appreciate your time today i know that it is sad that we could not be together in person i hope next year that we can be together in person uh thank you for your time if you have questions i'm happy to answer them you'll see my information and my my email and so on at the end of this presentation thank you
Info
Channel: SpringDeveloper
Views: 48,486
Rating: 4.9063416 out of 5
Keywords: Web Development (Interest), spring, Web Application (Industry) Web Application Framework (Software Genre), Java (Programming Language), Spring Framework, Software Developer (Project Role), Java (Software), Weblogic, IBM WebSphere (Software), WildFly (Software), JBoss (Venture Funded Company), cloud foundry, spring boot, spring cloud, rsocket, spring integration, messaging, netflix, facebook, vmware, alibaba, security
Id: ipVfRdl5SP0
Channel Id: undefined
Length: 38min 48sec (2328 seconds)
Published: Thu Aug 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.