Java Microservices හැඳින්වීම | Scalability සහ API | SC Guide

Java Microservices හැඳින්වීම | Scalability සහ API | SC Guide

හැඳින්වීම: ලොකු බිංගෙයින් පොඩි කෑලිවලට...

ආයුබෝවන් යාළුවනේ! අද අපි කතා කරන්න යන්නේ Software Engineering ලෝකයේ දැන් තියෙන ලොකුම Trends එකක් ගැන – ඒ තමයි Microservices Architecture. කලින් කාලේ Software හදපු විදිහයි, දැන් හදන විදිහයි ගොඩක් වෙනස් වෙලා. අද කාලේ Applications හැදෙන්නේ හරිම වේගයෙන්, ඒ වගේම ඒවාට ලොකු Traffic එකක් handle කරන්නත් ඕනේ. මේකට හොඳම විසඳුමක් තමයි Microservices කියන්නේ.

ඔබ Java Developer කෙනෙක් නම්, නැත්නම් Software Development ගැන ඉගෙන ගන්න පටන් ගත්ත කෙනෙක් නම්, Microservices කියන්නේ අනිවාර්යයෙන්ම දැනගෙන ඉන්න ඕන දෙයක්. මේ Guide එකෙන් අපි Microservices Architecture එක කියන්නේ මොකක්ද, ඒකෙන් මොනවද අපිට ලැබෙන වාසි, අභියෝග මොනවද, ඒ වගේම Java සහ Spring Boot භාවිතයෙන් සරල Microservice එකක් හදාගන්නේ කොහොමද කියලා කතා කරමු.

1. මොකක්ද මේ Microservices Architecture?

සරලවම කිව්වොත්, Microservices Architecture කියන්නේ එක ලොකු Application එකක්, පොඩි පොඩි, ස්වාධීන කොටස් (Services) ගොඩකට කඩලා හදන ක්‍රමයක්. මේ හැම පොඩි කොටසක්ම තමන්ට අදාළ විශේෂ කාර්යයක් කරනවා. මේක තේරුම් ගන්න හොඳම උදාහරණය තමයි Monolithic Application එකක් එක්ක සංසන්දනය කරන එක.

Monolithic Application එකක් කියන්නේ මොකක්ද?

හිතන්න, ඔයා ලොකු ගෙයක් හදනවා කියලා. ඒ ගෙදර හැම කාමරයක්ම (Kitchen, Living Room, Bedroom) එකටම සම්බන්ධයි, එකම පදනමක් උඩ. Software එකකදී Monolithic කියන්නේ, මුළු Application එකම එකම code base එකක් විදිහට, එකම Unit එකක් විදිහට Build කරලා, Deploy කරන එක. ඒක හරියට 'All-in-one' පැකේජයක් වගේ. මේක මුලදී ලේසියි වගේ පෙනුනත්, Application එක ලොකු වෙනකොට, Team එක ලොකු වෙනකොට ගැටලු එනවා.

Microservices එතකොට කොහොමද?

අර උදාහරණයෙන්ම කිව්වොත්, Microservices කියන්නේ අර ලොකු ගේ වෙනුවට, පොඩි පොඩි කුටි ගොඩක් (මේවා Kitchen එකට, Bedroom එකට, Living Room එකට වගේ විශේෂිත වෙන්න පුළුවන්) තියෙන එකක්. මේ හැම කුටියක්ම ස්වාධීනයි. එකක් කැඩුනොත් අනිත් ඒවාට බලපාන්නේ නැහැ, ඕන කුටියක් වෙන වෙනම හදන්න, පින්තාරු කරන්න පුළුවන්. Software එකකදී, Customer Management, Product Catalog, Order Processing වගේ එක එක Business Functions වෙන වෙනම Services විදිහට හදන එක තමයි Microservices Architecture එකේ තියෙන්නේ.

  • Small and Independent: හැම Service එකක්ම පොඩියි, තමන්ගේම කාර්යයක් තියෙනවා, තමන්ගේම database එකක් තියෙන්න පුළුවන්.
  • Loosely Coupled: Services එකිනෙකට තදින් සම්බන්ධ වෙලා නැහැ. ඒ කියන්නේ එක Service එකක වෙනසක් වුනොත් අනිත් ඒවාට ලොකුවට බලපාන්නේ නැහැ.
  • APIs: Services එකිනෙකාට කතා කරන්නේ APIs (Application Programming Interfaces) හරහා. ගොඩක් වෙලාවට REST APIs තමයි මේකට භාවිත කරන්නේ.

2. Microservices වල වාසි මොනවද?

දැන් ඔයාලට තේරෙනවා ඇති Monolithic Architecture එකට වඩා Microservices හරිම වෙනස් කියලා. එතකොට මේකෙන් අපිට ලැබෙන වාසි මොනවද?

a. Scalability (ප්‍රසාරණය වීමේ හැකියාව)

හිතන්න, අපේ Application එකට Customer Management Service එකට විතරක් ලොකු Traffic එකක් ආවා කියලා. Monolithic එකක නම් මුළු Application එකම Scale කරන්න වෙනවා. නමුත් Microservices වලදී, අපිට Customer Management Service එක විතරක් වෙනම Scale කරන්න පුළුවන්. ඒ කියන්නේ, අවශ්‍ය Service එකට විතරක් Resources (CPU, Memory) වැඩි කරන්න පුළුවන්. මේකෙන් අනවශ්‍ය වියදම් වළක්වා ගන්න පුළුවන්.

b. Independent Deployment (ස්වාධීනව Deploy කිරීමේ හැකියාව)

Monolithic Application එකක පොඩි වෙනසක් කරත්, මුළු Application එකම නැවත Build කරලා Deploy කරන්න වෙනවා. මේක වෙලාව නාස්ති කරන, Risk එක වැඩි කරන දෙයක්. Microservices වලදී, Product Service එකේ වෙනසක් කරා නම්, අපිට ඒ Service එක විතරක් Deploy කරන්න පුළුවන්. අනිත් Services වලට ඒකෙන් කිසිම බලපෑමක් වෙන්නේ නැහැ. මේක Continuous Delivery (CD) වලට හරිම වැදගත්.

c. Technology Diversity (විවිධ තාක්ෂණික විසඳුම් භාවිතය)

Monolithic එකක් ගොඩක් වෙලාවට එකම Programming Language එකකින්, එකම Framework එකකින් හදන්නේ. Microservices වලදී, අපිට එක Services එකක් Java වලින්, තව එකක් Python වලින්, තව එකක් Node.js වලින් හදන්න පුළුවන්. Business requirement එකට අනුව හොඳම Technology Stack එක තෝරා ගන්න මේකෙන් අවස්ථාව ලැබෙනවා.

d. Resilience (ප්‍රතිශක්තිය)

එක Microservice එකක් Fail වුණත්, මුළු Application එකම කඩාවැටෙන්නේ නැහැ. Monolithic එකකදී නම් එක component එකක් වැරදුනොත් මුළු Application එකම Down වෙන්න පුළුවන්. Microservices වලදී, Customer Service එක Fail වුණොත්, Order Service එක තවදුරටත් වැඩ කරන්න පුළුවන්. මේකෙන් User Experience එකට වෙන බලපෑම අඩු වෙනවා.

e. Easier Maintenance and Development (පහසු නඩත්තුව සහ සංවර්ධනය)

කුඩා code base එකක් නිසා, Developers ලට code එක තේරුම් ගන්න, වෙනස්කම් කරන්න ලේසියි. නව Developers ලාට Project එකට සම්බන්ධ වෙන්න ලේසියි. Team වලට එකිනෙකට ස්වාධීනව වැඩ කරන්න පුළුවන් නිසා Development Process එක වේගවත් වෙනවා.

3. අභියෝග සහ සලකා බැලීම්

Microservices වල වාසි ගොඩක් තිබුණත්, මේක හැම Project එකකටම සුදුසු නැහැ. මේකේ තියෙන අභියෝග ටිකකුත් තියෙනවා.

a. Complexity (සංකීර්ණත්වය)

Monolithic එකකට වඩා Microservices Architecture එකක් හදන එක, Maintain කරන එක සංකීර්ණයි. Services ගොඩක් තියෙන නිසා, ඒවා Manage කරන්න, Deploy කරන්න, Monitoring කරන්න වෙනම Tools සහ Expertise ඕනේ.

b. Distributed Data Management (Distributed Data කළමනාකරණය)

සෑම Service එකකටම තමන්ගේම Database එකක් තිබ්බොත් Data Consistency එක Maintain කරන එක අභියෝගයක් වෙනවා. Transactions manage කරන එකත් Monolithic එකකට වඩා සංකීර්ණයි.

c. Inter-service Communication (සේවා අතර සන්නිවේදනය)

Services එකිනෙකාට කතා කරන්න ඕනේ. මේකට Synchronous (REST APIs) සහ Asynchronous (Message Queues like RabbitMQ, Kafka) ක්‍රම භාවිත කරනවා. මේ Communication flows නිවැරදිව Design කිරීම අත්‍යවශ්‍යයි.

d. Monitoring and Debugging (නිරීක්ෂණය සහ Debugging)

Services ගොඩක් තිබ්බම, error එකක් ආවොත් ඒක හොයාගන්න එක, Trace කරන එක අමාරු වෙන්න පුළුවන්. Distributed Tracing, Centralized Logging වගේ Concepts මේකට භාවිත කරනවා.

4. Java වලින් Microservices – සරල උදාහරණයක්

Java කියන්නේ Microservices හදන්න ගොඩක් ජනප්‍රිය Language එකක්. විශේෂයෙන්ම Spring Boot Framework එක මේකට ගොඩක් උදව් වෙනවා. Spring Boot නිසා Microservice එකක් හදන එක හරිම ලේසියි.

අපි හිතමු, අපිට ProductService එකක් හදන්න ඕනේ කියලා. මේකෙන් Product විස්තර Manage කරනවා. OrderService එකකට Product විස්තර ඕන නම් මේ ProductService එකෙන් ගන්න පුළුවන්.

පළමු පියවර: Dependencies

ඔබට Maven හෝ Gradle භාවිතයෙන් Spring Boot Project එකක් ආරම්භ කළ හැකියි. අවශ්‍ය වන ප්‍රධාන Dependency එක වන්නේ spring-boot-starter-web යන්නයි. මේකෙන් REST APIs හදන්න අවශ්‍ය සියල්ලම ලැබෙනවා.

<!-- pom.xml (Maven) -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

දෙවන පියවර: Product Model එක

අපි Product එකක් නිරූපණය කරන්න සරල Java Class එකක් හදාගමු.

// src/main/java/com/example/productservice/model/Product.java
package com.example.productservice.model;

public class Product {
    private String id;
    private String name;
    private double price;

    public Product() {
    }

    public Product(String id, String name, double price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    // Getters and Setters
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product{" +
               "id='" + id + '\'' +
               ", name='" + name + '\'' +
               ", price=" + price +
               '}';
    }
}

තෙවන පියවර: ProductService Controller එක

දැන් අපි Product Service එකේ APIs හදාගමු. මේක Spring Boot Controller එකක් විදිහට හදනවා. මෙතනදී අපි සරලව Product ටිකක් Memory එකේ තියාගෙන තමයි වැඩ කරන්නේ, නමුත් ඇත්ත Project එකකදී Database එකක් භාවිත කරනවා.

// src/main/java/com/example/productservice/ProductServiceApplication.java
package com.example.productservice;

import com.example.productservice.model.Product;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@SpringBootApplication
@RestController
@RequestMapping("/products")
public class ProductServiceApplication {

    private List<Product> products = new ArrayList<>();

    public static void main(String[] args) {
        SpringApplication.run(ProductServiceApplication.class, args);
    }

    @PostConstruct // This method runs after the application starts
    public void init() {
        products.add(new Product("P001", "Laptop", 1200.00));
        products.add(new Product("P002", "Mouse", 25.00));
        products.add(new Product("P003", "Keyboard", 75.00));
    }

    @GetMapping // GET /products
    public List<Product> getAllProducts() {
        return products;
    }

    @GetMapping("/{id}") // GET /products/{id}
    public Optional<Product> getProductById(@PathVariable String id) {
        return products.stream()
                       .filter(p -> p.getId().equals(id))
                       .findFirst();
    }

    @PostMapping // POST /products
    public Product addProduct(@RequestBody Product newProduct) {
        products.add(newProduct);
        return newProduct;
    }
}

මේ Code එක ගැන කෙටියෙන් පැහැදිලි කළොත්:

  • @SpringBootApplication: මේක Spring Boot Application එකක් කියලා පෙන්නන Annotation එක.
  • @RestController: මේ Class එක REST API Endpoint එකක් කියලා කියනවා.
  • @RequestMapping("/products"): මේ Service එකේ හැම API එකක්ම /products වලින් පටන් ගන්නවා.
  • @PostConstruct: Application එක පටන් ගත්තම මුලින්ම Product ටිකක් Memory එකට දාගන්න අපි මේක භාවිත කළා.
  • @GetMapping, @PostMapping: මේවා HTTP Request types වලට Map කරනවා. (GET කියන්නේ Data ගන්න, POST කියන්නේ Data යවන්න).
  • @PathVariable: URL එකේ කොටසක් Variable එකක් විදිහට ගන්න.
  • @RequestBody: HTTP Request Body එකෙන් Object එකක් විදිහට Data ගන්න.

දැන් මේ Application එක Run කරාම (ProductServiceApplication.java එකේ main method එක Run කිරීමෙන්) ProductService එක Localhost එකේ Run වෙනවා. ඔබට Postman වගේ Tool එකක් භාවිත කරලා මේ APIs Call කරන්න පුළුවන්:

  • GET http://localhost:8080/products: හැම Product එකක්ම ගන්න.
  • GET http://localhost:8080/products/P001: 'P001' ID එක තියෙන Product එක ගන්න.
  • POST http://localhost:8080/products (Body එකට JSON විදිහට Product details දාන්න): අලුත් Product එකක් Add කරන්න.

මේ තමයි සරලම Microservice එකක්. මේ වගේ තවත් Service එකක් (උදාහරණයක් විදිහට OrderService එකක්) හදලා, ඒක ඇතුලෙන් RestTemplate හෝ WebClient වගේ දෙයක් භාවිත කරලා ProductService එකේ APIs Call කරන්න පුළුවන්. එතකොට ඒ Services දෙක අතර API හරහා Communication වෙනවා.

අවසන් වශයෙන්

අද අපි Microservices Architecture එකේ මූලික සංකල්ප, එහි වාසි සහ අභියෝග, ඒ වගේම Java සහ Spring Boot භාවිතයෙන් සරල Microservice එකක් හදාගන්න ආකාරය ගැන කතා කළා. Microservices කියන්නේ ලොකු Topic එකක්, මේක ආරම්භයක් විතරයි.

මේ Architecture එකෙන් ඔබට Scalable, Resilient, සහ Maintain කරන්න ලේසි Applications හදන්න පුළුවන්. නමුත් මතක තියාගන්න, හැම Project එකකටම Microservices සුදුසු නැහැ. ඔබේ Project එකේ අවශ්‍යතා අනුව තමයි නිවැරදි Architecture එක තෝරාගන්න ඕනේ.

ඔබේ මීළඟ Project එකට Microservices Try කරලා බලන්න. මේ Guide එක ඔබට ප්‍රයෝජනවත් වෙන්න ඇති කියලා හිතනවා. ඔබට මේ ගැන තියෙන ප්‍රශ්න, අදහස්, නැත්නම් ඔබේ අත්දැකීම් පහළ Comment Section එකේ කියන්න අමතක කරන්න එපා. අපි තවත් මේ වගේ වැදගත් Topics ගැන ඉදිරියේදී කතා කරමු!