Spring Boot සහ Consul: Service Discovery, Configuration (Sinhala)

Spring Boot and Consul: Service Discovery and Configuration Sinhala Guide
ආයුබෝවන් යාළුවනේ! අද අපි කතා කරන්න යන්නේ මේ වෙනකොට Software Engineering ලෝකේ ගොඩක් වැදගත් තැනක් අරගෙන තියෙන Microservices Architecture එකේදී අනිවාර්යයෙන්ම ඕන වෙන component එකක් ගැන. ඒ තමයි Service Discovery. විශේෂයෙන්ම අපි බලමු අපේ Spring Boot Application එකක් Consul එක්ක කොහොමද Service Discovery සහ Distributed Configuration සඳහා පාවිච්චි කරන්නේ කියලා. මේක ටිකක් complicated වගේ පෙනුනත්, මම පුළුවන් තරම් සරලව සහ පියවරෙන් පියවර පැහැදිලි කරන්නම්.
ඔබට මතක ඇති සාමාන්යයෙන් monolithic application එකකදී අපේ code එකේ හැම component එකක්ම එකම තැනක, එකම project එකක් විදියට තියෙන්නේ. ඒත් Microservices වලදී පොඩි පොඩි services ගොඩක් තියෙනවා, ඒ හැම එකක්ම වෙනම Deploy කරලා, වෙනම Run වෙනවා. දැන් හිතන්න මේ Services එකිනෙකාට කතා කරගන්න ඕනේ කියලා. කොහොමද ඒගොල්ලෝ එකිනෙකා හොයාගන්නේ? ඒකට තමයි Service Discovery කියන concept එක අපිට උදව් කරන්නේ. ඒ වගේම, මේ services ගොඩක Common Configuration එකක් share කරගන්නේ කොහොමද කියන එකත් ලොකු ප්රශ්නයක්. මේ හැමදේටම Consul කියන tool එකෙන් අපිට නියම solution එකක් ලැබෙනවා.
මේ guide එක කියෙව්වට පස්සේ ඔබට පහත දේවල් ගැන පැහැදිලි අවබෝධයක් ලැබෙයි:
- Service Discovery කියන්නේ මොකක්ද සහ ඒක වැදගත් වෙන්නේ ඇයි කියලා.
- HashiCorp Consul කියන්නේ මොකක්ද සහ ඒක Service Discovery වලට කොහොමද උදව් කරන්නේ කියලා.
- ඔබේ Spring Boot Application එක Consul එක්ක Register කරගන්න ආකාරය.
- Consul වල Key-Value (KV) Store එක භාවිතා කරලා Distributed Configuration manage කරන හැටි.
එහෙනම් අපි පටන් ගමු නේද!
Service Discovery කියන්නේ මොකක්ද?
සරලවම කිව්වොත්, Service Discovery කියන්නේ Microservices architecture එකකදී එක Service එකකට තවත් Service එකක් (හෝ Services සමූහයක්) කොහෙද තියෙන්නේ කියලා හොයාගන්න උදව් කරන ක්රමයක්. Monolithic Application එකකදී මේ වගේ ප්රශ්නයක් එන්නේ නැහැ, මොකද හැම component එකක්ම එකම application එක ඇතුලේ තියෙන නිසා.
ඒත් Microservices වලදී, අපිට Services ගොඩක් තියෙන්න පුළුවන්. ඒ Services වෙන වෙනම Servers වල, වෙන වෙනම Ports වල Run වෙන්න පුළුවන්. ඒ වගේම Traffic එක වැඩි වෙනකොට, අපිට තව Service instances අලුතෙන් Add කරන්නත් පුළුවන්. Traffic එක අඩු වෙනකොට තියෙන instances අඩු කරන්නත් පුළුවන්. මේ වගේ Dynamic environment එකකදී Service එකක IP Address එකයි, Port එකයි Hardcode කරන්න බැහැ නේද? මොකද ඒගොල්ලෝ ඕන වෙලාවක වෙනස් වෙන්න පුළුවන්. මේ ප්රශ්නයට තමයි Service Discovery කියන concept එකෙන් විසඳුම ලැබෙන්නේ.
Service Discovery ප්රධාන වශයෙන් කොටස් දෙකකට බෙදන්න පුළුවන්:
- Client-Side Discovery: මේකෙදි Client Service එක Service Registry එකකින් (ඒ කියන්නේ Services කොහෙද තියෙන්නේ කියලා තොරතුරු තියෙන තැනකින්) අවශ්ය Service එකේ Network locations ටිකක් ඉල්ලනවා. ඊට පස්සේ Client එක Load Balancing algorithm එකක් පාවිච්චි කරලා ඒ locations වලින් එකක් තෝරගෙන Request එක යවනවා. Netflix Eureka මේකට හොඳ උදාහරණයක්.
- Server-Side Discovery: මේකෙදි Client Service එක Request එක යවන්නේ Load Balancer එකකට. මේ Load Balancer එක Service Registry එකත් එක්ක කතා කරලා අවශ්ය Service එකේ Network location එක හොයාගෙන Request එක ඒ Service එකට යවනවා. AWS ELB (Elastic Load Balancer) වගේ ඒවා මේ category එකට අයිතියි.
Consul කියන්නේ මේ දෙකටම උදව් කරන්න පුළුවන් Distributed Service Discovery සහ Configuration tool එකක්. ඒක ගැන අපි තව දුරටත් බලමු.
Consul කියන්නේ මොකක්ද? ඇයි අපිට Consul ඕනේ?
HashiCorp Consul කියන්නේ Distributed Systems වල Service Discovery, Health Checking, සහ Configuration Management වලට පාවිච්චි කරන Open Source tool එකක්. ඒක නිර්මාණය කරලා තියෙන්නේ HashiCorp ආයතනය විසින්.
Consul වල ප්රධානම Features ටිකක් පහතින් බලමු:
- Service Discovery: මේක තමයි ප්රධානම feature එක. Consul Service Registry එකක් විදියට වැඩ කරනවා. Services වලට තමන්ව Register කරගන්න පුළුවන්. අනිත් Services වලට Consul ගෙන් අහලා, තමන්ට ඕන Service එක කොහෙද තියෙන්නේ කියලා හොයාගන්න පුළුවන්.
- Health Checking: Consul එකට පුළුවන් Register කරලා තියෙන Services වල Health එක Continuous Check කරන්න. මොකක් හරි Service එකක් Down වුනොත් හෝ Unhealthy වුනොත් Consul Registry එකෙන් ඒ Service එක ඉවත් කරනවා. ඒ නිසා Clients ලා Healthy Services වලට විතරයි Request යවන්නේ. මේක ගොඩක් වැදගත්, මොකද Production environment එකකදී Service එකක් Down වුනොත් Client request එක අනිවාර්යයෙන්ම Healthy Service එකකට redirect විය යුතුයි.
- Key/Value (KV) Store: Consul වල Distributed Key/Value Store එකක් තියෙනවා. මේක පාවිච්චි කරලා අපිට Global Configuration, Feature Flags වගේ දේවල් Store කරන්න පුළුවන්. Microservices environment එකකදී Common Configuration එකක් Share කරන්න මේක ගොඩක් ප්රයෝජනවත්.
- Multi-Datacenter Support: Consul වලට පුළුවන් Datacenters කීපයක් පුරා Services manage කරන්න.
- Secure Service Communication: Consul Connect වගේ features පාවිච්චි කරලා Service-to-Service communication එක Secure කරන්නත් පුළුවන්.
ඇයි අපිට Consul ඕනේ? මොකද Microservices Architecture එකකදී Application එකක Scalability, Resilience, සහ Maintainability කියන දේවල් maintain කරන්න Service Discovery සහ Distributed Configuration අත්යවශ්යයි. Consul මේ හැමදේටම එකම තැනකින් විසඳුමක් දෙනවා.
Spring Boot Application එකක් Consul එක්ක Register කරමු
හරි, දැන් අපි බලමු Spring Boot Application එකක් Consul එක්ක Register කරන්නේ කොහොමද කියලා. මේකට අපිට Spring Cloud Consul කියන Project එකේ උපකාරය ලැබෙනවා.
පළමු පියවර: Consul Server එකක් පටන් ගමු
අපිට Local environment එකේදී Consul Server එකක් Run කරගන්න පුළුවන් Docker පාවිච්චි කරලා. මේක තමයි සරලම ක්රමය. ඔබගේ machine එකේ Docker install කරලා නැත්නම්, මුලින්ම ඒක install කරගන්න.
docker run -d --name=dev-consul -p 8500:8500 consul
මේ command එකෙන් Consul Server එක background එකේ run වෙන්න පටන් ගන්නවා. Consul UI එකට පිවිසෙන්න පුළුවන් http://localhost:8500 කියන URL එකෙන්. මේ UI එකේදී ඔබට register වෙන services සහ Key-Value entries බලන්න පුළුවන්.
දෙවන පියවර: Spring Boot Project එකක් හදමු
Spring Initializr ( https://start.spring.io/ ) එකට ගිහින් අලුත් Spring Boot project එකක් හදාගන්න. Dependencies විදියට පහත ඒවා add කරගන්න:
- Spring Web (REST APIs හදන්න)
- Spring Boot DevTools (Optional, Development වලට පහසුයි)
- Spring Cloud Starter Consul Discovery (මේක තමයි Consul එක්ක සම්බන්ධ වෙන්න අවශ්ය Library එක)
ඔබ Maven පාවිච්චි කරනවා නම්, ඔබේ pom.xml
එකේ dependencies
section එක මේ වගේ වෙයි (version එක ඔබ තෝරගන්න Spring Boot version එක අනුව වෙනස් වෙන්න පුළුවන්):
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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>2023.0.1</version> <!-- Replace with the latest suitable version -->
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
තුන්වන පියවර: Application Configuration
දැන් අපි src/main/resources/application.yml
(හෝ application.properties
) file එකට Consul සම්බන්ධ කරගන්න අවශ්ය configuration ටික add කරමු. application.yml
file එකක් භාවිතා කිරීම වඩාත් පහසු සහ කියවීමට පහසුයි.
spring:
application:
name: my-spring-consul-app
cloud:
consul:
host: localhost
port: 8500
discovery:
enabled: true
instance-id: ${spring.application.name}:${random.value}
health-check-path: /actuator/health
health-check-interval: 10s
prefer-ip-address: true # Optional: Register with IP address instead of hostname
management:
endpoints:
web:
exposure:
include: health # Expose health endpoint for Consul to check
මේ configuration එක ගැන පොඩ්ඩක් පැහැදිලි කරමු:
spring.application.name
: මේක තමයි අපේ Service එක Consul එකේ Register වෙන්නේ මොන නමින්ද කියන එක. මේ නම අනිවාර්යයි.spring.cloud.consul.host
සහspring.cloud.consul.port
: Consul Server එක Run වෙන IP Address එකයි Port එකයි. අපේ Local setup එකේදී මේකlocalhost:8500
.spring.cloud.consul.discovery.enabled: true
: මේකෙන් කියවෙන්නේ අපේ application එක Consul Service Registry එකට Register විය යුතුයි කියලා.spring.cloud.consul.discovery.instance-id
: Consul එකේදී Service එකක Unique Identifier එක. අපි${spring.application.name}:${random.value}
වගේ එකක් දානවා, ඒකෙන් Service name එකට random String එකක් එකතු වෙලා Unique ID එකක් හැදෙනවා. මේක වැදගත් වෙන්නේ එකම Service එකේ instances ගොඩක් Run කරනකොට.health-check-path
සහhealth-check-interval
: Consul එකට පුළුවන් අපේ Service එක Healthy ද කියලා Check කරන්න. මේකට අපි Spring Boot Actuator වල/actuator/health
endpoint එක පාවිච්චි කරනවා.health-check-interval
කියන්නේ කොච්චර වෙලාවකට සැරයක්ද මේ Health Check එක කරන්න ඕනේ කියන එක.management.endpoints.web.exposure.include: health
: Consul වලට Health Check කරන්න පුළුවන් වෙන්න, Actuator/health
endpoint එක expose කරන්න ඕනේ.
හතරවන පියවර: Spring Boot Application එක Run කරමු
දැන් ඔබගේ Spring Boot Application එකේ Main Class එක මේ වගේ වෙයි:
package com.example.springconsulapp;
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 SpringConsulAppApplication {
public static void main(String[] args) {
SpringApplication.run(SpringConsulAppApplication.class, args);
}
@GetMapping("/hello")
public String hello() {
return "Hello from Spring Boot Service powered by Consul!";
}
}
දැන් Spring Boot Application එක Run කරන්න. (IDE එකකින් හෝ mvn spring-boot:run
වගේ command එකකින්).
Application එක Start වුනාට පස්සේ, Consul UI එකට (http://localhost:8500
) ගිහින් බලන්න. ඔබට Services tab එක යටතේ my-spring-consul-app
කියලා Service එකක් Register වෙලා තියෙනවා දකින්න පුළුවන්. ඒ වගේම ඒ Service එක Healthy කියලාත් පෙන්නයි.
ඔබට දැන් මේ Service එකට http://localhost:8080/hello (ඔබේ app එක 8080 port එකේ run වෙනවා නම්) ගිහින් බලන්න පුළුවන්. Consul UI එකේදී මේ Service එකේ IP Address එකයි, Port එකයි දකින්න පුළුවන්.
Consul එකෙන් Configuration Management කරමු
Service Discovery වලට අමතරව Consul වල තියෙන තවත් නියම Feature එකක් තමයි Distributed Configuration Management. මේකෙන් පුළුවන් අපේ application වලට අවශ්ය Configuration properties Consul වල KV Store එකේ Store කරලා, අවශ්ය වෙලාවට Load කරගන්න.
Consul KV Store එකට Configuration එකතු කරමු
Consul UI එකේදී Key/Value tab එකට යන්න. ඔබට අලුත් Key එකක් Add කරන්න පුළුවන්. අපි config/my-spring-consul-app/message
කියලා Key එකක් හදමු. Value එකට "This is a message from Consul KV Store!"
කියලා දෙමු.
Spring Cloud Consul Configuration, default එකෙන් Configuration එක load කරන්නේ config/application/
සහ config/{application-name}/
කියන Prefixes වලින්. අපේ application name එක my-spring-consul-app
නිසා, config/my-spring-consul-app/
කියන Prefix එක යටතේ අපි දාන values ටික අපේ application එකට load වෙයි.
Spring Boot App එකෙන් Consul Configuration එක කියවමු
දැන් අපි අපේ Spring Boot Application එකට message
කියන property එක Consul KV Store එකෙන් කියවන්න add කරමු.
මුලින්ම, application.yml
එකට පහත config එක එකතු කරන්න:
spring:
cloud:
consul:
config:
enabled: true # Enable Consul Config
prefix: config # Prefix for fetching config from Consul KV store
defaultContext: application # Default context to fetch global properties
data-key: data # Key for data under a profile
format: YAML # Or JSON, PROPERTIES based on your Consul KV format
# profiles: dev, prod # Optional: if you have environment-specific configs
# If you want to fetch properties under 'config/my-spring-consul-app/', this is usually enough.
# If you have specific keys like 'my-service/message', then use 'aclToken', 'kv' etc.
# To demonstrate dynamic refresh, we need Spring Cloud Context and Spring Boot Actuator
# Make sure to include spring-boot-starter-actuator dependency in pom.xml
management:
endpoints:
web:
exposure:
include: refresh, health # Expose refresh endpoint for dynamic config updates
endpoint:
health:
show-details: always
දැන් අපේ Main Application Class එකට තවත් Endpoint එකක් එකතු කරමු, ඒකෙන් Consul එකෙන් ගත්ත message
එක පෙන්නන්න.
package com.example.springconsulapp;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@RefreshScope // This annotation allows dynamic refreshing of configuration properties
public class SpringConsulAppApplication {
@Value("${message:Default Message if not found}") // Default value if 'message' is not found
private String message;
public static void main(String[] args) {
SpringApplication.run(SpringConsulAppApplication.class, args);
}
@GetMapping("/hello")
public String hello() {
return "Hello from Spring Boot Service powered by Consul!";
}
@GetMapping("/config-message")
public String getConfigMessage() {
return "Config message from Consul: " + message;
}
}
මෙහිදී @RefreshScope
කියන Annotation එක ගොඩක් වැදගත්. මේකෙන් කියවෙන්නේ මේ Bean එකේ Properties Consul Configuration එකේ වෙනසක් වුනොත් Automatically Refresh විය යුතුයි කියලා. මේකට spring-cloud-starter-bus-amqp
(RabbitMQ), spring-cloud-starter-bus-kafka
(Kafka) වගේ Dependencies අවශ්ය වෙන්න පුළුවන්, නැත්නම් Manually /actuator/refresh
endpoint එකට POST Request එකක් යවන්න පුළුවන්. සරලවම කිව්වොත්, Consul KV Store එකේ අපේ message
එක වෙනස් වුනොත්, අපේ Application එක Restart නොකරම අලුත් Value එක ගන්න පුළුවන්.
දැන් application එක run කරලා, http://localhost:8080/config-message ගිහින් බලන්න. ඔබට Consul KV Store එකේ තිබ්බ message එක දකින්න පුළුවන් වෙයි.
Configuration එක Dynamic විදියට වෙනස් කරමු
Consul UI එකට ගිහින් config/my-spring-consul-app/message
කියන Key එකේ Value එක "Updated message from Consul KV Store!"
කියලා වෙනස් කරන්න. දැන් application එක Restart කරන්න එපා. ඒ වෙනුවට, Terminal එකකින් හෝ Postman වගේ tool එකකින් POST
Request එකක් http://localhost:8080/actuator/refresh
කියන URL එකට යවන්න.
curl -X POST http://localhost:8080/actuator/refresh
දැන් ආයෙත් http://localhost:8080/config-message ගිහින් බලන්න. ඔබට පෙනෙයි Application එක Restart නොකරම, අලුත් message එක Load වෙලා තියෙනවා. මේක තමයි Distributed Configuration Management වල තියෙන ලොකුම වාසියක්!
අවසාන වශයෙන් (Conclusion)
ඉතින් යාළුවනේ, අපි මේ Guide එකෙන් Service Discovery කියන්නේ මොකක්ද, HashiCorp Consul කියන්නේ මොකක්ද, සහ අපේ Spring Boot Applications Consul එක්ක Register කරලා Service Discovery සහ Distributed Configuration manage කරගන්නේ කොහොමද කියලා බැලුවා. මේ Concepts දෙකම Microservices Architecture එකකදී Application එකක Scalability, Resilience, සහ Flexibility වැඩි කරන්න අත්යවශ්යයි. Consul වගේ tool එකක් භාවිතා කරන එකෙන් අපේ Microservices environment එක ගොඩක් පහසුවෙන් Manage කරගන්න පුළුවන් වෙනවා.
ඔබට දැන් පුළුවන් මේ අත්දැකීම ඔබේම Project එකක Implement කරන්න උත්සාහ කරන්න. තව දුරටත් Consul වල Health Checks, ACLs (Access Control Lists), සහ Service Mesh වගේ දේවල් ගැනත් ඉගෙන ගන්න පුළුවන්. මේවා ගැනත් ඉගෙන ගන්න එක ගොඩක් වටිනවා.
මේ Tutorial එක ඔබට ප්රයෝජනවත් වුණා නම්, ඔබේ අදහස් හෝ මේ ගැන ඔබගේ අත්දැකීම් පහතින් Comment කරන්න. ඔබට මොනවා හරි ගැටළු තිබුනොත් අහන්නත් පුළුවන්. එහෙනම් තවත් අලුත් Tutorial එකකින් හමු වෙමු! සුබ දවසක්!