SpringBoot Management

Actuator

์Šคํ”„๋ง ๋ถ€ํŠธ ๊ณต์‹๋ฌธ์„œ(https://docs.spring.io/spring-boot/docs/2.1.8.RELEASE/reference/html/production-ready.html)

Spring Boot includes a number of additional features to help you monitor and manage your application when you push it to production. You can choose to manage and monitor your application by using HTTP endpoints or with JMX. Auditing, health, and metrics gathering can also be automatically applied to your application.

์Šคํ”„๋ง ๋ถ€ํŠธ์—๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์šด์˜์— ์˜ฌ๋ ธ์„ ๋•Œ, ํ•ด๋‹น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋ชจ๋‹ˆํ„ฐ๋ง ํ•ด์ฃผ๊ณ  ๊ด€๋ฆฌํ•ด์ฃผ๋Š” ๋‹ค์ˆ˜์˜ ๊ธฐ๋Šฅ๋“ค์ด ์žˆ๋‹ค ๊ทธ๊ฒƒ์„ Actuator๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์‚ฌ์šฉ์ž๋Š” ๋ชจ๋‹ˆํ„ฐ๋ง, ๊ด€๋ฆฌํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์ธ actuator๋ฅผ HTTP endpoints๋‚˜ JMX์„ ํ†ตํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์ ์šฉ

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

์š”๋ ‡๊ฒŒ 2๊ฐœ์˜ ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•ด์ฃผ๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•˜๊ณ 

/actuator

์š” ์ฃผ์†Œ๋กœ ์ ‘์†ํ•˜๊ฒŒ ๋˜๋ฉด ํ˜„์žฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ํ†ตํ•ด์„œ ์ ‘์†ํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์—”๋“œํฌ์ธํŠธ๋“ค์— ๋Œ€ํ•œ ์ •๋ณด๋“ค์„ ์ œ๊ณตํ•ด์ค€๋‹ค โ†’ parse๋˜์ง€ ์•Š์€ ์ƒํƒœ๋กœ ์ถœ๋ ฅ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ธŒ๋ผ์šฐ์ €์—์„œ json parser ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ํ†ตํ•ด์„œ ๋” ๊ฐ€๋…์„ฑ ์ข‹๊ฒŒ ํ™•์ธํ•˜์ž

๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•ด์ฃผ๋Š” ์—”๋“œ ํฌ์ธํŠธ๋“ค์€ actuator, health, info๊ฐ€ ์žˆ๊ณ  ์ถ”๊ฐ€์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“  ์—”๋“œ ํฌ์ธํŠธ๋“ค์€ actuator/{์—”๋“œํฌ์ธํŠธ} ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•œ๋‹ค.

actuator : ํ˜„์žฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ œ๊ณตํ•ด์ฃผ๊ณ  ์žˆ๋Š” ๋ชจ๋“  endpoint๋ฅผ ๋ณด์—ฌ์คŒ

health : ํ˜„์žฌ ๊ตฌ๋™ ์ค‘์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ฑด๊ฐ• ์ƒํƒœ์— ๋Œ€ํ•ด์„œ ํ‘œ์‹œ

info : ๊ฐœ๋ฐœ์ž๊ฐ€ ์ •์˜ํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ •๋ณด๋“ค์„ ํ‘œ์‹œ

์‚ฌ์šฉ ๋ฐฉ๋ฒ•

application.properties์— ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ๊ฑธ include, excludeํ•˜๋ฉด ๋จ

management.endpoints.web.exposure.include=health, info, beans, conidtions
management.endpoints.web.exposure.exclude=

์ƒํƒœ๋Š” ํฌ๊ฒŒ UP๊ณผ DOWN์„ ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ UP์€ ์‹œ์Šคํ…œ์ด ์ž˜ ์ž‘๋™ ์ค‘์ด๊ณ  ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๋œป์ด๊ณ  DOWN์€ ์‹œ์Šคํ…œ์ด ์ž‘๋™ ์ค‘์ด์ง€ ์•Š๊ณ  ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค

์—ฌ๋Ÿฌ๊ฐ€์ง€ actuator์—์„œ ์ œ๊ณตํ•ด์ฃผ๋Š” ๋‹ค์–‘ํ•œ ์—”๋“œ ํฌ์ธํŠธ๋“ค์ด ์กด์žฌํ•˜๊ณ  ํ•„์š”ํ•œ ๊ฒƒ๋“ค์„ include์— ์ถ”๊ฐ€ํ•ด์ค˜์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๊ฒ ๋‹ค..!

Spring Boot Admin

์š”๋†ˆ์€ spring boot ์—์„œ ์ œ๊ณตํ•ด์ฃผ๋Š” actuator๋ฅผ ๊น”๋”ํ•œ UI๋กœ ๋ชจ๋‹ˆํ„ฐ๋ง ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ œ๊ณตํ•ด์ฃผ๋Š” ์„œ๋น„์Šค์ด๋‹ค

ํฌ๊ฒŒ ์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ๋กœ ๋‚˜๋ˆ ์ง€๋Š”๋ฐ โ†’ ํ•ญ์ƒ ์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ์˜ ๋ฒ„์ „์„ ๋งž์ถฐ์ค˜์•ผํ•˜๋Š” ๊ฒƒ์ด ํฌ์ธํŠธ`

์ผ๋‹จ ์„œ๋ฒ„ ๋จผ์ €

์ผ๋‹จ์€ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” build.gradle์— ์ถ”๊ฐ€ + spring security๋„ ์‚ฌ์šฉํ•ด์•ผํ•จ

implementation 'de.codecentric:spring-boot-admin-starter-server:2.4.1'
implementation 'org.springframework.boot:spring-boot-starter-security'

์ถ”๊ฐ€์ ์œผ๋กœ application.properties์— ํ•„์š”ํ•œ ์„ธํŒ…๋“ค์„ ์ถ”๊ฐ€

server.port=8090
spring.security.user.name=client
spring.security.user.password=client

์ดํ›„์—๋Š” ํ•ด๋‹น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ @SpringBootApplication์ด ์žˆ๋Š” ์œ„์น˜์— @EnableAdminServer๋ฅผ ํ†ตํ•ด์„œ UI๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๊ณ  ์Šคํ”„๋ง์—๊ฒŒ ์•Œ๋ ค์คŒ

@EnableAdminServer
@SpringBootApplication
public class JpaProjectApplication {

    public static void main(String[] args) {
        SpringApplication.run(JpaProjectApplication.class, args);
    }

}

๊ทธ๋ฆฌ๊ณ  config ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์„œ ์‹œํ๋ฆฌํ‹ฐ๋ฅผ ์ ์šฉ

package config;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import java.util.UUID;

@Configuration
@RequiredArgsConstructor
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    private final AdminServerProperties adminServer;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        SavedRequestAwareAuthenticationSuccessHandler successHandler =
                new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(this.adminServer.getContextPath() + "/");

        http
                .authorizeRequests()
                .antMatchers(this.adminServer.getContextPath() + "/assets/**").permitAll()
                .antMatchers(this.adminServer.getContextPath() + "/login").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage(this.adminServer.getContextPath() + "/login")
                .successHandler(successHandler)
                .and()
                .logout()
                .logoutUrl(this.adminServer.getContextPath() + "/logout")
                .and()
                .httpBasic()
                .and()
                .csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringRequestMatchers(
                        new AntPathRequestMatcher(this.adminServer.getContextPath() +
                                "/instances", HttpMethod.POST.toString()),
                        new AntPathRequestMatcher(this.adminServer.getContextPath() +
                                "/instances/*", HttpMethod.DELETE.toString()),
                        new AntPathRequestMatcher(this.adminServer.getContextPath() + "/actuator/**"))
                .and()
                .rememberMe()
                .key(UUID.randomUUID().toString())
                .tokenValiditySeconds(1209600);
    }
}

์„œ๋ฒ„์˜ ์„ค์ •์€ ์ด๋ ‡๊ฒŒ ํ•ด๋‘๊ณ  localhost:8080/applications ๋กœ ์ ‘์†ํ•ด๋ณด๋ฉด ํ˜„์žฌ 0๊ฐœ์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ๋‹ค๊ณ  ํ‘œ์‹œ๋œ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ํด๋ผ์ด์–ธํŠธ ์‚ฌ์ด๋“œ์—์„œ๋„ ์ถ”๊ฐ€๋ฅผ ํ•ด์ค˜์•ผํ•œ๋‹ค

adminํŽ˜์ด์ง€๋ฅผ ํ†ตํ•ด์„œ ๊ด€๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋˜๋Š” ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด๋‹ค

ํด๋ผ์ด์–ธํŠธ ์ธก์—์„œ๋Š” application.properties์— ํ•„์š”ํ•œ ๊ฐ’๋“ค ๋ช‡๊ฐ€์ง€๋งŒ ํ•ด์ฃผ๋ฉด ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ

## ํด๋ผ์ด์–ธํŠธ์˜ ์ฃผ์†Œ๋Š” ์„œ๋ฒ„์™€์˜ ์ฃผ์†Œ๊ฐ€ ๋‹ฌ๋ผ์•ผ ํ•˜๊ฒ ์ง€์š”?
server.port=8089
## ์„œ๋ฒ„์˜ ์ฃผ์†Œ
spring.boot.admin.client.url=http://localhost:8080
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin
spring.boot.admin.client.instance.metadata.user.name=${์„œ๋ฒ„์—์„œ์˜ ๋„ค์ž„}
spring.boot.admin.client.instance.metadata.user.password=${์„œ๋ฒ„์—์„œ์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ}

์•ž์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด์„œ ๋” ์ถ”๊ฐ€์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์žˆ๋‹ค๋ฉด ๊ธฐ๋กํ•˜์ž!

์ฐธ๊ณ 

https://www.baeldung.com/spring-boot-admin

https://otrodevym.tistory.com/entry/spring-boot-์„ค์ •ํ•˜๊ธฐ-12-adminsecurity-์„ค์ •-๋ฐ-ํ…Œ์ŠคํŠธ-์†Œ์Šค

Last updated

Was this helpful?