Feign 사용법 ( Neflix의 Rest Client 라이브러리) API GET/POST 하기~
이전에 RestTemplate으로 api get하기 란 글을
블로그에 썼는데..
난 내가 개발한거 정리하려고 쓴건데 조회수가 생각보다 잘나와서 놀랬다 ;;
그이후 Retrofit이란 것도 써봤다
retorifit을 써보니 Feign은 크게 다른게 없는거 같았다..
그전에 쓸때 retrofit에 있는 제약사항이 있었는데.. Feign은 해당 제약사항이 없는진 잘모르겠다.
c.f ) Retrofit에서 제약사항이란?
Retrofit에서 인터페이스 하나당 서비스 하나를 구성해야한다는 것이다. 나는 비지니스 적으로 한 인터페이스를 구성해놓고 그것을 extends해서 쓰고싶었다.. 그런데 retrofit에서 해당 기능을 제공안하더라... ;ㅁ;
It's not possible to have a base Retrofit interface. Retrofit favors composition over inheritance. One interface per service. So as you already found out, the only solution is to create three independents interfaces.
다음글에서 2.7.0에는 이러한 제한을 없앤다고 하는데 현재 메이븐에는 2.6.1 버전까지 밖에 나오지 않았다. 그래서.. extends하도록은 설계하지 못하였다,,(API interfaces must not extend other interfaces.)
|
ㅎㅎ.. 말이 길어졌는데
Feign의 사용법에 대해서 그렇담 알아보자!
(다음엔 Retrofit에 대해서 써야겠다ㅋㅅㅋ)
1. maven일 경우 pom.xml에 dependency 추가
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> |
2. Application.java 파일에 @EnableFeignClients annotation 추가
이것은 난 Feign을 쓸거야~ 라고 알려주는 것이다
package com.example.feignTest; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; @EnableFeignClients // Feign Client를 사용할 것임을 알려줌 @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } |
@EnableFiegnClients는 돌아다니면서 @FeignCleint를 찾아 구현체를 만들어 준다.
root package에 있어야하며 아니면 basePackage를 지정해 주어야한다.
3. Feign Cleint 작성
인터페이스로 작성하고 인터페이스 위에 @FeignClient annotation을 넣어준다.
그리고 가져와야되는 url이랑 configuration이 필요하다면 작성해서 넣어주면 된다.
package com.client; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; @FeignClient(name="test", url="http://localhost:8000" , configuration={FeignConfiguration.class}) public interface TestClient { @GetMapping("/test") String test(@PathVariable("id") int id); } |
5. Client 사용(호출)
이제 서비스에서 해당 Client만 주입해서 API만 호출해주면 된다.
package com.service; testClient.test(id); ~~~
|
참쉽죠잉~?
ㅎㅎ 더 세부적인건 나중에 적어보도록 하겠다..
사실 이 밑에 있는 spring의 문서를 자세히 보는것이 가장 좋긴하다
참고
https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html
7. Declarative REST Client: Feign
Feign is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. Feign also supports pluggable
cloud.spring.io
https://woowabros.github.io/experience/2019/05/29/feign.html
우아한 feign 적용기 - 우아한형제들 기술 블로그
안녕하세요. 저는 비즈인프라개발팀에서 개발하고 있는 고정섭입니다.이 글에서는 배달의민족 광고시스템 백엔드에서 feign 을 적용하면서 겪었던 것들에 대해서 공유 하고자 합니다.
woowabros.github.io
https://velog.io/@skyepodium/2019-10-06-1410-%EC%9E%91%EC%84%B1%EB%90%A8
Feign Client 알아보기
2. 사용법 1) 스프링부트 프로젝트 만들기 spring start에서 스프링부트 프로젝트를 만들었습니다. (maven, gradle 상관없습니다.) 라이브러리는 web만 넣어주었습니다. 스크린샷 2019-10-06 오후 2.57.09.png 2) feign client 라이브러리 넣기 - maven 일 경우 - gra...
velog.io