반응형

공식문서 : link

getting started : link

 

1. 개요

Spring Cloud Gateway는 API Gateway 기능을 구현할 수 있는 라이브러리로 대표적인 기능으로는 routing, filtering 있다.

@SpringBootApplication
public class DemogatewayApplication {
	@Bean
	public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
		return builder.routes()
			.route("path_route", r -> r.path("/get")
				.uri("http://httpbin.org"))
			.route("host_route", r -> r.host("*.myhost.org")
				.uri("http://httpbin.org"))
			.route("rewrite_route", r -> r.host("*.rewrite.org")
				.filters(f -> f.rewritePath("/foo/(?<segment>.*)", "/${segment}"))
				.uri("http://httpbin.org"))
			.route("hystrix_route", r -> r.host("*.hystrix.org")
				.filters(f -> f.hystrix(c -> c.setName("slowcmd")))
				.uri("http://httpbin.org"))
			.route("hystrix_fallback_route", r -> r.host("*.hystrixfallback.org")
				.filters(f -> f.hystrix(c -> c.setName("slowcmd").setFallbackUri("forward:/hystrixfallback")))
				.uri("http://httpbin.org"))
			.route("limit_route", r -> r
				.host("*.limited.org").and().path("/anything/**")
				.filters(f -> f.requestRateLimiter(c -> c.setRateLimiter(redisRateLimiter())))
				.uri("http://httpbin.org"))
			.build();
	}
}

 

 

2. 설정 클래스

- org.springframework.cloud.gateway.route.RouteDefinition  : 라우터 IO

- org.springframework.cloud.gateway.route.Route : RouteDefinitionRouteLocator에서 RouteDefinition을 Route로 변경

- org.springframework.cloud.gateway.route.Route.AsyncBuilder : Route Builder

- org.springframework.cloud.gateway.route.Route.Builder : Route Builder

 

3. 중요 클래스

org.springframework.cloud.gateway.route.CachingRouteLocator

- Route를 캐싱하는 클래스

- RefreshRoutesEvent 이벤트를 받아 RouteLocator에서 라우터를 다시 읽어 캐시 갱신

- 요청이 들어올 경우 RoutePredicateHandlerMapping.lookupRoute에서 CachingRouteLocator.getRoutes를 호출하여 캐싱된 Route 리스트 Get

 

org.springframework.cloud.gateway.handler.RoutePredicateHandlerMapping

- 클라이언트 요청에 대해 알맞은 라우팅을 lookup하는 클래스

 

org.springframework.cloud.gateway.handler.FilteringWebHandler

- 글로벌 필터와 라우터에 등록되어 있는 필터를 합쳐 필터를 수행한다.

 

org.springframework.cloud.gateway.filter.NettyRoutingFilter

- 실제 라우팅 URI에 요청하는 필터

 

org.springframework.cloud.gateway.actuate.AbstractGatewayControllerEndpoint, GatewayControllerEndpoint

- 실제 actuate 요청을 받는 컨트롤러

-  5. actuator 설정 참조

 

4. 요청 흐름도

0. Client Request

 

1. reactor.netty.http.server.HttpServer.HttpServerHandle.onStateChange

 

2. org.springframework.http.server.reactive.ReactorHttpHandlerAdapter.apply

 

3. org.springframework.web.server.adapter.HttpWebHandlerAdapter.handle

 

4. org.springframework.web.reactive.DispatcherHandler.handle

 

5. org.springframework.cloud.gateway.handler.RoutePredicateHandlerMapping.getHandlerInternal

 

6. org.springframework.cloud.gateway.handler.FilteringWebHandler.handle

 

7. org.springframework.cloud.gateway.handler.FilteringWebHandler.DefaultGatewayFilterChain.filter

 

 

5. actuator 설정

- pom.xml

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

 

- application.yml

management:
  endpoints:
    web:
      exposure:
        include: 
          - gateway
  endpoint:
    gateway:
      enabled: true

 

- endpoints

  • GET /actuator/gateway/globalfilters: Displays the list of global filters applied to the routes.
  • GET /actuator/gateway/routefilters: Displays the list of GatewayFilter factories applied to a particular route.
  • POST /actuator/gateway/refresh: Clears the routes cache.
  • GET /actuator/gateway/routes: Displays the list of routes defined in the gateway.GET /actuator/gateway/routes/{id}: 특정 route 설정 정보
  • GET /actuator/gateway/routes/{id} : Displays information about a particular route.
  • POST /actuator/gateway/routes/{id}: Add a new route to the gateway.
  • DELETE /actuator/gateway/routes/{id}: Remove an existing route from the gateway.

 

 

참고

https://blog.jungbin.kim/spring/2021/02/27/spring-cloud-gateway-route-locator.html

https://dlsrb6342.github.io/2019/05/14/spring-cloud-gateway-%EA%B5%AC%EC%A1%B0/

DB를 이용한 동적반영

https://medium.com/bliblidotcom-techblog/spring-cloud-gateway-dynamic-routes-from-database-dc938c6665de

반응형

+ Recent posts