Posts SIY Spring Cloud Api GateWay 1
Post
Cancel

SIY Spring Cloud Api GateWay 1

Spring Cloud Api Gateway(Filter)


๐Ÿ’ฟ APi Gateway ์ฃผ์š” ๊ธฐ๋Šฅ

  1. ์ธ์ฆ ๋ฐ ์ธ๊ฐ€(Authentication and Authorization)

์ธ์ฆ์„œ ๊ด€๋ฆฌ๋‚˜, ์ธ์ฆ, SSL, ํ”„๋กœํ† ์ฝœ ๋ณ€ํ™˜๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ๋“ค์€ API Gateway์—์„œ ์˜คํ”„๋กœ๋“œ ํ•จ์œผ๋กœ, ๊ฐ๊ฐ์˜ ์„œ๋น„์Šค์˜ ๋ถ€๋‹ด์„ ์ค„์ด๊ณ , ์„œ๋น„์Šค์˜ ๊ด€๋ฆฌ ๋ฐ ์—…๊ทธ๋ ˆ์ด๋“œ๋ฅผ ๋ณด๋‹ค ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌ

Authentication(์ธ์ฆ)๊ณผ Authorization(์ธ๊ฐ€)์˜ ์ฐจ์ด

Authentication์€ ์œ ์ €๊ฐ€ ๋ˆ„๊ตฌ์ธ์ง€ ํ™•์ธํ•˜๋Š” ์ ˆ์ฐจ(A๋ผ๊ณ  ํ•˜๋ฉฐ ์ ‘๊ทผํ•˜๋Š” ์‚ฌ๋žŒ์ด ์ง„์งœ A์ธ์ง€ ํ™•์ธํ•˜๋Š” ์ ˆ์ฐจ)์ด๊ณ , Authorization์€ ์–ด๋– ํ•œ ์œ ์ €๊ฐ€ ํŠน์ • ์ž์›์— ์ ‘๊ทผํ•˜๋ ค ํ• ๋•Œ, ๊ทธ์—๋Œ€ํ•œ ์ ‘๊ทผ ๊ถŒํ•œ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์ ˆ์ฐจ์ž…๋‹ˆ๋‹ค.

  1. ์š”์ฒญ ์ ˆ์ฐจ์˜ ๋‹จ์ˆœํ™”

API Gateway๋Š” ์—ฌ๋Ÿฌ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋‹จ์ผ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์œผ๋กœ ๋Œ€์ฒด ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํด๋ผ์ด์–ธํŠธ์™€ ๋ฐฑ์—”๋“œ ๊ฐ„์˜ API ํ†ต์‹ ๋Ÿ‰์„ ์ค„์—ฌ์ฃผ์–ด ๋Œ€๊ธฐ์‹œ๊ฐ„์„ ์ค„์ด๊ณ  ํšจ์œจ์„ฑ์„ ๋†’์—ฌ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. ๋ผ์šฐํŒ… ๋ฐ ๋กœ๋“œ๋ฐธ๋Ÿฐ์‹ฑ

API Gateway๋Š” ํด๋ผ์ด์–ธํŠธ๋กœ ๋ถ€ํ„ฐ ์ ‘์ˆ˜๋œ ๋ฉ”์„ธ์ง€์— ๋”ฐ๋ผ, API ํ˜ธ์ถœ์„ ์ ์ ˆํ•œ ์„œ๋น„์Šค์— ๋ผ์šฐํŒ… ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ์„œ๋น„์Šค ์ธ์Šคํ„ด์Šค๋“ค์— ๋Œ€ํ•œ ๋ถ€ํ•˜๋ถ„์‚ฐ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. ์„œ๋น„์Šค ์˜ค์ผ€์ŠคํŠธ๋ ˆ์ด์…˜

์˜ค์ผ€์Šคํฌ๋ ˆ์ด์…˜์€ ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋งˆ์ดํฌ๋กœ ์„œ๋น„์Šค๋ฅผ ๋ฌถ์–ด ์ƒˆ๋กœ์šด ์„œ๋น„์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ์˜ค์ผ€์ŠคํŠธ๋ ˆ์ด์…˜ ๋กœ์ง์„ ๊ณผ๋„ํ•˜๊ฒŒ ๋„ฃ๋Š” ๊ฒƒ์€, API Gateway์˜ ๋ถ€๋‹ด์„ ๋Š˜๋ฆฌ๋Š” ๊ฒƒ์œผ๋กœ, ์„ฑ๋Šฅ ์ €ํ•˜๋ฅผ ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ์–ด, MSA์™€ API Gateway์— ๋Œ€ํ•œ ๋†’์€ ์ˆ˜์ค€์˜ ๊ธฐ์ˆ ์  ์ดํ•ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ด๋ฃจ์–ด์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  1. ์„œ๋น„์Šค ๋””์Šค์ปค๋ฒ„๋ฆฌ

ํด๋ผ์šฐ๋“œ ํ™˜๊ฒฝ์—์„œ๋Š” ๋™์ ์ธ ํ™˜๊ฒฝ์—์„œ ๋ฐฐํฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋น„์Šค์˜ ์œ„์น˜๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์„œ๋น„์Šค์˜ ์œ„์น˜(IP ์ฃผ์†Œ์™€ ํฌํŠธ๋ฒˆํ˜ธ)๋ฅผ ์ฐพ๋Š” ๊ฒƒ์„ โ€œService Discoveryโ€๋ผ ํ•˜๋ฉฐ, API Gateway์—์„œ๋Š” ์„œ๋ฒ„์‚ฌ์ด๋“œ๋‚˜, ํด๋ผ์ด์–ธํŠธ ์‚ฌ์ด๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•˜์—ฌ ์„œ๋น„์Šค ๋””์Šค์ปค๋ฒ„๋ฆฌ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๐Ÿ’ฟ APi Gateway ๊ณ ๋ ค ์‚ฌํ•ญ

  1. Api Gateway๋ฅผ ๋‚ด๋ถ€ ms์™€ ๊ฒฐํ•ฉํ•œ๋‹ค๋Š” ๊ฒƒ: SOA์—์„œ์˜ ESB๋ฌธ์ œ์—์„œ ๋ฐœ์ƒํ–ˆ๋˜ ๋ฌธ์ œ์ ์ด ๋‹ค์‹œ ๋ฐœ์ƒ ๊ฐ€๋Šฅ
    • ESB ๋‚ด๋ถ€ ์ฒ˜๋ฆฌ ๋กœ์ง์„ XML์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜์˜€๋Š”๋ฐ, XML์˜ ํŒŒ์‹ฑ์€ ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ํฐ ์ž‘์—…
    • ๊ณผ๋„ํ•œ Orchestration ๋“ฑ ๋ฌด๊ฑฐ์šด ๋กœ์ง์„ ๊ฐ€์ง€๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ ESB๋ฅผ Gateway๋กœ์˜ ํŠน์„ฑ์ด ์•„๋‹Œ ์‹œ์Šคํ…œ์„ ํ†ตํ•ฉํ•˜๊ธฐ ์œ„ํ•œ ์—ญํ• ๋กœ ๋งŽ์ด ๊ตฌํ˜„ํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— ๋งŽ์€ ์‹คํŒจ ์‚ฌ๋ก€๊ฐ€ ๋ฐœ์ƒ
  2. Gateway์˜ Scale-Out ์ ์šฉ์— ์›ํ• ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ Api Gateway๊ฐ€ ๋ณ‘๋ชฉ์ง€์ ์ด ๋  ๊ฐ€๋Šฅ์„ฑ
  3. ์ถ”๊ฐ€ ์ ์ธ gateway๋ผ๋Š” ๊ณ„์ธต ์ƒ์„ฑ์œผ๋กœ ์ธํ•œ ์†๋„ ์ €ํ•˜

๐Ÿ’ฟ Spring Cloud Gateway

  • Spring Cloud Eureka Server(Discovery Service)์™€ ๊ฒฐํ•ฉ
  • ์ธ์ฆ ๋ฐ ์ธ๊ฐ€ ์„œ๋น„์Šค

Filter

์ข…๋ฅ˜

  • GlobalFilter
  • CustomFilter
    • PreFilter
    • PostFilter

CustomFilter ์ƒ์„ฑ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@Component
@Slf4j
public class CustomAuthFilter extends AbstractGatewayFilterFactory<CustomAuthFilter.Config> {
    public CustomAuthFilter() {
        super(Config.class);
    }

    @Override
    public GatewayFilter apply(Config config) {
        return ((exchange, chain) -> {
           ServerHttpRequest request = exchange.getRequest();
           ServerHttpResponse response = exchange.getResponse();

           if(config.isPreLogger()) {
              log.info("PreLogger");
           }

           return chain.filter(exchange).then(Mono.fromRunnable(() -> {
              if(config.isPostLogger()) {
                 log.info("PostLogger");
              }
           }));
        });
    }

    @Data
    public static class Config {
       private String baseMessage;
       private boolean preLogger;
       private boolean postLogger;
    }
}

CustomFilter ๋“ฑ๋ก

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
server:
  port: 8000

spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
        - id: user-service
          uri: lb://USER-SERVICE
          predicates:
            - Path=/user/**
          filters:
            - RewritePath=/user/?(?<segment>.*), /$\{segment}
            - name: CustomAuthFilter
              args:
                 baseMessage: Hi, there.
                 preLogger: true
                 postLogger: true

        - id: order-service
          uri: lb://ORDER-SERVICE
          predicates:
            - Path=/order/**
          filters:
            - RewritePath=/user/?(?<segment>.*), /$\{segment}
  • RewritePath: Gateway๋กœ ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด Path ๊ฐ’์„ ์ž๋™์œผ๋กœ ์žฌ์ž‘์„ฑ
  • predicates: Gateway๋กœ ๋“ค์–ด์˜จ ์š”์ฒญ์— ๋”ฐ๋ผ Load balancing
    • ์˜ˆ๋ฅผ ๋“ค์ž๋ฉด /order/** (์˜ค๋”๋กœ ๋“ค์–ด์˜จ ์š”์ฒญ ์€ ServiceDiscovery์—์„œ ORDER-SERVICE์˜ URL ๋™์ ์œผ๋กœ ์ฐพ์•„์„œ ์š”์ฒญ์„ ๋ณด๋ƒ„)
    • lb๋ž€ service Discovery์—์„œ URL์„ ๊ฒ€์ƒ‰ํ•˜๊ฒ ๋‹ค๋Š” ๋œป
  • name: filter๋ฅผ preFilter์™€ postFilter๋ฅผ ๊ตฌ๋ถ„ํ•ด์„œ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด name์„ ๋ถ™ํ˜€ Filter๋ฅผ ์ •์˜ํ•˜๊ณ  args๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋ถ€๋ถ„์„ ์ž‘์„ฑํ•ด์•ผ ํ•จ

GlobalFilter ์ƒ์„ฑ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@Component
@Slf4j
public class GlobalFilter extends AbstractGatewayFilterFactory<GlobalFilter.Config> {
    public CustomAuthFilter() {
        super(Config.class);
    }

    @Override
    public GatewayFilter apply(Config config) {
        return ((exchange, chain) -> {
           ServerHttpRequest request = exchange.getRequest();
           ServerHttpResponse response = exchange.getResponse();

           if(config.isPreLogger()) {
              log.info("PreLogger");
           }

           return chain.filter(exchange).then(Mono.fromRunnable(() -> {
              if(config.isPostLogger()) {
                 log.info("PostLogger");
              }
           }));
        });
    }

    @Data
    public static class Config {
       private String baseMessage;
       private boolean preLogger;
       private boolean postLogger;
    }
}

GlobalFilter ๋“ฑ๋ก

1
2
3
4
5
6
7
8
9
10
spring:
  cloud:
    gateway:
      default-filters:
        - name: GlobalFilter
          args:
            baseMessage: Spring Cloud GlobalFilter
            preLogger: true
            postLogger: true
    routes: ...
This post is licensed under CC BY 4.0 by the author.

Network-AMQP

SIY Spring Cloud Api GateWay 2