The increasing demands of users for high-quality web services create the need to integrate various technologies into our application. This will cause the code base to grow larger, making maintenance more difficult over time. A microservices approach offers a solution, where the application is built by combining multiple smaller services, each with a distinct function. For example, one service handles authentication, another manages business functions, another maintains file uploads, and so on. These services communicate and integrate through a common channel.
On the client side, users don't need to understand how the application is built or how it functions internally. They simply send a request to a single endpoint, and processes like authentication, caching, or database querying happen seamlessly. This is where an API gateway is effective. It handles user requests and directs them to the appropriate handler. There are several tools available for building an API gateway, such as Kong, Tyk, or KrakenD. KrakenD, for instance, is a high-performance API gateway built with Go, and it includes common functionalities like TLS, load balancing, authentication, caching, rate-limiting, and more.
Let's consider the following example. Suppose we want to use a single domain to serve the main website, the payment API, and the user API, without revealing to the client that these are three separate backend services. Additionally, we want to create a path that combines the results from both the payment and user APIs.
With KrakenD, we only need to create a configuration in a single JSON file and run the KrakenD executable with that configuration.
{
"$schema": "https://www.krakend.io/schema/krakend.json",
"version": 3,
"name": "API Gateway",
"timeout": "5000ms",
"cache_ttl": "300s",
"endpoints": [{
"endpoint": "/",
"output_encoding": "no-op",
"backend": [{
"url_pattern": "/",
"host": [ "https://domain.com" ]
}]
}, {
"endpoint": "/api/all",
"backend": [{
"url_pattern": "/",
"host": [ "https://payment.api.domain.com" ]
}, {
"url_pattern": "/",
"host": [ "https://user.api.domain.com" ]
}]
}]
}
Then, we can run the API gateway server with a simple command.
krakend run -c krakend.json
Of course, this is just a basic example of what KrakenD can do. There are many more features available, such as monitoring, traffic management, authentication, security, or data manipulation, which we can explore in its documentation. Additionally, the executable comes with a built-in command to audit the configuration and provide recommendations for improving the security of the gateway.
Comments
Post a Comment