উদাহরণ সহ Ribbon এর ব্যবহার

Spring Cloud Ribbon (Client-Side Load Balancing) - স্প্রিং ক্লাউড (Spring Cloud) - Java Technologies

284

Spring Cloud Ribbon হল একটি client-side load balancer যা মাইক্রোসার্ভিসে লোড ব্যালান্সিং সাপোর্ট দেয়। এটি মূলত Netflix Ribbon এর একটি অংশ, যা সার্ভিস ডিসকভারি এবং লোড ব্যালান্সিং প্রক্রিয়া সহজ করে তোলে। Ribbon ক্লায়েন্টের পক্ষ থেকে সার্ভিসগুলোর মধ্যে রিকোয়েস্টকে বিভিন্ন সার্ভারে লোড ব্যালেন্স করতে সাহায্য করে।

এখানে Spring Cloud Ribbon এর ব্যবহার নিয়ে উদাহরণ দেওয়া হয়েছে, যেখানে সার্ভিস রেজিস্ট্রেশন এবং ডিসকভারি, লোড ব্যালান্সিং সহ একাধিক সার্ভিস থেকে রিকোয়েস্ট সঠিকভাবে রাউট করা হবে।


Spring Cloud Ribbon উদাহরণ:

এই উদাহরণে দুটি সার্ভিস থাকবে:

  1. Service A – এটি একটি সাধারণ Spring Boot অ্যাপ্লিকেশন হবে যা কিছু ডেটা রিটার্ন করবে।
  2. Service B – এটি Ribbon ব্যবহার করে Service A থেকে ডেটা ফেচ করবে এবং লোড ব্যালান্সিং ব্যবহার করবে।

১. Maven Dependencies

Service A এবং Service B উভয় সার্ভিসেই Ribbon ব্যবহারের জন্য আপনাকে কিছু নির্দিষ্ট ডিপেনডেন্সি যোগ করতে হবে।

Service A - Maven Dependency:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

Service B - Maven Dependency:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

২. Service A - তৈরি করা

Service A একটি সাধারণ Spring Boot অ্যাপ্লিকেশন হবে যা /hello পাথে একটি বার্তা রিটার্ন করবে।

Service A Controller:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ServiceAController {

    @GetMapping("/hello")
    public String sayHello() {
        return "Hello from Service A!";
    }
}

Service A Application Class:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

Service A - application.yml:

spring:
  application:
    name: service-a
server:
  port: 8081
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

৩. Service B - তৈরি করা

Service B হল ক্লায়েন্ট সার্ভিস যা Ribbon ব্যবহার করে Service A থেকে ডেটা ফেচ করবে। এখানে, Eureka এবং Ribbon ব্যবহৃত হবে।

Service B Controller:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ServiceBController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/fetch")
    public String fetchFromServiceA() {
        return restTemplate.getForObject("http://service-a/hello", String.class);
    }
}

Service B Application Class:

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

@SpringBootApplication
@EnableDiscoveryClient
@RibbonClient(name = "service-a")
public class ServiceBApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceBApplication.class, args);
    }
}

Service B - application.yml:

spring:
  application:
    name: service-b
server:
  port: 8082
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

ribbon:
  eureka:
    enabled: true

RestTemplate Bean Configuration in Service B:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RibbonConfiguration {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

৪. Eureka Server Setup

Eureka Server ব্যবহার করে আমরা Service A এবং Service B সার্ভিসগুলো রেজিস্টার এবং ডিসকভার করব।

Eureka Server - application.yml:

server:
  port: 8761

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false

৫. সার্ভিস চালানো

  1. প্রথমে Eureka Server চালু করুন:

    mvn spring-boot:run -Dspring-boot.run.profiles=eureka
    
  2. তারপর Service A চালু করুন:

    mvn spring-boot:run -Dspring-boot.run.profiles=service-a
    
  3. এরপর Service B চালু করুন:

    mvn spring-boot:run -Dspring-boot.run.profiles=service-b
    

৬. ফিচার ডেমো

এখন, Service B-এর /fetch রাউটে রিকোয়েস্ট পাঠালে এটি Ribbon ব্যবহার করে Service A থেকে ডেটা ফেচ করবে।

  • Service A-এর /hello পাথ রিটার্ন করবে: "Hello from Service A!"
  • Service B /fetch রাউটে রিকোয়েস্ট পাঠানোর পর, এটি "Hello from Service A!" বার্তা রিটার্ন করবে।

Service B - /fetch পাথ টেস্ট করা:

GET http://localhost:8082/fetch

৭. লোড ব্যালান্সিং

যেহেতু Ribbon ক্লায়েন্ট-সাইড লোড ব্যালান্সিং সাপোর্ট করে, এটি Eureka Server এর মাধ্যমে বিভিন্ন Service A ইনস্ট্যান্স থেকে রিকোয়েস্ট পাঠাবে। Ribbon সঠিক সার্ভার নির্বাচন করবে এবং সার্ভিসের রিকোয়েস্ট রাউট করবে।


Conclusion

Spring Cloud Ribbon ব্যবহারের মাধ্যমে আপনি ক্লায়েন্ট-সাইড লোড ব্যালান্সিং সক্ষম করতে পারেন। এই উদাহরণে, Service A এবং Service B মাইক্রোসার্ভিসের মধ্যে Eureka এবং Ribbon ব্যবহৃত হয়েছে, যা Service A থেকে রিকোয়েস্ট ফেচ করার জন্য লোড ব্যালান্সিং ব্যবস্থা প্রয়োগ করে।

Content added By
Promotion

Are you sure to start over?

Loading...