Skip to content

emka.web.id

writing knowledge, recording civilization

Menu
  • Home
  • Tutorial
  • Search
Menu

Contoh Arsitektur Microservices di NodeJS

Posted on May 18, 2024

Dunia pengembangan perangkat lunak terus berkembang, dan arsitektur microservices telah menjadi pendekatan yang efektif untuk membangun aplikasi yang skalabel (scalable), tangguh (resilient), dan mudah dipelihara (maintainable). Menggunakan Node.js, developer dapat menciptakan microservices yang mendukung pengembangan aplikasi berbasis produk, memastikannya agile dan efisien. Panduan ini akan membahas prinsip, pola desain, dan praktik terbaik untuk membangun arsitektur microservices dengan Node.js, lengkap dengan contoh terperinci termasuk service communicator untuk memfasilitasi komunikasi antar-layanan.

Mengenal Arsitektur Microservices:

Arsitektur microservices membangun aplikasi sebagai kumpulan layanan yang longgar (loosely coupled), masing-masing menangani domain bisnis tertentu. Layanan ini dapat diterapkan dan diubah skalanya secara mandiri, sehingga meningkatkan kelincahan (agility) dan ketangguhan (resilience) aplikasi.

Merancang Microservices:

1. Dekonstruksi Monolith:

  • Identifikasi dan ekstrak domain bisnis atau fungsionalitas yang berbeda dari aplikasi monolitik menjadi layanan terpisah.

2. Mendefinisikan Kontrak Layanan:

  • Tetapkan interface dan protokol komunikasi yang jelas antar microservices, biasanya menggunakan RESTful HTTP atau GraphQL.

3. Manajemen Data:

  • Setiap layanan harus memiliki basis data atau penyimpanan data sendiri, sehingga meminimalkan berbagi data antar layanan untuk menghindari keterkaitan yang erat.

Membangun Microservices dengan Node.js:

Mari kita membangun tiga microservices: UserService, ProductService, dan OrderService. Selain itu, kita akan membuat modul Communicator untuk komunikasi antar layanan.

1. Persiapan Proyek Microservices:

Buat project Node.js baru untuk setiap microservice:

mkdir user-service product-service order-service communicator
cd user-service
npm init -y
npm install express axios

Ulangi langkah di atas untuk service lainnya dan modul communicator.

2. Membangun Microservices dengan Express.js:

UserService:

JavaScript

// user-service/index.js
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3001;

app.get('/api/users', (req, res) => {
  res.json({ users: [{ id: 1, name: 'John Doe' }] });
});

app.listen(PORT, () => {
  console.log(`User Service is running on port ${PORT}`);
});

ProductService:

// product-service/index.js
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3002;

app.get('/api/products', (req, res) => {
  res.json({ products: [{ id: 1, name: 'Product A' }] });
});

app.listen(PORT, () => {
  console.log(`Product Service is running on port ${PORT}`);
});

OrderService:

// order-service/index.js
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3003;

app.get('/api/orders', (req, res) => {
  res.json({ orders: [{ id: 1, user_id: 1, product_id: 1 }] });
});

app.listen(PORT, () => {
  console.log(`Order Service is running on port ${PORT}`);
});

3. Membuat Communicator:

Communicator akan mengelola klien HTTP untuk semua layanan, memfasilitasi komunikasi antar layanan.

Communicator:

// communicator/index.js
const axios = require('axios');

class Communicator {
  constructor() {
    this.userServiceClient = axios.create({ baseURL: 'http://localhost:3001/api' });
    this.productServiceClient = axios.create({ baseURL: 'http://localhost:3002/api' });
    this.orderServiceClient = axios.create({ baseURL: 'http://localhost:3003/api' });
  }

  async getUsers() {
    const response = await this.userServiceClient.get('/users');
    return response.data;
  }

  async getProducts() {
    const response = await this.productServiceClient.get('/products');
    return response.data;
  }

  async getOrders() {
    const response = await this.

Terbaru

  • How to Install Clean Windows 11 Without Any Bloatware (Step-by-Step Guide)
  • IP Address Rate Limiting is Flawed—Here is the Right Way to Do It
  • Are Fedora 45 Stuck in Its Comfort Zone? A Short Review
  • Hacktober Recap 2026, A Month Full of Dangerous CVE: eMudhra, Gitlab, Brotli, PHP, NodeJS etc
  • 7 Skill Keamanan Jaringan yang Membuat Network Engineer Makin Dibutuhkan
  • How to Spin Up Disposable macOS and Linux VMs on Apple Silicon with TartVM
  • Belajar Cyber Security dari Nol hingga Siap Kerja, Ini Roadmap Lengkapnya
  • Running Windows 11 on Apple Silicon: UTM vs. Parallels Desktop vs. VMware Fusion
  • Testing PikaOS, a Rolling Debian Distribution Special for Gaming and Content Creation
  • Engineering the On-Premises AI Data Center: An In-Depth Technical Review of the HP ZGX Fury AI Station
  • Trying my new Favourite Task Manager, TMOG on Omarchy Linux
  • ASRock Industrial AI Box A395: Is This the Best Compact Strix Halo System for Local AI?
  • Redox OS: A Rust-Based Microkernel Operating System (And How It Differs from Linux)
  • Canonical Is Bringing Back Ubuntu Core Desktop — This Time With a Clearer Plan
  • systemd Officially Adds Support for Rust Components
  • Rocky Linux Creator Builds a Brand-New “Not Linux” Kernel in 11 Days Using AI
  • Building a local AI agent in Workshop on Ubuntu 26.10
  • Omarchy 4.5 RC: 5 New Apps That Actually Look Useful
  • Worth It! Switching from Fedora KDE to Kubuntu
  • I’m Happy Using ‘Herdr’, The Fastest Growing Devtool written in Rust
  • Testing Goose on Omarchy Linux (App & CLI): The Open-Source Solution for Daily Task Automation
  • GNOME Founder Attacks Omarchy Founder as “Lazy, Racist, Ignorant”
  • Why I Stopped Using Plain Command Line Docker and Use Portainer 3.0 To Make My Home Projects Fun Again
  • Why I Switched to a Proper Firewall Box with OPNSense for My Homelab Stuff
  • Fixing Windows Error 0x80070005 Hyper-V Export Access Denied Error the Right Way
  • Fixing Hyper V Export Errors and Building a Better Virtual Machine Setup (error 0x80070002, 0x80070005)
  • Fixing the Dreaded Windows Audio Service Stuck on Stopping Error
  • Windows SIM Error, Unknown error 0xc1420127
  • How I’m Building a Cat Breeding Management App with Python Qt6 + Microsoft Litebox, in 1 Week
  • Why Windows Keeps Nuking Your GPU Drivers (And How to Take Back Control)
  • Canonical: Ubuntu coming soon to Snapdragon X2 Series platforms
  • Finally, wolfSSL adds post-quantum algorithms!
  • How to Run Gemma Embedding Models Using Docker
  • Deploy Nginx Rootful Container with Podman
  • How to Sandboxing Browser on Linux Desktop with Flatpak
  • OpenCode Browse Explained: Automate Any Website WITHOUT APIs
  • Leaked Qwen 4 and GLM 5.5 Post-Training, Beating the 27B Models
  • Do you Agree? Anthropic Secretly Watermarks All Claude’s Text!
  • Some of New Gemini Notebook Update is CRAZY GOOD: Native Mobile Recording
  • Clover 35B A3B Local Test | Diffusion Model Beats Qwen 35B MoE? | Local AI Setup with llama.cpp
  • Lagi rame soal ASN pake mobil dinas buat liburan keluarga, kok bisa dibilang korupsi?
  • Sering lewat di FYP atau X, ini arti istilah seme dan uke yang sering dibahas penggemar anime dan manga
  • Kisah pilu Gilang Dirga pas kehilangan Ace, sempat curhat di podcast Ivan Gunawan soal perjuangan jadi orang tua
  • Aktris muda Noa Nakada meninggal dunia di usia 23 tahun, padahal masih main di Ultraman Teo
  • SpongeBob Bakal Pindah ke RTV November 2026? Warga Lagi Rame Bahas Rumor Ini

©2026 emka.web.id | Design: Newspaperly WordPress Theme