Integration with Feign Client - TheOpenCloudEngine/uEngine-cloud GitHub Wiki

RIntegration with Feign Client

์—ฌ๋Ÿฌ ์„œ๋น„์Šค๋กœ ๋‚˜๋‰˜์–ด์ง„ ๋งˆ์ดํฌ๋กœ ์„œ๋น„์Šค๋“ค์ด ํ•˜๋‚˜์˜ ๊ฑฐ๋Œ€ํ•œ ์„œ๋น„์Šค์ฒ˜๋Ÿผ ํ†ตํ•ฉ๋˜๋Š” ๊ฒƒ์„ Integration ์ด๋ผ๊ณ  ํ•œ๋‹ค.

FeignClient

์—ฌ๋Ÿฌ ๋งˆ์ดํฌ๋กœ ์„œ๋น„์Šค๋“ค์ด ์กด์žฌ๋ฅผ ํ•˜๋Š” ํ™˜๊ฒฝ์—์„œ
์ผ์ผ์ด ๋‹ค๋ฅธ ๋งˆ์ดํฌ๋กœ ์„œ๋น„์Šค๋“ค์˜ ์ฃผ์†Œ๋ฅผ ๋งค๋ฒˆ ์ฐพ์•„์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋น„ํšจ์œจ์ ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
์‹ฌ์ง€์–ด ์ฐธ์กฐํ•˜๋Š” ๋งˆ์ดํฌ๋กœ ์„œ๋น„์Šค์˜ uri ํŒจํ„ด์ด ๋ฐ”๋€Œ์—ˆ์„ ๊ฒฝ์šฐ, ์„œ๋น„์Šค ๋‚ด์˜ ๋ชจ๋“  ์ฐธ์กฐ uri๋ฅผ ์ฐพ์•„์„œ ๋ณ€ํ™˜ํ•ด์•ผํ•˜๋Š” ์ƒํ™ฉ์ด ๋ฒŒ์–ด์งˆ ์ˆ˜ ์žˆ๋‹ค.
ํ•˜์ง€๋งŒ Feign Client๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์ด ์ˆ˜๊ณ ๊ฐ€ ์ค„์–ด๋“ค๊ฒŒ ๋œ๋‹ค.

Feign Client๋Š” ๋‹จ์ˆœํžˆ Interface๋กœ์จ๋งŒ ์กด์žฌ๋ฅผ ํ•˜๋ฉฐ,
Interface์— @FeignClient(name="service_name") ์–ด๋…ธํ…Œ์ด์…˜์ด์„ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋œ๋‹ค.

FeignClient๋Š” Eureka์—์„œ service_name์ด๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„ ์„œ๋น„์Šค๋ฅผ ์ฐพ์•„์„œ ์ž๋™์œผ๋กœ ์—ฐ๊ฒฐ์„ ํ•œ๋‹ค.

๋˜ํ•œ FeignClient์˜ ๋ฉ”์†Œ๋“œ๋“ค์€ @RequestMapping("/uri_path") ์–ด๋…ธํ…Œ์ด์…˜์„ ํ†ตํ•ด ๋ฉ”์†Œ๋“œํ˜ธ์ถœ์ด ๋˜๋ฉด ์„ ์–ธ๋œ ์„œ๋น„์Šค์˜ uri๋ฅผ ํ˜ธ์ถœ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

service_name ์„œ๋น„์Šค์—์„œ /uri_path๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.
์ฆ‰, ๊ฐœ๋ฐœ์ž๋Š” ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” API๊ฐ€ ์–ด๋””์— ์žˆ๋˜์ง€ ๋กœ์ปฌ ๋ฉ”์†Œ๋“œ ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ๋œ๋‹ค.

์ด์ œ ์˜ˆ์ œ๋ฅผ ํ†ตํ•˜์—ฌ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€ ์‚ดํŽด๋ณธ๋‹ค.
Spring HATEOAS API ์—์„œ SharedCalendarServiceImpl ์„ ์‚ดํŽด ๋ณธ ๊ฒƒ์„ ๊ธฐ์–ตํ•  ๊ฒƒ์ด๋‹ค.
SharedCalendarServiceImpl์€ SharedCalendarService ๋ฅผ implements ํ•œ ๊ฒƒ์ธ๋‹ค.

SharedCalendarService.java

@FeignClient("calendar")
public interface SharedCalendarService {
    @RequestMapping(
        method = {RequestMethod.GET},
        path = {"/calendar/{instructorId}/{date}"}
    )
    Resources getSchedules(@PathVariable("instructorId") Long var1, @PathVariable("date") String var2);
}

์•ž์„œ ์„ค๋ช… ํ•œ ๋‚ด์šฉ์ฒ˜๋Ÿผ SharedCalendarService๋Š” interface ์ด๊ณ ,
@FeignClient("calendar") ๋ผ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ™์˜€๋‹ค.
๊ทธ๋ฆฌ๊ณ  Impl ์—์„œ๋„ @RequestMapping ์ด ์žˆ์ง€๋งŒ ์—ฌ๊ธฐ์—์„œ๋„ @RequestMapping ์ด ์กด์žฌํ•œ๋‹ค.

์‚ฌ์‹ค interface์—์„œ @RequestMapping์„ ๋ถ™์˜€์œผ๋ฉด Impl์—์„œ๋Š” @RequestMapping์„ ๋ถ™์ด๋ฉด ์•ˆ๋œ๋‹ค.
๋‹ค๋งŒ ์˜ˆ์ œ๋กœ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•˜์—ฌ SharedCalendarServiceImpl์—๋„ @RequestMapping์„ ๋ถ™์˜€๋‹ค.

์ด์ œ ์ด๋ ‡๊ฒŒ FeignClient๋กœ ์„ ์–ธํ•œ ์„œ๋น„์Šค๋ฅผ ๋‹ค๋ฅธ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค์—์„œ ํ˜ธ์ถœํ•˜์—ฌ ๋ณด๊ฒ ๋‹ค.
๋ณดํ†ต REST ๋ฐฉ์‹์œผ๋กœ ๋‹ค๋ฅธ ์„œ๋น„์Šค๋ฅผ APIํ˜•์‹์œผ๋กœ call์„ ํ•˜์—ฌ return data๋กœ ์ž‘์—…ํ•˜๋Š” ๊ฒƒ์ด
์ต์ˆ™ ํ• ์ˆ˜๋„ ์žˆ์ง€๋งŒ, FeignClient ๋งŒ์˜ ์žฅ์ ๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ํ†ตํ•˜์—ฌ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

https://github.com/uengine-oss/msa-tutorial-class-management-msa/blob/master/clazz/src/main/java/hello/Clazz.java

clazz/Clazz.java

@Entity
public class Clazz{
   // more
   
   private void checkAvailabilityAndSetInstructor() {
        if(getInstructor()!=null){
            SharedCalendarService sharedCalendarService = 
                Application.applicationContext.getBean(SharedCalendarService.class);
            for(ClazzDay clazzDay : getClazzDays()){
                ResourceSupport schedules = 
                    sharedCalendarService.getSchedules(getInstructor().getId(), clazzDay.getDate()
                    .toString());
            // more
            }
        }
   }
}

SharedCalendarService sharedCalendarService = Application.applicationContext.getBean(SharedCalendarService.class); ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ
๋‹ค๋ฅธ ์„œ๋ฒ„์— ์ž‘๋™ํ•˜๊ณ  ์žˆ๋Š” ๋งˆ์ดํฌ๋กœ ์„œ๋น„์Šค๋ฅผ ๊ฐ€์ ธ์™€์„œ,
sharedCalendarService.getSchedules ๋ฅผ local ๊ฐ์ฒด์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

โš ๏ธ **GitHub.com Fallback** โš ๏ธ