0. Spring Cloud Config Server
MSA์ ๊ฐ์ ๋ถ์ฐ ์์คํ ์์ ๊ฐ ์๋ฒ๋ค์ ๊ตฌ์ฑ์ ํ์ํ ์ค์ ๋ค์ ์ธ๋ถ ์์คํ ์์ ํ๋ฒ์ ๊ด๋ฆฌํ ์ ์๋ค.
Spring Boot ๊ฐ์ ๊ฒฝ์ฐ๋, ์ค์ ํ์ผ์ธ application.yml ๋๋ propertiesํ์ผ์ด ๊ทธ ๋์์ด ๋๊ฒ ๋ค.
Spring Cloud์ Config Server๋ฅผ ๊ธฐ๋ํ์ฌ ์ค์ ํ์ผ๋ค์ ํ๊บผ๋ฒ์ ๋ฌถ์ด ์ฌ์ฉํ๋ค๋ฉด, ํ ์ ์ฅ์์์ ๊ด๋ฆฌํ ์ ์๊ณ ,
์ค์ ์ ๋ณด๋ฅผ ๋ฐ๊พธ๋๋ผ๋ ๋ค๋ฅธ ์๋น์ค๋ค์ ๋ค์ ๋น๋ํ์ง ์์๋ ๋๋ค.
์๋น์ค๋ฅผ ๊ฐ๋ฐํ๋ ๊ฒฝ์ฐ, DEV์๋ฒ์ PROD ์๋ฒ๋ฅผ ๋ถ๋ฆฌํด์ ๊ฐ๋ฐํ๊ธฐ๋ ํ๋๋ฐ,
๊ฐ ์๋ฒ๋ค ๋ง๋ค ์ฌ์ฉํ๋ ์ค์ ์ ๋ณด๊ฐ ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ์๋ฒ์ ๋ง์ถฐ์ ๊ธฐ๋ํ ์ ์๊ฒ ๋ค.
1. Actuator
์์์ Config Server๋ฅผ ์ด์ฉํ๋ฉด ์ค์ ์ ๋ณด๋ค์ด ๋ณ๊ฒฝ๋์ด๋ ์๋ฒ๋ฅผ ๋ค์ ๋น๋ํ์ง ์์๋ ๋๋ค๋ ์ฅ์ ์ด ์๋ค๊ณ ํ๋ค.
๋ณดํต์ Spring Boot์ ์ํ๋ฅผ ๋ชจ๋ํฐ๋ง ํ ์ ์๋ ํด์ธ Actuator๋ฅผ Refreshํจ์ผ๋ก์จ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ค.
์๋ ๊ณต์ ๋ฌธ์์์ ์ฌ๋ฌ ์ค๋ํฌ์ธํธ๋ค์ ํ์ธํ ์ ์๋ค.
https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html
Actuator๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด, client ์๋ฒ์ dependency๋ฅผ ์ถ๊ฐํ์.
implementation 'org.springframework.boot:spring-boot-starter-actuator'
์ด์ client ์๋ฒ์ application.yml์ ๋ค์์ ์ถ๊ฐํ๋ค.
- application.ym
management:
endpoints:
web:
exposure:
include: refresh
actuator์์ ์ด์ด๋ ์ค๋ํฌ์ธํธ๋ฅผ ์ค์ ํด์ค ์ ์๋ค. ์ผ๋จ์ refresh๋ง ์ค์ ํด๋๊ณ , ๋์ค์ ํ์ํ ๋ ์ถ๊ฐํ๋ฉด ๋๊ฒ ๋ค.
๋ง์ดํฌ๋ก ์๋น์ค์์ ์ด๋ฌํ Actuator๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด gradle์ ๋ค์๊ณผ ๊ฐ์ด ์์กด์ฑ์ ์ฃผ์ ํ๋ฉด ๋๋ค.
dependencies {
//Spring Boot Actuator
implementation 'org.springframework.boot:spring-boot-starter-actuator'
}
ํ๋ก์ ํธ์ Actuator๋ฅผ ์ถ๊ฐํ๋ค๋ฉด, ํ๋ก์ ํธ์์ Security ์ ์ฑ ์ ๊ฐ์ง๊ณ ์๋ค๋ฉด
actuator์ ๊ด๋ จ๋ uri๋ค์ ๋ชจ๋ permitAll ํด์ฃผ์ด์ผ ํ๋ค.
- SecurityConfig.java์ ์ผ๋ถ
//...
http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/actuator/**").permitAll()
.anyRequest().authenticated()
);
//...
์ด๋ก์จ actuator์ ๋ํ ์์ฒญ์ ๋ชจ๋ ๋ก๊ทธ์ธ์ ๊ฑฐ์น์ง ์์๋ ๋ ์ ์๋๋ก ํ์ฉํ๋ค.
ํด๋น refresh ๊ธฐ๋ฅ์ ์ด์ฉํ๋ ค๋ฉด "IP์ฃผ์/actuator/refresh"๋ก POST์์ฒญ์ ๋ณด๋ด๋ฉด ๋๋ค.
2. profile์ ์ด์ฉํ ์ค์ ํ์ผ ์ด๋ฆ
์ฌ๋ฌ ์ค์ ํ์ผ๋ค์ ์ฌ์ฉํ ์ ์๋ ์ฅ์ ์ค ํ๋๋, ์์์ ์ธ๊ธํ๋ dev์๋ฒ์ prod์๋ฒ๋ฅผ ๋ถ๋ฆฌํด์ ๊ฐ๋ฐํ ๋, ๊ฐ ์ค์ ์ ๋ณด๋ค์ ์ฌ๋ฌ ํ์ผ๋ก ๋๋๊ธฐ๋ ํ๋ค.
์ด๋ ๊ฒ ๋๋์ด์ง ์ค์ ํ์ผ๋ค์ default ์ค์ ์์ ์ค์ ํ profile์ ์ํด์ ๊ฐ ๋ค๋ฅธ ์ค์ ์ ๋ณด๋ค์ด ๋ค์ด๊ฐ๊ฒ ๋๋ค.
๊ทธ๋ ๋ค๋ฉด Spring Cloud Config Server๋ฅผ ์ฌ์ฉํ ๋, ์ด๋ค ์์ผ๋ก ๋ค์ด๋ฐ์ ํ๋ฉด ์ข์๊น?
๋ง์ฝ ์๋น์ค๋ค ์ค user-service๋ฅผ ๊ธฐ๋ํ๋ค๊ณ ํ๋ฉด, ์๋์ ๊ฐ์ด ๋ค์ด๋ฐ์ ์ง์ผ๋ฉด ๋๊ฒ ๋ค.
- user-service-prod.yml
- user-service-dev.yml
- user-service-local.yml
์ด๋ฐ ์ค์ ํ์ผ๋ค์ ๋ค์ด๋ฐ๋ค์ ์ ์ ํ๊ฒ ์ด์ฉํ์ฌ ํ์ผ๋ค์ ์ฝ๊ฒ ๊ด๋ฆฌํ๊ณ ์ ์ฉ์ํค๋ฉด ๋๊ฒ ๋ค!
3. Config Server
์๋ก์ด Config Server ์ญํ ์ ํด์ค ํ๋ก์ ํธ๋ฅผ ์์ฑํ๊ณ , gradle์ ๋ค์ ์์กด์ฑ์ ์ถ๊ฐํด์ค๋ค.
- build.gradle
ext {
set('springCloudVersion', "2023.0.0")
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-config-server'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
ํด๋น ์๋ฒ๋ Config Server๋ก ๋์๋ ์ ์๊ฒ @EnableConfigServer๋ฅผ ํ์ฑํ ์์ผ์ค๋ค.
- ConfigServiceApplication.java
@SpringBootApplication
@EnableConfigServer
public class ConfigServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServiceApplication.class, args);
}
}
์ด์ ์ค์ ํ์ผ๋ค์ ์๋ฒ์ ๋ฑ๋กํด ๋ณด๊ฒ ๋ค.
server:
port: 8888
spring:
profiles:
active: prod
application:
name: config-service
cloud:
config:
server:
git:
uri: https://github.com/DDonghyeo/spring-msa-yml
๋ณดํต์ ๊นํ๋ธ์ private repository๋ฅผ ๋ง๋ค์ด์ ์ฌ์ฉํ๋ค.
private repository ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ ค๋ฉด ๋ณดํต ๋น๋ฐํค๋ฅผ ์ฌ์ฉํด์ ssh๋ก ๊ฐ์ ธ์จ๋ค.
ํด๋น ๋ด์ฉ์ ๋ค์์ ๋ค๋ฃจ๊ธฐ๋ก ํ๊ณ , ์ง๊ธ์ public repository๋ก ์ฌ์ฉํ๊ฒ ๋ค.
- uri : ๊นํ๋ธ ๋งํฌ
- search-paths : ํ์ผ ๊ฒฝ๋ก
- default-label : ๋ธ๋์น๋ช
๊ธฐ๋ ์ค์ธ Config Server์์ ์ค์ ํ์ผ์ด ์ ์ฉ๋ ํ์ผ์ ๋ณด๋ ค๋ฉด ์๋์ ๊ฐ์ด ์ ์ํ๋ฉด ๋๋ค.
http://localhost:8888/{ํ์ผ ์ด๋ฆ}/{profile}
ํน๋ณํ profile์ด ์๋ค๋ฉด default๋ก ์ ์ํ๋ฉด ๋๊ฒ ๋ค.
๋๋ ํ ํฐ์ ์ฌ์ฉํ๊ธฐ ์ํ ์ค์ ์ ๋ณด ํ์ผ์ ์๋ฒ์ ๋ฑ๋กํ๋ค.
4. Client Server
Config Server ์ค์ ์ ๋ชจ๋ ์๋ฃํ๋ค๋ฉด, Client Server ์ค์ ์ ์์๋ณด๊ฒ ๋ค.
๋จผ์ gradle์ ๋ค์ ์์กด์ฑ๋ค์ ์ถ๊ฐํด์ค๋ค.
- build.gradle
dependencies {
//...
implementation 'org.springframework.cloud:spring-cloud-config-client:4.0.3'
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap:4.0.3'
}
์ด์ ์ด client server์ bootstrap.yml ํ์ผ์ ๋ง๋๋๋ฐ,
์ด bootstrap.yml์ application.yml ํ์ผ๋ณด๋ค ๋ ๋์ ์ฐ์ ์์๋ฅผ ๊ฐ์ ธ ๋จผ์ ์ ์ฉ๋๋ค.
์ฑ์์ ๊ฐ์ฅ ๋จผ์ ์ค์ ๋๋ ์ ๋ณด๋ผ๊ณ ์๊ฐํ๋ฉด ๋๊ฒ ๋ค.
-bootstrap.yml
server:
port: 9000
spring:
cloud:
config:
uri: http://localhost:8888
name: token
application:
name: user-service # micro service unique ID
eureka:
instance:
instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
client:
register-with-eureka: true # register to eureka server
fetch-registry: true # Getting instances' information from eureka server
service-url:
defaultZone : http://127.0.0.1:8761/eureka #Eureka Server's address
management:
endpoints:
web:
exposure:
include: refresh
User service์ actuator ์ค๋ํฌ์ธํธ refresh๋ฅผ ์ด์ด๋๊ณ , config server๋ก๋ถํฐ name์ด token์ธ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์จ๋ค.
์๋ฒ ๋ก๊ทธ๋ฅผ ์ดํด๋ณด๋ฉด, config server๋ก๋ถํฐ ์ค์ ์ ๋ณด๋ฅผ ์ ๊ฐ์ ธ์ค๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
ํด๋น ์ค์ ์ ๋ํ ์์ธํ ๋ด์ฉ์ ๊ณต์ ๋ฌธ์๋ฅผ ์ดํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค.
https://docs.spring.io/spring-cloud-config/docs/current/reference/html/
Spring Cloud Config
Spring Cloud Config provides server-side and client-side support for externalized configuration in a distributed system. With the Config Server, you have a central place to manage external properties for applications across all environments. The concepts o
docs.spring.io
๋๋ ์ด๋ฅผ ํ์ธํด๋ณด๊ธฐ ์ํด ๋ค์ ์ปจํธ๋กค๋ฌ๋ฅผ ๋ง๋ค์๋ค.
@RestController
@RequestMapping("/user")
public class UserController {
@Value("${jwt.secret}")
private String secret;
@GetMapping("/test")
public String test() {
return "token secret : " + secret ;
}
}
ํ ์คํธ ๊ฒฐ๊ณผ, ์ค์ ์ ๋ณด๋ฅผ ์ ๊ฐ์ ธ์ค๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
4. Actuator Refresh ํ ์คํธ
์ด์ ์๋ฒ๋ฅผ ๊ธฐ๋ ์ค์๋ ์ค์ ์ ๋ณด๋ค์ด ์ ๋ฐ์ ๋๋์ง ํ์ธํด๋ณด๋ ค๊ณ ํ๋ค.
๊นํ๋ธ์ ์๋ ๋ด์ฉ์ ๋ฐ๊ฟ์ฃผ์๋ค.
์์ง์ ์๋ฒ์ ๋ฐ์๋์ง ์์๋ค.
config server์์๋ ์ ์์ ์ผ๋ก ๊ฐ์ ธ์จ๋ค.
POST์์ฒญ์ ๋ณด๋ด์ผํ๊ธฐ ๋๋ฌธ์, postman์ ์ฌ์ฉํ๋ค.
์๋ฒIP/actuator/refresh๋ก POST์์ฒญ์ ๋ณด๋ด๋ฉด ๋๋ค.
์๋ฒ ๋ก๊ทธ์์ config server๋ก๋ถํฐ ์ค์ ์ ๋ณด๋ฅผ ๋ค์ ๊ฐ์ ธ์ค๋ ๊ฒ์ ๋ณผ ์ ์๊ณ ,
๋ฐ๋ ๊ฒฐ๊ณผ ๋ํ ํ์ธํ ์ ์๋ค.
์ด๋ฐ ๊ณผ์ ์ ์๋ํ ํ๊ณ ์ถ๋ค๋ฉด,
Github Action์ ํตํด์ yml์ด ๋ชจ์ฌ์๋ ๋ ํฌ์งํ ๋ฆฌ์ push๋ฅผ ๊ฐ์งํ๋ฉด /actuator/refresh์ POST์์ฒญ์ ๋ณด๋ด๋ ๊ณผ์ ์ ์ถ๊ฐํ๋ฉด ๋ ๊ฒ ๊ฐ๋ค. ๐ค
'BackEnd > Spring Cloud' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring Cloud] Spring Cloud Config Server ์ค์ ๊ฐ์ Private Repository ์์ ๊ฐ์ ธ์ค๊ธฐ (0) | 2024.02.12 |
---|---|
[Spring Cloud] Spring Cloud Bus (0) | 2024.02.05 |
[Spring Cloud] API Gateway, Filter (1) | 2024.01.08 |
[Spring Cloud] Eureka Server์ Micro Service ์ค์ (0) | 2024.01.08 |
[Spring Cloud] Srping Cloud Services (0) | 2024.01.07 |