๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java/SpringBoot

[SpringBoot] Spring Cloud OpenFeign ์ƒ์„ฑํ•˜๊ธฐ

by ๋ฆฌ์š”_ 2024. 10. 5.
๋ฐ˜์‘ํ˜•

[SpringBoot] Spring Cloud OpenFeign ์ƒ์„ฑํ•˜๊ธฐ

๐ŸŒŸ INDEX


Spring Cloud OpenFeign 

OpenFeign์€ Java ๊ธฐ๋ฐ˜์˜ HTTP ํด๋ผ์ด์–ธํŠธ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์„ ์–ธ์  ์›น ์„œ๋น„์Šค ํด๋ผ์ด์–ธํŠธ์ž…๋‹ˆ๋‹ค. Spring Cloud์˜ ์ผ๋ถ€๋กœ ์ œ๊ณต๋˜๋ฉฐ, RESTful API ์™€์˜ ํ†ต์‹ ์„ ๊ฐ„์†Œํ™”ํ•˜๋Š” ๋ฐ ๋„์›€์„ ์ค๋‹ˆ๋‹ค.

 

OpenFeign์€ ์ธํ„ฐํŽ˜์ด์Šค ๊ธฐ๋ฐ˜์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ง€์›ํ•˜์—ฌ, API ํ˜ธ์ถœ์„ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.


OpenFeign ์ฃผ์š” ํŠน์ง•

  1. ์„ ์–ธ์  API ํ˜ธ์ถœ:
    • OpenFeign์„ ์‚ฌ์šฉํ•˜๋ฉด ์ธํ„ฐํŽ˜์ด์Šค์™€ ์• ๋„ˆํ…Œ์ด์…˜์„ ํ†ตํ•ด HTTP ์š”์ฒญ์„ ์„ ์–ธ์ ์œผ๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ง€๊ณ  ๊ฐ€๋…์„ฑ์ด ๋†’์•„์ง‘๋‹ˆ๋‹ค.
  2. ์ž๋™ํ™”๋œ HTTP ์š”์ฒญ:
    • OpenFeign์€ HTTP ๋ฉ”์„œ๋“œ(GET, POST, PUT, DELETE ๋“ฑ)๋ฅผ ์ธํ„ฐํŽ˜์ด์Šค ๋ฉ”์„œ๋“œ์— ๋งคํ•‘ํ•˜์—ฌ, ๋ณต์žกํ•œ HTTP ์š”์ฒญ์„ ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  3. ํ†ตํ•ฉ๊ณผ ํ™•์žฅ์„ฑ:
    • Spring Cloud์™€ ํ†ตํ•ฉ๋˜์–ด, Eureka์™€ ๊ฐ™์€ ์„œ๋น„์Šค ๋“ฑ๋ก ๋ฐ ๋ฐœ๊ฒฌ ๊ธฐ๋Šฅ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ํ•„ํ„ฐ, ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ๊ธฐ, ์ธ์ฝ”๋” ๋ฐ ๋””์ฝ”๋” ๋“ฑ์„ ํ†ตํ•ด ๊ธฐ๋Šฅ์„ ํ™•์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  4. ์œ ์—ฐํ•œ ๊ตฌ์„ฑ:
    • ๋‹ค์–‘ํ•œ ๊ตฌ์„ฑ ์˜ต์…˜์„ ์ œ๊ณตํ•˜์—ฌ, ํƒ€์ž„์•„์›ƒ, ๋ฆฌํŠธ๋ผ์ด ์ •์ฑ…, ๋กœ๊น… ๋“ฑ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  5. REST API ๋ฌธ์„œํ™”:
    • OpenFeign์€ Swagger์™€ ๊ฐ™์€ ๋„๊ตฌ์™€ ํ†ตํ•ฉํ•˜์—ฌ API ๋ฌธ์„œ๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Spring Boot ๋ฒ„์ „ ๋ณ„ Spring Cloud OpenFeign ํ˜ธํ™˜์„ฑ

๋ฒ„์ „๋ณ„ ํ˜ธํ™˜์„ฑ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. Spring Boot 2.1.x
    • Spring Cloud 2.1.x (OpenFeign 2.1.x)
    • Spring Cloud 2.2.x (OpenFeign 2.2.x)
  2. Spring Boot 2.2.x
    • Spring Cloud 2.2.x (OpenFeign 2.2.x)
    • Spring Cloud 2.3.x (OpenFeign 2.3.x)
  3. Spring Boot 2.3.x
    • Spring Cloud 2.3.x (OpenFeign 2.3.x)
    • Spring Cloud 2.4.x (OpenFeign 2.4.x)
  4. Spring Boot 2.4.x
    • Spring Cloud 2020.0.x (OpenFeign 3.0.x)
    • Spring Cloud 2021.x (OpenFeign 3.1.x)
  5. Spring Boot 2.5.x
    • Spring Cloud 2020.0.x (OpenFeign 3.0.x)
    • Spring Cloud 2021.x (OpenFeign 3.1.x)
  6. Spring Boot 2.6.x
    • Spring Cloud 2021.x (OpenFeign 3.1.x)
  7. Spring Boot 3.x
    • Spring Cloud 2022.x (OpenFeign 4.x)

https://spring.io/projects/spring-cloud#overview

 

Spring Cloud

Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus, short lived microservices and

spring.io

Spring Cloud ํ™ˆํŽ˜์ด์ง€์—์„œ ๊ฐ ๋ฆด๋ฆฌ์ฆˆ ๋ณ„ ํ˜ธํ™˜์„ฑ ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign

๋ฉ”์ด๋ธ ๋ ˆํฌ์ง€ํ† ๋ฆฌ ํ™ˆํŽ˜์ด์ง€์—์„œ ๋ฒ„์ „์— ๋งž๋Š” ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


OpenFeign ์ ์šฉํ•˜๊ธฐ

์˜์กด์„ฑ ์ถ”๊ฐ€

[SpringBoot] Spring Cloud OpenFeign ์ƒ์„ฑํ•˜๊ธฐ

// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign
implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-openfeign', version: '4.0.0'

 

์ €๋Š” Gradle ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์–ด, build.gradle ํŒŒ์ผ์— ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

Spring Boot 3.2.2 ๋ฒ„์ „์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์–ด OpenFeign 4.0 ๋ฒ„์ „์˜ ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.


OpenFeign ํ™œ์„ฑํ™” (@EnableFeignClients ์–ด๋…ธํ…Œ์ด์…˜ ์ถ”๊ฐ€)

[SpringBoot] Spring Cloud OpenFeign ์ƒ์„ฑํ•˜๊ธฐ

 

import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableFeignClients
@SpringBootApplication
public class LiyoApplication {

	public static void main(String[] args) {
		SpringApplication.run(LiyoApplication.class, args);
	}

}

main ํด๋ž˜์Šค์— @EnableFeignClients ์–ด๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•ด ์ค๋‹ˆ๋‹ค.

์˜์กด์„ฑ ์ถ”๊ฐ€ ํ›„ JAVA Clean ํ˜น์€ ์žฌ๋นŒ๋“œ ํ•˜์ง€ ์•Š์œผ๋ฉด import ๊ฐ€ ์ œ๋Œ€๋กœ ๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

[SpringBoot] Spring Cloud OpenFeign ์ƒ์„ฑํ•˜๊ธฐ

๋นจ๊ฐ„ ์ค„์ด ๋œจ๊ณ  ์–ด๋…ธํ…Œ์ด์…˜ ์ถ”๊ฐ€ ๋ฐ import ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด ์ž๋ฐ” ํด๋ฆฐ ์ง„ํ–‰ํ•ด ์ฃผ์„ธ์š”.


ํ™˜๊ฒฝ ์„ค์ • ( url ์„ธํŒ… )

application.property ํ˜น์€ application.yml ํŒŒ์ผ์— ํ™˜๊ฒฝ ์„ค์ • ์ •๋ณด๋ฅผ ์„ธํŒ…ํ•ฉ๋‹ˆ๋‹ค.

[SpringBoot] Spring Cloud OpenFeign ์ƒ์„ฑํ•˜๊ธฐ

spring:
  cloud:
    openfeign:
      client:
        config:
          liyoServiceClient:    # ํด๋ผ์ด์–ธํŠธ ์ด๋ฆ„
            url: http://liyo-service-url.com
          anotherLiyoServiceClient:
            url: http://anotherLiyo-service-url.com

 

๊ธฐ๋ณธ์ ์ธ url ๋ถ€ํ„ฐ ํ•„์š”์— ๋”ฐ๋ผ Timeout, loggerLevel  ๋“ฑ ๋‹ค์–‘ํ•œ ์„ค์ •์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


FeignClient ๊ตฌํ˜„ํ•˜๊ธฐ

์ธํ„ฐํŽ˜์ด์Šค์— @FeignClient ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ™์—ฌ API ๋ฅผ ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์„ฑํ•ด ์ค๋‹ˆ๋‹ค.

 

์ €๋Š” liyoService ์™€ anotherLiyoService ์ด 2๊ฐœ์˜ API ๋ฅผ ์„ธํŒ…ํ•ด์ฃผ์—ˆ์œผ๋ฏ€๋กœ, 2 ๊ฐœ์˜ FeignCilent ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด ์ค„ ๊ฒ๋‹ˆ๋‹ค.

[SpringBoot] Spring Cloud OpenFeign ์ƒ์„ฑํ•˜๊ธฐ

 

client ํด๋”๋ฅผ ์ƒ์„ฑํ•˜์—ฌ, FeignClient ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

์ด๋ฏธ์ง€์—๋Š” Service ๋ผ๊ณ  ๋˜์–ด์žˆ์ง€๋งŒ, ์‹ค์ œ Service ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ด๋ฆ„์ด ๊ฒน์ณ ์‹ค์ œ ์ฝ”๋“œ์—๋Š” Client ๋ฅผ ๋ถ™์—ฌ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

package com.liyo.exemple.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import com.liyo.exemple.dto.fireSafety.LiyoServiceResponse;

@FeignClient(name = "liyoServiceClient", url = "${spring.cloud.openfeign.client.config.liyoServiceClient.url}")
public interface LiyoServiceClient {

    @GetMapping("/some-endpoint/{id}") // ์‹ค์ œ ์—”๋“œํฌ์ธํŠธ์— ๋งž๊ฒŒ ์ˆ˜์ •
    LiyoServiceResponse getLiyoServiceData(@PathVariable("id") String id);
}

 

๋™์ผํ•œ ์–‘์‹์œผ๋กœ ํด๋ผ์ด์–ธํŠธ ๊ฐœ์ˆ˜๋งŒํผ ์ƒ์„ฑํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

 


์‘๋‹ต DTO ํด๋ž˜์Šค ์ƒ์„ฑํ•˜๊ธฐ

package com.liyo.example.dto.fireSafety;

public class LiyoServiceResponse {

}

 

๊ฐ API์˜ ์‘๋‹ต์„ ๋ฐ›์„ DTO ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•ด ์ค๋‹ˆ๋‹ค.


FeignConfig ์ƒ์„ฑํ•˜๊ธฐ

Feign ํด๋ผ์ด์–ธํŠธ์˜ ์„ค์ •์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” Config ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. 

 

ํ•ด๋‹น Config ๊ฐ€ ์ ์šฉ๋˜๋Š” FeginClient ์— ๊ณตํ†ต์œผ๋กœ ๋กœ๊น…์ด๋‚˜ ํƒ€์ž„์•„์›ƒ ์„ค์ • ๋“ฑ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

[SpringBoot] Spring Cloud OpenFeign ์ƒ์„ฑํ•˜๊ธฐ

 

package com.liyo.example.config;

import feign.Logger;
import feign.RequestInterceptor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeginConfig {

    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL; // ๋ชจ๋“  ์š”์ฒญ๊ณผ ์‘๋‹ต์„ ๋กœ๊น…
    }

    @Bean
    public RequestInterceptor requestInterceptor() {
        return requestTemplate -> {
            requestTemplate.header("Content-Type", "application/json");
            requestTemplate.header("Accept", "application/json");
        };
    }

}

 

RequestInterceptor

- ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” header๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

 

Log์„ค์ •

 - NONE : ๋กœ๊น…ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.(DEFAULT) 

 - BASIC : Request Method์™€ URL, Reponse ์ƒํƒœ ์ฝ”๋“œ ๋ฐ ์‹คํ–‰ ์‹œ๊ฐ„์„ ๋กœ๊น…ํ•ฉ๋‹ˆ๋‹ค.

 - HEADERS : Request, Response Header + BASIC ์ •๋ณด๋ฅผ ๋กœ๊น…ํ•ฉ๋‹ˆ๋‹ค,

 - FULL : Request, Response์˜ Header, Body, ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๊น…ํ•ฉ๋‹ˆ๋‹ค.


Service ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ

FeignClient ๋ฅผ ์ฃผ์ž…๋ฐ›์•„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์„œ๋น„์Šค ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

package com.liyo.example.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ebab.lsdtgl.client.EabFireSafetyClient;
import com.ebab.lsdtgl.dto.fireSafety.FireSafetyResponse;

@Service
public class LiyoService {

    @Autowired
    private LiyoServiceClient liyoServiceClient;

    public LiyoServiceResponse getLiyoServiceData(String id) {
        return liyoServiceClient.getLiyoServiceData(id);
    }
}

๊ฐœ๋ฐœ ํ™˜๊ฒฝ

  • Frontend
    • Framework: Vue 3
    • Build Tool: Vite
  • Backend
    • Framework: Spring Boot 3.2.2
    • Language: Java
    • Gateway: Spring Cloud Gateway
  • Database
    • DBMS: MS SQL Server
    • Container: Docker
  • Development Tools
    • IDE: Visual Studio Code (VSCode)
๋ฐ˜์‘ํ˜•