DEV Community

Glenn all
Glenn all

Posted on

what is microservice architecture?

Microservice architecture is a design approach for building software systems that are composed of small, independent, and modular components, known as microservices.

Each microservice is designed to perform a specific function or set of related functions, and can be developed, deployed, and maintained independently of the other microservices in the system. This allows for flexibility, scalability, and resiliency, as the system can be easily adapted and evolved to meet changing requirements.

Example of microservice:

let's define a simple microservice that exposes a single endpoint /add which accepts two numbers as query parameters, a and b, and returns the result of adding them.

tech stack Express

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

// A simple function that adds two numbers
function add(a, b) {
  return a + b;
}

// Define an endpoint that accepts two numbers
// and returns the result of adding them
app.get('/add', (req, res) => {
  const a = parseInt(req.query.a);
  const b = parseInt(req.query.b);
  const result = add(a, b);
  res.send({result});
});

// Start the server on port 3000
app.listen(3000, () => {
  console.log('Microservice listening on port 3000!');
});
Enter fullscreen mode Exit fullscreen mode

To run this microservice, you would need to install the required dependencies, such as Express, and then start the server by running the code. Once the server is running, you can send HTTP requests to the /add endpoint to test it out. For example, you could use the following curl command to send a request to add 2 and 3:

curl 'http://localhost:3000/add?a=2&b=3'

This would return the following response:

{"result":5}

Top comments (0)