Spring Boot සහ Consul: Service Discovery සහ Configuration සිංහලෙන් ඉගෙන ගනිමු

Spring Boot සහ Consul: Service Discovery සහ Configuration සිංහලෙන් ඉගෙන ගනිමු

ආයුබෝවන්! Spring Boot එක්ක Consul වල බලය unleash කරමු!

ඉතින් කොහොමද ඔයාලට? අද අපි කතා කරන්න යන්නේ නූතන Software Development වලදී ගොඩක් වැදගත් වෙන මාතෘකාවක් ගැන - ඒ තමයි Spring Boot Microservices එක්ක Consul එකතුවීම! ඔයා දැනටමත් Microservices ගැන අහලා ඇති. ඒක ලොකු application එකක් පොඩි, පොඩි, ස්වාධීන කොටස් වලට කඩලා හදන architecture එකක්. ඒකෙන් application එක maintain කරන එක, scale කරන එක, develop කරන එක ගොඩක් පහසු වෙනවා.

ඒත් මේ Microservices වල ලොකුම challenge එකක් තමයි Service Discovery කියන එක. ඒ කියන්නේ, එක service එකක් තව service එකක් එක්ක කතා කරන්න ඕන වුණාම, ඒ service එක කොහෙද තියෙන්නේ කියලා හොයාගන්නේ කොහොමද කියන එක. ඒ වගේම, අපේ applications වල configuration (settings) ටික manage කරන එකත් ලොකු ප්‍රශ්නයක් වෙන්න පුළුවන්. මේ හැමදේටම නියම විසඳුමක් තමයි HashiCorp Consul කියන්නේ.

අද මේ Article එකෙන් අපි Consul කියන්නේ මොකක්ද, ඒක Spring Boot එක්ක connect කරන්නේ කොහොමද, Service Discovery සහ Configuration Management වලට ඒක පාවිච්චි කරන්නේ කොහොමද කියලා පියවරෙන් පියවර බලමු. කට්ටියම සූදානම්ද? එහෙනම් පටන් ගමු!

1. Microservices සහ Service Discovery - ඇයි මේක වැදගත්?

හිතන්න ඔයාට ලොකු Online Shopping Application එකක් හදන්න තියෙනවා කියලා. මේකේ Customer Service එක, Product Service එක, Order Service එක, Payment Service එක වගේ ගොඩක් පොඩි, පොඩි services තියෙනවා. මේ හැම service එකක්ම වෙන වෙනම develop කරලා, deploy කරලා, manage කරන්න පුළුවන්.

Challenge එක: Customer Service එකට Order Service එකෙන් customer කෙනෙක්ගේ orders ටික ගන්න ඕන වුණාම, Order Service එක කොහෙද තියෙන්නේ, ඒකේ IP Address එක මොකක්ද, Port එක මොකක්ද කියලා දැනගන්නේ කොහොමද? මේ IP Address සහ Port එක hardcode කරන්න බැහැ, මොකද services scale කරනකොට, deploy කරනකොට ඒවා වෙනස් වෙන්න පුළුවන්.

මේකට තමයි Service Discovery කියන concept එක එන්නේ. Service Discovery කියන්නේ services වල IP addresses සහ ports dynamic විදිහට register කරලා, discover කරන්න පුළුවන් system එකක්. මේකෙන් වෙන්නේ:

  • Services වල ස්ථානය (location) ස්වයංක්‍රීයව සොයාගැනීම.
  • Services down වුණාම හෝ අලුතින් add වුණාම automatically update වීම.
  • Load Balancing පහසු වීම.

Consul කියන්නේ මේ Service Discovery Solution එකක්. ඒ වගේම තව ගොඩක් features Consul එකේ තියෙනවා. අපි දැන් Consul ගැන ටිකක් ගැඹුරින් බලමු.

2. Consul කියන්නේ මොකක්ද?

HashiCorp Consul කියන්නේ distributed system එකක Service Discovery, Configuration, සහ Segmentation solve කරන්න හදපු tool එකක්. Consul එකේ ප්‍රධාන Features කිහිපයක් තියෙනවා:

  • Service Discovery: Services register වෙලා, Client applications වලට වෙනත් services discover කරන්න පුළුවන්.
  • Health Checking: Services වල තත්ත්වය (healthy ද, unhealthy ද) continuous විදිහට check කරනවා. Unhealthy services discovery list එකෙන් remove කරනවා.
  • Key/Value Store: Distributed Configuration Management වලට සහ dynamic feature flags වලට පාවිච්චි කරන්න පුළුවන්.
  • Multi-Datacenter: විවිධ datacenters හරහා Consul එක scale කරන්න පුළුවන්.
  • Secure Service Communication (Consul Connect): Service Mesh functionality එකක්.

Consul Architecture එක සරලව ගත්තොත්, Consul Server agents සහ Client agents වලින් සමන්විතයි. Client agents services register කරලා, Health Checks run කරනවා. Server agents මේ data ටික manage කරලා, distributed consensus algorithm (Raft) එකකින් data consistency maintain කරනවා.

Consul Local Environment එකේ Run කරමු (Docker එක්ක)

Consul install කරන එක ගොඩක් පහසුයි, විශේෂයෙන්ම Docker පාවිච්චි කරනවා නම්. ඔයාගේ machine එකේ Docker install කරලා තියෙනවා නම්, මේ command එකෙන් Consul server එකක් run කරන්න පුළුවන්:

docker run -d --name=consul -p 8500:8500 -p 8600:8600/udp consul agent -dev -client=0.0.0.0

මේ command එකෙන් වෙන්නේ:

  • -d: Background එකේ run කරන්න.
  • --name=consul: Docker container එකට 'consul' කියලා නමක් දාන්න.
  • -p 8500:8500: Consul UI (Web Interface) සහ HTTP API access කරන්න.
  • -p 8600:8600/udp: DNS query කරන්න (Service Discovery වලට).
  • consul agent -dev -client=0.0.0.0: Development mode එකේ Consul agent එකක් run කරන්න. මේකෙන් වෙනවා, client agents connect කරන්න පුළුවන් හැම IP address එකකින්ම.

Consul server එක run වුණාට පස්සේ, ඔයාට web browser එකේ http://localhost:8500 වලට ගිහින් Consul UI එක බලන්න පුළුවන්. ඒකෙන් ඔයාට services, key/value entries, health checks වගේ දේවල් manage කරන්න පුළුවන්. ඒක නියමයි නේද!

3. Spring Boot එක්ක Consul Connect කරමු

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

Step 1: Project එකක් හදාගමු

අපි Spring Initializr (start.spring.io) එකට ගිහින් අලුත් Spring Boot project එකක් හදාගමු. මේකට පහත Dependencies add කරන්න:

  • Spring Web (REST APIs හදන්න)
  • Spring Boot Actuator (Health Checks වලට)
  • Spring Cloud Starter Consul Discovery (Consul Service Discovery වලට)
  • Spring Cloud Starter Consul Config (Consul Configuration වලට)

ඔයා Maven පාවිච්චි කරනවා නම්, pom.xml එකේ dependencies section එක මේ වගේ වෙයි (version numbers Spring Cloud Release Train එක අනුව වෙනස් වෙන්න පුළුවන්):

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-config</artifactId>
    </dependency>
</dependencies>

<!-- Spring Cloud Dependencies -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2023.0.0</version> <!-- Replace with your Spring Cloud Version -->
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Note: spring-cloud-dependencies version එක ඔයාගේ Spring Boot version එකට ගැලපෙන විදිහට දාන්න ඕනේ. ඒ ගැන information Spring Cloud documentation එකේ තියෙනවා.

Step 2: Spring Boot Application එක Configure කරමු

දැන් අපි application.yml (හෝ application.properties) file එකට Consul සම්බන්ධීකරණ settings ටික add කරමු.

# src/main/resources/application.yml

spring:
  application:
    name: my-spring-consul-service # Application එකට නමක් දෙන්න
  cloud:
    consul:
      host: localhost # Consul server එකේ IP Address එක
      port: 8500    # Consul server එකේ Port එක
      discovery:
        prefer-ip-address: true # IP Address එක register කරන්න
        instance-id: ${spring.application.name}:${random.value} # Unique ID එකක් දෙන්න
        health-check-path: /actuator/health # Health check endpoint එක
        health-check-interval: 10s # Health check කරන interval එක

මේ settings වලින් අපි අපේ Spring Boot application එක Consul server එක කොහෙද තියෙන්නේ කියලා කියනවා. spring.application.name එක තමයි Consul එකේ service එක register වෙන්නේ. prefer-ip-address: true කියන්නේ host name එකට වඩා IP address එක register කරන්න කියන එක. ඒ වගේම health-check-path එකෙන් Actuator health endpoint එක Consul එකට provide කරනවා, ඒකෙන් Consul අපේ service එක healthy ද කියලා check කරනවා.

Step 3: Main Application Class එක Update කරමු

Consul Service Discovery enable කරන්න, ඔයාගේ main Spring Boot application class එකේ @EnableDiscoveryClient annotation එක add කරන්න ඕනේ:

// src/main/java/com/example/demo/MySpringConsulServiceApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient // Consul Service Discovery enable කරන්න මේක අනිවාර්යයි
public class MySpringConsulServiceApplication {

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

4. Service Registration සහ Health Checks

දැන් ඔයාගේ Spring Boot application එක run කරලා බලන්න. ඒක successful විදිහට start වුණාට පස්සේ, http://localhost:8500 Consul UI එකට ගිහින් බලන්න. ඔයාට Services tab එක යටතේ my-spring-consul-service කියලා අලුත් service එකක් register වෙලා තියෙනවා දකින්න පුළුවන්.

ඒ වගේම, ඒ service එකට අදාළව Health Check එකක් auto configure වෙලා තියෙනවා. මේ Health Check එක /actuator/health endpoint එකට request කරලා, අපේ application එක healthy ද කියලා Consul එකට report කරනවා. ඒක නියමයි නේද? මොකද අපිට manually health checks configure කරන්න අවශ්‍ය වෙන්නේ නැහැ, Spring Cloud Consul ඒක automatically කරනවා.

ඔයාගේ application එක stop කරලා බලන්න. ටික වෙලාවකින් Consul UI එකෙන් service එක Unhealthy වෙලා, ඊට පස්සේ disappear වෙනවා දකින්න පුළුවන්. ඒකෙන් Consul වල dynamic nature එක හොඳට පැහැදිලි වෙනවා.

5. Consul Key-Value Store එක Configuration වලට පාවිච්චි කරමු

Consul වල තියෙන තවත් නියම feature එකක් තමයි Key/Value (KV) Store එක. මේක අපිට අපේ application වල configuration centrally manage කරන්න පාවිච්චි කරන්න පුළුවන්. ඒ කියන්නේ, අපිට application එක rebuild හෝ restart නොකරම configuration වෙනස් කරන්න පුළුවන්.

Step 1: bootstrap.yml එක Configure කරමු

Spring Boot application එකක් start වෙනකොට, මුලින්ම load වෙන්නේ bootstrap.yml (හෝ bootstrap.properties) file එක. Consul configuration load කරන්න අපි මේ file එක පාවිච්චි කරනවා.

src/main/resources folder එකේ bootstrap.yml කියලා අලුත් file එකක් හදලා, මේ content එක add කරන්න:

# src/main/resources/bootstrap.yml

spring:
  application:
    name: my-spring-consul-service
  cloud:
    consul:
      host: localhost
      port: 8500
      config:
        enabled: true # Consul Config Server enable කරන්න
        format: YAML  # Consul KV Store එකේ config format එක YAML විදිහට specify කරන්න
        data-centers: dc1 # ඔයාගේ datacenter නම (default dc1)
        prefix: config # Consul KV Store එකේ root path එක

මෙහිදී, spring.cloud.consul.config.enabled: true කියන එකෙන් Consul KV Store එකෙන් configuration load කරන්න enable කරනවා. format: YAML කියන්නේ අපේ configuration Consul KV store එකේ YAML format එකෙන් තියෙනවා කියන එක.

Note: bootstrap.yml සහ application.yml අතර වෙනස තේරුම් ගන්න. bootstrap.yml එක Spring Context එක build වෙන්නත් කලින් load වෙනවා, ඒ නිසා Consul connection details වගේ දේවල් මෙහිදී specify කරනවා.

Step 2: Consul KV Store එකේ Configuration Add කරමු

දැන් අපි Consul UI එකට ගිහින් (http://localhost:8500) Key/Value tab එකට යන්න. මෙතනදී අපි අපේ application එකට අවශ්‍ය configuration add කරන්න ඕනේ.

අපි my-spring-consul-service කියන application එකට message එකක් set කරමු. Consul එකේ convention එක අනුව, application specific configs තියෙන්නේ config/<application-name> කියන path එකේ. ඒ නිසා අපි config/my-spring-consul-service කියන path එක ඇතුලේ අපේ config එක හදමු.

දැන් config/my-spring-consul-service කියන path එකේ අලුත් Key එකක් හදමු (Add Key button එක ඔබලා):

  • Key: my.greeting.message
  • Value: කොහොමද, ලෝකේ! Consul එක්ක සම්බන්ධයි!

Value එකේ format එක YAML නිසා, ඔයාට complex structures වුණත් දාන්න පුළුවන්. උදාහරණයක් විදිහට:

# Key: my.app.settings
# Value:
message: "කොහොමද, ලෝකේ!"
author: "Java Guru"
version: 1.0

Step 3: Spring Boot Application එකේ Configuration Access කරමු

දැන් අපි Controller එකක් හදලා, Consul KV store එකෙන් load කරපු message එක expose කරමු. අපි @Value annotation එක පාවිච්චි කරනවා.

// src/main/java/com/example/demo/MyController.java

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

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

    @GetMapping("/greet")
    public String greet() {
        return "මචන්, " + greetingMessage;
    }
}

මෙහිදී ${my.greeting.message:Default Message} කියන්නේ my.greeting.message කියන property එක Consul එකෙන් load කරන්න කියලා. 만약 Consul එකේ ඒ property එක නැත්නම් Default Message කියන එක පාවිච්චි වෙනවා.

Step 4: Run කරලා Test කරමු

දැන් ඔයාගේ Spring Boot application එක run කරන්න. ඒක successful විදිහට start වුණාට පස්සේ, browser එකේ http://localhost:8080/greet වලට ගිහින් බලන්න. ඔයාට Consul KV store එකේ තියෙන message එක display වෙනවා දකින්න පුළුවන්.

Magic එක: දැන් Consul UI එකට ගිහින් my.greeting.message කියන Key එකේ Value එක වෙනස් කරන්න (උදාහරණයක් විදිහට: ආයුබෝවන්, Consul!). Spring Boot Actuator එක refresh කරන්න POST request එකක් යවන්න පුළුවන් /actuator/refresh endpoint එකට. නැත්නම්, Spring Boot Cloud Bus dependency එක add කරලා, changes publish කරන්න පුළුවන්. සරලම විදිහ තමයි service එක restart කරන එක. Restart කරලා බලන්න, අලුත් message එක load වෙලා තියෙයි!

මේකෙන් වෙන්නේ ඔයාගේ application එක rebuild හෝ redeploy නොකරම configuration වෙනස් කරන්න පුළුවන් වෙන එක. ඒක Microservices වලදී ගොඩක් වැදගත් feature එකක්!

අවසන් වශයෙන් (Conclusion)

අද අපි Spring Boot Microservices එක්ක Consul එකතු කරගෙන Service Discovery සහ Centralized Configuration Management කරන හැටි ඉගෙන ගත්තා. මේ technologies දෙක එකතු කිරීමෙන් ඔයාගේ distributed applications වල Flexibility, Scalability, සහ Maintainability ගොඩක් වැඩි කරගන්න පුළුවන්.

  • අපි Consul කියන්නේ මොකක්ද කියලා තේරුම් ගත්තා.
  • Docker පාවිච්චි කරලා Consul local environment එකේ run කරන හැටි බැලුවා.
  • Spring Boot application එකක් Consul එක්ක connect කරන හැටි සහ Service Registration වෙන හැටි ඉගෙන ගත්තා.
  • Consul Key/Value store එක පාවිච්චි කරලා Centralized Configuration manage කරන හැටි ප්‍රායෝගිකව බැලුවා.

දැන් ඔයාට මේ knowledge එක ඔයාගේ next Microservices project එකේදී පාවිච්චි කරන්න පුළුවන්. මේක හොඳ ආරම්භයක් විතරයි. Consul වල තව ගොඩක් features තියෙනවා (Service Mesh, ACLs වගේ), ඒ ගැනත් ඉගෙන ගන්න පුළුවන්.

මේ tutorial එකෙන් ඔයාට අලුත් දෙයක් ඉගෙන ගන්න ලැබුණා නම්, අනිවාර්යයෙන්ම පහළින් comment එකක් දාලා යන්න. ඔයාට ප්‍රශ්න තියෙනවා නම් ඒවත් අහන්න. මේක ඔයාගේම project එකක implement කරලා බලන්න. ඒක තමයි හොඳම ඉගෙනීමේ ක්‍රමය!

එහෙනම් ආයෙත් නියම tech topic එකකින් හමුවෙමු! සුභ දවසක්!