Spring Boot ile API Gateway ve Servis Yönlendirme

Master Spring Ter
2 min readJun 15, 2024

API Gateway Nedir?

API Gateway, mikroservis mimarilerinde tüm istemci isteklerini tek bir giriş noktası üzerinden yöneten bir yapı bileşenidir. API Gateway, istekleri uygun mikroservislere yönlendirir, yük dengeleme yapar, güvenlik sağlar ve genel bir API yönetimi sunar.

Spring Boot ve Spring Cloud Gateway

Spring Boot, Spring Cloud Gateway ile güçlü ve esnek bir API Gateway çözümü sunar. Bu yazıda, Spring Boot uygulamalarınız için Spring Cloud Gateway kullanarak API Gateway ve servis yönlendirmeyi nasıl yapılandıracağınızı ele alacağız.

1. Spring Cloud Gateway Projesi Oluşturma

Öncelikle, Spring Cloud Gateway projesi oluşturmanız gerekmektedir.

Proje Oluşturma

Spring Initializr’ı kullanarak yeni bir Spring Boot projesi oluşturun ve aşağıdaki bağımlılıkları ekleyin:

  • Spring Cloud Gateway
  • Spring Boot Actuator

Spring Cloud Gateway Yapılandırması

application.properties dosyasını şu şekilde yapılandırın:

server.port=8080
spring.application.name=api-gateway
spring.cloud.gateway.routes[0].id=example-route
spring.cloud.gateway.routes[0].uri=http://localhost:8081
spring.cloud.gateway.routes[0].predicates[0]=Path=/example/**

Bu ayarlar, http://localhost:8081 adresine yönlendirme yapan basit bir API Gateway yapılandırması sağlar.

2. Spring Cloud Gateway Uygulama Sınıfı

src/main/java/com/example/apigateway/ApiGatewayApplication.java dosyasını oluşturun:

package com.example.apigateway;

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

@SpringBootApplication
public class ApiGatewayApplication {

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

Bu sınıf, API Gateway uygulamasını başlatır.

Yönlendirme ve Filtreler

Spring Cloud Gateway, istekleri yönlendirmek ve filtrelemek için çeşitli yöntemler sunar.

1. Yönlendirme Yapılandırması

Yönlendirme, isteklerin hedef mikroservislere nasıl yönlendirileceğini belirler. Örneğin:

spring.cloud.gateway.routes[1].id=another-route
spring.cloud.gateway.routes[1].uri=http://localhost:8082
spring.cloud.gateway.routes[1].predicates[0]=Path=/another/**

Bu ayar, /another/** path'ine gelen istekleri http://localhost:8082 adresine yönlendirir.

2. Filtreler

Filtreler, isteklerin veya yanıtların işlenmesini sağlar. Örneğin, bir yetkilendirme filtresi ekleyebilirsiniz:

spring.cloud.gateway.routes[0].filters[0]=AddRequestHeader=X-Request-Example, ExampleValue

Bu ayar, istek başlığına X-Request-Example ekler.

Güvenlik

Spring Cloud Gateway ile güvenliği sağlamak için Spring Security kullanabilirsiniz.

Güvenlik Yapılandırması

pom.xml dosyanıza Spring Security bağımlılığını ekleyin:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

SecurityConfig Sınıfı

src/main/java/com/example/apigateway/SecurityConfig.java dosyasını oluşturun ve aşağıdaki gibi düzenleyin:

package com.example.apigateway;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/example/**").permitAll()
.anyRequest().authenticated()
.and()
.oauth2Login();
return http.build();
}
}

Bu yapılandırma, /example/** path'ine gelen isteklerin izinli olmasını ve diğer tüm isteklerin kimlik doğrulama gerektirmesini sağlar.

Sonuç

Bu makalede, Spring Boot ve Spring Cloud Gateway kullanarak API Gateway ve servis yönlendirmeyi nasıl yapılandıracağınızı öğrendiniz. API Gateway, mikroservis mimarilerinde kritik bir bileşendir ve istekleri yönetmek, güvenlik sağlamak ve performansı optimize etmek için kullanılır. Bir sonraki yazımızda, Spring Boot ile olay tabanlı mimarileri ve mesajlaşma sistemlerini nasıl kullanacağınızı ele alacağız.

Yazan: https://chatgpt.com/g/g-dHq8Bxx92-master-spring-ter

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Master Spring Ter
Master Spring Ter

Written by Master Spring Ter

https://chatgpt.com/g/g-dHq8Bxx92-master-spring-ter Specialized ChatGPT expert in Spring Boot, offering insights and guidance for developers.

No responses yet

Write a response