Building an API Gateway in Java with Spring Cloud Gateway

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back Friends Dan Vega here your friendly neighborhood spring developer Advocate and today we're going to the clouds we're going to Spring Cloud that is and what is spring Cloud spring cloud is a set of tools that allows developers to build some of the common patterns you'll find in distributed systems like like configuration Management Service Discovery circuit breakers and more while we're in the cloud today we're going to take a look at Spring Cloud Gateway now what is an API Gateway why should I care about one Dan and what is spring Cloud Gateway going to offer me these are all questions that we'll try and answer in this introduction to Spring Cloud Gateway now you may have never heard of a Gateway you may have never even thought that you needed one that is kind of the point of this video that is why my friend Nate chuda and I have been doing a talk called Spring recipes I will go ahead and leave a link to that in the description below and really the point of this is the spring ecosystem is so massive often times you'll come across problems and you won't know that there's a solution for it in spring so part of our job is just making you aware of some of the solutions out there and I think that's one thing I really want to accomplish today is making you aware of what spring Cloud Gateway is what problems it will solve for the next time that you run into the situation you know which tool to reach for with that I want to take a little bit of a diagram before we jump into some code just to talk about what we're going to go through today all right so here I am in Escala draw I know everybody always asks me where I come up with these drawings for I'm not great at art but this is a really great tool that helps me kind of put these together so I'll try and share this uh graphic out as well uh we're going to talk about first what is an API Gateway so an API Gateway is really a single point of entry for all of our backend services this could be our distributed architecture we could have a bunch of backend services I don't want to say just microservices because they don't need to be microservices any services that we want to put behind the gateway that's fine uh so we could have a bunch of services behind here also there is a situation where you might want to put a monolith behind a Gateway as you start to deconstruct a monolith into different services this is a great uh use case for an API Gateway it's all about manipulating the request before it goes off to the proxied service and then manipulating the response before you send it back to the client so that is one kind of function of a Gateway the other big one is cross cutting concerns we have all these different backend services we don't want to do all of the the cross cutting concerns in all of the applications we kind of want to have one place to do them things like authentication and security load balancing and circuit breaking observability when we're talking about logging monitoring distributed metrics service Discovery and caching so that at a high level is what an API Gateway is uh we'll talk about spring Cloud Gateway and what that offers in a second but I want to talk about the service we're kind of building out today so I've been doing this uh in some recent videos and we're kind of continuing this trend here there's a Json placeholder service out there pretty cool Service uh I'm kind of replicating that to to kind of show off some of this stuff uh we're calling it danon's placeholder service and what we're going to do today is we have all these different microservices I built out two of them so far which is posts and comments and these are going to instead of having all these clients like figure out where to call these different Services we're going to put this Gateway in front of them so now the clients have a single point of entry they call the Gateway the Gateway will Route traffic down to the different microservices and you get a lot of benefits out of this things that we just talked about uh in the previous image um but also now that it's behind a Gateway they don't all need to be written in Spring if you have a bunch of different teams in your organization maybe you have some python developers some go developers these Services could be written in different languages um and then we have some uh cross cutting concerns here that we can solve for in Spring things like configuration service registry and circuit breaking and ultimately we're going to push out posts and comments in our gateway to containers and we can deploy them somewhere we're not going to do that in this video but we could do that so that's the service that's the API Gateway what does springcloud Gateway give us so spring Cloud Gateway built on Spring framework 5 project AER and spring boot 2.0 there's an asterisk there because in a future video we'll take a look at a new project that brings spring MVC to Spring Cloud Gateway right now if you need to write uh any kind of code in there uh it's based on web flux so we're going to use that kind of reactive style which is good because again if we're building out a Gateway that is really just passing requests on to other services we don't want that to be blocking right so uh features are I'm able to match routes on any requests I have predicates and filters we'll see what that means in a second circuit breaker integration Discovery client integration easy to write predicate and filters uh rate limiting so if you wanted to have that AP that Gateway sitting in front of services and limit the amount that certain clients can call in you can do that you can also rewrite paths here's a representation of one of the graphics in the documentation uh what we're doing here is what what is this what happens in a spring Cloud Gateway request so first a client makes a request to the spring Cloud Gateway if the Gateway Handler mapping determines that a request matches a route so we figure out like hey does it match something it does let's send it to the Gateway web Handler then we go down here and it says the Handler runs a request through a filter chain that is specific to that request the reason reason the filters are divided by dots is that the filter can run logic both before and after the proxy requests is sent all prefilter logic is executed once it's sent to the proxy service then the proxy request is made after the proxy request is made the post filter logic is run so you can have pre and post filters and again that is kind of the idea with this hey you call into the gateway we call down to posts we can manipulate the request from the gateway to post we can manipulate the response from post back to the client really so that's how it works um how is it going to work uh we can do this through uh the spring Cloud Gateway configuration this can be either in a application. Properties or a yo file or there's a Java fluent API config then I just have some kind of definitions here of what routes are what predicates are what filters are and we're going to get into all of this as we go throughout the tutorial so that's that I think I just wanted to give an overview of why you might want to need it what spring Cloud Gateway is now what we're going to do is we'll jump over to start. spring. we'll create a Gateway project and then we'll talk about what that project needs to do to talk to our Downstream backend services so what are we waiting for let's write some code all right so we'll look at our two Downstream services in a second the post and the comments but we need to create the gway so we just like any other project we're going to head over to start. spring.io I'm going to choose Maven I'm going to choose the latest version of 3.2 I'm going to say Dev do dev. Dan Vega we'll call this a Gateway demo and this is spring Cloud Gateway demo and I'm going to choose Java 21 you you don't have to you can choose uh Java 17 uh I'm going to go ahead and pick a dependency now what dependency you need do you need simply one go in here and look for Gateway spring Cloud Gateway this gives you spring Cloud routing provides a simple yet effective way to Route apis uh to provide cross cutting concerns such as security monitoring metrics and resiliency so with that in place that's all you need go ahead and generate your project uh unzip it and we'll open it up in my favorite ID which is intell but you can open it up in whatever ID or text editor you're most productive in with that let's write some code all right so if you remember back to the diagram I had some Downstream Services the post service and the comment service and that's what we're looking at now just to kind of show you what those look like again we've kind of covered these in previous videos but just a quick overview the post one has a controller a repository we're using spring data jdbc it reads from this post. Json which is the hundred list of posts that the Json placeholder Service uh gives you and then finally um we're we're using this on Port 8081 not to clap with the gateways 8080 Port um and then uh yeah I think that's really all that's interesting it uses Docker compos has its own database the comments is very similar it runs on a different Port Port 882 so those are our two Downstream Services here's our Gateway application that we just created at start. spring.io we're going to create some routes a way to go ahead and Route traffic to our Downstream services now as I mentioned in the beginning we can do this using a fluent Java API so we can write Java code or we can do this through configuration so I'm going to start with configuration I'll show you an example of the Java code but mainly this is where I go ahead and configure this because we could take advantage of configuration changes later on like if we need to externalize that configuration and maybe change it for different environments we can do so so I'm going to go ahead and say that this is a yo file it just makes it a little bit easier to configure so now we need to set up some routes to our Downstream Services how do we do that so we're going to start with spring Cloud Gateway routes and then in here we're going to create a route for every service that we need to route to so the first thing that I do is give it an ID I'm going to call this the post route and then you need to give it a URI so I could start with just hardcoding this in I could say hey that post route is located at po uh Local Host 881 slash nope sorry just 8081 so I could do that but what I also want to do is go ahead and provide some environment variable here that we could change later on so I'm going to say the posts route URI is located here and then if you do that with a colon that basically gives it a default value if one is not defined so now it'll be locost 8081 if we want to override that in a different environment that's great now let's talk about our post service let's go ahead and fire up our post service here uh I want to run it and see if everything works okay and if it does uh I just want to make a request to it so we can see what's going on uh so I'll go ahead and as this gets fired up it's just okay so that started so I'm able to go into the terminal here I'm going to use a command line tool called HTTP so it's kind of like curl but just a little bit more readable uh if you don't have it installed you can install it or you can use something like curl so I'm going to say hey 8081 API posts that's where I'm going to go and if I do that I'll get a list of 100 posts I can go to a particular post and get that entry I can go to one that doesn't exist and get it not found so that's what is that's what's going on on the post side I want to uh fil I want basically want to forward requests onto that service so we know that localhost 881 API poost is where we're going to send it but we need to have some predicate so a predicate is a way for us to kind of uh determine if this is a request that we're going to send down to that proxy service so what I'm going to do is instead of SL API poost I'm going to have simple routes on my Gateway so I'm going to say hey if somebody is calling the path slost on the gateway then I want to go ahead and send it down to my Downstream service so that is the path I'm trying to match on if we match on this then we can go ahead and send it down stream but before we send it Downstream we can apply some filters to it so this is a good place to go ahead and check out in the docs there are a couple there are a whole bunch of built-in filters uh the one I'm going to use here is called prefix path so I'm basically prefixing a path onto this route all right and then the filter that I want to use for this is called prefix path and so I'm going to prefix the path with Slash API so whenever you uh find something with SL poost we're going to forward it on down to our Downstream service which is located at Local Host 881 and I'm going to prefix the path SL API onto it now there are other filters that I can go ahead and add and one of one one of which is called the add response header now there is a long form way of adding a filter that is built into uh spring Cloud Gateway and there is a short form the long form is saying what is the name of the filter in this case it is ADD response header and then there are the args and in the case of the response header there's a name and a value so I could say name is equal to X Power by so this is just a a response header that I'm adding and then the value could be something like sorry the value could be something like Danon now this is the long form of doing it there's also a short form of adding a filter in this case it's the filter name so I'm going to say add response header so header is equal to X powered by and then a comma and then the value so this is the name this is the value this is the or these are the arguments and then this is the value so the value is going to be Danon Gateway service all right so that is our first route we've defined a route called post route we're going to send it down to local 881 one we're going to match on anything of SL posts and we're going to prefix it with APF we didn't prefix it with ad with API this wouldn't work right because we saw in the previous example it's localhost 881 API posts so let's take a look at how we can go ahead and run this let's run this application so I'm going to go ahead and run this again this is going to run on 8080 our posts is on 8081 our comments is on 8082 so if we go ahead and fire up terminal here we're going to say HTTP on 880posts and that should give us all of the posts now again in our configuration we said uh we gave it this wild card which means we're going to forward any requests so we're going to forward the gets the posts the puts the deletes uh in this case I'm just going to te test out being able to grab a random one and it looks like that works out as well and you also notice here that we have added the response header so X powered by we have dance on Gateway service so we're manipulating the request and the response uh as we're forwarding this down to the downstream service so that's a quick intro into kind of setting up the route here in application.yml now we looked at predicates we looked at filters how did I know what filters were available to me in springcloud Gateway that's a good question I'm glad you asked let's head back over to the browser and uh this is another question I get a lot like how do I find documentation on a particular product uh the first place to start always head over to spring.io go under projects in this case this spring Cloud Gateway falls under the spring Cloud project so if you go into spring cloud and then go to Spring Cloud Gateway here is the spring Cloud Gateway we'll see an example of this but this is the Java fluent API for writing routes we're doing it in configuration right now but you can also do it in Java Java code if you go into the learn section and go into the reference documentation this is where all of that information is so if you want to find out about predicates now predicates are hey what are we matching on you can match on much more than the path uh if you wanted to find out hey I want to match on a particular header or a cookie or a method or a path or a query program these are all things that you could match on as a predicate then from a filter standpoint what are all the filters in involved we saw the add response header one so we went in we added a response header here's the name of the header here's the value of the header uh this can also be um you can use it in the long form speaking of which how did I figure out what was long form and what was short form you'll see in here that there is the shortcut configuration so cookie equals the name of the cookie and then comma and then the value you can also fully expand those so this is where you can learn a little bit more about that um so we learned about predicates we learned about filters um everything right here in the documentation uh you can go into the fluent Java API routes and why don't we do that now why don't we um go ahead and comment this out in here so I'm going to say let's get rid of this and then I'm going to come into my main application here I'm going to just paste some code in we don't need to to watch me write this together and now I'm going to Define two routes one for posts and one for comments so now if we go ahead and fire up this application again we should be able to basically go to localhost 880posts or slash comments so I'm going to say SL posts and there's our posts and then I'm going also say comments and that is not working what is happening there um did we forward oh we don't have uh our comment service started so we may want to do that that let's go ahead and run this application all right now that that's running let's try that again and there's our comments so you can see our Gateway is now forwarding a request down to our Downstream service and in this case we've set it up using the Java fluent API so we come in here we create a bean um and this is going to take a route locator Builder I called it Builder and we're just building some routes so here's route one and here's route two and then we build it and we return that so in there we're defining the predicate which is the path the filters here's the URI everything that we do in the application. yo file for configuration we can do here in Java using the fluent API all right so I think we're going to cover one more thing here that you'll find in the documentation and that has to do with the actuator I've talked about this in the past but the spring boot starter actuator is one of those uh starters that I think you should be including in every single project and this is no exception uh because we can get to do some some cool things here one of which is there's a new Gateway endpoint so I'm going to go ahead and paste in some configuration here I'm just exposing all of the endpoints I'm including details for health and I'm going to enable the Gateway endpoint and this will allow Downstream services to talk to this actuator and get some information about the routes uh that are configured in our application so I'm going to go ahead and refresh this and and if everything starts up okay we should be able to head over to the browser and you'll notice all of the endpoints are here including a new one for Gateway which will give you information about your routes so while we're here I just wanted to mention one more thing uh that is springcloud Gateway the open source version there's also a commercial version that you can get from VMware tanzo and it adds a lot of really great features on top of it the biggest one in my opinion is single sign on so in a lot of organizations you might need SSO uh on top of your applications and then springcloud Gateway can basically talk to SSO and uh log you into all of your different Services besides that it has a whole bunch of different features another one of my favorite is the open API Discovery so basically it can talk to all the downstream services and generate uh open API specs and then you can bring that into some documentation viewer like Swagger so that one client uh so that a client could read that document about all the downstream services so a bunch of really great features in Spring Cloud Gateway commercial if you're interested go ahead and check that out all right I really hope you enjoyed that introduction to Spring Cloud Gateway and I just want to let you know this is just the beginning of this story we have a lot to cover when it comes to Spring Cloud Gateway uh we I threw out this on Twitter I said hey I'm working with spring Cloud Gateway do you have any questions about gateways in general and how to use them if so let me know and you guys guys did not disappoint I got a ton of really great questions I think we answered a couple of them today but you can as you can see there is a lot of them and we need to answer a lot of these questions so I'm going to continue to uh put together content on this and if I can't answer that directly in a video I'll see if we can't just uh get a list of questions and put put some answers to those and get those back out to the community so I really appreciate the questions I really hope you're enjoying uh some of this Gateway content if you have more questions that we didn't cover today or in this list of questions let me know in the description below We'll add it to that list and we see if we can't get to it so hey I hope you learned something new today if you did do me a big favor give me a thumbs up subscribe to the channel and as always friends happy [Music] coding we here we go we
Info
Channel: Dan Vega
Views: 35,196
Rating: undefined out of 5
Keywords: dan vega, Spring Cloud, Spring Boot Microservices, Spring Boot, Spring Framework, Spring, Java, Spring Cloud Gateway
Id: EKoq98KqvrI
Channel Id: undefined
Length: 23min 0sec (1380 seconds)
Published: Wed Oct 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.