Spring Boot Consul Tutorial Sinhala | Service Discovery | Centralized Config | Sri Lanka

Spring Boot Consul Tutorial Sinhala | Service Discovery | Centralized Config | Sri Lanka

හැඳින්වීම: Consul එක්ක Spring Boot App එකක් Supercharge කරගමු!

ආයුබෝවන් යාළුවනේ! අද අපි කතා කරන්න යන්නේ Microservices ලෝකයේදී අතිශයින්ම වැදගත් වෙන, ඒ වගේම අපේ Spring Boot Applications Smart කරගන්න පුදම සහයෝගයක් දෙන ටූල් එකක් ගැන. ඒ තමයි Consul. අද වෙනකොට Cloud-native Applications හදනකොට Service Discovery, Distributed Configuration, Health Checking වගේ දේවල් වලට Consul කියන්නේ නැතුවම බැරි දෙයක් වෙලා. මේ ලිපියෙන් අපි බලමු Spring Boot Application එකක් Consul එක්ක integrate කරලා, Service Discovery සහ Centralized Configuration කියන දේවල් කොහොමද කරගන්නේ කියලා.

සමහරවිට ඔයා හිතනවා ඇති, "මොනවද මේ Service Discovery, Centralized Configuration කියන්නේ?" කියලා. කණගාටු වෙන්න එපා! අපි මේ හැමදෙයක්ම පියවරෙන් පියවර, සරලව කියලා දෙනවා. අන්තිමට ඔයාටම පුළුවන් වෙයි පොඩි Spring Boot App එකක් හදලා Consul එක්ක Register කරලා, Config values Consul වලින් ගන්න විදිය බලන්න.

ඉතින්, අපි පටන් ගමු නේද?

Consul කියන්නේ මොකක්ද? ඇයි අපිට Consul ඕන වෙන්නේ?

සරලවම කිව්වොත්, Consul කියන්නේ HashiCorp සමාගම විසින් හදපු Service Mesh එකක කොටසක්. ඒකෙන් අපිට Service Discovery, Configuration, Segmentation, සහ Orchestration වගේ දේවල් කරන්න පුළුවන්. Microservices architecture එකක් පාවිච්චි කරනකොට Consul නැතුවම බැරි ඇයි කියලා පොඩ්ඩක් බලමු:

1. Service Discovery

  • ප්‍රශ්නය: Microservices ගොඩක් තියෙනකොට එක Service එකක් තවත් Service එකක් හොයාගන්නේ කොහොමද? IP addresses සහ Port numbers ස්ථිර නැහැ. ඒවා හැමවෙලේම වෙනස් වෙන්න පුළුවන්.
  • විසඳුම: Consul එකේ තියෙනවා Service Registry එකක්. අපේ Service එකක් Deploy වුණාම ඒක Consul එකේ Register වෙනවා. ඊට පස්සේ වෙන Service එකකට මේ Consul Registry එකෙන් අදාළ Service එකේ IP address එකයි Port එකයි අරගෙන ඒකට Call කරන්න පුළුවන්. මේක හරියට දුරකථන නාමාවලියක් (phone directory) වගේ.

2. Centralized Configuration

  • ප්‍රශ්නය: Microservices ගොඩක් තියෙනකොට හැම Service එකටම තමන්ගේම configuration files තියෙනවා. ඒවා වෙනස් කරන්න ඕන වුණාම හැම Service එකකම වෙන වෙනම ගිහින් වෙනස් කරන්න වෙනවා. මේක මාරම අමාරු වැඩක්.
  • විසඳුම: Consul එකේ තියෙනවා Key-Value Store එකක්. අපිට පුළුවන් අපේ application configurations මේකේ save කරලා තියන්න. ඊට පස්සේ හැම Spring Boot Service එකකටම පුළුවන් Consul එකෙන් අදාළ configurations ගන්න. මේකේ තියෙන විශේෂත්වය තමයි, අපි Consul එකේ configuration එකක් update කළොත්, අපේ Application එක Restart නොකරම අලුත් configuration එක apply කරගන්න පුළුවන් වීම! නියමයි නේද?

3. Health Checking

  • Consul, register වෙලා තියෙන Services වල Health එක හැමවෙලේම check කරනවා. Service එකක් down වුණොත්, Consul ඒක Registry එකෙන් ඉවත් කරනවා. ඒ නිසා වෙන Services වලට down වෙලා තියෙන Service එකකට call කරලා time waste වෙන්නේ නැහැ.

Consul Local එකේ Set up කරගමු

අපි මේ tutorial එකට Consul Docker container එකක් විදියට run කරමු. ඒක තමයි පහසුම විදිය.

පියවර 1: Docker Install කරගන්න

ඔයාගේ machine එකේ Docker install කරලා නැත්නම්, මුලින්ම Docker Desktop install කරගන්න ඕනේ. (Windows, macOS, Linux වලට අදාළව).

පියවර 2: Consul Docker Container එක Run කරමු

Terminal එක Open කරලා මේ command එක Run කරන්න:

docker run -d --name=consul -p 8500:8500 -p 8600:8600/udp consul agent -dev -client=0.0.0.0
  • -d: Container එක background එකේ run කරන්න.
  • --name=consul: Container එකට "consul" කියලා නමක් දෙන්න.
  • -p 8500:8500: Consul Web UI එකට access වෙන්න අවශ්‍ය Port එක map කරනවා. (http://localhost:8500)
  • -p 8600:8600/udp: DNS queries වලට අවශ්‍ය Port එක map කරනවා.
  • consul agent -dev -client=0.0.0.0: Development mode එකේ Consul agent එකක් run කරන්න. -client=0.0.0.0 කියන්නේ ඕනම IP address එකකින් මේ Consul agent එකට access වෙන්න පුළුවන් කියන එක.

Container එක successfuly run වුණාද කියලා බලන්න, ඔයාට පුළුවන් docker ps command එක පාවිච්චි කරන්න.

දැන් http://localhost:8500/ කියන URL එකට ගිහින් Consul Web UI එක බලන්න පුළුවන්. Services, Key/Value වගේ tabs ටික දැක්කොත් ඒක successful.

Spring Boot Application එකක් Consul එක්ක Register කරමු (Service Discovery)

අපි දැන් අපේ Spring Boot Application එක Consul Service Registry එකට Register කරන්නේ කොහොමද කියලා බලමු. මේකට අපි Spring Cloud Consul dependency එක පාවිච්චි කරනවා.

පියවර 1: Spring Boot Project එකක් හදමු

ඔයාට පුළුවන් Spring Initializr (https://start.spring.io/) එකට ගිහින් අලුත් Spring Boot project එකක් හදාගන්න. Dependencies විදියට පහත ඒවා add කරගන්න:

  • Spring Web
  • Spring Boot DevTools (Optional, නමුත් development වලදී පහසුයි)
  • Spring Cloud Starter Consul Discovery

Project එක Generate කරලා, Download කරලා, ඔයාගේ IDE එකට (IntelliJ IDEA, VS Code වගේ) Import කරගන්න.

පියවර 2: pom.xml Dependencies Add කරමු

ඔයා Maven පාවිච්චි කරනවා නම්, pom.xml එකේ පහත dependencies ටික තියෙනවාද කියලා check කරන්න. නැත්නම් add කරන්න. Spring Cloud version එකක් අනිවාර්යයෙන්ම add කරන්න ඕන.

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2021.0.8</version> <!-- ඔබේ Spring Boot version එකට ගැලපෙන එක යොදන්න -->
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

සැලකිය යුතුයි: spring-cloud-dependencies වල version එක ඔයාගේ Spring Boot version එකට ගැලපෙන එකක් වෙන්න ඕනේ. https://spring.io/projects/spring-cloud වෙබ් අඩවියට ගිහින් Compatible versions බලන්න පුළුවන්. උදාහරණයක් ලෙස Spring Boot 2.7.x සඳහා Spring Cloud 2021.0.x භාවිතා කළ හැක.

පියවර 3: application.yml Configuration එක එකතු කරමු

src/main/resources/application.yml file එක හදලා (හෝ තිබුණා නම් modify කරලා) මේ configuration එක add කරන්න:

spring:
  application:
    name: my-first-consul-service
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        health-check-path: /actuator/health
        health-check-interval: 10s
        instance-id: ${spring.application.name}:${random.value}
        register: true
        # tags: env=dev,version=1.0 # optional tags
server:
  port: 8081 # මේ service එකට වෙනම port එකක් දෙමු
  • spring.application.name: මේක තමයි Consul Registry එකේ අපේ Service එක Register වෙන නම.
  • spring.cloud.consul.host සහ port: Consul agent එක run වෙන host එකයි, port එකයි. අපි local එකේ run කරන නිසා localhost:8500.
  • discovery.health-check-path: Spring Boot Actuator health endpoint එක පාවිච්චි කරලා Consul එකෙන් අපේ Service එකේ Health check කරන්න පුළුවන්. මේක හොඳ practice එකක්. මේකට Spring Boot Actuator dependency එකත් ඕනේ වෙනවා, ඒත් Service Discovery වලට අනිවාර්ය නැති නිසා මම මුලින් add කරන්න කිව්වේ නැහැ. නමුත් production එකකදී අනිවාර්යයෙන්ම දාගන්න.
  • discovery.health-check-interval: කොච්චර වෙලාවකට සැරයක්ද Health check එක කරන්න ඕන කියලා.
  • discovery.instance-id: Service එකේ unique ID එකක්.
  • discovery.register: true: අපේ Service එක Consul එකේ Register කරන්න කියලා කියනවා.
  • server.port: මේ service එකේ Port එක.

පියවර 4: Simple REST Controller එකක් හදමු

Application එක Run කරලා බලන්න කලින්, පොඩි REST Controller එකක් හදමු.

package com.example.myfirstconsulservice;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String helloConsul() {
        return "Hello from my-first-consul-service!" + 
               " I am running on port 8081 and registered with Consul.";
    }
}

පියවර 5: Application එක Run කරලා බලමු

දැන් ඔයාගේ Spring Boot Application එක Run කරන්න. (මේන් Class එක run කරන්න). Console එකේ මේ වගේ message එකක් දකින්න පුළුවන්:

... c.h.c.c.c.ConsulServiceRegistry        : Registering service with consul: my-first-consul-service...

දැන් http://localhost:8500/ කියන Consul Web UI එකට යන්න. "Services" tab එක යටතේ ඔයාගේ my-first-consul-service කියන Service එක දැකගන්න පුළුවන් වෙයි. ඒ වගේම ඒකේ Health status එක passing කියලා පෙන්නයි. නියමයි නේද? ඔයාගේ Spring Boot Application එක සාර්ථකව Consul එකේ Register වෙලා!

Spring Boot Application එකක් Consul Config Store එකක් විදියට පාවිච්චි කරමු

දැන් අපි බලමු Consul එකේ Key-Value Store එක Centralized Configuration Store එකක් විදියට කොහොමද පාවිච්චි කරන්නේ කියලා. මේකෙන් අපිට පුළුවන් අපේ application configurations Consul එකේ තියාගෙන, Application එක Restart නොකරම ඒවා update කරන්න.

පියවර 1: Dependency එක එකතු කරමු

pom.xml එකට spring-cloud-starter-consul-config dependency එක එකතු කරන්න:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>

spring-cloud-dependencies section එක දැනටමත් උඩින් තියෙනවා නම්, ආයෙ දාන්න ඕන නැහැ.

පියවර 2: bootstrap.yml Configuration එක හදමු

Consul Config එක පාවිච්චි කරනකොට, application.yml වෙනුවට bootstrap.yml file එකක් පාවිච්චි කරන එක හොඳ practice එකක්. මොකද configurations Load වෙන්න ඕනේ Application Context එක create වෙන්න කලින් නිසා.

src/main/resources/bootstrap.yml file එක හදලා මේ configuration එක add කරන්න:

spring:
  application:
    name: my-first-consul-service
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        health-check-path: /actuator/health
        health-check-interval: 10s
        instance-id: ${spring.application.name}:${random.value}
        register: true
      config:
        enabled: true
        prefix: config
        format: YAML # or KEY_VALUE, PROPERTIES
        data-key: data
server:
  port: 8081

මෙහි අලුතින් එකතු කරලා තියෙන්නේ spring.cloud.consul.config section එකයි. එහි enabled: true කියන්නේ Consul Config Enable කරනවා කියන එකයි. prefix: config කියන්නේ Consul Key-Value Store එකේදී අපේ configuration values config/ කියන path එක යටතේ තියෙන්න ඕනේ කියන එකයි.

පියවර 3: Consul එකට Configuration Value එකක් Add කරමු

Consul Web UI එකට (http://localhost:8500/) යන්න. "Key/Value" tab එකට ගිහින් "Create" button එක click කරන්න. දැන් Key එකට config/my-first-consul-service/message කියලා දෙන්න. Value එකට ආයුබෝවන් Consul! මේක centralised config එකක්! කියලා දෙන්න. Format එක JSON හෝ YAML ලෙස තෝරාගත හැක.

නැත්නම්, ඔයාට පුළුවන් config/my-first-consul-service.yml කියන key එකට YAML format එකෙන් Value එකක් දාන්න:

Key: config/my-first-consul-service.yml

Value:

my:
  greeting:
    message: "ආයුබෝවන් Consul! මේක centralised config එකක්!"
  custom:
    value: "Spring Boot Application එකෙන් Consul config access කරනවා!"

අපි මේ tutorial එකට YAML format එක යොදා ගනිමු.

පියවර 4: Configuration Value එක Application එකෙන් Access කරමු

අපි දැන් Controller එක වෙනස් කරලා Consul එකෙන් config value එකක් කියවමු. මේකට අපි @Value annotation එක සහ @RefreshScope annotation එක පාවිච්චි කරනවා.

package com.example.myfirstconsulservice;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope // මේක දාන්නේ configuration changes automaticlly refresh වෙන්න
public class HelloController {

    @Value("${my.greeting.message}")
    private String greetingMessage;

    @Value("${my.custom.value}")
    private String customValue;

    @GetMapping("/hello")
    public String helloConsul() {
        return "Hello from my-first-consul-service! " +
               "Registered with Consul. " +
               "My greeting: <strong>" + greetingMessage + "</strong>" +
               "<br>Custom value: <strong>" + customValue + "</strong>";
    }
}
  • @RefreshScope: මේක අතිශයින්ම වැදගත්! මේකෙන් කියවෙන්නේ මේ Bean එකේ configuration properties, Consul එකේ වෙනස් වුණොත්, Application එක Restart නොකරම Refresh කරන්න කියලා.
  • @Value("${my.greeting.message}"): Consul එකේ තියෙන my.greeting.message කියන Key එකේ Value එක මේ greetingMessage කියන variable එකට load කරනවා.

පියවර 5: Application එක Run කරලා Config Changes බලමු

Application එක Run කරන්න. දැන් http://localhost:8081/hello කියන URL එකට ගිහින් බලන්න. ඔයා Consul එකට දාපු message එක එතනින් display වෙයි.

දැන් Consul Web UI එකට ගිහින් config/my-first-consul-service.yml Key එකේ Value එක වෙනස් කරන්න. උදාහරණයක් විදියට, my.greeting.message එක "ආයුබෝවන් Consul! මේක Dynamic config එකක්!" කියලා වෙනස් කරලා "Save" කරන්න.

ඊට පස්සේ ආයෙමත් http://localhost:8081/hello කියන URL එක refresh කරන්න. පුදුමයි නේද? ඔයාගේ Application එක Restart නොකරම අලුත් message එක එතන display වෙයි! මේක තමයි Centralized Configuration වල තියෙන ලොකුම වාසිය.

සමහර වෙලාවට Consul update එකක් වෙලා refresh වෙන්න පොඩි වෙලාවක් යන්න පුළුවන්. ඒකට /actuator/refresh endpoint එකට POST request එකක් යවන්න පුළුවන් (ඒකට Spring Boot Actuator සහ Spring Cloud Bus වගේ දේවල් පාවිච්චි කරන්න පුළුවන්). නමුත් @RefreshScope දාපු නිසා බොහෝ විට ස්වයංක්‍රීයව refresh වේ.

අවසන් වචන සහ ඉදිරි පියවර

අද අපි Spring Boot Application එකක් Consul එක්ක integrate කරලා, Service Discovery සහ Centralized Configuration කියන වැදගත් Concepts දෙකක් ගැන ඉගෙන ගත්තා. මේවා Microservices architecture එකකදී අත්‍යවශ්‍ය දේවල්. ඔයා දැන් දන්නවා Service එකක් Consul එකේ Register කරන්නේ කොහොමද, ඒ වගේම Consul එකෙන් configuration values අරගෙන Application එක Restart නොකරම update කරගන්නේ කොහොමද කියලා.

මේ tutorial එකෙන් ඔයාට හොඳ ආරම්භයක් ලැබුණා කියලා මම හිතනවා. දැන් ඔයාට පුළුවන් මේ දැනුම ඔයාගේම Microservices project වලට apply කරන්න. තවත් ගැඹුරට යන්න ඕන නම්, Consul Health Checking, ACLs, Service Mesh capabilities, සහ Spring Cloud Gateway එක්ක Consul පාවිච්චි කරන හැටි වගේ දේවල් ගැන හොයලා බලන්න පුළුවන්.

මතක තියාගන්න, Software Engineering කියන්නේ නිතරම අලුත් දේවල් ඉගෙන ගන්න ඕන ක්ෂේත්‍රයක්. මේ වගේ Concepts තේරුම් අරගෙන ඒවා practice කරන එක තමයි සාර්ථක Developer කෙනෙක් වෙන්න තියෙන හොඳම මාර්ගය.

මේ Tutorial එක ගැන ඔයාගේ අදහස් පහලින් Comment කරන්න. ඔයාට ප්‍රශ්න තියෙනවා නම් අහන්න. අපි උදව් කරන්න ලෑස්තියි!

ඔබ සැමට ජය!