API Gateway Service

  • API Gateway는 모든 서버로의 요청을 단일지점을 거쳐서 처리하도록 한다. 이를 통해 공통된 로직처리나 인증 및 인가 처리, 라우팅 처리등을 할 수 있다.
  • 시스템의 내부 구조는 숨기고 외부의 요청에 의해 적절한 형태로 응답할 수 있는 장점이 있다.

SpringCloud에서의 MSA간 통신

RestTemplate

  • 전통적으로 다른 서버와의 통신을 위한 방법

FeignClient

  • 특정한 인터페이스를 만들고 이름을 등록user라는 서비스에서 feign client 등록하고 사용하면 직접적인 서버의 주소라던가 포트번호 없이 마이크로 서비스 이름을 가지고 호출할 수 있게된다.
    • 문제는 로드밸런싱을 하기위해 어디에 구축해서 작업할 것인가?
      • Ribbon(netflix 로드밸런싱 기능)
        • 비동기 처리 리액티브 방식과는 호환이 안됨 → 최근에는 잘 사용하지 않는다.
        • 마이크로서비스의 이름만으로 호출할 수 있다.
        • Gateway의 역할을 client side에서 처리하는 방식
    Spring Cloud Gateway
    • Dependencies
      • DevTools, Eureka Discovery Client, Gateway
    • application.yml
    • userService에 Controller를 구현하고 테스트 해보자
server:
    port: 8080

eureka:
    client:
        register-with-eureka: false
        fetch-registry: false
        service-url:
            defaultZone: http://localhost:8761/eureka
spring:
    application:
        name: gateway-service
    cloud:
        gateway:
            routes:
              - id: first-service
                uri: http://localhost:8081/
                predicates:
                    - Path=/first-service/**
              - id: first-service
                uri: http://localhost:8082/
                predicates:
                    - Path=/second-service/**
package com.example.userservice;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController("/")
@Slf4j
public class UserController {

    @GetMapping("first-service")
    public String first(){
        log.info("first 호출됨");
        return "first-service";
    }

    @GetMapping("second-service")
    public String second(){
        log.info("second 호출됨");
        return "second-service";
    }
}

 

각각 다른 서버에서 호출되어야할 api가 호출되는것을 확인!

 

 

'DevOps > MSA' 카테고리의 다른 글

Apache kafka  (0) 2023.05.24
Spring Cloud Gateway - Load Balancer  (0) 2023.05.15
Spring Cloud Gateway - Filter 적용  (0) 2023.05.14
Spring Cloud 와 Eureka  (0) 2023.05.14

+ Recent posts