본문 바로가기

Spring

Netflix Zuul Gateway Filter

728x90
반응형

MSA서비스를 개발하면서 우리가 gateway 를 직접 개발한다면

개발해야할 내용들이 굉장히 많다.

 

Netflix에서는 직접 개발한 Zuul gateway를 공개함으로써 

많은 개발자들이 편하게 가져다 쓰도록 지원하고 있다.

지원하고 있다기 보다는 지원했다라고 하는게 맞는가.

 

무튼 지금은 Spring Cloud Gatway 사용을 권장하고 있다.

가볍게 알아보려고 한다.

 

Zuul gateway 중심에는 HTTP Request와 Response를 처리하는 동안 다양한 작업을 처리할 수 있는 필터가 존재한다.

  • Type: most often defines the stage during the routing flow when the filter will be applied (although it can be any custom string)
  • Execution Order: applied within the Type, defines the order of execution across multiple filters
  • Criteria: the conditions required in order for the filter to be executed
  • Action: the action to be executed if the Criteria are met

몇 가지 표준 필터가 존재한다.

- PRE Filters : origin으로 routing하기전에 실행되는 필터로 인증, Origin Server 선택, 로깅 등을 정의한다.

- Routing Filters : Request를 Origin으로 라우팅하는 필터이다. Apache HTTP Client or Netflix Ribbon을 사용하여 전송한다.

- Post Filters : Request가 Origin으로 요청한 후 실행된다. Response에 헤더를 추가하거나 Statics, Metrics를 수집한다.

- Error Filters : 다른 단계에서 하나라도 에러 발생시 실행한다. 

 

Pre filter

package com.example.routingandfilteringgateway.filters.pre;

import javax.servlet.http.HttpServletRequest;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.ZuulFilter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleFilter extends ZuulFilter {

  private static Logger log = LoggerFactory.getLogger(SimpleFilter.class);

  @Override
  public String filterType() {
    return "pre";
  }

  @Override
  public int filterOrder() {
    return 1;
  }

  @Override
  public boolean shouldFilter() {
    return true;
  }

  @Override
  public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();

    log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));

    return null;
  }

}

ZuulFilter를 extends받아서 RequestContext에 downstream에서 사용할 데이터를 정의할 수 있다.

 

  • filterType() : 필터 유형을 나타낸다. 위에는 Pre로 되어있다.
  • filterOrder() : 다른 필터와 비교해서 필터가 실행되는 순서를 나타낸다.
  • shouldFilter(): 이 필터를 실행할 시기를 결정하는 logic을 포한한다.
  • run() : 필터 기능을 정의한다.

Zuul filter는 RequestContext에 요청 및 상태 정보를 저장하고 공유한다.

이를 통해 HttpServletRequest를 가져올 수 있고 전송되기 전에 HTTP method와 URL을 기록할 수 있다.

package com.example.routingandfilteringgateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import com.example.routingandfilteringgateway.filters.pre.SimpleFilter;

@EnableZuulProxy
@SpringBootApplication
public class RoutingAndFilteringGatewayApplication {

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

  @Bean
  public SimpleFilter simpleFilter() {
    return new SimpleFilter();
  }

}

 

Application class에 필터를 Bean으로 정의하여 넣는다. 

 

출처 고맙고

spring.io/guides/gs/routing-and-filtering/

 

Routing and Filtering

this guide is designed to get you productive as quickly as possible and using the latest Spring project releases and techniques as recommended by the Spring team

spring.io

 

728x90
반응형