SpringCloud - API Gateway - Zuul

이재철·2021년 7월 25일
0

MSA

목록 보기
2/13

API Gateway 란?

  • 사용자가 설정한 라우팅 설정에 따라서 각각 엔드포인트로 클라이언트 대신 요청해서 대신 응답을 받고 요청을 해주는 Proxy 역할
  • 시스템 내부 구조는 숨기고 외부 요청에 대해 적절한 형태로 가공해서 응답할 수 있다.

Netflix Ribbon

  1. RestTemplete (MSA간 통신)
    private static final String url = "http://localhost:8082/products/";
    private final RestTemplate restTemplate;

    @Override
    public String getProductInfo(String productId) {
        return restTemplate.getForObject(url + productId, String.class);
    }
  1. Feign Client (MSA간 통신)
@FeignClient(name = "product", url = "http://localhost:8082/")
public interface FeignProductRemoteService {
    @RequestMapping(path = "/products/{productId}")
    String getProductInfo(@PathVariable("productId") String productId);
}
  1. Client side Load Balancer (Netflix) - 서버 x, 클라이언트 사이드에 존재
    • 스프링 초창기에 사용
    • 비동기 처리(Functional api, 리액트자바 등)가 안 되기 때문에 최근에 잘 사용하지 않음
    • 서비스 이름으로 호출
    • Health Check
    • Spring Cloud Ribbon은 Spring Boot 2.4에서 Maintenance 상태 (더이상 지원하지 않는 보류 상태)
    • Spring Boot 2.4 이상에서 지원 안 함

Netflix Zuul

  • API Gateway 역할
  • Routing 역할
  • Spring Cloud Ribbon은 Spring Boot 2.4에서 Maintenance 상태 (더이상 지원하지 않는 보류 상태)
  • Spring Boot 2.4 이상에서 지원 안 함
  • 2.4이상 대채

실습

1. first-service, second-service (first : 8081, second : 8082

- lombok, spring web, netflix-eureka-client
- lombok, spring web, netflix-eureka-client
# yml 설정
server:
  port: 8081

spring:
  application:
    name: my-first-service

eureka:
  client:
    fetch-registry: false
    register-with-eureka: false
// Controller
package com.example.secondservice;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class SecondController {

  @GetMapping("/welcome")
  public String welcom(){
    return "Welcome to the First service";
  }
}


2. Zuul-service 설정

  • zuul-service yml 설정
server:
  port: 8000

spring:
  application:
    name: my-zuul-service

zuul:
  routes:
    first-service:
      path: /first-service/**
      url: http://localhost:8081
    second-service:
      path: /second-service/**
      url: http://localhost:8082
  • ZuulServiceApplication @EnableZuulProxy 추가
package com.example.zuulservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableZuulProxy
public class ZuulServiceApplication {

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


3. Zuul Filter 적용

package com.example.zuulservice.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import com.netflix.zuul.context.RequestContext;

import javax.servlet.http.HttpServletRequest;


@Component
@Slf4j
public class ZuulLogginFilter extends ZuulFilter {

  // 사전 필터인지 사후 필터인지 정의
  @Override
  public String filterType() {
    return "pre"; // pre 사전필터
  }

  // 여러개의 필터가 있을경우 필터 순서
  @Override
  public int filterOrder() {
    return 1;
  }

  // 필터 사용 여부
  @Override
  public boolean shouldFilter() {
    return true;
  }

  // 어디에서 어떤 요청이 들어왔다는 로그 필터
  @Override
  public Object run() throws ZuulException {
    log.info("******** printing logs: ");

    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
    log.info("******** " + request.getRequestURI());
    return null;
  }
}

0개의 댓글