9 Kasım 2020 Pazartesi

SpringCloud Gateway Kullanımı

Giriş
SpringCloud Gateway (SCG) altta Netty kullanır. Şeklen şöyle

Özellikleri
Açıklaması şöyle. Zengi bir Predicate ve Filtre desteği veriyor.
- Rich set of predicate and filter support — provides various predicates like: path, cookie, time, header, host, method, query, remote address, weight etc. Similar to predicates, it provide number of filters like: Add/Set/Remove/Map Request/Response Headers, Set response code, Redirect, Rewrite etc. filters
- Circuit breakers — supports circuit-breakers based on time and re-direct on failure
- TLS & SSL — TLS and SSL can be configured for Gateway and httpClient inside the Gateway
Route Metadata — Routes can be configured with metadata which further can be utilized for processing
- Easy integration with discovery service and load balancing
- Easy metrics with Actuator
- Easy cache implementation

SCG konfigürasyonu
1. SCG konfigürasyonu application.properties veya kodla yapılabilir Açıklaması şöyle
The routing configuration can be created by using pure Java (RouteLocator) or by using properties configuration
- Ayarları dosyadan yapmak için SpringCloud Gateway application.properties Ayarları yazısına bakabilirsiniz veya 
- Kodla yapmak için SpringCloud Gateway RouteLocator Arayüzü yazısına bakabilirsiniz

Zuul 1 vs SCG
Zuul 1 ile SCG arasında önemli bir fark var. Açıklaması şöyle. Yani SCG reactive çalışıyor
However Zuul is a blocking API. A blocking gateway api makes use of as many threads as the number of incoming requests. So this approach is more resource intensive. If no threads are available to process incoming request then the request has to wait in queue.
...
Spring Cloud Gateway is a non blocking API. When using non blocking API, a thread is always available to process the incoming request. These request are then processed asynchronously in the background and once completed the response is returned. So no incoming request never gets blocked when using Spring Cloud Gateway.

Maven
Şu satırı dahil ederiz. SCG içinde genellikle Eureka kullanıldığı için onu da dahil etmekte fayda var
<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>
Kavramlar
3 tane önemli kavram var. Açıklaması şöyle
Route: Think of this as the destination that we want a particular request to route to. It comprises of destination URI, a condition that has to satisfy — Or in terms of technical terms, Predicates, and one or more filters.

Predicate: This is literally a condition to match. i.e. kind of “if” condition..if requests has something — e.g. path=blah or request header contains foo-bar etc. In technical terms, it is Java 8 Function Predicate

Filter: These are instances of Spring Framework WebFilter. This is where you can apply your magic of modifying request or response. There are quite a lot of out of box WebFilter that framework provides. But of course, we are talking about Spring Framework. So, rest easy folks!!! You can always add your own filter with your own logic :)
Bir başka açıklama şöyle
- Route: Route the basic building block of the gateway. It consists of
  - ID
  - destination URI
  - Collection of predicates and a collection of filters
- A route is matched if aggregate predicate is true.
- Predicate: This is similar to Java 8 Function Predicate. Using this functionality we can match HTTP request, such as headers , url, cookies or parameters.
- Filter: These are instances Spring Framework GatewayFilter. Using this we can modify the request or response as per the requirement.
- Yani her Route için bir ID vardır. 
- Gelen istek Route için tanımlı tüm Predicate'lar true dönerse geçerlidir. 
- Gelen isteği veya döndürülen cevabı değiştirmek için Filter'lar kullanılır

Filter Çeşitleri
3 çeşit filtre var
1. GlobalFilter
2. GatewayFilter
3. WebFilter

GlobalFilter
Açıklaması şöyle
Applies to all the routes but not the controllers hosted in the gateway. Spring provides many inbuilt filters for normal use cases and if required we can implement custom filters as well.

For example, we can apply an inbuilt filter ‘AddRequestHeader’ that adds the header ‘request-header’ in all requests with the value ‘request-header-value’ in the following way:

//Apply filter for all routes using springs inbuilt filter //"AddRequestHeader"
spring.cloud.gateway.default-filters[0]=AddRequestHeader=request-header, request-header-value
GatewayFilter
Açıklaması şöyle
For configuring specific routes, we can apply a route-specific filter in the following way:
GatewayFilter yazısına bakabilirsiniz.

WebFilter
Açıklaması şöyle
Applies to all routes as well as the controllers defined in the gateway by us. We need to implement the WebFilter interface in our custom filter and then write our custom logic by overriding the filter() method 
Örnek
Şöyle yaparız
@Component
@Slf4j
public class CustomFilter implements WebFilter {
  @Override
  public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
    log.trace("inside custom filter");
    ServerHttpRequest request = exchange.getRequest();
    // logic for request validation
    return chain.filter(exchange);
  }
}

Filter İki Amaç İçin Kullanılır
Açıklaması şöyle
There are 2 different types of filters.
Pre Filters — if you want to add or change request object before you pass it down to destination service, you can use these filters.
Post Filters — if you want to add or change response object before you pass it back to client, you can use these filters.

Yani şeklen şöyle
Rate Limiting
SCG ile Rate Limitin de yapılabilir. RequestRateLimiter yazısına bakabilirsiniz

Hiç yorum yok:

Yorum Gönder