티스토리 뷰
- Interface 선언을 통해 자동으로 Http Client를 생성
- RestTemplate은 concreate 클래스라 테스트하기 어렵다
- 관심사의 분리
- 서비스의 관심 - 다른 리소스, 외부 서비스 호출과 리턴값
- 관심 X - 어떤 URL, 어떻게 파싱할 것인가
- Spring Cloud에서 Open-Feign 기반으로 Wrapping한 것이 Spring Cloud Feign
Feign
- Feign은 Netflix에서 만든 선언적 Http 클라이언트이다.
- 선언적 Http클라이언트란, 어노테이션 선언만으로 Http 클라이언트를 만들 수 있고 이를 통해서 Http Api 호출이 가능한 것을 의미함.
Feign 설정 방법
- 인터페이스 선언 만으로 Http Client 구현물을 만들어 줌
@FeignClient(name="dp", url = "http://localhost:8080/")
public interface ProductResource {
@RequestMapping(value = "/query/{itemId}", method=RequestMethod.GET)
String getItemDetail(@PathVariable(value = "itemId") String itemId);
}
- @FeignClient를 Interface에 명시
- 각 API를 Spring MVC Anntation을 이용해서 정의
- DI 받아서 사용
Feign + Hystrix, Ribbon, Eureka
- Feign의 또 다른 강점은 Ribbon + Eureka + Hystrix와 통합되어 있다는 점.
- Feign의 동작
- @FeignClient에 URL 명시 시 -> 순수 Feign Client로서만 동작
- @FeignClient에 URL 명시하지 않으면?
- Feign + Ribbon + Eureka 모드로 동작
- 어떤 서버를 호출하나? @FeignClient(name='product') (명시된 서버)
- 즉, Eureka에서 서버 목록을 조회해서 Ribbon을 통해 Load-Balancing하면서 HTTP 호출을 수행
- Hystrix도 추가로 사용하려면 설정 파일에 설정을 해줘야함 (feign.hystrix.enable: true)
- 메소드 하나 하나가 Hystrix Command로서 호출됨.
- Hystrix Fallback 처리방법 2가지
- 인터페이스 구현해서 Spring Bean으로 선언 -> 에러 확인하기 어려움
- FallbackFactory 구현한 클래스 만들어서 처리하도록 -> 에러 확인 쉬움
인터페이스 구현해서 Spring Bean으로 선언 후 등록하는 방법
public class FeignProductRemoteServiceImpl implements FeignProductRemoteService {
@Override
public String getProductInfo(String productId) {
return "[ This product is sold out ]";
}
}
@FeignClient(name = "product", fallback = FeignProductRemoteServiceImpl.class)
public interface FeignProductRemoteService {
@RequestMapping("/products/{productId}")
String getProductInfo(@PathVariable String productId);
}
FallbackFactory 구현한 클래스 만들어서 등록하는 방법
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
@Component
public class FeignProductRemoteServiceFallbackFactory implements FallbackFactory<FeignProductRemoteService> {
@Override
public FeignProductRemoteService create(Throwable cause) {
// Throwable이 인자로 들어오므로 확인이 쉽다
System.out.println("t = " + cause);
return productId -> "[ This product is sold out ]";
}
}
@FeignClient(name = "product", fallbackFactory = FeignProductRemoteServiceFallbackFactory.class)
public interface FeignProductRemoteService {
@RequestMapping("/products/{productId}")
String getProductInfo(@PathVariable String productId);
}
Feign용 Hystrix 프로퍼티 정의하는 법
- 인터페이스명#추상메서드 ( ex. FeignProductRemoteService#getProductInfo(String) )
정리
- Feign은 Interface 선언을 통해 자동으로 HTTP Client를 만들어주는 Declarative Http Client
- Open-Feign 기반으로 Spring Cloud가 Wrapping한 것이 Spring Cloud Feign
- 인터페이스에 URL을 명시할 경우
- No Ribbon
- No Eureka
- No Hystrix
- Feign은 인터페이스 선언 + 설정으로 다음과 같은 것들이 가능하다
- Http Client
- Eureka 타겟 서버 주소 획득
- Ribbon을 통한 Client-Side Load Balancing
- Hystrix를 통한 메소드 별 Circuit Breaker
[참고 및 출처]
www.youtube.com/watch?v=SOmn6BGL884&list=PL9mhQYIlKEhdtYdxxZ6hZeb0va2Gm17A5&index=7
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- non-blocking
- 프로그래머스 Level 2
- 프로그래머스 Level 3
- 인터셉터
- blocking
- 프로그래머스 Level 1
- http://www.nextree.co.kr/p6960/
- Asynchronous
- 코딩테스트 고득점 Kit
- 비동기
- Synchronous
- Filter
- 스택/큐
- a
- 필터
- 논블로킹
- 해시
- 블로킹
- 프로그래머스
- 동기
- Handler Interceptor
- 핸들러 인터셉터
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함