티스토리 뷰
728x90
반응형
Spring Cloud OpenFeign
Spring Cloud 프로젝트의 일부로, 마이크로서비스 아키텍처에서 HTTP 클라이언트로 사용되는 도구
Spring 어플리케이션에서 RESTful 서비스를 손쉽게 호출하고 통합하는데 도움
build.gradle 의존성 추가
ext {
springCloudVersion = "2022.0.3"
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
dependencies {
implementation "org.springframework.cloud:spring-cloud-starter-openfeign"
}
Spring Boot Include Feign
@SpringBootApplication
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Feign Client 구현
- name 속성에는 Feign Client를 구분할 수 있는 값 입력 (Spring Cloud LoadBalancer Client에서 사용)
- url 속성에는 호출할 url 주소 입력 (placeholder 사용 가능)
@FeignClient(name = "stores", url = "${api.url}")
public interface StoreClient {
@RequestMapping(method = RequestMethod.GET, value = "/stores")
List<Store> getStores();
@RequestMapping(method = RequestMethod.GET, value = "/stores")
Page<Store> getStores(Pageable pageable);
@RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json")
Store update(@PathVariable("storeId") Long storeId, Store store);
@RequestMapping(method = RequestMethod.DELETE, value = "/stores/{storeId:\\d+}")
void delete(@PathVariable Long storeId);
}
Feign Configuration
- Java 설정 방법과 yml 설정 방법 두가지가 있다.
- 두가지 모두 설정하게 되면 application.yml 설정으로 적용된다.
- Feign Default 설정은 FeignClientsConfiguration
- Feign Client 별로 설정하려면 Configuration 생성하고 Feign Client configuration 속성에 바인딩
- FeignClientsConfiguration의 설정들이 Overriding 된다
- Configuration 클래스에는 @Configuration 어노테이션을 붙이지 않아도 된다
- @Configuration을 선언하면 전체 Feign Client에 Default 설정으로 적용된
public class FooConfiguration {
@Bean
public Contract feignContract() {
return new feign.Contract.Default();
}
@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("user", "password");
}
}
@FeignClient(name = "stores", configuration = FooConfiguration.class)
public interface StoreClient {
//..
}
# application.yml
spring:
cloud:
openfeign:
client:
config:
feignName:
url: http://remote-service.com
connectTimeout: 5000
readTimeout: 5000
loggerLevel: full
errorDecoder: com.example.SimpleErrorDecoder
retryer: com.example.SimpleRetryer
defaultQueryParameters:
query: queryValue
defaultRequestHeaders:
header: headerValue
requestInterceptors:
- com.example.FooRequestInterceptor
- com.example.BarRequestInterceptor
responseInterceptor: com.example.BazResponseInterceptor
dismiss404: false
encoder: com.example.SimpleEncoder
decoder: com.example.SimpleDecoder
contract: com.example.SimpleContract
capabilities:
- com.example.FooCapability
- com.example.BarCapability
queryMapEncoder: com.example.SimpleQueryMapEncoder
micrometer.enabled: false
@SpringQueryMap
- GET 요청을 보낼 때 쿼리 매개변수를 자동으로 생성
- 매개변수 Map에 더 많은 제어가 필요한 경우 QueryMapEncoder Bean 구현
public class Params {
private String param1;
private String param2;
}
@FeignClient("demo")
public interface DemoTemplate {
@GetMapping(path = "/demo")
String demoEndpoint(@SpringQueryMap Params params);
}
// /demo?param1=xx¶m2=yy
https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html
728x90
반응형
'Spring' 카테고리의 다른 글
[Spring Boot] WireMock API 테스트 (0) | 2023.10.03 |
---|---|
[Spring Boot] OpenFeign 로깅 설정 (0) | 2023.10.01 |
[Spring] @SpringBootTest 통합테스트 (0) | 2023.08.27 |
[Spring] MockMvc 사용 시 403 Forbidden 에러 발생 (0) | 2023.08.27 |
[Spring] @WebMvcTest MockMvc 테스트 (0) | 2023.08.27 |
반응형
300x250