Spring Cloud Config: මධ්‍යගත Configuration කළමනාකරණය - SC Guide

Spring Cloud Config: මධ්‍යගත Configuration කළමනාකරණය - SC Guide

Spring Cloud Config: මධ්‍යගත Configuration කළමනාකරණය (Centralized Configuration Management)

හායි යාළුවනේ! කොහොමද ඔයාලට? අද අපි කතා කරමු Microservices ලෝකේ අපිට හැමදාම වගේ එන ලොකු ප්‍රශ්නයකට තියෙන නියම විසඳුමක් ගැන – ඒ තමයි Spring Cloud Config. ඔයාලා දැනටමත් Microservices එක්ක වැඩ කරනවා නම්, මේක ඇත්තටම ඔයාලගේ ජීවිතය ගොඩක් පහසු කරන tool එකක් වේවි. කවුරු හරි අහනවා නම් “මොකක්ද මේ මයික්‍රෝසර්විස් වල ලොකුම headache එක?” කියලා, ගොඩක් වෙලාවට උත්තරේ වෙන්නේ “Config files manage කරන එක” කියන එක වෙන්න පුළුවන්, නේද?

Config Management කියන්නේ මොකක්ද? (What is Configuration Management?)

අපි හිතමු ඔයාලා service දහයක් පහළොවක් තියෙන Microservices application එකක් හදනවා කියලා. මේ හැම service එකකටම වෙන වෙනම database credentials, API keys, third-party service URLs, port numbers වගේ දේවල් manage කරන්න වෙනවා. ඒවා අපි සාමාන්‍යයෙන් application.properties හෝ application.yml වගේ files වල තමයි දාන්නේ.

හැබැයි මෙතන ප්‍රශ්න කිහිපයක්ම මතුවෙනවා:

  • Distributed Nature: Service ගොඩක් තියෙනකොට හැම service එකේම config files වෙන වෙනම manage කරන එක ලොකු කරදරයක්.
  • Environment Specifics: Development, Staging, Production වගේ environment වලට වෙනස් config තියෙන්න පුළුවන්. ඒ හැම එකක්ම update කරන එක මාර risk එකක්. එක තැනක වැරදුනොත් වැඩේ අවුල්.
  • Security: Sensitive data (passwords වගේ) config files වල plain text විදිහට තියෙන එක භයානකයි.
  • Dynamic Updates: සමහර වෙලාවට අපිට application එක restart නොකර runtime එකේදී config වෙනස් කරන්න අවශ්‍ය වෙනවා. සාමාන්‍ය විදිහට එහෙම බෑ.

මේ වගේ ප්‍රශ්න වලට තියෙන පට්ටම විසඳුම තමයි Centralized Configuration Management කියන්නේ. ඒ කියන්නේ, ඔයාලගේ හැම service එකටම අවශ්‍ය වෙන config එක තැනක තියලා, ඕන වෙලාවට ඒ ඒ service එකට ඒවා ලබා දෙන එක.

Spring Cloud Config කියන්නේ මොකක්ද? (What is Spring Cloud Config?)

සරලවම කිව්වොත්, Spring Cloud Config කියන්නේ distributed externalized configuration support එකක් සපයන Library එකක්. මේකෙන් වෙන්නේ ඔයාලගේ application එකේ configuration settings එක තැනක, Centralized Server එකක, manage කරන්න පුළුවන් වෙන එක. ඒ server එකෙන් තමයි ඔයාලගේ Microservices (ඒ කියන්නේ clients) වලට අවශ්‍ය config files සපයන්නේ.

මේ server එකට config files store කරන්න විවිධ backends use කරන්න පුළුවන්. ඒ අතරින් වැඩිපුරම ජනප්‍රිය වෙන්නේ Git repositories. මොකද Git කියන්නේ version control කරන්න, වෙනස්කම් track කරන්න වගේ දේවලට නියම tool එකක් නිසා, config files manage කරන්නත් ඒක හොඳටම ගැලපෙනවා.

Spring Cloud Config වල වාසි මොනවද? (What are the benefits of Spring Cloud Config?)

  • Centralized Management: හැම service එකේම config එක තැනකින් manage කරන්න පුළුවන්. ඒ නිසා updates කරන එක, tracking කරන එක ගොඩක් ලේසියි.
  • Version Control: Git integration නිසා config file වලට කරන හැම වෙනසක්ම track කරන්න පුළුවන්. වැරදි configuration එකක් Deploy වුණොත්, කලින් version එකට ආපහු යන්න පුළුවන්.
  • Environment Specific Configurations: Development, Staging, Production වගේ environment වලට වෙනස් config profile හදන්න පුළුවන්. application-dev.yml, application-prod.yml වගේ files හදලා environment එක අනුව අවශ්‍ය config එක ලබා ගන්න පුළුවන්.
  • Security: Sensitive data (passwords, API keys) encrypt කරලා store කරන්න පුළුවන්. Config Server එකේදී ඒවා decrypt කරලා clients වලට යවනවා.
  • Dynamic Updates: Spring Cloud Bus සහ @RefreshScope වගේ දේවල් භාවිතයෙන් application එක restart නොකර runtime එකේදී config වෙනස් කරන්න පුළුවන්.

Config Server එකක් හදාගමු (Setting up a Config Server)

හරි, දැන් අපි theoretical පැත්ත කතා කරා ඇති. දැන් අපි බලමු කොහොමද මේ Spring Cloud Config Server එකක් හදා ගන්නේ කියලා. මේක පියවර කිහිපයකින් අපිට කරගන්න පුළුවන්.

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

මුලින්ම, Spring Initializr (start.spring.io) හරහා අලුත් Spring Boot project එකක් හදාගමු. මෙතනදී dependencies විදිහට අපිට Spring Cloud Config Server සහ Spring Web අවශ්‍ය වෙනවා.

ඔයාලට Maven හෝ Gradle use කරන්න පුළුවන්. මම මෙතනදී Maven use කරනවා.

පියවර 2: Dependencies එකතු කරන්න

ඔයාලගේ pom.xml file එකට පහත dependencies එකතු කරන්න:


<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</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>2023.0.1</version> <!-- Replace with your Spring Cloud version -->
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

spring-cloud-dependencies වල version එක ඔයාලගේ Spring Boot version එකට ගැලපෙන විදිහට දාන්න මතක තියාගන්න. (උදා: Spring Boot 3.2.x නම් Spring Cloud 2023.0.x වගේ).

පියවර 3: Config Server එක Enable කරන්න

ඔයාලගේ Spring Boot application එකේ main class එකේ @SpringBootApplication annotation එකට අමතරව @EnableConfigServer annotation එක එකතු කරන්න. මේකෙන් තමයි Spring Boot application එක Config Server එකක් විදිහට වැඩ කරන්න පටන් ගන්නේ.


package com.example.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

}

පියවර 4: Git Repository එක Configure කරන්න

දැන් ඔයාලගේ Config Server එකට කියන්න ඕනේ config files තියෙන්නේ කොහෙද කියලා. අපි මේ සඳහා Git repository එකක් use කරනවා. ඔයාලට පුළුවන් GitHub, GitLab, Bitbucket වගේ ඕනෑම Git hosting service එකක private හෝ public repository එකක් හදන්න. මම මෙතනදී public repository එකක් use කරනවා.

src/main/resources/application.yml (හෝ application.properties) file එකට පහත code එක එකතු කරන්න:


server:
  port: 8888

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/your-username/your-config-repo.git # Replace with your Git repo URL
          search-paths: config-data,shared-configs # Optional: specify subdirectories in your repo
          default-label: main # Or master, depending on your repo's default branch
          # username: your-git-username # Uncomment for private repos
          # password: your-git-password # Uncomment for private repos (use environmental variables or Vault for production)
          # clone-on-start: true # For caching, useful in dev

uri එකට ඔයාලගේ Git repository එකේ URL එක දෙන්න. search-paths එකෙන් කියන්නේ Git repo එකේ config files තියෙන්නේ මොන folder වලද කියන එක. මේක ඉතා වැදගත්! උදාහරණයක් විදිහට, මගේ Git repo එකේ config-data කියන folder එක ඇතුළේ තමයි මගේ config files තියෙන්නේ.

පියවර 5: Config Repository එක හදන්න

දැන් ඔයාලගේ Git repository එකට ගිහින් config files හදන්න. මේවා සාමාන්‍ය .properties හෝ .yml files වගේමයි. නමුත් Spring Cloud Config මේවා හඳුනාගන්න විශේෂ naming convention එකක් use කරනවා:

  • {application-name}.properties (or .yml) - Default configuration for the application.
  • {application-name}-{profile}.properties (or .yml) - Environment-specific configuration (e.g., my-service-dev.yml, my-service-prod.yml).

අපි හිතමු ඔයාලගේ client service එකේ නම my-service කියලා. එතකොට ඔයාලගේ Git repository එකේ structure එක මේ වගේ වෙන්න පුළුවන් (config-data කියන folder එක ඇතුළේ):


my-config-repo/
└── config-data/
    ├── my-service.yml
    ├── my-service-dev.yml
    └── my-service-prod.yml
    └── shared-messages.yml # A common config file for all services

my-service.yml file එකේ මේ වගේ දෙයක් තියෙන්න පුළුවන්:


message: Hello from default config in Git!
server.port: 8080

my-service-dev.yml file එකේ මේ වගේ දෙයක්:


message: Hello from DEV config in Git!
database.url: jdbc:mysql://localhost:3306/dev_db

my-service-prod.yml file එකේ මේ වගේ දෙයක්:


message: Hello from PROD config in Git!
database.url: jdbc:mysql://production_db_server:3306/prod_db

Config Server එක Test කරමු

දැන් ඔයාලගේ Config Server application එක Run කරන්න. සාමාන්‍යයෙන් මේක 8888 port එකේදී run වෙන්නේ. ඔයාලට පුළුවන් Browser එකේ හෝ Postman වගේ tool එකකින් පහත URLs වලට ගිහින් config එක check කරන්න:

  • http://localhost:8888/{application-name}/{profile} (e.g., http://localhost:8888/my-service/dev)
  • http://localhost:8888/{application-name}/{profile}/{label} (label කියන්නේ Git branch name එක)
  • http://localhost:8888/{application-name}/{label}

http://localhost:8888/my-service/dev එකට ගියාම ඔයාලට JSON response එකක් ලැබෙයි. ඒකේ ඔයාලගේ my-service.yml සහ my-service-dev.yml වල තියෙන properties merge වෙලා පෙන්වයි.

Config Client එකක් හදාගමු (Setting up a Config Client)

Config Server එක හදා ගත්තා වගේම ලේසියි client එකක් හදන එකත්.

පියවර 1: Spring Boot Project එකක් හදන්න (Client)

Spring Initializr හරහා අලුත් Spring Boot project එකක් හදන්න. මෙතනදී dependencies විදිහට අපිට Spring Cloud Config Client සහ Spring Web අවශ්‍ය වෙනවා.

පියවර 2: Dependencies එකතු කරන්න

ඔයාලගේ client project එකේ pom.xml file එකට පහත dependencies එකතු කරන්න:


<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</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>2023.0.1</version> <!-- Replace with your Spring Cloud version -->
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

පියවර 3: Client එක Configure කරන්න

Config client එකට Server එක ගැන කියන්න ඕනේ. මේක කරන්න අපි bootstrap.yml (හෝ bootstrap.properties) file එකක් use කරනවා. මතක තියාගන්න, සාමාන්‍ය application.yml එකට කලින් bootstrap.yml එක load වෙනවා. Config Server එකේ URL එක සහ application name එක වගේ දේවල් මෙතන දාන එක වැදගත්.


spring:
  application:
    name: my-service # This name must match the config file name in your Git repo (e.g., my-service.yml)
  config:
    import: optional:configserver:http://localhost:8888 # URL of your Config Server
  profiles:
    active: dev # This will load my-service-dev.yml. Change to prod for production config.

server:
  port: 8081 # Port for your client service (different from Config Server)

පියවර 4: Properties Read කරන්න

දැන් ඔයාලගේ client service එකේ Config Server එකෙන් එන properties Read කරන්න පුළුවන්. මේ සඳහා සාමාන්‍යයෙන් @Value annotation එක හෝ Environment interface එක use කරන්න පුළුවන්.


package com.example.myclientapp;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class MyClientApplication {

    @Value("${message:Default message if config not found}")
    private String message;

    @Value("${database.url:Default DB URL if config not found}")
    private String databaseUrl;

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

    @GetMapping("/")
    public String getMessage() {
        return "Config Message: " + message + "<br/>Database URL: " + databaseUrl;
    }

    @GetMapping("/ping")
    public String ping() {
        return "Client is running!";
    }
}

Config Client එක Test කරමු

මුලින්ම ඔයාලගේ Config Server එක Run වෙලා තියෙනවද කියලා බලන්න. ඊට පස්සේ client application එක Run කරන්න. මේක 8081 port එකේදී run වෙයි.

දැන් browser එකේ http://localhost:8081/ එකට යන්න. ඔයාලට ලැබෙයි Config Server එකෙන් ලබා ගත්තු message සහ database.url properties. ඔයාලා active profile එක dev විදිහට දාලා තියෙන නිසා my-service-dev.yml එකේ තියෙන values තමයි load වෙන්නේ. active: prod කියලා දැම්මොත් my-service-prod.yml එකේ තියෙන values load වෙයි.

මීට එහාට (Beyond the Basics)

Spring Cloud Config කියන්නේ සරලව config management වලට විතරක් සීමා වෙන්නේ නෑ. තව ගොඩක් දේවල් කරන්න පුළුවන්:

  • Encryption and Decryption: Sensitive data (passwords, API keys) Git repository එකේ encrypt කරලා තියන්න පුළුවන්. Config Server එකේදී ඒවා automatically decrypt කරනවා. මේ සඳහා JCE (Java Cryptography Extension) Unlimited Strength Jurisdiction Policy Files install කරන්න වෙනවා, ඒ වගේම Config Server එකට symmetric key එකක් (encrypt.key) හෝ asymmetric key pair (RSA keys) එකක් දෙන්න පුළුවන්.
  • Dynamic Refresh: Config Server එකේ config එක වෙනස් කරාට පස්සේ client services restart නොකර config එක update කරන්න @RefreshScope annotation එක සහ Spring Cloud Bus (හෝ manual refresh endpoint call කිරීමෙන්) use කරන්න පුළුවන්. Spring Cloud Bus වලදී Kafka/RabbitMQ වගේ message broker එකක් use කරලා config changes ගැන clients වලට දැනුම් දෙනවා. ඒක නියම feature එකක්!
  • Different Backends: Git වලට අමතරව HashiCorp Vault, JDBC-based databases, Redis, S3 bucket වගේ තැන්වල config files store කරන්න පුළුවන්.
  • High Availability: Production environment වලදී Config Server එකක single point of failure එකක් වෙන්න දෙන්න බෑ. ඒ නිසා Load Balancer එකක් පිටිපස්සේ Config Servers කිහිපයක් දාලා high availability හදාගන්න පුළුවන්. මේවා Eureka Service Registry එකත් එක්ක integrate කරන්නත් පුළුවන්.

අවසන් කතාව (Conclusion)

Spring Cloud Config කියන්නේ Microservices architecture එකේදී configuration management කරන එක මාර විදිහට පහසු කරන tool එකක්. centralized management, version control, environment-specific configurations, security, dynamic updates වගේ වාසි ගොඩක් මේකෙන් ලැබෙනවා. මේක use කරනකොට ඔයාලගේ code base එක clean වෙනවා වගේම, operations team එකට config changes manage කරන එකත් ගොඩක් ලේසි වෙනවා.

දැන් ඔයාලට Spring Cloud Config Server එකක් සහ Client එකක් හදාගෙන වැඩ කරන්න අවශ්‍ය මූලික දැනුම තියෙනවා. මේක ඇත්තටම Practical Guide එකක් විදිහට පාවිච්චි කරන්න පුළුවන්. මයික්‍රෝසර්විස් architect කරනකොට මේ වගේ Tools කොච්චර වටිනවද කියලා දැන් ඔයාලට තේරෙනවා ඇති.

මේ blog post එක ගැන ඔයාලගේ අදහස්, ප්‍රශ්න, අත්දැකීම් comment section එකේ කියන්න අමතක කරන්න එපා. මේක ඔයාලට වැදගත් වුණා නම් අනිත් අයත් එක්කත් share කරන්න. තව මොනවගේ topics ගැනද ඔයාලට දැනගන්න ඕනේ කියලා කියන්නත් පුළුවන්. එහෙනම් තවත් අලුත් Technical Article එකකින් හම්බවෙමු! Happy Coding!