0628 - 0704


# 0628 - 0704

# 0628 - ๋ฆฌ๋ˆ…์Šค ๋„คํŠธ์›Œํฌ ๊ด€๋ จ

# ๋ผ์šฐํŒ…์„ค์ •

  • ์ž„์‹œ ์ ์šฉ
$ route add -net ์•„์ดํ”ผ netmask ๋„ท๋งˆ์Šคํฌ dev ์žฅ์น˜๋ช…
  • ์˜๊ตฌ ์ ์šฉ
$ vi /etc/sysconfig/network-scripts/route-์žฅ์น˜๋ช…

// ์ž‘์„ฑ
ADDRESS0=์•„์ดํ”ผ
NETMASK0=๋„ท๋งˆ์Šคํฌ
GATEWAY0=๊ฒŒ์ดํŠธ์›จ์ด

// ์žฌ์‹œ์ž‘
$ systemctl restart network
  • ํ™•์ธ
$ route

# DNS ์„ค์ •

$ vi /etc/resolv.conf

// ์ž‘์„ฑ
nameserver ์•„์ดํ”ผ

// ์žฌ์‹œ์ž‘
$ systemctl restart network

# 0629 - ํฌ์›Œ๋“œ ํ”„๋ก์‹œ(forward proxy) ๋ฆฌ๋ฒ„์Šค ํ”„๋ก์‹œ(reverse proxy)์˜ ์ฐจ์ด

์•„ํŒŒ์น˜ ์›น์„œ๋ฒ„(apache web server)์—๋Š” mod_proxy๋ผ๋Š” ๋ชจ๋“ˆ์—์„œ forward proxy์™€ reverse proxy ๋‘ ๊ฐ€์ง€ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋ฉฐ nginx๋Š” ํ•„์š”ํ•œ ๊ธฐ๋Šฅ๋งŒ ์ œ๊ณตํ•˜๋Š” ๊ณ ์„ฑ๋Šฅ ์›น ์„œ๋ฒ„์— ๋งž๊ฒŒ reverse proxy ๊ธฐ๋Šฅ๋งŒ ์ œ๊ณตํ•œ๋‹ค.

# Forward Proxy

์‚ฌ์šฉ์ž <=> ํฌ์›Œ๋“œ ํ”„๋ก์‹œ ์„œ๋ฒ„ <=> ์ธํ„ฐ๋„ท <=> ์„œ๋ฒ„

ํฌ์›Œ๋“œ ํ”„๋ก์‹œ๋Š” ๋Œ€๊ฐœ ์บ์Š ๊ธฐ๋Šฅ์ด ์žˆ์œผ๋ฏ€๋กœ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ์ปจํ…์ธ ๋ผ๋ฉด ์›”๋“ฑํ•œ ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ •ํ•ด์ง„ ์‚ฌ์ดํŠธ๋งŒ ์—ฐ๊ฒฐํ•˜๊ฒŒ ์„ค์ •ํ•˜๋Š” ๋“ฑ ์›น ์‚ฌ์šฉ ํ™˜๊ฒฝ์„ ์ œํ•œ ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋ณด์•ˆ์ด ๋งค์šฐ ์ค‘์š”ํ•œ ๊ธฐ์—… ํ™˜๊ฒฝ๋“ฑ์—์„œ ์‚ฌ์šฉ.

# Reverse Proxy

์‚ฌ์šฉ์ž <=> ์ธํ„ฐ๋„ท <=> ๋ฆฌ๋ฒ„์Šค ํ”„๋ก์‹œ ์„œ๋ฒ„ <=> ์„œ๋ฒ„

๋ฆฌ๋ฒ„์Šค ํ”„๋ก์‹œ๋กœ ์›น ์„œ๋ฒ„๋ฅผ ์„ค์ •ํ•  ๊ฒฝ์šฐ ์‚ฌ์šฉ์ž๊ฐ€ ์›น ์„œ๋น„์Šค์— ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•˜๋ฉด ๋ฆฌ๋ฒ„์Šค ํ”„๋ก์‹œ๋Š” ์ด ์š”์ฒญ์„ ๋ฐ›์•„์„œ ๋‚ด๋ถ€ ์„œ๋ฒ„์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์€ ํ›„์— ์ด ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค.

  • ์žฅ์ 
  1. ๋ณด์•ˆ : DMZ์กด ์‚ฌ์šฉ์œผ๋กœ ๋ณด์•ˆ ๊ฐ•ํ™”
  2. ์†๋„์™€ ์•ˆ์ •์„ฑ : Cache Server๋‚˜ SSL ํ•˜๋“œ์›จ์–ด ๊ฐ€์†๊ธฐ ์—ฐ๋™ ๋“ฑ ์•„ํ‚คํ…์ฒ˜ ์ธก๋ฉด์—์„œ ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ํ•˜๊ธฐ์— ์šฉ์ด
  3. ์‹ ๋ขฐ์„ฑ ์ฆ๋Œ€ : ๋ฆฌ๋ฒ„์Šค ํ”„๋ก์‹œ๋ฅผ cluster๋กœ ๊ตฌ์„ฑํ•˜๋ฉด ๊ฐ€์šฉ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๊ณ  ์‚ฌ์šฉ์ž์ˆ˜์— ๋”ฐ๋ผ Web Server๋‚˜ WAS๋ฅผ ์œ ์—ฐํ•˜๊ฒŒ ๋Š˜๋ฆด ์ˆ˜ ์žˆ๋Š” ์žฅ์ , ๋ฆฌ๋ฒ„์Šค ํ”„๋ก์‹œ ์•ž์— L4๋‚˜ load balancer๋ฅผ ๋ถ™์ด๋ฉด Round Robin, Least connection ๋“ฑ ์ƒํ™ฉ์— ๋งž๋Š” ๋ถ„๋ฐฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ ์šฉํ•ด ์„œ๋น„์Šค ์‹ ๋ขฐ์„ฑ์„ ๋†’์ž„

# 0630 - ๋ฐฐํฌ ์ „๋žต์˜ ์ข…๋ฅ˜

# ๋กค๋ง(Rolling)

์ผ๋ฐ˜์ ์ธ ๋ฐฐํฌ๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, ๋‹จ์ˆœํ•˜๊ฒŒ ์„œ๋ฒ„๋ฅผ ๊ตฌ์„ฑํ•˜์—ฌ ๋ฐฐํฌํ•˜๋Š” ์ „๋žต์ด๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๊ตฌ ๋ฒ„์ „์—์„œ ์‹  ๋ฒ„์ „์œผ๋กœ ํŠธ๋ž˜ํ”ฝ์„ ์ ์ง„์ ์œผ๋กœ ์ „ํ™˜ํ•˜๋Š” ๋ฐฐํฌ์ด๋‹ค. ๊ด€๋ฆฌ๊ฐ€ ํŽธํ•˜์ง€๋งŒ, ๋ฐฐํฌ ์ค‘ ํ•œ์ชฝ ์ธ์Šคํ„ด์Šค์˜ ์ˆ˜๊ฐ€ ๊ฐ์†Œ๋˜๋ฏ€๋กœ ์„œ๋ฒ„ ์ฒ˜๋ฆฌ ์šฉ๋Ÿ‰์„ ๋ฏธ๋ฆฌ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค.

# ๋ธ”๋ฃจ ๊ทธ๋ฆฐ(Blue Green)

๊ตฌ ๋ฒ„์ „์„ ๋ธ”๋ฃจ, ์‹  ๋ฒ„์ „์„ ๊ทธ๋ฆฐ์ด๋ผ๊ณ  ํ•ด์„œ ๋ถ™์—ฌ์ง„ ์ด๋ฆ„์ด๋‹ค. ์‹  ๋ฒ„์ „์„ ๋ฐฐํฌํ•˜๊ณ  ์ผ์ œํžˆ ์ „ํ™˜ํ•˜์—ฌ ๋ชจ๋“  ์—ฐ๊ฒฐ์„ ์‹  ๋ฒ„์ „์„ ๋ฐ”๋ผ๋ณด๊ฒŒ ํ•˜๋Š” ์ „๋žต์ด๋‹ค. ๊ตฌ ๋ฒ„์ „, ์‹  ๋ฒ„์ „ ์„œ๋ฒ„๋ฅผ ๋™์‹œ์— ๋‚˜๋ž€ํžˆ ๊ตฌ์„ฑํ•˜์—ฌ ๋ฐฐํฌ ์‹œ์ ์— ํŠธ๋ž˜ํ”ฝ์ด ์ผ์ œํžˆ ์ „ํ™˜๋œ๋‹ค. ๋น ๋ฅธ ๋กค๋ฐฑ์ด ๊ฐ€๋Šฅํ•˜๊ณ , ์šด์˜ํ™˜๊ฒฝ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๊ณ  ์‹ค์ œ ์„œ๋น„์Šค ํ™˜๊ฒฝ์œผ๋กœ ์‹  ๋ฒ„์ „ ํ…Œ์ŠคํŠธ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋‹จ ์ด๋Ÿฐ ๊ตฌ์„ฑ์€ ์‹œ์Šคํ…œ ์ž์›์ด ๋‘๋ฐฐ๋กœ ํ•„์š”ํ•˜์—ฌ ๋น„์šฉ์ด ๋” ๋งŽ์ด ๋ฐœ์ƒํ•œ๋‹ค.

# ์นด๋‚˜๋ฆฌ(Canary)

์นด๋‚˜๋ฆฌ ๋ฐฐํฌ์— ๋Œ€ํ•œ ์„ค๋ช…์— ์•ž์„œ ์นด๋‚˜๋ฆฌ์— ๋Œ€ํ•œ ์–ด์›์„ ์•Œ์•„๋ณด์ž. Canary์˜ ์‚ฌ์ „์  ์˜๋ฏธ์—๋Š” ์นด๋‚˜๋ฆฌ์•„๋ผ๋Š” ์ƒˆ์ด๋‹ค. ์นด๋‚˜๋ฆฌ์•„๋Š” ์œ ๋…๊ฐ€์Šค์— ๊ต‰์žฅํžˆ ๋ฏผ๊ฐํ•œ ๋™๋ฌผ๋กœ ์„ํƒ„ ๊ด‘์‚ฐ์—์„œ ์œ ๋…๊ฐ€์Šค ๋ˆ„์ถœ์˜ ์œ„ํ—˜์„ ์•Œ๋ฆฌ๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉ๋˜์–ด์™”๋‹ค. ๋ฏธ๋ฆฌ ์œ„ํ—˜์„ ๊ฐ์ง€ํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค.
์นด๋‚˜๋ฆฌ ๋ฐฐํฌ๋Š” ์œ„ํ—˜์„ ๋น ๋ฅด๊ฒŒ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฐํฌ ์ „๋žต์ด๋‹ค. ์ง€์ •ํ•œ ์„œ๋ฒ„ ๋˜๋Š” ํŠน์ • user์—๊ฒŒ๋งŒ ๋ฐฐํฌํ–ˆ๋‹ค๊ฐ€ ์ •์ƒ์ ์ด๋ฉด ์ „์ฒด๋ฅผ ๋ฐฐํฌํ•œ๋‹ค. ์„œ๋ฒ„์˜ ํŠธ๋ž˜ํ”ฝ ์ผ๋ถ€๋ฅผ ์‹  ๋ฒ„์ „์œผ๋กœ ๋ถ„์‚ฐํ•˜์—ฌ ์˜ค๋ฅ˜ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ์ „๋žต์€ A/B ํ…Œ์ŠคํŠธ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์„ฑ๋Šฅ ๋ชจ๋‹ˆํ„ฐ๋ง์— ์œ ์šฉํ•˜๋‹ค. ํŠธ๋ž˜ํ”ฝ์„ ๋ถ„์‚ฐ์‹œํ‚ฌ ๋•Œ๋Š” ๋ผ์šฐํŒ…์„ ๋žœ๋ค ํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๊ณ , ์‚ฌ์šฉ์ž๋กœ ๋ถ„๋ฅ˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.


# 0701 - L4 ๋กœ๋“œ๋ฐธ๋Ÿฐ์„œ vs L7 ๋กœ๋“œ๋ฐธ๋Ÿฐ์„œ

# L4(Transport Layer)?

์ฃผ๋กœ Round Robin ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉฐ, ๋„คํŠธ์›Œํฌ ๊ณ„์ธต(IP, IPX)์ด๋‚˜ ํŠธ๋žœ์ŠคํฌํŠธ ๊ณ„์ธต(TCP, UDP)์˜ ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋กœ๋“œ๋ฅผ ๋ถ„์‚ฐ์‹œํ‚จ๋‹ค.

# L7(Application Layer)?

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ณ„์ธต(HTTP, FTP, SMTP)์—์„œ ๋กœ๋“œ๋ฅผ ๋ถ„์‚ฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— HTTP ํ—ค๋”, ์ฟ ํ‚ค ๋“ฑ๊ณผ ๊ฐ™์€ ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์„ ๊ธฐ์ค€์œผ๋กœ ํŠน์ • ์„œ๋ฒ„์— ํŠธ๋ž˜ํ”ฝ ๋ถ„์‚ฐํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
์‰ฝ๊ฒŒ ๋งํ•ด ํŒจํ‚ท์˜ ๋‚ด์šฉ์„ ํ™•์ธํ•˜๊ณ  ๊ทธ ๋‚ด์šฉ์— ๋”ฐ๋ผ ๋กœ๋“œ๋ฅผ ํŠน์ • ์„œ๋ฒ„์— ๋ถ„๋ฐฐํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ด๋‹ค. URL์— ๋”ฐ๋ผ ๋ถ€ํ•˜๋ฅผ ๋ถ„์‚ฐ์‹œํ‚ค๊ฑฐ๋‚˜, HTTP ํ—ค๋”์˜ ์ฟ ํ‚ค๊ฐ’์— ๋”ฐ๋ผ ๋ถ€ํ•˜๋ฅผ ๋ถ„์‚ฌํ•˜๋Š” ๋“ฑ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์„ ๋ณด๋‹ค ์„ธ๋ถ„ํ™”ํ•ด ์„œ๋ฒ„์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ L7 ๋กœ๋“œ๋ฐธ๋Ÿฌ์„œ์˜ ๊ฒฝ์šฐ ํŠน์ •ํ•œ ํŒจํ„ด์„ ์ง€๋‹Œ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ๊ฐ์ง€ํ•ด ๋„คํŠธ์›Œํฌ๋ฅผ ๋ณดํ˜ธํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, Dos/DDoS์™€ ๊ฐ™์€ ๋น„์ •์ƒ์ ์ธ ํŠธ๋ž˜ํ”ฝ์„ ํ•„ํ„ฐ๋งํ•  ์ˆ˜ ์žˆ์–ด ๋„คํŠธ์›Œํฌ ๋ณด์•ˆ๋ถ„์•ผ์—์„œ๋„ ํ™œ์šฉ๋œ๋‹ค.

# ๋น„๊ต

image

# ๋กœ๋“œ๋ฐธ๋Ÿฐ์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜?

  • Round Robin(์ˆœ์ฐจ๋ฐฉ์‹)
    : ์š”์ฒญ์„ ์ˆœ์„œ๋Œ€๋กœ ๊ฐ ์„œ๋ฒ„์— ๊ท ๋“ฑํ•˜๊ฒŒ ๋ถ„๋ฐฐํ•˜๋Š” ๋ฐฉ์‹
    ์„œ๋ฒ„ ์ปค๋„ฅ์…˜ ์ˆ˜๋‚˜ ์‘๋‹ต์‹œ๊ฐ„์— ์ƒ๊ด€์—†์ด ๋ชจ๋“  ์„œ๋ฒ„๋ฅผ ๋™์ผํ•˜๊ฒŒ ์ฒ˜๋ฆฌ, ๋‹ค๋ฅธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋น„ํ•ด์„œ ๊ฐ€์žฅ ๋น ๋ฅด๋‹ค.
  • IP ํ•ด์‹œ ๋ฐฉ์‹(IP Hash Method)
    : ํด๋ผ์ด์–ธํŠธ์˜ IP ์ฃผ์†Œ๋ฅผ ํŠน์ • ์„œ๋ฒ„๋กœ ๋งคํ•‘ํ•˜์—ฌ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹, ์‚ฌ์šฉ์ž์˜ IP๋ฅผ ํ•ด์‹ฑํ•ด ๋กœ๋“œ๋ฅผ ๋ถ„๋ฐฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ์ž๊ฐ€ ํ•ญ์ƒ ๋™์ผํ•œ ์„œ๋ฒ„๋กœ ์—ฐ๊ฒฐ๋˜๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•œ๋‹ค.
  • Least Connection(์ตœ์†Œ์ ‘์†๋ฐฉ์‹)
    : ์„œ๋ฒ„์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” Connection ๊ฐœ์ˆ˜๋งŒ ๊ฐ–๊ณ  ๋‹จ์ˆœ๋น„๊ตํ•˜์—ฌ ๊ฐ€์žฅ ์ ์€๊ณณ์œผ๋กœ ์—ฐ๊ฒฐ
  • Weighted Least Connections(๊ฐ€์ค‘์น˜ ์ตœ์†Œ์ ‘์†๋ฐฉ์‹)
    : ์„œ๋ฒ„์— ๋ถ€์—ฌ๋œ Weight ๊ฐ’์„ ๊ธฐ๋ฐ˜์œผ๋กœ Connection ์ˆ˜์˜ ๊ฐœ์ˆ˜์™€ ๊ฐ™์ด ๊ณ ๋ คํ•˜์—ฌ ํ• ๋‹น
  • Fastest Response Time(์‘๋‹ต์‹œ๊ฐ„๋ฐฉ์‹)
    : ๊ฐ€์žฅ ๋นจ๋ฆฌ ์‘๋‹ตํ•˜๋Š” ์„œ๋ฒ„์— ์ด์šฉ์ž ์š”๊ตฌ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•. ์‘๋‹ต์‹œ๊ฐ„์€ ๊ฐ ์„œ๋ฒ„๊ฐ€ ํŒจํ‚ท ํ˜•ํƒœ์˜ ์š”๊ตฌ๋ฅผ ์†ก์ˆ˜์‹ ํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ์ธก์ •ํ•œ ๊ฒƒ์ด๋‹ค.
  • Adaptive(์ตœ์†Œ๋Œ€๊ธฐ๋ฐฉ์‹)
    : Open ๋˜๋Š” Pending(๊ณ„๋ฅ˜์ค‘์ธ) ์ปค๋„ฅ์…˜์„ ์ ๊ฒŒ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์„œ๋ฒ„๋กœ ๋„คํŠธ์› ์ปค๋„ฅ์…˜ ๋ฐฉํ–ฅ์„ ์ง€์ •ํ•œ๋‹ค. Pending ์ปค๋„ฅ์…˜์€ Full TCP Handshake๋ฅผ ์™„์„ฑํ•˜์ง€ ์•Š์€ ๊ฒƒ์œผ๋กœ, ์ด๊ฒƒ์€ ์ดˆ๋‹น ํด๋ผ์ด์–ธํŠธ Thread์˜ ์ˆ˜๊ฐ€ ์ฆ๊ฐ€ํ•  ๋•Œ ๋”์šฑ ์ž˜ ์ˆ˜ํ–‰๋œ๋‹ค.

# 0703 - ์ž๋ฐ” ๋žŒ๋‹ค์‹

# ๋žŒ๋‹ค ํ‘œํ˜„์‹(Lambda Expressions)

# ๋ž€๋‹ค์‹์ด๋ž€?

์‹๋ณ„์ž ์—†์ด ์‹คํ–‰๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜

  • ๋ฉ”์†Œ๋“œ๋ฅผ ํ•˜๋‚˜์˜ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
  • ๋žŒ๋‹ค์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด return์ด ์—†์–ด์ง€๋ฏ€๋กœ ๋žŒ๋‹ค์‹์„ anonymous function(์ต๋ช… ํ•จ์ˆ˜)์ด๋ผ๊ณ ๋„ ํ•œ๋‹ค.

# ๋žŒ๋‹ค์‹์˜ ์žฅ๋‹จ์ 

์žฅ์ 

  • ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
  • ๊ฐ€๋…์„ฑ์ด ํ–ฅ์ƒ๋œ๋‹ค.
  • ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œํ™˜๊ฒฝ์—์„œ ์šฉ์ดํ•˜๋‹ค.
  • ํ•จ์ˆ˜๋ฅผ ๋งŒ๋‹ค๋Š” ๊ณผ์ • ์—†์ด ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๊ธฐ์— ์ƒ์‚ฐ์„ฑ์ด ๋†’์•„์ง„๋‹ค.

๋‹จ์ 

  • ๋žŒ๋‹ค๋กœ ์ธํ•œ ๋ฌด๋ช…ํ•จ์ˆ˜๋Š” ์žฌ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•œ๋‹ค.
  • ๋””๋ฒ„๊น…์ด ๋งŽ์ด ๊นŒ๋‹ค๋ก๋‹ค.
  • ๋žŒ๋‹ค๋ฅผ ๋ฌด๋ถ„๋ณ„ํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ํด๋ฆฐํ•˜์ง€ ๋ชปํ•˜๋‹ค.
  • ์žฌ๊ท€๋กœ ๋งŒ๋“ค ๊ฒฝ์šฐ ๋ถ€์ ํ•ฉํ•˜๋‹ค.

# ๋žŒ๋‹ค์‹ ์‚ฌ์šฉ๋ฒ•

(๋งค๊ฐœ๋ณ€์ˆ˜) -> ํ‘œํ˜„ ๋ฐ”๋””
(๋งค๊ฐœ๋ณ€์ˆ˜) -> { ํ‘œํ˜„ ๋ฐ”๋”” }
() -> { ํ‘œํ˜„๋ฐ”๋”” }
() -> ํ‘œํ˜„ ๋ฐ”๋””
  • ๊ธฐ๋ณธ ์˜ˆ์ œ
    LeagueOfLegend, Setting
@FunctionalInterface
interface Setting {
    void setup();
}

public class LeagueOfLegend {
    public void running(Setting setting) {
        setting.setup();
        System.out.println("LeagueOfLegend running");
    }
}

lambdaSample

public class lambdaSample {
    public static void main(String[] args) {
        LegueOfLegend leagueOfLegend = new LegueOfLegend();
        leagueOfLegend.running(new Setting() {
            @Override
            public void setup() {
                System.out.println("leagueOfLegend is setup");
            }
        });
    }
}

=> ๊ฒฐ๊ณผ

leagueOfLegend is setup
LeagueOfLegend running
  • ๋žŒ๋‹ค์˜ˆ์ œ lambdaSample
public class lambdaSample {
    public static void main(String[] args) {
        LeagueOfLegend leagueOfLegenda = new LeagueOfLegend();
        leagueOfLegend.running(() -> System.out.println("leagueOfLegend is setup"));
    }
}

=> ๊ฒฐ๊ณผ

leagueOfLegend is setup
LeagueOfLegend running

# @FunctionalInterface

  • Java8 ์ด์ „์—์„œ๋Š” ์ž๋ฐ”์—์„œ ๊ฐ’์ด๋‚˜ ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๋ฅผ ๋ณ€์ˆ˜์— ๋‹ด์•„๋‘๋Š” ๊ฒƒ์€ ํ—ˆ์šฉ๋˜์ง€ ์•Š์•˜๋‹ค.
  • Java8์—์„œ ๋žŒ๋‹ค์‹์ด ์ถ”๊ฐ€๋˜๊ณ  ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜์— ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๋ฅผ ๋งคํ•‘ํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ์ œ

@FunctionalInterface
public interface Funtional {
    public int calc(int a, int b);
}
  • 1๊ฐœ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์ง„ ๊ฒƒ์ด Functional interface ์ด๋‹ค. Single Abstract Method(SAM)์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๊ธฐ๋„ํ•œ๋‹ค.
  • @FunctionalInterface๋ฅผ ์ง€์ •ํ•˜๊ฒŒ๋˜๋ฉด ์ด ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค๋ผ๊ณ  ๋ช…์‹œ๋ฅผ ํ•ด์ฃผ๊ณ  ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ SAM ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.

์˜ˆ์ œ2 - Various ์‚ฌ์šฉ

Functional add = (int a, int b) -> {return a + b};
Functional add1 = (int a, int b) -> a + b;
Functional add2 = Integer::sum;
  • ์ด ์˜ˆ์ œ๋Š” ๋ชจ๋‘ ๊ฒฐ๊ณผ๋Š” ๊ฐ™๋‹ค.
int result = add.calc(1, 1) + add1.calc(2, 2) + add2.calc(3, 3);
// ๊ฒฐ๊ณผ 12
Last update: July 22, 2021 19:09
Contributors: jaesungahn91