Spring Boot Consul Integration | Service Discovery Sinhala Tutorial

කොහොමද යාලුවනේ! Spring Boot එක්ක Consul පාවිච්චි කරලා Service Discovery සහ Centralized Configuration කරන හැටි
අද අපි කතා කරන්න යන්නේ නූතන Software Engineering වල හරිම වැදගත් මාතෘකාවක් ගැන – ඒ තමයි Spring Boot Applications, Consul එක්ක එකතු කරලා Service Discovery සහ Centralized Configuration කරන හැටි. දැනටමත් Microservices Architecture එකත් එක්ක වැඩ කරන අයට වගේම, ඒ ගැන ඉගෙන ගන්න පටන් ගන්න අයටත් මේ Tutorial එක හරිම ප්රයෝජනවත් වේවි.
දැන් කාලේ ගොඩක් Applications හදන්නේ Microservices Architecture එක පාවිච්චි කරලා. මේකෙන් අපේ Application එක එකිනෙකට ස්වාධීන (independent) පොඩි පොඩි Services ගොඩකට කඩලා හදනවා. ඒකේ වාසි ගොඩක් තිබුණට, අභියෝග (challenges) කිහිපයකුත් තියෙනවා:
- Service Discovery: එක Service එකක් තව Service එකක් හොයාගන්නේ කොහොමද? IP Address එකක් Hardcode කරන්න බැහැ, මොකද Services නිතරම Scale වෙනවා, එහෙම නැත්නම් Restart වෙනකොට IP Addresses වෙනස් වෙන්න පුළුවන්.
- Configuration Management: Application එකේ Configuration Properties (Database connections, API Keys, etc.) ගොඩක් තියෙන්න පුළුවන්. මේවා Microservices ගොඩක් අතර share කරන්නේ සහ Manage කරන්නේ කොහොමද? ඒ වගේම, Application එක Restart නොකර Configuration වෙනස් කරන්න පුළුවන්ද?
මේ වගේ අභියෝග වලට නියම විසඳුමක් තමයි Consul කියන්නේ. මේ Tutorial එක අවසානයේදී ඔබට පුළුවන් වෙයි Spring Boot Application එකක් Consul එක්ක සාර්ථකව integrate කරලා, මේවාට විසඳුම් ලබාගන්න. එහෙනම් අපි පටන් ගමු!
Consul කියන්නේ මොකක්ද? (What is Consul?)
HashiCorp Consul කියන්නේ distributed systems වලට අවශ්ය Service Discovery, Health Checking, Key-Value Store (K/V Store), Multi-Datacenter Federation වගේ සේවාවන් සපයන tool එකක්. Microservices Architectures වලදී, Consul කියන්නේ නැතිවම බැරි Component එකක්.
Consul වල ප්රධාන Feature ටිකක් මෙන්න:
- Service Discovery (සේවා හඳුනාගැනීම): Consul වල ප්රධානම Feature එක තමයි මේක. Services වලට තමන්ගේ IP Address සහ Port එකත් එක්ක Consul register වෙන්න පුළුවන්. වෙනත් Services වලට අවශ්ය Service එක Consul ළඟින් ඉල්ලලා, ඒකේ IP Address එක සහ Port එක ලබාගන්න පුළුවන්. මේකෙන් Hardcoded IP Addresses නැතිව Dynamic Service Locating කරන්න පුළුවන්.
- Health Checking (සෞඛ්ය පරීක්ෂාව): Consul වලට Registered Services වල Health එක Check කරන්න පුළුවන්. ඒ කියන්නේ, Service එකක් Down වුණොත්, Consul එකට ඒක detect කරලා, ඒ Service එක Available Services list එකෙන් අයින් කරන්න පුළුවන්. මේකෙන් System Resilience එක වැඩි වෙනවා.
- Key-Value Store (K/V Store - යතුරු-අගය ගබඩාව): Consul එකේ Distributed K/V Store එකක් තියෙනවා. මේක පාවිච්චි කරලා Configuration Properties වගේ දේවල් ගබඩා කරන්න පුළුවන්. මේ Configuration, Services වලට Dynamically ලබාගෙන, Application එක Restart නොකර Refresh කරන්න පුළුවන්.
- Multi-Datacenter Federation (බහු දත්ත මධ්යස්ථාන ෆෙඩරේෂණය): Consul වලට Multiple Datacenters හරහා Services connect කරන්න පුළුවන්. මේක ලොකු Distributed Systems වලට හරිම ප්රයෝජනවත්.
Spring Boot එක්ක Consul පාවිච්චි කරන්නේ ඇයි? (Why use Consul with Spring Boot?)
Spring Boot කියන්නේ Java Applications හදන්න තියෙන ජනප්රියම Framework එකක්. Microservices Architecture එකක් හදනකොට, Spring Boot Applications අතරේ Service Discovery සහ Configuration Management වලට Consul පාවිච්චි කරන එක හරිම Smart විසඳුමක්.
Spring Cloud Consul project එක, Spring Boot applications වලට Consul වල features පහසුවෙන් integrate කරන්න පුළුවන් විදියට හදලා තියෙනවා. ඒකෙන් අපිට Boilerplate Code ගොඩක් ලියන එක මගහරවා ගන්න පුළුවන්.
Consul සහ Spring Boot එකට පාවිච්චි කිරීමෙන් ලැබෙන වාසි:
- Dynamic Service Registration: Application එක Startup වෙනකොටම Consul ළඟ Register වෙනවා.
- Load Balancing: Consul එකෙන් Service Instances ගොඩක් අතර Load Balancing කරන්න පුළුවන්. (Spring Cloud LoadBalancer වගේ Client-side Load Balancers එක්ක වැඩ කරනවා).
- Centralized Configuration: සියලුම Configuration එක තැනකින් (Consul K/V Store) Manage කරන්න පුළුවන්.
- Runtime Configuration Refresh: Application එක Restart නොකර Configuration වෙනස් කරන්න පුළුවන්.
- Increased Resilience: Health Checks නිසා Down වුණු Service Instances වලට Requests යවන එක නවතිනවා.
Spring Boot Project එකක් Consul එක්ක integrate කරමු (Integrating a Spring Boot Project with Consul)
හරි, දැන් අපි බලමු Spring Boot Project එකක් Consul එක්ක integrate කරන්නේ කොහොමද කියලා. මේකට පියවර කිහිපයක් තියෙනවා.
1. Consul Server එක Setup කරගනිමු
මුලින්ම අපිට Consul Server එකක් ඕනේ. Local development වලට Docker පාවිච්චි කරන එක ලේසියි. ඔබේ System එකේ Docker Install කරලා නැත්නම්, Docker Website එකට ගිහින් Install කරගන්න. Consul Server එක Development Mode එකේ Start කරන්න මේ Command එක භාවිතා කරන්න:
docker run -d --name consul -p 8500:8500 hashicorp/consul:latest agent -dev -client=0.0.0.0
මේ Command එකෙන් Consul Container එක Background එකේ Run වෙනවා. දැන් ඔබට පුළුවන් Consul UI එකට මේ URL එකෙන් පිවිසෙන්න: http://localhost:8500/ui. මෙතනින් ඔබට Registered Services සහ Key-Value data බලන්න පුළුවන්.
2. Maven Dependencies එකතු කිරීම (pom.xml)
ඊළඟට, ඔබේ Spring Boot Project එකට අවශ්ය Maven Dependencies (හෝ Gradle නම් ඒකට අවශ්ය Dependencies) එකතු කරන්න ඕනේ. Service Discovery සහ Configuration දෙකටම වෙන වෙනම Starter Dependencies තියෙනවා. ඔබේ pom.xml
file එකට මේවා එකතු කරන්න:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version> <!-- ඔබේ Spring Boot Version එකට අනුව මේක වෙනස් වෙන්න පුලුන් -->
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>my-hello-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>my-hello-service</name>
<description>Demo Spring Boot service with Consul</description>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2023.0.1</spring-cloud.version> <!-- Spring Cloud Version එක මෙතනින් සඳහන් කරන්න -->
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Consul Service Discovery Starter -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- Consul Config Starter -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</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>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
සැලකිය යුතුයි: spring-boot.version
සහ spring-cloud.version
ඔබේ Project එකට ගැලපෙන විදියට යාවත්කාලීන කරන්න. මේවා එකිනෙකට Compatible වන Versions විය යුතුයි. Spring Cloud Project එකේ Official Documentation එකෙන් මේ ගැන තොරතුරු හොයාගන්න පුළුවන්.
3. Application Properties Configuration (application.yml)
ඊළඟට, ඔබේ src/main/resources/application.yml
(හෝ application.properties
) file එකට Consul Configuration එකතු කරන්න. අපි YAML format එක පාවිච්චි කරමු, මොකද ඒක කියවන්න පහසුයි:
spring:
application:
name: my-hello-service # ඔබේ Service එකට නමක් දෙන්න
cloud:
consul:
host: localhost # Consul Server එකේ IP Address/Hostname එක
port: 8500 # Consul Server එකේ Port එක
discovery:
enabled: true # Service Discovery Enable කරනවා
instance-id: ${spring.application.name}:${random.value} # Unique Instance ID එකක් generate කරනවා
register: true # Consul ළඟ Register වෙනවා
prefer-ip-address: true # IP Address එකෙන් Register වෙන්න කියනවා (Hostname එක වෙනුවට)
config:
enabled: true # Centralized Configuration Enable කරනවා
format: KEY_VALUE # Consul K/V store එකේ data format එක. YAML, PROPERTIES වගේ ඒවාත් පුළුවන්.
prefix: config # Consul K/V store එකේ base path එක
default-context: application # Default config context එක. 'config/application' යටතේ ඇති config ගන්න.
data-key: data # K/V store එකේ value එක තියෙන Key එක (Format KEY_VALUE නම්)
profile-specific-info: true # active profile එකට අනුවත් config ගන්න (e.g., config/my-hello-service/development/data)
server:
port: 0 # Service එක Random Port එකක Run කරන්න (Multiple instances වලට වැදගත්)
විස්තර:
spring.application.name
: මේක හරිම වැදගත්. ඔබේ Service එක Consul ළඟ Register වෙන්නේ මේ නමින්. වෙනත් Services වලට මේ නම පාවිච්චි කරලා ඔබේ Service එක හොයාගන්න පුළුවන්.server.port: 0
: මේකෙන් ඔබේ Spring Boot Application එක Random available Port එකක Start වෙනවා. මේක Microservices වලට හරිම වැදගත්, මොකද ඔබට එකම Machine එකේ එකම Service එකේ Multiple Instances Run කරන්න අවශ්ය වෙන්න පුළුවන්.spring.cloud.consul.discovery.*
: මේවා Service Discovery සම්බන්ධ Configurations.register: true
කියන්නේ ඔබේ Application එක Consul ළඟ Register වෙනවා කියන එකයි.spring.cloud.consul.config.*
: මේවා Centralized Configuration සම්බන්ධ Configurations.prefix: config
කියන්නේ Consul K/V Store එකේconfig/
කියන Path එකෙන් Configuration බලනවා කියන එකයි.default-context: application
කියන්නේconfig/application
යටතේ තියෙන Global Configurations ගන්න කියන එකයි. ඒ වගේමconfig/your-service-name
යටතේ තියෙන Service-specific configurations ත් මේකෙන් ලබාගන්නවා.
4. Spring Boot Main Application Class
ඔබේ Main Application Class එකේ @EnableDiscoveryClient
Annotation එක එකතු කරන්න. මේකෙන් Spring Cloud වලට Discovery Client එක Enable කරන්න පුළුවන්.
package com.example.helloservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; // මේක එකතු කරන්න
@SpringBootApplication
@EnableDiscoveryClient // මේ Annotation එක එක් කරන්න
public class MyHelloServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MyHelloServiceApplication.class, args);
}
}
ප්රායෝගික උදාහරණයක් (A Practical Example)
දැන් අපි මේ සියල්ල එකට සම්බන්ධ කරලා සරල Hello World Service එකක් හදමු.
Project එකක් හදමු
ඔබට පුළුවන් Spring Initializr එකෙන් අලුත් Maven Project එකක් හදාගන්න. Dependencies විදියට Spring Web
, Consul Discovery
, සහ Consul Config
තෝරන්න. Java 17+
සහ Spring Boot 3.x+
භාවිතා කරන්න.
1. `pom.xml` (උඩ දැක්වූ පරිදි)
ඔබේ pom.xml
file එක, Section 2 හි දැක්වූ පරිදි Consul Dependencies එක්ක Configure කරන්න.
2. `application.yml` (උඩ දැක්වූ පරිදි)
ඔබේ application.yml
file එකත් Section 3 හි දැක්වූ පරිදි Configure කරන්න. spring.application.name
එක my-hello-service
විදියට තියන්න.
3. `MyHelloServiceApplication.java` (උඩ දැක්වූ පරිදි)
ඔබේ Main Application Class එකට @EnableDiscoveryClient
Annotation එක එකතු කරන්න.
4. සරල REST Controller එකක් හදමු (`HelloController.java`)
දැන් අපි Consul K/V Store එකෙන් Configuration එකක් Load කරන REST Controller එකක් හදමු. src/main/java/com/example/helloservice/controller/HelloController.java
කියන Path එකේ මේ Class එක හදන්න:
package com.example.helloservice.controller;
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 // මේ Annotation එකෙන් config වෙනස් වුණාම bean එක refresh කරනවා.
public class HelloController {
@Value("${my.greeting:Hello from default config!}") // Consul K/V එකෙන් "my.greeting" කියන key එකේ value එක ගන්නවා.
private String greetingMessage;
@GetMapping("/hello")
public String hello() {
// Service Instance ID එකත් එක්ක Message එකක් Return කරනවා.
return greetingMessage + " - This is My Hello Service, running on port " + System.getProperty("server.port");
}
}
විස්තර:
@RestController
: මේක RESTful API එකක් හදනවා කියලා Spring එකට කියනවා.@RefreshScope
: මේ Annotation එක හරිම වැදගත්! මේකෙන් කියවෙන්නේ, මේ Bean එකේ තියෙන Properties, Consul K/V Store එකේ Configuration වෙනස් වුණොත් Automatically Refresh වෙන්න ඕනේ කියලා. මේක Application එක Restart නොකර Configuration යාවත්කාලීන කරන්න උදව් වෙනවා.@Value("${my.greeting:Hello from default config!}")
: මේකෙන්my.greeting
කියන Property එකේ Value එකgreetingMessage
කියන Variable එකට Inject කරනවා.:Hello from default config!
කියන්නේ,my.greeting
Property එක හොයාගන්න බැරි වුණොත් Default Value එක විදියට මේ String එක පාවිච්චි කරනවා කියන එකයි.
5. Consul K/V Store එකට Configuration එකතු කිරීම
දැන් අපි my.greeting
කියන Property එක Consul K/V Store එකට එකතු කරමු:
- ඔබේ Web Browser එකෙන් http://localhost:8500/ui Consul UI එකට යන්න.
- වම් පැත්තේ මෙනු එකෙන් "Key/Value" කියන Tab එක තෝරන්න.
- ඉහළින්ම තියෙන "Create" බොත්තම ක්ලික් කරන්න.
- "Key" field එකට
config/my-hello-service/my.greeting
කියලා Type කරන්න. (application.yml
එකේprefix: config
සහspring.application.name: my-hello-service
නිසා.) - "Value" field එකට ඔබට අවශ්ය Message එකක් Type කරන්න. උදාහරණයක් විදියට:
ආයුබෝවන්, Consul වෙතින්!
- "Save" බොත්තම ක්ලික් කරන්න.
ඔබ default-context: application
භාවිතා කරන්නේ නම්, ඔබට config/application/my.greeting
ලෙසද Key එක එකතු කළ හැක. Service-specific configuration (config/my-hello-service/my.greeting
) Global configuration (config/application/my.greeting
) override කරයි.
6. Application එක Run කර Test කරමු
- ඔබේ Spring Boot Application එක Run කරන්න. (ඔබේ IDE එකෙන් හෝ
mvn spring-boot:run
command එකෙන්) - Application එක Start වුණාට පස්සේ, Consul UI එකේ "Services" Tab එකට යන්න. ඔබට
my-hello-service
කියන Service එක Registered වෙලා තියෙනවා දකින්න පුළුවන්. - Terminal එකේ හෝ IDE console එකේ ඔබට ඔබේ Service එක Run වන Port එක දැකගන්න පුළුවන් (
server.port: 0
නිසා Random Port එකක්). උදාහරණයක් විදියට,http://localhost:52425/hello
වගේ URL එකක්. ඒ URL එකට Browser එකෙන් යන්න. ඔබට Consul K/V Store එකේ දැමූ Message එක පෙන්විය යුතුයි:ආයුබෝවන්, Consul වෙතින්! - This is My Hello Service, running on port 52425
- දැන්, ආයෙත් Consul UI එකට යන්න (
http://localhost:8500/ui
). "Key/Value" Tab එකේconfig/my-hello-service/my.greeting
කියන Key එකේ Value එක වෙනස් කරන්න. උදාහරණයක් විදියට:ආයුබෝවන්, Consul Update!
කියලා. Save කරන්න. - දැන් ආයෙත්
http://localhost:YOUR_APP_PORT/hello
URL එකට Browser එකෙන් ගිහින් බලන්න. ඔබට අලුත් Message එක (ආයුබෝවන්, Consul Update! - This is My Hello Service, running on port 52425
) පෙනේවි! මේකෙන් Application එක Restart නොකර Configuration වෙනස් කරන්න පුළුවන් කියලා ඔප්පු වෙනවා.
ඔබේ Application එකේ Configuration Refresh කරන්න, POST Request එකක් /actuator/refresh
කියන Endpoint එකට යවන්න. මේකට ඔබට cURL හෝ Postman වගේ Tool එකක් පාවිච්චි කරන්න පුළුවන්:
curl -X POST http://localhost:YOUR_APP_PORT/actuator/refresh
(YOUR_APP_PORT
වෙනුවට ඔබේ Application එක Run වන Port එක දාන්න)
Service Discovery Test කිරීම (අමතර):
දැන් ඔබ Spring Boot Application එකේ තවත් Instance එකක් Run කරන්න (ඔබේ IDE එකෙන් හෝ වෙනත් Terminal එකකින්). server.port: 0
නිසා මේ Instance එක වෙනත් Random Port එකක Start වේවි. Consul UI එකේ "Services" Tab එකට ගියාම ඔබට my-hello-service
එකේ Instances දෙකක් දැකගන්න පුළුවන්. මේකෙන් Consul Service Discovery වැඩ කරනවා කියලා තහවුරු වෙනවා.
වෙනත් Service එකකට (උදාහරණයක් විදියට, API Gateway එකකට) මේ my-hello-service එකට Request යවන්න අවශ්ය නම්, Spring Cloud වල DiscoveryClient
හෝ RestTemplate
(@LoadBalanced
එක්ක) පාවිච්චි කරලා Service Name එකෙන් කෙලින්ම Call කරන්න පුළුවන්. Consul එකෙන් Available Instance එකක් තෝරලා Request එක Route කරයි.
නිගමනය (Conclusion)
අද අපි Spring Boot Applications, HashiCorp Consul එක්ක එකතු කරලා Service Discovery සහ Centralized Configuration ලබාගන්නේ කොහොමද කියලා විස්තරාත්මකව ඉගෙන ගත්තා. මේ Integration එක Microservices Architecture එකක් හදනකොට හරිම ප්රබලයි, මොකද මේකෙන් අපේ Services වලට තවත් Resilient, Scalable, සහ Maintainable වෙන්න පුළුවන්.
ඔබට දැන් පුළුවන් Service එකක් Consul ළඟ Register කරන්නේ කොහොමද, Configuration එක Consul K/V Store එකේ ගබඩා කරලා Runtime එකේදී Refresh කරගන්නේ කොහොමද කියලා. මේ දැනුම ඔබේ ඊළඟ Distributed System Project එකට අනිවාර්යයෙන්ම ප්රයෝජනවත් වේවි.
අමතක කරන්න එපා, Distributed Systems වලදි Monitoring, Logging, සහ Tracing වගේ අනිත් අංගත් හරිම වැදගත්. ඒ ගැනත් ඉදිරියේදී කතා කරන්න අපි බලාපොරොත්තු වෙනවා.
ඔබේ අත්දැකීම් පහළින් Comment කරන්න! මේක ඔබේ ඊළඟ Project එකටත් පාවිච්චි කරලා බලන්න!