1) Oepn Feign 개념
- Netflix에 의해 처음 만들어진 Declaratic(선언적인) HTTP Client 도구로써, 외부 API 호출을 쉽게할 수 있도록 도와준다.
- 인터테이스와 어노테이션 기반으로 쉽게 작성할 수 있다.
2) Open Feign 구현
dependency 추가
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-gateway -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>4.1.1</version>
</dependency>
OpenFeign 활성화
메인 클래스에 @EnableFeignClients 어노테이션을 추가한다.
@SpringBootApplication
@EnableFeignClients
public class OpenfeignApplication {
public static void main(String[] args) {
SpringApplication.run(OpenfeignApplication.class, args);
}
}
FeignClient 구현
interface에 @FeignClient 어노테이션을 붙여주면 된다.
@FeignClient(name = "BookClient", url = "${book.client.url}")
public interface BookClient {
@RequestMapping(method = RequestMethod.GET, value = "/books")
List<Book> getBooks();
@RequestMapping(method = RequestMethod.POST, value = "/books/{bookId}", consumes = "application/json")
Book update(@PathVariable("bookId") Long bookId, Book book);
// req: MultipartFile, res: JSON
@RequestMapping(method = RequestMethod.POST
, value = "/books"
, consumes = MediaType.MULTIPART_FORM_DATA_VALUE
, produces = MediaType.APPLICATION_JSON_VALUE)
List<Book> uploadBooks(@RequestPart(value = "file", required = false) MultipartFile file);
}
참고
'Spring Cloud' 카테고리의 다른 글
Spring Cloud Gateway 와 Eureka 연동 (0) | 2024.03.11 |
---|---|
Service Discovery - Spring Cloud Netflix Eureka 설정 (0) | 2024.03.11 |
Spring Cloud Gateway 개념 및 설정 방법 (0) | 2024.03.11 |
Service Discovery (0) | 2024.03.11 |
Spring Cloud Config (2) - Actuator refresh (0) | 2024.03.11 |