19 Eylül 2021 Pazar

SpringCloud Gateway RouteLocatorBuilder Sınıfı

Giriş
Şu satırı dahil ederiz
import org.springframework.cloud.gateway.route.RouteLocator;
Bu sınıf yerine application.properties de kullanılabilir.

routes metodu
1. route Kullanımı
Örnek
Şöyle yaparız
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;

@Configuration
public class GatewayMicroservicesConfig {

  public RouteLocator gatewayConfig(RouteLocatorBuilder locatorBuilder) {

    return locatorBuilder.routes()
      .route(r -> r.path("/firstmicroservice/**").uri("http://localhost:8081/"))
      .route( r -> r.path("/secondmicroservice/**").uri("http://localhost:8082/"))
       .build();
    }
}
Aynı şeyi  application.properties ile şöyle yaparız
# application.properties file for spring cloud gateway 
spring.application.name=gateway
sever.port=8080

## microservices mapping ##
spring.cloud.gateway.routes[0].id=microservice-one
spring.cloud.gateway.routes[0].uri=http://localhost:8081/
spring.cloud.gateway.routes[0].predicates[0]=Path=/firstmicroservice/**

spring.cloud.gateway.routes[1].id=microservice-two
spring.cloud.gateway.routes[1].uri=http://localhost:8082/
spring.cloud.gateway.routes[1].predicates[0]=Path=/secondmicroservice/
Örnek
Şöyle yaparız
@Bean
public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
  return builder.routes()
    .route(r -> r.path("/employee/**")
                 .uri("http://localhost:8081/")
                 .id("employeeModule"))

    .route(r -> r.path("/consumer/**")
                 .uri("http://localhost:8082/")
                 .id("consumerModule"))
    .build();
}
Aynısını application.properties şeklinde yapsaydık şöyle olurdu
spring:
  cloud:
    gateway:
      routes:
      - id: employeeModule
        uri: http://localhost:8081/
        predicates:
        - Path=/employee/**
      - id: consumerModule
        uri: http://localhost:8082/
        predicates:
        - Path=/consumer/**
2. filter Kullanımı
Açıklaması şöyle. Yani Http isteğine ekleme, çıkarma, değişiklikler yapılabilir
In addition to creating a route, RouteLocatorBuilder allows you to add various predicates and filters.
Örnek
Şöyle yaparız
@Bean
public RouteLocator myRoutes(RouteLocatorBuilder builder) {
  return builder.routes()
    .route(p -> p
      .path("/get")
      .filters(f -> f.addRequestHeader("Hello", "World"))
      .uri("http://httpbin.org:80"))
    .build();
}
Açıklaması şöyle
The route created above can make requests for “/get” forwarded to “http://httpbin.org/get". On the route configuration, we added a filter, which will add a header to the request, the key is hello, and the value is world.

Örnek
Şöyle yaparız
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.security.oauth2.gateway.TokenRelayGatewayFilterFactory;


@EnableEurekaClient
@SpringBootApplication
public class ApiGatewayApplication {

  @Bean
  public RouteLocator customRouteLocator(RouteLocatorBuilder builder,
                                         TokenRelayGatewayFilterFactory filterFactory) {
    return builder.routes()
      .route("car-service", r -> r.path("/cars")
      .filters(f -> f.filter(filterFactory.apply()))
      .uri("lb://car-service"))
      .build();
  }  
}
3. Hystrix Kullanımı
Fallback tanımlamak içindir
Örnek
Şöyle yaparız
@Bean
public RouteLocator myRoutes(RouteLocatorBuilder builder) {
  String httpUri = "http://httpbin.org:80";
  return builder.routes()
    .route(p -> p
      .path("/get")
      .filters(f -> f.addRequestHeader("Hello", "World"))
      .uri(httpUri))
    .route(p -> p
      .host("*.foo.com")
      .filters(f -> f
        .hystrix(config -> config
          .setName("mycmd")
          .setFallbackUri("forward:/fallback")))
        .uri(httpUri))
    .build();
}
Fallback şöyledir
@RequestMapping("/fallback")
public Mono<String> fallback() {
  return Mono.just("fallback");
}
İsteği şöyle göndeririz
curl --dump-header - --header 'Host: www.foo.com' http://localhost:8080/delay/3





Hiç yorum yok:

Yorum Gönder