Zuul을 이용하여 MSA의 Api Gateway 구축을 테스트 삼아 하고 있는데 설정 값을 지정하면서 알게 된 내용을 정리한다.
application.yml 파일을 아래와 같이 설정 하였다.
spring:
  application:
    name: api-gateway
server:
  port: 8801
eureka:
  instance:
    instance-id: zuul-inst001
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
zuul:
  routes:
    chauffeur:
      path: /chauffeur/**
      stripPrefix: true
path 에 지정하는 값은 client에서 api gateway로 호출할 때의 url path 이다.
해당 path와 matching이 되면 chauffeur 로 이름이 지정된 Micro Service로 연결 되게 된다. 물론 해당 서비스는 Eureka에 등록이 되어 있어야 한다.
stripPrefix 값은 maching 된 url path를 제거하고 해당 Micro Service로 routing 할지 결정하는 것이다.
나 같은 경우는 각 micro service에서 각자 지정된 서비스명을 중복해서 사용할 필요가 없다 판단하여 사용하지 않고 있기 때문에 strip 처리하여 보내는 것으로 셋팅 하였다.
덧. zuul 이 deprecated 되었고, Spring Cloud Gateway가 속도면에서나 유지보수 면에서 더 낫다는 말을 해서 바로 바꿔 테스트 해 보니 마찬가지로 잘 되네요.
Dependency는 아래와 같이 바꿨습니다.
dependencies {
	implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
	implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
	implementation 'org.springframework.cloud:spring-cloud-starter-netflix-hystrix'
//	implementation 'org.springframework.cloud:spring-cloud-starter-netflix-hystrix-dashboard'
	implementation 'org.springframework.cloud:spring-cloud-starter-netflix-ribbon'
//	implementation 'org.springframework.cloud:spring-cloud-starter-netflix-zuul'
	implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
	compileOnly 'org.projectlombok:lombok'
	annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
	annotationProcessor 'org.projectlombok:lombok'
	testImplementation('org.springframework.boot:spring-boot-starter-test') {
		exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
	}
}
zuul을 제거하고 실행해보니 starter-web이 있어서 안된다는 말이 있어서 의존성 hierarchy 살펴보니 hystrix-dashboard에서 사용 중.
아마도 웹 UI에서 뭔가를 보여주는 기능이 있어서 그런 듯...
두 개 제거하고 application.yml 파일에 다음과 같이 셋팅 함.
spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      routes:
      - id: chauffeur
        uri: lb://CHAUFFEUR
        predicates:
        - Path=/chauffeur/**
        filters:
          - StripPrefix=1
server:
  port: 8801
eureka:
  instance:
    instance-id: zuul-inst001
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
zuul과 마찬가지로 stripPrefix를 지원하는데 사용하는 구문이 살짝 차이가 있어 보임.
기본적인 내용은 spring.io/guides/gs/gateway/#initial 참고 필요
Building a Gateway
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
'Architecture > MSA' 카테고리의 다른 글
| Spring Cloud Gateway - Custom Predicate with AbstractRoutePredicateFactory (0) | 2021.01.06 | 
|---|---|
| Spring Cloud Gateway - Global Filter (0) | 2021.01.06 | 
| Spring Cloud Gateway - Route Predicate & Gateway Filter Factory (0) | 2020.12.28 | 
| Hystrix, Feign을 이용한 개발 (0) | 2020.12.18 | 
| MSA 관련 북마크 (0) | 2020.11.20 |