Junhyunny's Devlogs

글 검색

기술 글 제목, 본문, 카테고리에서 검색합니다.

    이전 화면으로

    스프링 클라우드(spring cloud) OpenFeign

    선언적 HTTP 클라이언트인 스프링 클라우드 OpenFeign을 사용해 서비스 간 API를 호출하는 예제를 구현하고 도커 컴포즈로 동작을 확인했다.

    • spring-boot
    • spring-cloud
    이 글의 목차

    1. Spring Cloud Openfeign

    Spring Cloud Openfeign API Reference
    Feign is a declarative web service client. It makes writing web service clients easier.

    MSA(MicroService Architecture)를 지원하는 스프링 클라우드(spring cloud) 프로젝트들 중 하나다. 서비스들 사이의 API 요청, 응답을 쉽게 할 수 있도록 돕는 라이브러리다. 간단한 예제 코드를 통해 사용 방법을 살펴보자.

    2. Practice

    간단한 예제를 통해 동작을 살펴보자. 다음과 같은 API 호출이 필요하다.

    1. 사용자는 서비스A의 /health 경로로 API 요청을 수행한다.
    2. 서비스A는 /health 요청을 받으면 서비스B로 API 요청을 수행한다.
    3. 서비스B는 자신의 상태를 응답한다.
    4. 서비스A는 서비스B 응답과 자신의 상태를 함께 응답한다.
    5. 사용자는 서비스A로부터 결과를 응답 받는다.

    pom.xml 파일에 spring-cloud-starter-openfeign 의존성을 추가한다.

        <properties>
            <spring-cloud.version>2021.0.5</spring-cloud.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
        </dependencies>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>

    이 작업은 ServiceA 프로젝트에서 작업을 수행한다.

    • 서비스B에게 API 요청하기 위한 클라이언트를 만든다.
    • 이름과 URL을 지정한다. 이름은 필수 값이다. 테스트를 위해 도커 컴포즈(docker compose) 파일에 명시된 서비스 이름을 작성한다.
    • GET 요청이므로 @GetMapping 애너테이션을 사용한다. 서비스B에는 요청을 받기 위한 /health 경로가 존재한다.
    package action.in.blog.client;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    
    @FeignClient(name = "health-client", url = "http://b-service:8080")
    public interface HealthClient {
    
        @GetMapping(path = "/health")
        String health();
    }

    ServiceA 프로젝트에 요청을 받을 수 있도록 /health 경로를 생성한다.

    package action.in.blog.controller;
    
    import action.in.blog.client.HealthClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HealthController {
    
        private final HealthClient healthClient;
    
        public HealthController(HealthClient healthClient) {
            this.healthClient = healthClient;
        }
    
        @GetMapping("/health")
        public String health() {
            return String.format("ServiceA's Health - OK / ServiceB's Health - %s", healthClient.health());
        }
    }

    ServiceA 프로젝트에 @FeignClient 사용을 위해 @EnableFeignClients 애너테이션을 추가한다.

    package action.in.blog;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    @EnableFeignClients
    @SpringBootApplication
    public class AServiceApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(AServiceApplication.class, args);
        }
    
    }

    ServiceB 프로젝트에 요청을 받기 위한 /health 경로를 생성한다.

    package action.in.blog.controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HealthController {
    
        @GetMapping("/health")
        public String health() {
            return "OK";
        }
    }

    3. Test

    도커 컴포즈(docker compose)로 테스트 환경을 구축한다.

    3.1. Dockerfile

    다음과 같은 도커 파일을 준비한다.

    FROM maven:3.8.6-jdk-11 as MAVEN_BUILD
    
    WORKDIR /build
    
    COPY pom.xml .
    
    RUN mvn dependency:go-offline
    
    COPY src ./src
    
    RUN mvn package -Dmaven.test.skip=true
    
    FROM openjdk:11-jdk-slim-buster
    
    WORKDIR /app
    
    ARG JAR_FILE=*.jar
    
    COPY --from=MAVEN_BUILD /build/target/${JAR_FILE} ./app.jar
    
    EXPOSE 8080
    
    CMD ["java", "-jar", "app.jar"]

    3.2. docker-compose.yml

    도커 컴포즈 실행을 위해 컨테이너 YAML 파일을 준비한다.

    version: "3.9"
    services:
      a-service:
        build: ./a-service
        ports:
          - "8080:8080"
      b-service:
        build: ./b-service
        ports:
          - "8081:8080"

    3.3. Run Docker Compose

    도커 컴포즈로 컨테이너들을 실행한다.

    $ docker-compose up -d
    ...
    [+] Running 2/2
     - Container 2021-03-04-spring-cloud-openfeign-a-service-1  Started                            0.8s 
     - Container 2021-03-04-spring-cloud-openfeign-b-service-1  Started                            0.8s

    테스트 결과를 확인하기 위해 사용자 터미널에서 cURL 명령어를 통해 테스트를 수행한다.

    $ curl http://localhost:8080/health
                            
    
    StatusCode        : 200
    StatusDescription :
    Content           : ServiceA's Health - OK / ServiceB's Health - OK
    RawContent        : HTTP/1.1 200
                        Keep-Alive: timeout=60
                        Connection: keep-alive
                        Content-Length: 47
                        Content-Type: text/plain;charset=UTF-8
                        Date: Sat, 04 Feb 2023 12:43:58 GMT
    
                        ServiceA's Health - OK / ServiceB's He...
    Forms             : {}
    Headers           : {[Keep-Alive, timeout=60], [Connection, keep-alive], [Content-Length, 47], [Content-Type, text/plain;charset=UTF-8]...}
    Images            : {}
    InputFields       : {}
    Links             : {}
    ParsedHtml        : mshtml.HTMLDocumentClass
    RawContentLength  : 47

    CLOSING

    유틸성 클래스를 만들어 HTTP 요청을 다루는 코드를 오래된 시스템에서 종종 봤다. HttpURLConnection, I/O Stream 클래스를 사용해 불필요한 코드가 많았는데, FeignClient는 인터페이스와 애너테이션을 통해 비즈니스와 관련 없는 코드들을 최대한 단순화시킨다. spring-cloud 생태계를 조성하는 다른 컴포넌트들과 함께 사용하면 더욱 좋다.

    KEEP READING

    DISCUSSION

    댓글