Spring Cloud Gateway 와 OpenFeign 순환 참조 이슈

송형근·2024년 9월 2일

TIL

목록 보기
28/43
post-thumbnail

프로젝트에서 Gateway를 통해 각 서비스로 요청을 전달하게 되는데,
이 때 FeignClient를 통해 Auth Server에 유저의 Role을 확인하게 됨

위의 기능 구현 과정에서 순환 참조 이슈가 발생

원인

  • Auth Service를 접근하기 위해서는 Gateway를 거쳐야 하는데, Gateway에서 Auth Service를 사용하려 하니 순환 참조가 발생하게 됨

해결 방법

  • Auth Client를 사용할 때 @Lazy 어노테이션을 사용해서, 필요한 시점에 의존성 주입해서 사용하면 됨
    private AuthService authService;
    
    public JwtAuthorizationFilter(@Lazy AuthService authService) {
            this.authService = authService;
    }
  • Decode Exception 발생 시 FeignConfig를 작성
    @Configuration
    public class FeignConfig {
        @Bean
        public Decoder feignDecoder() {
    
            ObjectFactory<HttpMessageConverters> messageConverters = () -> {
                HttpMessageConverters converters = new HttpMessageConverters();
                return converters;
            };
            return new SpringDecoder(messageConverters);
        }
    }
  • 위의 설정 후 block()/blockfirst()/blocklast() are blocking 에러가 발생하면, Spring Starter Web 의존성을 추가해서 해결
    • implementation 'org.springframework.boot:spring-boot-starter-web
profile
티스토리로 이주 (https://lucas-song94.tistory.com)

0개의 댓글