Spring Boot Zookeeper Distributed Config | සිංහල | Dynamic Properties

Spring Boot Zookeeper Distributed Config | සිංහල | Dynamic Properties

ආයුබෝවන් හැමෝටම! 👋 අද අපි කතා කරන්න යන්නේ ඔයාලා ගොඩක් දෙනෙක්ට වැදගත් වෙන, විශේෂයෙන් distributed systems එක්ක වැඩ කරන අයට අත්‍යවශ්‍යම මාතෘකාවක් ගැන. ඒ තමයි Spring Boot එක්ක Apache Zookeeper පාවිච්චි කරලා distributed configuration management කරගන්න හැටි. 🚀

ඉතින්, අපි සාමාන්‍යයෙන් Spring Boot application එකක settings හරි configurations හරි තියාගන්නේ application.properties නැත්නම් application.yml වගේ local files වල. ඒත් හිතන්නකෝ ඔයාට microservices ගොඩක් තියෙනවා, ඒ හැම එකකටම පොදු configurations ටිකක් තියෙනවා කියලා. ඒ වගේම ඒ configurations run-time එකේදී, එහෙමත් නැත්නම් application එක restart නොකර වෙනස් කරන්න ඕන වුණොත් මොකද කරන්නේ? 🤔

ඒකට තියෙන නියම විසඳුමක් තමයි Zookeeper වගේ distributed coordination service එකක් පාවිච්චි කරන එක. මේ tutorial එකෙන් අපි බලමු Zookeeper කියන්නේ මොකක්ද, ඒක Spring Boot එක්ක සම්බන්ධ කරගන්නේ කොහොමද, ඒ වගේම configuration dynamic විදියට manage කරගන්නේ කොහොමද කියලා. මේක ඔයාලගේ cloud-native applications වලට වගේම microservices architecture වලටත් ගොඩක් වැදගත් වෙයි. එහෙනම් අපි පටන් ගමු! 💪

Zookeeper කියන්නේ මොකක්ද? 🐘

හරි, මුලින්ම අපි බලමු මේ Zookeeper කියන්නේ හරියටම මොකක්ද කියලා. සරලව කිව්වොත්, Apache Zookeeper කියන්නේ distributed applications වලට අවශ්‍ය coordination service එකක්. ඒ කියන්නේ, distributed system එකක තියෙන servers, nodes හරි microservices කිහිපයක් එකිනෙකා එක්ක කතා කරගන්න, state එක maintain කරගන්න, සහ consistency එක තියාගන්න Zookeeper උදව් කරනවා.

Zookeeper එක hierarchical file system එකක් වගේ structure එකක් පාවිච්චි කරනවා. මේකේ තියෙන nodes වලට කියන්නේ znode කියලා. මේ znode වලට data ටිකක් ගබඩා කරන්න පුළුවන්, ඒ වගේම sub-nodes හදන්නත් පුළුවන්. හරියට file system එකක directories වගේ.

Zookeeper ගේ ප්‍රධාන කාර්යයන් කිහිපයක්:

  • Configuration Management: System එකට අවශ්‍ය configuration data centralised විදියට තියාගන්න පුළුවන්. මේකේ තියෙන විශේෂත්වය තමයි, configuration එක වෙනස් වුණාම, Zookeeper එක්ක connect වෙලා ඉන්න හැම client කෙනෙක්ටම ඒ ගැන දැනුම් දෙන එක.
  • Naming: Distributed system එකේ තියෙන services හරි resources හරි හඳුනාගන්න පුළුවන් mechanism එකක් සපයනවා.
  • Distributed Synchronization: Distributed locks වගේ දේවල් implement කරන්න උදව් කරනවා. ඒ කියන්නේ, එකම resource එකක් multiple applications වලින් access කරනකොට ගැටුම් ඇති නොවෙන්න වළක්වනවා.
  • Group Services: Distributed system එකේ servers group කරලා, group membership manage කරන්න පුළුවන්.
  • Leader Election: Distributed system එකක leader කෙනෙක් තෝරගන්න වගේ සංකීර්ණ කාර්යයන් Zookeeper හරහා පහසුවෙන් කරන්න පුළුවන්.

Zookeeper වල තියෙන වැදගත්ම ලක්ෂණයක් තමයි ඒක highly reliable සහ consistent වීම. ඒක Cluster එකක් විදියට වැඩ කරන නිසා, එක Zookeeper server එකක් fail වුණත්, service එක දිගටම ක්‍රියාත්මක වෙනවා. මේකෙන් ඔයාලගේ applications වලට ඉහළ availability එකක් ලබාගන්න පුළුවන්.

Spring Boot Project එකකට Zookeeper එකතු කරගමු 🛠️

හරි, දැන් අපි Zookeeper කියන්නේ මොකක්ද කියලා දන්නවනේ. ඊළඟට අපි බලමු Spring Boot application එකක් එක්ක Zookeeper සම්බන්ධ කරගන්නේ කොහොමද කියලා. මේකට අපිට Spring Cloud Zookeeper කියන project එක ගොඩක් උදව් වෙනවා. මේක Spring Cloud Ecosystem එකේම කොටසක්.

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

මුලින්ම ඔයාලගේ pom.xml file එකට අවශ්‍ය dependencies ටික එකතු කරගන්න ඕනේ. අපි මෙතනදී Spring Cloud Zookeeper Config dependency එක පාවිච්චි කරනවා.

<dependencies>
    <!-- Spring Boot Starter Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Spring Cloud Zookeeper Config -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-config</artifactId>
    </dependency>

    <!-- Spring Boot Starter Test (optional) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<!-- Spring Cloud Dependencies BOM (Bill of Materials) -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2021.0.8</version> <!-- Replace with your Spring Cloud version -->
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

spring-cloud-dependencies වල version එක ඔයාලා පාවිච්චි කරන Spring Boot version එකට ගැලපෙන එකක් වෙන්න ඕනේ. ඒක Spring Cloud project page එකෙන් බලාගන්න පුළුවන්. (උදා: Spring Boot 2.7.x වලට Spring Cloud 2021.0.x series එක ගැළපෙනවා.)

පියවර 2: Configuration කිරීම

දැන් අපි Spring Boot application එකට කියන්න ඕනේ Zookeeper server එක තියෙන්නේ කොහෙද කියලා. මේක කරන්නේ bootstrap.properties නැත්නම් bootstrap.yml file එකේ. application.properties වෙනුවට bootstrap.properties පාවිච්චි කරන්නේ, Spring Boot application එක load වෙන මුල් අවස්ථාවේදීම configurations load කරගන්න ඕන නිසා.

src/main/resources/bootstrap.properties file එක මේ විදියට හදාගන්න:

# Zookeeper server address
spring.cloud.zookeeper.connect-string=localhost:2181

# Enable Zookeeper Config
spring.cloud.zookeeper.config.enabled=true

# Root folder for configurations in Zookeeper
spring.cloud.zookeeper.config.root=/config

මේ connect-string එකට ඔයාලගේ Zookeeper server එකේ IP address එකයි port එකයි දෙන්න ඕනේ. සාමාන්‍යයෙන් default port එක 2181. root කියන්නේ Zookeeper cluster එකේ කොතනින්ද configurations කියවන්න පටන් ගන්නේ කියලා. අපි මේක /config කියලා දුන්නොත්, අපේ application එකේ configurations Zookeeper එකේ /config/<application-name> path එක යටතේ බලනවා.

Zookeeper server එකක් ඔයාගේ local machine එකේ run කරගන්න නැත්නම්, Docker වගේ එකක් පාවිච්චි කරන්න පුළුවන්:

docker run --name zookeeper -p 2181:2181 -d zookeeper

මේ Docker command එකෙන් ඔයාට ඉක්මනින්ම local Zookeeper instance එකක් හදාගන්න පුළුවන්.

Zookeeper පාවිච්චි කරලා Distributed Configuration Management ⚙️

දැන් අපේ Spring Boot application එක Zookeeper එක්ක connect වෙන්න සූදානම්. ඊළඟට අපි බලමු කොහොමද Zookeeper වල configuration data store කරලා, ඒක අපේ application එක ඇතුලෙන් access කරගන්නේ කියලා.

Zookeeper වල Configuration Data ගබඩා කිරීම

Zookeeper එකේ configurations ගබඩා කරන්නේ znode විදියට. Spring Cloud Zookeeper Config එක විසින් අපේ application එකේ නම පාවිච්චි කරලා Zookeeper එකේ path එකක් හදාගන්නවා. උදාහරණයක් විදියට, ඔයාගේ application එකේ නම my-app නම් (spring.application.name=my-app), Zookeeper එකේ /config/my-app කියන path එක යටතේ configurations හොයනවා.

අපි හිතමු අපිට message.greeting කියන property එක Zookeeper එකේ store කරන්න ඕනේ කියලා. අපිට මේක Zookeeper CLI (Command Line Interface) එකෙන් හරි, GUI tool එකකින් හරි හදාගන්න පුළුවන්. Zookeeper CLI එක පාවිච්චි කරනවා නම්:

# Zookeeper client එකට connect වෙන්න
zkCli.sh -server localhost:2181

# /config/my-app/message/greeting කියන znode එක හදන්න
# (අවශ්‍ය නම් parent nodes ටිකත් හදාගන්න ඕනේ)
create /config "config_root"
create /config/my-app "app_root"
create /config/my-app/message "message_root"
create /config/my-app/message/greeting "ආයුබෝවන්, Zookeeper!"

# හදපු znode එකේ data බලන්න
get /config/my-app/message/greeting

මතක තියාගන්න, Zookeeper එකේ default behavior එක තමයි properties flatten කරන එක. ඒ කියන්නේ message.greeting වගේ එකක් /config/my-app/message/greeting කියන path එකට map වෙනවා. ඒක හරියට file system එකක file එකක් වගේ.

Spring Boot එකේ Configuration Access කිරීම

දැන් Zookeeper එකේ configuration එක තියෙනවනේ. අපිට මේක Spring Boot application එක ඇතුලෙන් සාමාන්‍ය විදියටම access කරන්න පුළුවන්, හරියට application.properties එකේ තියෙන property එකක් වගේ. මේකට අපිට @Value annotation එක පාවිච්චි කරන්න පුළුවන්.

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

@RestController
public class GreetingController {

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

    @GetMapping("/greeting")
    public String getGreeting() {
        return greetingMessage;
    }
}

දැන් ඔයාගේ Spring Boot application එක run කරලා /greeting endpoint එකට request එකක් යැව්වොත්, Zookeeper එකේ තියෙන "ආයුබෝවන්, Zookeeper!" කියන message එක ඔයාට ලැබෙයි. මෙතනදී අපි Local application.properties එකේ message.greeting property එක define කරලා තිබ්බත්, Spring Cloud Zookeeper Config එකේ precedence එක වැඩි නිසා Zookeeper එකෙන් configuration එක load වෙනවා.

මේක තමයි distributed configuration management වල මූලික අදහස. අපිට අවශ්‍ය හැම configuration එකක්ම centralized Zookeeper cluster එකක තියාගෙන, ඕනෑම Spring Boot application එකකට ඒක load කරගන්න පුළුවන්.

ප්‍රායෝගික උදාහරණයක්: Dynamic Configuration ♻️

දැන් අපි බලමු මේ Zookeeper configurations වල තියෙන සුපිරිම විශේෂාංගයක් ගැන - ඒ තමයි dynamic configuration updates. ඒ කියන්නේ, අපිට application එක restart නොකරම, run-time එකේදී configurations වෙනස් කරන්න පුළුවන්. මේක microservices architecture එකකදී ගොඩක් වටිනවා, මොකද service එකක් restart කරන එකෙන් downtime ඇතිවෙන්න පුළුවන්.

Spring Cloud Zookeeper Config මේ functionality එක සපයන්නේ Spring Cloud Context සහ Spring Cloud Bus වගේ dependencies එක්ක. හැබැයි මේ tutorial එක සරලව තියාගන්න අපි @RefreshScope annotation එක පාවිච්චි කරමු. @RefreshScope කියන්නේ Spring Boot application එකේ beans වලට, configuration changes වුණාම automatically refresh වෙන්න පුළුවන් හැකියාව දෙන එක.

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

@RefreshScope පාවිච්චි කරන්න නම්, අපිට spring-cloud-starter dependency එක අවශ්‍ය වෙනවා. මේකෙන් Spring Cloud Context module එක එනවා.

<dependencies>
    <!-- Existing dependencies -->
    <!-- ... -->

    <!-- Spring Cloud Starter (for @RefreshScope) -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter</artifactId>
    </dependency>

</dependencies>

pom.xml එකේ dependencyManagement block එකේ තියෙන spring-cloud-dependencies BOM එක නිසා, මේ spring-cloud-starter dependency එකේ version එක automatic manage වෙනවා.

පියවර 2: @RefreshScope පාවිච්චි කිරීම

දැන් අපි කලින් හදපු GreetingController එකට @RefreshScope annotation එක එකතු කරමු. ඒ වගේම @Value එකෙන් එන greetingMessage එකට අලුත් value එකක් ආවම controller එක refresh වෙන්න අවශ්‍ය නිසා.

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 // This annotation enables dynamic updates for this bean
public class GreetingController {

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

    @GetMapping("/greeting")
    public String getGreeting() {
        return greetingMessage;
    }
}

දැන් ඔයාලා Spring Boot application එක run කරන්න. /greeting endpoint එකට ගිහින් බලන්න. ඔයාට "ආයුබෝවන්, Zookeeper!" කියලා පෙනෙයි.

ඊළඟට, Zookeeper client එකෙන් හරි, Zookeeper GUI එකකින් හරි, /config/my-app/message/greeting කියන znode එකේ value එක වෙනස් කරන්න. උදාහරණයක් විදියට, "සුබ සන්ධ්‍යාවක්, Spring Boot!" කියලා වෙනස් කරමු.

# Zookeeper client එකට connect වෙන්න
zkCli.sh -server localhost:2181

# znode එකේ value එක වෙනස් කරන්න
set /config/my-app/message/greeting "සුබ සන්ධ්‍යාවක්, Spring Boot!"

දැන් ආයෙත් browser එකේ /greeting endpoint එක refresh කරන්න. පුදුමයි නේද? ඔයාට "සුබ සන්ධ්‍යාවක්, Spring Boot!" කියන අලුත් message එක පෙනෙයි. අපි application එක restart කරේ නෑ! 😍

මේක තමයි @RefreshScope වල බලය. Spring Cloud Zookeeper Config එක Zookeeper එකේ changes listen කරලා, change එකක් ආවම refresh event එකක් trigger කරනවා. @RefreshScope කරලා තියෙන beans ඒ event එකට react කරලා, අලුත් configuration values reload කරගන්නවා.

මේ ක්‍රමයෙන් ඔයාලට database connection strings, API keys, feature toggles වගේ ඕනෑම configuration එකක් production environment එකේදී වුණත්, application downtime එකක් නැතුවම update කරන්න පුළුවන්. නියමයි නේද? 😉

නිගමනය: Distributed Configuration වල බලය 💪

ඉතින් යාලුවනේ, මේ tutorial එකෙන් අපි Spring Boot application එකක් Zookeeper එක්ක සම්බන්ධ කරලා distributed configuration manage කරගන්න හැටි ඉගෙන ගත්තා. අපි දැක්කා Zookeeper කියන්නේ මොකක්ද, ඒක කොච්චර වැදගත්ද, ඒ වගේම Spring Cloud Zookeeper Config පාවිච්චි කරලා කොහොමද පහසුවෙන් dynamic configuration updates කරගන්න පුළුවන් කියලා.

Distributed systems සහ microservices architecture වලදී, centralized configuration management කියන්නේ අත්‍යවශ්‍ය දෙයක්. Zookeeper වගේ powerful tool එකක් පාවිච්චි කරන එකෙන් ඔයාලගේ applications වල maintainability එක, scalability එක, වගේම reliability එකත් වැඩි කරගන්න පුළුවන්. ඒ වගේම dynamic updates නිසා operations team එකටත් ගොඩක් පහසුවක් වෙනවා.

මේක ඔයාලගේ ඊළඟ Spring Boot project එකේදී අනිවාර්යයෙන්ම implement කරලා බලන්න. මොකද, theory දැනගෙන ඉන්නවට වඩා practice කරන එකෙන් තමයි හොඳටම ඉගෙන ගන්න පුළුවන්. මේ tutorial එක ගැන ඔයාලගේ අදහස්, ප්‍රශ්න, නැත්නම් ඔයාලා Zookeeper පාවිච්චි කරලා තියෙන අත්දැකීම් පහළ comments section එකේ ලියන්න අමතක කරන්න එපා. අපි ඊළඟ tutorial එකෙන් හම්බවෙමු! තව සුපිරි දේවල් ඉගෙන ගමු! 🤩