简体   繁体   中英

Nodejs Parallel API Hits

I am trying to develop an node js application that acts as api-gateway / facade layer for Services(developed in Spring Boot).

Is it a good practice or not?

If yes, which nodejs framework should I use?(Async / co / Promise / Async-Await ) etc. I mean what is currently used mostly on production enviornemnt?

"Is it a good practice or not?"

What is your question related to? Using an API gateway/facade? Using spring boot? Using async/await...? What exactly is your problem?

I guess you want to develop a spring boot based microservice architecture with a nodeJS based api orchestrator as a frontcontroller and single entry point?

Don't confuse the technical side of naive routing (load balancing with nginx, round robin, reverse proxy, etc.) to increase capacity, speed, availability etc. with the semantic business integration of services through url path mapping. An API Orchestrator addresses the semantic abstraction and integration of an underlying service landscape. API Gateway vs. API Orchestrator!

To my personal view, using an API Orchestrator is a acceptable solution in conjunction with microservices. It is the easiest and modest way to integrate and componse an underlying service layer.

Just to state a few positive and negative aspects:

  • Single entry point for standard business cases such as
    authentification, security issues, session mangament, logging etc.
  • Can also be started and managend as a microservice. Feel free to use a 3-tier layered architecture for the API orchestrator microservice
  • Abstracts the complexity of an underlying microservice layer

  • Might become a god thing.
  • In context of microservices, the API Orchestrator performs to much of business cases
  • High coupling, complexity...

Design trial of a nodeJS based API Orchestrator with HTTP Communication ...

  1. Evaluate a (web) server (express.js, hapi.js, your-own-node-server)
  2. Evaluate a http-request API (axios, node-fetch, r2, your-own-http-api). HTTP-API should resolve to a promise object!

Example of an express.js based API Orchestrator:

const express = require('express');
const http = require('http');
const path = require('path');
const app = express();
const port = 3000;

// define middleware plugins in express.js for your API gateway like session management ...

app.use(express.static(path.join(__dirname, 'public')));

// define relevant business/use case relevant semantic routes or commands e.g. /getAllUsers or REST-URL or /whatever

app.get('/whatever', (request, response) => {

    //consumes whatever service
    const getWhatEverToGet = () => {
        return new Promise((resolve, reject) => {
            //connection data should be read from a service registry or by configuration management (process level, file level, environemnt level)
            http.get({
                hostname: 'localhost',  
                port: 3001,
                path: `/whatever_service_url`
            }, (res) => {
                // built-in HTTP-API http.get() uses streams, hence "onData"-event should be buffered, not done here!
                res.on('data', (data) => { 
                    resolve(data.toString());
                });
            });
        });
    }

    // Here you can consume more services with the same code, when they are connected to each other use async/await to share data synchronized...

    //consumes whatever2 service returns promise
    //consumes whatever3 service returns promise

    const respondWhatEverData = async () => {
         let whatEver = await getWhatEverToGet(); 
         response.send(whatEver)
    }

    // trigger service complete
    respondWhatEverData();
})

app.listen(port, (err) => {
  if (err) {
    return console.log('Shit happens...', err)
  }
  console.log(`server listens on ${port}`)
})

TL;DR If your NodeJS application is only expected to forward the request to Spring Boot application, then NodeJS setup would probably not be worth it. You should look at Nginx revere proxy which can do all that efficiently.

Async / co / Promise / Async-Await are not frameworks. Promise / async-await are programming constructs in NodeJS; Async / co are convenience libraries to make manage asynchronous code manageable before Promises and async-await were introduced. That said there are multiple rest frameworks, that you could use to receive and pipe requests to your SpringBoot servers. Take a look at Express.JS, Restify, Sails.js all of them can add REST capabilities to NodeJS. You will also need a Rest Client library (like axios or request both of them then support Promises) to be able to forward your requests to target server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM