Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Action Pattern
- 키클락
- Spring Cloud Netfilx Eureka
- 스프링 배치
- spring cloud
- Serial GC
- java 정렬
- 멀티스레드
- TypeScript
- 사가 패턴
- 디자인 패턴
- Java
- Parallel Old GC
- MSA
- 생산자 소비자 패턴
- 디자인패턴
- 스레드
- JPA
- Transaction Pattern
- Spring Boot Actuator
- 알고리즘
- zipkin
- thread
- saga pattern
- 배치
- spring batch
- 체인 패턴
- 타입스크립트
- Resilinece4j
- The law of Demeter
Archives
- Today
- Total
PSD( Private-Self-Development )
Feign Client 본문
Feign Client 란?
MSA 서버에서 다른 서버로 REST 통신을 하기 위한 추상화 된 인터페이스
기존 Rest Template 방식 보다 직관적 이고 간단하다.
단, 기존 Rest Template 방식 보다 양 MSA 서버간의 구조를 확실히 알아야 하는 단점도 있다.
사용
1. 디펜던시 추가
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2. 어노테이션 추가
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
3. 클라이언트 구현
@FeignClient(name = "order-service")
public interface OrderServiceClient {
@GetMapping("order-service/{userId}/orders")
List<ResponseOrder> getOrders(@PathVariable String userId);
}
4. 에러 디코더 구현
@Component
@RequiredArgsConstructor
public class FeignErrorDecoder implements ErrorDecoder {
private final Environment env;
@Override
public Exception decode(String methodKey, Response response) {
switch (response.status()){
case 400:
break;
case 404:
if(methodKey.contains("getOrders")){
return new ResponseStatusException(HttpStatus.valueOf(response.status()),
env.getProperty("order_service.exception.order_is_empty"));
}
break;
default:
return new Exception(response.reason());
}
return null;
}
}
'Backend > MSA' 카테고리의 다른 글
MSA 장애 처리 및 분산 추적 (0) | 2024.04.23 |
---|---|
Kafka( 카프카 ) (0) | 2024.04.22 |
Spring Cloud Config (0) | 2024.04.16 |
API Gateway (0) | 2024.03.26 |
Service Discovery (1) | 2024.01.09 |