๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
BackEnd๐ŸŒฑ/Spring

Actuator Refresh ๋ฐ˜์˜ ์•ˆ๋˜๋Š” ํ˜„์ƒ ํ•ด๊ฒฐ

by dkswnkk 2022. 11. 12.

๋ฌธ์ œ

ํ”„๋กœ์ ํŠธ ๋ฒ„์ „

  • Spring Cloud Config Server: Spring Boot 2.7.4
  • ๊ฐ๊ฐ์˜ ์„œ๋น„์Šค ์„œ๋ฒ„๋“ค: Spring Boot 2.7.4
  • Spring Discovery Server: Spring Boot 2.7.4
  • Spring GateWay Server: Spring Boot 2.7.4

ํ˜„์žฌ ์ƒํ™ฉ์€ ์œ„์˜ ์ด๋ฏธ์ง€์ฒ˜๋Ÿผ Spring Cloud Conifg ์„œ๋ฒ„๋ฅผ ๋„์šด ํ›„ ๊ฐ๊ฐ์˜ Spring Boot Application์—์„œ ํ•ด๋‹น Config ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์„ค์ • ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋•Œ Config Server์˜ ๋‚ด์šฉ์ด ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ ์ˆ˜์ •๋œ ๋‚ด์šฉ์„ ๊ฐ๊ฐ์˜ Spring Boot Application์— ๋ฐ˜์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. ์„œ๋ฒ„ ์žฌ๊ธฐ๋™
  2. Actuator Refresh
  3. Spring Cloud Bus 

์ €๋Š” ์ด ์ค‘์—์„œ 2๋ฒˆ ๋ฐฉ๋ฒ•์„ ํ† ๋Œ€๋กœ ํ•™์Šต์„ ํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.

๋จผ์ € ๊ฐ๊ฐ์˜ ์ด๋ฏธ์ง€๋Š” Config Server์— ๋“ฑ๋ก๋œ Config ๊ฐ’, Config Server์˜ ์„ธํŒ…, User Service Server์˜ ์„ธํŒ…์ž…๋‹ˆ๋‹ค.

config server yml ์„ค์ • ๊ฐ’
Config Server ์„ธํŒ…
User Service ์„ธํŒ…

๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ๋ฐ”๋ผ๋ณด๋Š” user-service ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ํ•ด๋‹น ๊ฐ’๋“ค์„ ์ž˜ ๋ถˆ๋Ÿฌ์˜ค๋Š”์ง€ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ controller๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

    @GetMapping("/health_check")
    public String status() {
        return String.format("It's Working is User Service "
                + ", port(local.server.port) = " + env.getProperty("local.server.port")
                + ", port(server.port) = " + env.getProperty("server.port")
                + ", token secret = " + env.getProperty("token.secret")
                + ", token expiration time = " + env.getProperty("token.expiration_time")
                + ", gateway ip = " + env.getProperty("gateway.ip")
        );
    }

/health_check ํ˜ธ์ถœ

๋ณด์ด๋Š” ๋ฐ”์™€ ๊ฐ™์ด user-service server์˜ health_check apiํ˜ธ์ถœ์„ ํ†ตํ•ด config-server์˜ ๊ฐ’์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ user-service์—์„œ ๊ฐ€์ ธ์™”๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ token.expiration_time์˜ ๊ฐ’์„ ๋ณ€๊ฒฝ์‹œ์ผœ์„œ ๋ฐ˜์˜์ด ๋˜๋Š”์ง€ ํ™•์ธํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

config server yml ์„ค์ • ๊ฐ’ ๋ณ€๊ฒฝ

config server token.expiration_time: 100000 -> 86400000๋กœ ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค.

git ๋“ฑ๋ก

 

spring cloud config server์—์„œ git์œผ๋กœ ํ•ด๋‹น ๊ฐ’์„ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๋•Œ๋ฌธ์—, ๋ณ€๊ฒฝ๋œ ๋‚ด์šฉ๋„ git์œผ๋กœ ๋ฐ˜์˜์‹œ์ผœ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

/actuator/refresh ํ˜ธ์ถœ

actuator/refresh ํ˜ธ์ถœ ์‹œ์ƒํƒœ ๊ฐ’์€ 200์œผ๋กœ ์„ฑ๊ณตํ–ˆ๋‹ค๊ณ  ๋‚˜์˜ต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์›๋ž˜๋ผ๋ฉด ๋ณ€๊ฒฝ๋œ ๊ฐ’์ธ  token.expiration_time์ด body์— ์ฐํ˜€์„œ ๋‚˜์™€์•ผ ํ•˜์ง€๋งŒ ๋‚˜์˜ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 

  

๋”๊ตฐ๋‹ค๋‚˜ Config Server์—์„œ๋Š” Could not merge remote for main remote: null์„ ๋‚ด๋ฑ‰์Šต๋‹ˆ๋‹ค.

 

์›์ธ ๋ฐ ํ•ด๊ฒฐ

์›์ธ์€ ๋ฐ”๋กœ Config Server๋ฅผ ๋ฐ”๋ผ๋ณด๋Š” ๊ฐ๊ฐ์˜ ์„œ๋น„์Šค ์ค‘ ํ•˜๋‚˜์ธ user service server์˜ ์„ธํŒ…๊ฐ’์ด ์›์ธ์ด์—ˆ์Šต๋‹ˆ๋‹ค.

User Service ์„ธํŒ…

์ €๋Š” ์œ„์™€ ๊ฐ™์ด User Service Server์˜ application.yml์—์„œ ์ง์ ‘์ ์œผ๋กœ config server๋ฅผ ์—ฐ๋™ํ–ˆ์Šต๋‹ˆ๋‹ค.

bootstrap.yml ์ƒ์„ฑ

ํ•˜์ง€๋งŒ ์œ„์™€ ๊ฐ™์ด application.yml์— ์ง์ ‘ ๋ช…์‹œํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ bootstrap.yml์— ๊ฐ’์„ ์ž‘์„ฑ ํ•œ ๋’ค application.yml์—์„œ ์ด bootstrap.yml์„ ๋ฐ”๋ผ๋ณด๋„๋ก ์„ค์ •ํ•˜๋ฉด ํ•ด๊ฒฐ์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

application.yml ์ˆ˜์ •

 

์žฌ์‹œ๋„

๋‹ค์‹œ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ๋ฐ˜์˜ํ•œ ๋’ค actuator/refresh๋ฅผ ํ˜ธ์ถœํ–ˆ์Šต๋‹ˆ๋‹ค.

/actuator/refresh ํ˜ธ์ถœ

์ด์ œ ์ •์ƒ์ ์œผ๋กœ ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ์žˆ๋Š” token.expiration_time์ด body๊ฐ’์œผ๋กœ ๋„˜์–ด์˜ต๋‹ˆ๋‹ค.

/health_check ํ˜ธ์ถœ

refresh ํ›„ /health_check๋ฅผ ํ˜ธ์ถœํ–ˆ์„ ๋•Œ ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ๋ฌด์‚ฌํžˆ ๋ฐ˜์˜๋˜์–ด ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ •๋ฆฌ

"Spring Boot 2.4 ๋ฒ„์ „ ์ดํ›„๋ถ€ํ„ฐ๋Š” bootstrap.yml ํŒŒ์ผ ๋Œ€์‹  spring.config.import ์„ค์ •์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋„๋ก ํ•˜๊ณ  ์žˆ๋‹ค."๋ผ๋Š” ๋ฌธ์žฅ์„ ์–ด๋””์„œ ๋ณด๊ฒŒ ๋˜์–ด ์ง์ ‘ application.yml์— cloud config๋ฅผ import ํ•ด์ฃผ์—ˆ์ง€๋งŒ ์˜คํžˆ๋ ค ํ•ด๋‹น ๋ฐฉ๋ฒ•์ด ๋…์ด ๋˜์—ˆ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ •ํ™•ํ•œ ์›์ธ์€ ์†”์งํžˆ ์ž˜ ๋ชจ๋ฅด๊ฒ ์œผ๋‚˜ bootstrap.yml์— ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด๊ฒฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ˜น์‹œ๋‚˜ ์•„์‹œ๋Š” ๋ถ„ ๊ณ„์‹œ๋ฉด ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๐Ÿฅน

๋Œ“๊ธ€