SpringBoot Gateway configuration with NodeJs  Service

SpringBoot Gateway configuration with NodeJs Service

Gateways - BackBone of the microservice architecture !!!

The server acts as an application entry point and routing to downstream services. It ensures scalable, secure communication between clients and services.

A few functionalities of gateways:

?? Routing - Incoming requests to respective backend microservices.

??Security - Handle token validations and end-to-end encryptions.

??Aggregation/Composition - Aggregate the data from various microservices and send it to the client in a single request and response.

??Load Balancing - Distribute the traffic across multiple instances to ensure High availability.

??Rate Limiting and Throttling - To prevent attacks, by controlling the number of requests.

??Protocol Transformations - Modify the protocols based on the client and requesting microservices.

??Monitoring and Logging - To track the metrics like number of requests, response times, error rates, and throughput.

??Service Discovery - Dynamically discover the services by using service registries.

??Caching - Cache the response of frequently requested APIs to reduce the load to backend services and reduce the latency.


API Gateways

Steps to finish this,

  • Project setup? - gateway services using Spring Boot
  • Microservice setup using node js
  • Configuring proxy?
  • Initialize the gateway and testing


Gateway setup using spring boot

  • ?Go to the spring starter website, https://start.spring.io/
  • ?Create one project with maven configuration.
  • ?Add a dependency - Spring Cloud Routing
  • ?And download the project.


Spring starter configuration

Create basic nodeJs service:

  • Npm init with the project name
  • Run npm Install expressJs?
  • Setup app.js with one get route
  • Initiate the application by using npm start or nodemon.

 const express = require("express");
 const app = express();

app.get('/call',(req,res)=>{res.send("Response from Microservice")})
 
app.listen(3000,()=>{
    console.log("App connected at 3000")
})        

Configuring proxy?

  • Spring boot uses external configuration to define the properties of the project and can run the project on different environments with different properties.
  • By default spring boot project comes up with an application. properties file, which uses a key-value format.
  • We can also use the YAML configuration file, which uses hierarchical configuration data.
  • Here's the YAML configuration for the basic gateway setup.

spring:
  cloud:
    gateway:
      mvc:
        routes:
          - id: first
            predicates:
              - Path=/gw/call
            filters:
                - StripPrefix=1
            uri: https://localhost:3000/call        

spring: Root configuration for spring boot.

cloud: Indicate the configuration is part of the spring cloud?

gateway: To configure the cloud gateway to mention the routing components.

mvc: Routing approach in MVC pattern

routes: The block that can define the incoming requests.

id: unique identifier of the route, to manage or debug the route.

predicts: path matching variable, contains Path.

filters:: The StripPrefix=1: removes the first part of the path-? /gw

uri: Here configure the nodeJs microservice URL in the URI.


Initializing gateway and testing

  • Initialize the gateway and note down the port.

verify the port while initializing the gateway


  • Go to the browser and run the gateway URL.
  • Now you can get the response from microservice.


Gateway response

Thank you

要查看或添加评论,请登录

Nanthakumar D的更多文章

社区洞察

其他会员也浏览了