OpenFegin Example
728x90
반응형
Fegin 은 Netflix 에서 개발하고 지원했으나 더이상 사용않기로 결정하여 오픈 소스로 OpenFegin을 공개되었다. Spring Cloud 에서 Spring 에 통합하여 쉽게 사용할 수 있도록 Spring boot 에 제공하고 있다.
Installing
스프링 부트 2.2.12 기준으로 의존성을 작성했다.
https://github.com/spring-cloud/spring-cloud-release/wiki/Spring-Cloud-Hoxton-Release-Notes 참고하여 호환성 버전을 확인해야 한다.
ext.springBootVersion = "2.2.12.RELEASE"
ext.springCloudVersion = "Hoxton.SR11"
ext.openFeignVersion = "10.12"
apply plugin: "org.springframework.boot"
apply plugin: "io.spring.dependency-management"
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
dependencies {
implementation "org.springframework.cloud:spring-cloud-starter-openfeign"
}
참고링크
HTTP Method PATCH 를 지원하지 않는 다.
patch 지원하기 위해 아래와 같이 의존성을 추가해야 한다.
implementation "io.github.openfeign:feign-httpclient:${openFeignVersion}"
참고링크
- https://stackoverflow.com/questions/61641977/spring-cloud-starter-openfeign-invalid-http-method-patch-executing-patch
- https://stackoverflow.com/questions/35374030/invalid-http-method-patch-executing-patch-caused-by-feign-retryableexcepti
Configuration
@FeignClient(name = "account-api", path = "/account/v1", url = "${app.gateway-uri}")
public interface AccountFeignClient {
}
@FeignClient
- name: REST API 서비스 명
- url: REST API 서버 uri
- path: 여러 API Path에 공통으로 사용할 접미사
Methods
@PostMapping(path = "/users/signup")
ResponseEntity<ResponseDto.Default> signup(@RequestBody RequestDto.Signup body);
@GetMapping(path = "/users/duplicate-username", produces = MediaType.TEXT_PLAIN_VALUE)
ResponseEntity<String> duplicateUsername(@RequestParam("username") String username);
URI Path
@PatchMapping(path = "/users/{uid}")
ResponseEntity<ResponseDto.Default> user(@PathVariable("uid") UUID uid);
Headers
// 구현
@PostMapping(path = "/token", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
ResponseEntity<String> token(
@RequestHeader HttpHeaders headers,
@RequestParam("grant_type") String grantType,
@RequestParam("username") String username,
@RequestParam("password") String password);
// 호출
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(clientId, clientSecret);
ResponseEntity<String> response = oAuth2FeignClient.token(headers, "password", "test", "1234");
728x90
반응형
'Tech' 카테고리의 다른 글
자주 사용하는 Git CLI (Command line interface) (0) | 2021.10.15 |
---|---|
HTTP 클라이언트 테스트를 위한 WireMock - Test MockServer (0) | 2021.10.08 |
Java Database Connection TLS 보안 이슈 (0) | 2021.10.07 |
QueryDSL for Spring Data JPA (0) | 2021.10.06 |