사용 계기
공공 api를 사용하는 미니 프로젝트를 진행하던 도중, 스프링 내부에서 외부 api를 호출해야 하는 상황이 발생.
기존에는 RestTemplate라는 것을 사용하였으나, Netfilx에서 FeignClient라는 것을 개발하여 대체되었다.
그래서 처음에는 Spring Cloud Nextlix Feign으로 불리었으나, 오픈 소스 변경 후 Spring Colud OpenFeign에 통합되면서 이름이 바뀌었고 SpringMVC 어노테이션에 대한 지원 및 HttpMessageConverters를 사용할 수 있게 되었다.
의존성 추가
dependencies {
...
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:3.1.5'
implementation group: 'io.github.openfeign', name: 'feign-gson', version: '11.0' # GsonDecoder를 사용하기 위해 더 추가.
...
}
해당 홈페이지에서 본인의 스프링 boot 버전과 spring-cloud-starter-openfeign 버전이 일치하는지 비교하고 의존성을 추가해야 한다. 그렇지 않으면 에러가 발생한다.
Spring | Home
Cloud Your code, any cloud—we’ve got you covered. Connect and scale your services, whatever your platform.
spring.io
[Spring cloud starter Error] creating bean with name 'configurationPropertiesBeans' defined in class path resource ...
에러 Error creating bean with name 'configurationPropertiesBeans' defined in class path resource [org/springframework/cloud/autoconfigure/ConfigurationPropertiesRebinderAutoConfiguration.class]: Post-processing of merged bean definition failed; nested ex
chordplaylist.tistory.com
어노테이션 추가
@SpringBootApplication이 있는 Class에 어노테이션을 추가한다.
@SpringBootApplication
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
config 설정
@Configuration
public class Config {
@Bean
Logger.Level feignLoggerLevel(){
return Level.FULL;
}
@Bean
Decoder feignDecoder(){
return new ApiDecoder();
}
}
api를 호출할 interface 생성
@FeignClient(name = "air", url = "${application.api.baseUrl}")
public interface AirPollutionClient {
@RequestMapping(method = RequestMethod.GET,
value = "/ArpltnInforInqireSvc/getCtprvnRltmMesureDnsty")
FinalResponse getPollution(
@RequestParam(value = "ServiceKey") String serviceKey,
@RequestParam(value = "returnType") String returnType,
@RequestParam(value = "numOfRows") String numOfRows,
@RequestParam(value = "pageNo") String pageNo,
@RequestParam(value = "sidoName") String sidoName,
@RequestParam(value = "ver") String ver);
}
interface를 활용한 Service
@Service
@RequiredArgsConstructor
public class AirPollutionService {
...
private final AirPollutionClient airPollutionClient;
...
public FinalResponse getAirPollutionData(String sidoName) {
FinalResponse datas = airPollutionClient.getPollution(serviceKey, returnType, numOfRows,
pageNo, sidoName, ver);
return datas;
}
...
}
참조
GitHub - build99k/feign-api-test: FeignClient Test Sample Code
FeignClient Test Sample Code. Contribute to build99k/feign-api-test development by creating an account on GitHub.
github.com
[springboot]Feign client 사용하기 - REST api 간편 호출
스프링 환경에서 간편하게 외부 api를 호출할 수 있는 라이브러리인 feign client 를 사용해보도록 하겠습니다. restTemplate 보다 훨씬 더 간편하게 api를 호출 할 수 있습니다. feign client의 다양한 기능
vmpo.tistory.com
FeignClient 기본적인 사용법 (Spring Cloud OpenFeign)
기존 프로젝트에서는 서비스 간 통신에서 RestTemplate을 사용했는데 FeignClient를 통해 조금 더 편리하게 사용할 수 있다고 하여 적용해보며 정리한 내용입니다. 잘못된 부분은 댓글로 남겨주시면
wildeveloperetrain.tistory.com
'Server > Spring&Spring Boot' 카테고리의 다른 글
[Docs] Spring Rest Docs HTML 출력하는 법. (0) | 2023.03.22 |
---|---|
[Spring Security] Spring Security을 추가하면 왜 로그인 화면으로 넘어가는 걸까? (0) | 2023.02.25 |
[Thymeleaf] th:object, th:field 사용법 (0) | 2023.02.21 |
[Thymeleaf] th:value와 th:field를 함께 쓰면 value는 무시된다. (0) | 2023.02.10 |
[Thymeleaf] @태그 안에 $태그 사용하기, Expression preprocessing(전처리) "__"(밑줄 2개) (0) | 2023.02.09 |