YTread Logo
YTread Logo

What is API gateway really all about? Java Brains - Brain Bytes

Jun 02, 2021
An API

gateway

is an important concept in microservices architecture, it forms an entry point for external clients or

really

anything that is not part of your core microservices system. By watching this video, you will learn all the API

gateway

basics that you need to know to You are a back-end developer and have built an app with microservices, your amazing online shopping app. We now have a handful of microservices that discover and communicate with each other using service discovery. You have a catalog service that provides the product catalog. a shopping cart microservice a search microservice a user microservice everything is great, you're super happy with

what

's happening on the server, your app's API is ready and now you need to hand it over to a front-end development team to You get it written by the UI client in HTML and Javascript, so the front-end web developer comes to you and says, "Okay, man, I need to create a catalog UI page." Is there an API?
what is api gateway really all about java brains   brain bytes
Surely the catalog microservice is here. Just call it, it has rest API. You know, okay, thanks man and it will create its UI catalog page some time later it comes back to you. Okay man, I have the catalog information, everything is fine, but now I also need to show the currently logged in user at the top of the page. I don't see that API, where is that API? Oh, that's a different microservice,

really

yes, you need to use this user microservice. Oh okay thanks man and it goes away to show the currently logged in user on your page and then comes back and says.
what is api gateway really all about java brains   brain bytes

More Interesting Facts About,

what is api gateway really all about java brains brain bytes...

Hi, I also need to show the number of items in the card, where is that API? Oh, that's a completely different microservice, that's the shopping cart service, are you kidding me? No, okay, thanks, so you carefully build your UI page and at the end of the day you called about 10 different microservices and said, "Are you kidding?", about 20 times and then the page finally finished and you you deploy to production, have a team outing to celebrate, get back to work to handle production issues and then realize that you should actually refactor your code, the user microservice should ideally be split into a user profile microservice and a session microservice, but wait for the UI guy, yeah, he's using it, so you go up to him and say, listen to that user microservice you're calling, yeah, you need to update the API, because because I'm refactoring the code, here are two new microservices you need to call now, are you kidding me?
what is api gateway really all about java brains   brain bytes
It's OK? So if you stop and think about

what

you did wrong, it basically boils down to a very simple concept in computer science, you need it to build an abstraction layer, there's no reason for the UI guy to know all the details of each and every one of the microservices you have on the backend. Imagine a simple abstraction service that acts as a façade, it doesn't actually do anything on its own except that it routes requests and responses made by anyone out of your microserver system and this abstraction layer is all you need to care about. , you give this information to the ux type and it will be Done, the subtraction layer has all the APIs you want to expose to the outside world and when the request comes in, it knows which actual microservice to call.
what is api gateway really all about java brains   brain bytes
In that sense, this abstraction service acts as a traffic controller or a router and forms a single entry. point for all your microservices, if you do this you won't be able to split your backup into as many microservices as you want or refactor them as many times as you want and have them talk to each other internally as you want for as long as you want. Since you retain the contract for this facade service, everyone accessing your APIs from the outside doesn't even need to know what they are doing inside this facade layer, ladies and gentlemen, it is what is called an API gateway , it's as simple as that, it's a gateway. at the very edge of your microservice architectural diagrams and as a result it is sometimes called an edge microservice when people refer to something as an edge microservice this is what they are referring to so given a Microserver system, what should you do?
What you need to do to bring an API gateway into the picture is to first identify what your external API will be. Each microservice in your system has its own rest APIs, but you obviously don't want them all to be accessible. the outside world, what APIs are you okay with other people calling? What will that contract be like? Identify that first design and create that API, then it will bring in this micro service that will act as a gateway. You can write your own. or use one of the available technologies, there's really nothing special about this, you're basically creating a bunch of APIs based on this public API you've decided to expose, but all you do internally is call one of your existing microservices and pass it on . the answer, this technique is also called api composition, right, you are composing an api from other existing api, okay, so it is for api gateway, well, it can be, but here is the thing, already Since you now have the single entry point that all your requests must go through, there are many other things you can do to take advantage of them, for example you can add some kind of monitoring system that measures how many requests arrive in how long they take and all that. this is great for ops and support teams, you can authenticate users here, you can pass security tokens like jwt, you can implement security measures and prevent things like handling denial of service attacks, preventing access to certain users and IPs and much more now if it ends.
If you don't need to do all these other things, then you probably want to avoid writing your own API gateway and look at one of the existing technologies. There are plenty of options to choose from. A popular option is implementing the Netflix microservice open source API gateway. stack, it's called zool, you download and configure zul and you run it wherever you run your microservices and there's your API gateway. There are other options including nginx and aws hosted implementations, no matter which one you choose, the patterns are pretty much the same as we do. Now we discuss the next obvious question, what are the disadvantages of using API gateway pattern and how to fix them.
The first obvious downside is that you added a network hop here, so things are going to be a little slow, but there's really nothing you can do. about that, since the pattern requires it, a second disadvantage, let me ask you how many of these API gateways do you need? You just create one. One could be a problem. You build microservices with fault tolerance and redundancy in mind, so that even if some of these instances were to fail, the system will still function. What if your API gateway fails, since it is a single entry point? The whole system fails, as a result, that's not good, so yes, you can create multiple API gateways and split your incoming calls. for them using things like load balancers and elastic ips, the other problem with api gateway is that technically it can get a bit complicated, so let's say you have a web client for your microservices and you have an ios client team and an android team .
You may need different APIs in different gateway configurations in that case, instead of overcomplicating the single API gateway, you can create multiple types of API gateways, one for each type of client, and you can have those clients call the correct API gateway or configure load balancers that route requests. to the right API gateway, some people call this pattern the backend for the frontend. You're creating separate backend endpoints for the frontend that calls it, so that's the API gateway in a nutshell to conclude our story, what you need to do. It's simple, go to your front-end developer and tell him to use your API gateway as the only endpoint you need to call.
Listen to a couple of questions, are you kidding me? And then once you refactor all your code to call your gateway. You are now free to do whatever you want in your microservice architecture as long as you keep this external API contract issue resolved and thanks for taking care of yourself.

If you have any copyright issue, please Contact