codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🚂

Express.js

61 / 76 topics
60Microservices Architecture with Express.js61Service Discovery in Microservices62API Gateway in Microservices Architecture
Tutorials/Express.js/Service Discovery in Microservices
🚂Express.js

Service Discovery in Microservices

Updated 2026-05-15
10 min read

Service Discovery in Microservices

Introduction

In a microservices architecture, services are designed to be small, independent, and loosely coupled. This design allows for greater flexibility and scalability but introduces challenges such as service discovery. Service discovery is the process of finding and locating services within a network. In this tutorial, we will explore how to implement service discovery mechanisms using Express.js.

Concept

Service discovery is crucial in microservices architectures because it enables services to communicate with each other dynamically. Without service discovery, services would need to hard-code the addresses of other services, which can lead to brittle and inflexible systems. Instead, service discovery allows services to discover each other at runtime, making the system more resilient and easier to manage.

There are several approaches to service discovery:

  1. Client-Side Discovery: The client is responsible for discovering the location of the service.
  2. Server-Side Discovery: A central registry or server maintains information about all available services.
  3. Hybrid Approach: Combines both client-side and server-side discovery.

In this tutorial, we will focus on a simple server-side discovery mechanism using a centralized registry.

Examples

Step 1: Setting Up the Centralized Registry

First, let's set up a simple registry that keeps track of all available services. We'll use Express.js to create a basic API for registering and discovering services.

// registry.js
const express = require('express');
const app = express();
const port = 3000;

let services = [];

app.use(express.json());

app.post('/register', (req, res) => {
    const { name, url } = req.body;
    services.push({ name, url });
    res.status(201).send('Service registered');
});

app.get('/services', (req, res) => {
    res.json(services);
});

app.listen(port, () => {
    console.log(`Registry running at http://localhost:\${port}`);
});

Step 2: Registering Services

Next, let's create a few services that register themselves with the registry.

// service1.js
const express = require('express');
const axios = require('axios');

const app = express();
const port = 3001;
const serviceName = 'service1';
const serviceUrl = `http://localhost:\${port}`;

app.get('/', (req, res) => {
    res.send(`Hello from \${serviceName}!`);
});

async function registerService() {
    try {
        await axios.post('http://localhost:3000/register', { name: serviceName, url: serviceUrl });
        console.log(`\${serviceName} registered with the registry.`);
    } catch (error) {
        console.error(`Failed to register \${serviceName}:`, error);
    }
}

app.listen(port, async () => {
    console.log(`\${serviceName} running at \${serviceUrl}`);
    await registerService();
});
// service2.js
const express = require('express');
const axios = require('axios');

const app = express();
const port = 3002;
const serviceName = 'service2';
const serviceUrl = `http://localhost:\${port}`;

app.get('/', (req, res) => {
    res.send(`Hello from \${serviceName}!`);
});

async function registerService() {
    try {
        await axios.post('http://localhost:3000/register', { name: serviceName, url: serviceUrl });
        console.log(`\${serviceName} registered with the registry.`);
    } catch (error) {
        console.error(`Failed to register \${serviceName}:`, error);
    }
}

app.listen(port, async () => {
    console.log(`\${serviceName} running at \${serviceUrl}`);
    await registerService();
});

Step 3: Discovering Services

Now, let's create a client service that discovers and communicates with other services.

// client.js
const express = require('express');
const axios = require('axios');

const app = express();
const port = 3003;

app.get('/discover', async (req, res) => {
    try {
        const response = await axios.get('http://localhost:3000/services');
        const services = response.data;
        res.json(services);
    } catch (error) {
        console.error('Failed to discover services:', error);
        res.status(500).send('Failed to discover services');
    }
});

app.listen(port, () => {
    console.log(`Client running at http://localhost:\${port}`);
});

Running the Example

  1. Start the registry:

    node registry.js
    
  2. Start the services:

    node service1.js
    node service2.js
    
  3. Start the client:

    node client.js
    
  4. Discover services using the client:

    curl http://localhost:3003/discover
    

You should see a JSON response listing all registered services.

What's Next?

In this tutorial, we explored how to implement a simple server-side service discovery mechanism using Express.js. In the next section, we will delve into API Gateway in Microservices Architecture, which acts as a single entry point for clients and manages requests to different microservices.

Stay tuned for more advanced topics in building robust microservices architectures!


PreviousMicroservices Architecture with Express.jsNext API Gateway in Microservices Architecture

Recommended Gear

Microservices Architecture with Express.jsAPI Gateway in Microservices Architecture