উদাহরণ সহ API Gateway বাস্তবায়ন

Spring Cloud Gateway (API Gateway) - স্প্রিং ক্লাউড (Spring Cloud) - Java Technologies

321

Spring Cloud API Gateway হল একটি শক্তিশালী এবং নমনীয় সমাধান যা বিভিন্ন মাইক্রোসার্ভিসের জন্য একটি একক প্রবেশদ্বার হিসেবে কাজ করে। এটি রিকোয়েস্ট রাউটিং, অথেনটিকেশন, রেট লিমিটিং, লোড ব্যালান্সিং, এবং আরও অনেক কাজ সহজে পরিচালনা করতে পারে। API Gateway-এর মাধ্যমে, ক্লায়েন্ট শুধুমাত্র একটি পয়েন্ট ব্যবহার করে একাধিক সার্ভিসে রিকোয়েস্ট পাঠাতে পারে।

এখানে আমরা Spring Cloud Gateway ব্যবহার করে API Gateway বাস্তবায়ন করার একটি উদাহরণ দেখবো।


Spring Cloud Gateway API Gateway বাস্তবায়ন উদাহরণ

এখানে আমরা দুটি সার্ভিস তৈরি করবো:

  1. Service A – একটি সাধারণ Spring Boot অ্যাপ্লিকেশন যা /hello পাথের মাধ্যমে একটি বার্তা রিটার্ন করবে।
  2. API Gateway – সার্ভিসগুলোর জন্য রিকোয়েস্ট রাউটিং করার জন্য একটি API Gateway।

১. Maven Dependencies

API Gateway (spring-cloud-starter-gateway)

API Gateway এ প্রয়োজনীয় ডিপেনডেন্সি:

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

Service A (spring-cloud-starter-eureka-client)

Service A এর জন্য মাইক্রোসার্ভিস রেজিস্ট্রেশন এবং ডিসকভারির জন্য ডিপেনডেন্সি:

<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-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.yml:

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

৩. Eureka Server তৈরি করা

Eureka Server হল একটি Service Discovery সার্ভিস যা সমস্ত মাইক্রোসার্ভিসকে রেজিস্টার করতে সহায়ক। API Gateway এবং Service A উভয় সার্ভিসই Eureka Server এর মাধ্যমে রেজিস্টার হবে।

Eureka Server - application.yml:

server:
  port: 8761

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false

৪. API Gateway (Spring Cloud Gateway) তৈরি করা

এখন আমরা একটি API Gateway তৈরি করবো যা Service A থেকে রিকোয়েস্ট রাউট করবে। এখানে আমরা Spring Cloud Gateway ব্যবহার করব, যা মাইক্রোসার্ভিসের জন্য একটি একক রাউটিং পয়েন্ট হিসেবে কাজ করবে।

API Gateway Controller (Gateway Configuration):

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriComponentsBuilder;

@Configuration
public class GatewayConfig {

    @Bean
    public FilterRegistrationBean<GatewayFilter> myFilter() {
        FilterRegistrationBean<GatewayFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new GatewayFilter());
        registrationBean.addUrlPatterns("/service-a/**");
        return registrationBean;
    }
}

API Gateway - application.yml:

spring:
  application:
    name: api-gateway
server:
  port: 8080
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
  cloud:
    gateway:
      routes:
        - id: service-a-route
          uri: lb://service-a  # Load-balanced URI for Service A
          predicates:
            - Path=/service-a/**  # Match requests to /service-a/**

এখানে lb://service-a দিয়ে Ribbon লোড ব্যালান্সিং ব্যবহার করা হচ্ছে। এতে API Gateway ক্লায়েন্ট-সাইড লোড ব্যালান্সিং সহ Service A-এ রিকোয়েস্ট রাউট করবে।


৫. Service A এবং API Gateway কে Eureka Server এর সাথে রেজিস্টার করা

আমরা Service A এবং API Gateway উভয় সার্ভিসকে Eureka Server এর সাথে রেজিস্টার করতে পারবো যাতে তারা একে অপরকে ডিসকভার করতে পারে।

API Gateway - application.yml (Eureka Client Configuration):

spring:
  application:
    name: api-gateway
server:
  port: 8080
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

Service A - application.yml (Eureka Client Configuration):

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

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

  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. এরপর API Gateway চালু করুন:

    mvn spring-boot:run -Dspring-boot.run.profiles=api-gateway
    

৭. API Gateway রাউটিং

এখন আপনি API Gateway-এর মাধ্যমে Service A থেকে রিকোয়েস্ট ফেচ করতে পারবেন।

Service A-এর /hello পাথটি রাউট করতে API Gateway-এর /service-a/hello পাথ ব্যবহার করুন।

API Gateway - /service-a/hello পাথ টেস্ট করা:

GET http://localhost:8080/service-a/hello

এটি Service A থেকে "Hello from Service A!" বার্তা রিটার্ন করবে।


৮. সার্ভিস ডিসকভারি এবং লোড ব্যালান্সিং

যেহেতু Eureka সার্ভিস ডিসকভারি ব্যবহার করা হচ্ছে এবং API Gateway লোড ব্যালান্সিং সহ Ribbon ব্যবহার করে রিকোয়েস্ট রাউট করছে, Service A এর একাধিক ইনস্ট্যান্স যোগ করা হলে, API Gateway সেগুলির মধ্যে রিকোয়েস্টগুলি ব্যালান্স করবে।


Conclusion

এভাবে, Spring Cloud Gateway ব্যবহার করে আপনি একটি API Gateway বাস্তবায়ন করতে পারেন যা আপনার সমস্ত মাইক্রোসার্ভিসের জন্য একটি একক প্রবেশপথ হিসেবে কাজ করবে। এটি Service Discovery, Routing, Load Balancing, এবং আরও অনেক গুরুত্বপূর্ণ ফিচার সরবরাহ করে। Spring Cloud Gateway ব্যবহার করে API গুলির রাউটিং এবং বিভিন্ন মাইক্রোসার্ভিসের মধ্যে যোগাযোগ আরও সহজ এবং কার্যকর হয়ে ওঠে।

Content added By
Promotion

Are you sure to start over?

Loading...