๐Ÿ“— Spring boot Actuator ์Šคํ”„๋ง ๋ถ€ํŠธ ์•ก์ถ”์—์ดํ„ฐ API + Spring Cloud๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์ œ

๊น€ํฌ๋งยท2023๋…„ 5์›” 3์ผ
1

๊ฐœ๋ฐœ์ผ์ง€

๋ชฉ๋ก ๋ณด๊ธฐ
12/17
post-thumbnail

๊ฐœ์š”

Spirng boot ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์—์„œ ๋ฐ์ดํ„ฐ๋“ค์ด ๋ณ€๊ฒฝ๋˜๊ฑฐ๋‚˜ yaml ์ด ๋ณ€๊ฒฝ๋˜์—ˆ์„๋•Œ ์„œ๋ฒ„๋ฅผ ์žฌ ๊ฐ€๋™์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ• ์ด์™ธ์— ๋ฐ์ดํ„ฐ ์ƒํƒœ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์„๊นŒ? ๋ฐ”๋กœ Spring Actuator๋ผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์˜ค๋Š˜์€ ๊ทธ Spring Actuator์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ๋‹ค.

๐Ÿง Spring Actuator

Spring Actuator๋ž€ ๋ฌด์—‡์ผ๊นŒ?

Spring Actuator๋Š” ์Šคํ”„๋ง ๋ถ€ํŠธ ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ์ œ๊ณต๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ์„œ ์Šคํ”„๋ง ํ‘ธํŠธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ชจ๋‹ˆํ„ฐ๋ง์ด๋‚˜ ๋ฉ”ํŠธ๋ฆญ๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ HTTP์™€ JMX ์—”๋“œ ํฌ์ธํŠธ๋ฅผ ํ†ตํ•ด์„œ ์ œ๊ณตํ•œ๋‹ค.

๋ฉ”ํŠธ๋ฆญ์ด๋ž€?
์‹œ์Šคํ…œ, ํ”„๋กœ์„ธ์Šค, ์ œํ’ˆ ๋˜๋Š” ์„œ๋น„์Šค์˜ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์ธก์ • ํ•ญ๋ชฉ์ด๋‚˜ ์ง€ํ‘œ๋‹ค.

Spring Actuator๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋‚ด๋ถ€ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์–ด๋Š ์ •๋„๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ž‘๋™์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

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

๐Ÿ–ฅย  Spring Actuator API

์Šคํ”„๋ง ์•ก์ธ„์—์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•ด์•ผํ•œ๋‹ค.

implementation("org.springframework.boot:spring-boot-starter-actuator")

์Šคํ”„๋ง ์•ก์ธ„์—์ดํ„ฐ์˜ ์Šคํƒ€ํ„ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋นŒ๋“œ๋˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—ฌ๋Ÿฌ ์•ก์ธ„์—์ดํ„ฐ ์—”๋“œํฌ์ธํŠธ๋กœ api๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

[

๐Ÿ”จ Spring Cloud์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์ œ

spring cloud๋ฅผ ํ†ตํ•ด ๋‹ค๋ฅธ ์ €์žฅ์†Œ์— ์žˆ๋Š” yml๋ฅผ ์ฝ์–ด returnํ•˜๋Š” RestController Client Server์™€ ConfigServer๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  yml์˜ ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ–ˆ์„๋•Œ ์Šคํ”„๋ง ๋ถ€ํŠธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ๋„ ์„œ๋ฒ„๋ฅผ ์žฌ์‹คํ–‰ํ•˜์ง€์•Š๊ณ  ๋ฐ”๋กœ ๊ฐ’์„ ๋คํ”„ํ•˜๋„๋ก refresh๋ผ๋Š” api๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๊ฒ ๋‹ค.

๐Ÿ“ฑ Config Server

@SpringBootApplication
@EnableConfigServer
class ConfigServerApplication

fun main(args: Array<String>) {
    runApplication<DmsBackendApplication>(*args)
}
server:
  port: 8081

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/๋Œ€์ถฉymlํŒŒ์ผ์žˆ๋Š”๊ณณ # configtest-dev

๐Ÿ“ฑ Client Server

Controller

@RestController
class TestController(){
    @Value("test.value")
    private val configStr: String
    
    @GetMapping("/")
    fun test() = configStr
}

application.yml

server:
  port: 8082
  
spring:
  cloud:
     config:
       name: configtest-dev
       import: optional:configserver:http://localhost8081

configtest-dev

test:
  value: dev-test

์—ฌ๊ธฐ์„œ http://localhost:8082๋กœ GET ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด configtest-dev์˜ yml ์ •๋ณด ๊ฒฐ๊ณผ ๊ฐ’์„ ๋ฐ›์•„๋ณผ ์ˆ˜ ์žˆ๋‹ค. test.value = dev-test๋ผ๋Š” ์ •๋ณด๋„ ํ•จ๊ป˜ ๋ง์ด๋‹ค.

response: dev-test

์ž ์—ฌ๊ธฐ์„œ test.value ์˜ ๊ฐ’์„ dev-test-2๋กœ ๋ฐ”๊ฟ”์„œ ๋‹ค์‹œ GET์š”์ฒญ์„ ๋ณด๋‚ด๋ณธ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ? ์ˆ˜์ •์„ ํ•˜๋”๋ผ๋„ ๋˜‘๊ฐ™์ด dev-test์˜ ๊ฒฐ๊ณผ๊ฐ’์ด ๋ฐ˜ํ™˜์ด๋œ๋‹ค. ๋ฐ์ดํ„ฐ ์ƒํƒœ๊ฐ€ ๋กœ๋”ฉ๋˜์ง€ ์•Š์•„์„œ ์ด๋Ÿฐ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.

์—ฌ๊ธฐ์„œ ์Šคํ”„๋ง ์•ก์ธ„์—์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด refresh ํ•ด๋ณด๊ฒ ๋‹ค.

application.yml์— ๋‹ค์Œ๊ณผ๊ฐ™์€ ์ •๋ณด๋“ค์„ ์ถ”๊ฐ€ํ•ด๋ณด์ž

<-- .... -->

management:
  endpoints:
    web:
      exposure:
        include : refresh # ์•ก์ธ„์—์ดํ„ฐ ๊ธฐ๋Šฅ์ค‘ refresh๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค * ๋ฅผ ๋‹ฌ๋ฉด ๋‹ค ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๋œป

๊ทธ๋ฆฌ๊ณ  TestController์— ๋‹ค์Œ๊ณผ๊ฐ™์€ ์–ด๋…ธํ…Œ์ด์…˜์„ ๋‹ฌ์•„์ค€๋‹ค.

@RestController
@RefreshScope
class TestController(){
    @Value("test.value")
    private val configStr: String
    
    @GetMapping("/")
    fun test() = configStr
}

์ž ์ด์ œ ๋‹ค์‹œ dev-test๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต๊ตฌ์‹œํ‚ค๊ณ  GET์„ ๋ณด๋‚ด๋ณด๋ฉด ๋˜‘๊ฐ™์ด test.value: dev-test๋กœ ์‘๋‹ต์ด ์˜ค๊ฒŒ๋œ๋‹ค.

dev-test-2๋กœ ๋ฐ”๊พธ๊ณ  GET์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด ๋˜‘๊ฐ™์ด ๊ฐ’์ด ๋ฐ”๋€Œ์ง€ ์•Š์€ ์ƒํƒœ๋กœ dev-test์‘๋‹ต์ด ์˜ค๊ฒŒ๋˜๋Š”๋ฐ ์—ฌ๊ธฐ์„œย 

http://localhost:8082/refresh๋กœ POST ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  ๋‹ค์‹œํ•œ๋ฒˆ http://localhost:8082์— GET์š”์ฒญ์„ ๋ณด๋‚ด์ฃผ๋ฉด

dev-test-2๋ผ๋Š” ๋ฐ์ดํ„ฐ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฆฌํ”„๋ ˆ์‰ฌ๋˜๋Š”๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

response : dev-test-2

๐Ÿ‘‹ ๋งˆ๋ฌด๋ฆฌ ์ด์ด

์ด๋ ‡๊ฒŒ ์Šคํ”„๋ง ์•ก์ธ„์—์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ ๋ฆฌํ”„๋ ˆ์‰ฌ๋ฅผ ํ•ด๋ดค๋‹ค. ๋ฆฌํ”„๋ ˆ์‰ฌ ์ด์™ธ์—๋„ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ๋“ค์ด ์žˆ์œผ๋‹ˆ ํ•œ๋ฒˆ ๊ณต๋ถ€ํ•ด๋ณด๊ณ  ์‚ฌ์šฉํ•ด๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค. ํฌ์ŠคํŒ… ์ฝ์–ด์ฃผ์‹ ๋ถ„๋“ค์—๊ฒŒ ๊ฐ์‚ฌ์˜ ๋ง์„ ์ „ํ•˜๋ฉฐ Spring Cloud์™€ MSA์— ๋Œ€ํ•ด์„œ๋„ ๋”์šฑ ๊ณต๋ถ€ํ•  ์˜ˆ์ •์ด๋‹ค.

profile
์†Œํ”„ํŠธ์›จ์–ด ์—”์ง€๋‹ˆ์–ด, ๊น€ํฌ๋ง์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€