๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/Web

[Spring] Spring Boot Admin ์‚ฌ์šฉํ•˜๊ธฐ - Log, Login ์ ์šฉ

by ynzu๐Ÿค 2022. 1. 25.
๋ฐ˜์‘ํ˜•

 

1. ๊ธฐ๋ณธ ์„ธํŒ…

1.1 Admin Server

  • maven ํ˜น์€ gradle์— spring-boot-admin-starter-server ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€
<!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-server -->
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.6.2</version>
</dependency>

 

// https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-server
implementation 'de.codecentric:spring-boot-admin-starter-server:2.6.2'

 

  • @EnableAdminServer ์ถ”๊ฐ€
@SpringBootApplication
@EnableAdminServer
public class AdminApplication {

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

}

 

1.2 Client

  • maven ํ˜น์€ gradle์— spring-boot-admin-starter-client ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€
<!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-client -->
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.6.2</version>
</dependency>

 

// https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-client
implementation 'de.codecentric:spring-boot-admin-starter-client:2.6.2'

 

  • application.yml ํ˜น์€ application.properties์— ์„ค์ • ์ถ”๊ฐ€
spring:
  boot:
    admin:
      client:
        url: http://localhost:58080 #admin server url
        instance:
          name: Client Server #admin UI ์—์„œ ๋ณด์—ฌ์งˆ ์ด๋ฆ„ ์„ค์ •

 

์ฃผ์˜์‚ฌํ•ญ

WAR, ์ฆ‰ ์™ธ๋ถ€ Tomcat์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—” 'spring.boot.admin.client.instance.service-base-url'๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์œผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋Ÿฌ ๋ฐœ์ƒ..!  'spring.boot.admin.client.instance.service-url'๋กœ ์„ค์ •ํ•ด๋„ ๋ฌด๋ฐฉํ•จ!

java.lang.IllegalStateException: couldn't determine local port. Please set spring.boot.admin.client.instance.service-base-url.
        at de.codecentric.boot.admin.client.registration.DefaultApplicationFactory.getLocalServerPort(DefaultApplicationFactory.java:192)
        at de.codecentric.boot.admin.client.registration.DefaultApplicationFactory.getServiceBaseUrl(DefaultApplicationFactory.java:104)
        at de.codecentric.boot.admin.client.registration.ServletApplicationFactory.getServiceUrl(ServletApplicationFactory.java:63)
        at de.codecentric.boot.admin.client.registration.ServletApplicationFactory.getManagementBaseUrl(ServletApplicationFactory.java:76)
        at de.codecentric.boot.admin.client.registration.DefaultApplicationFactory.getHealthUrl(DefaultApplicationFactory.java:154)
        at de.codecentric.boot.admin.client.registration.DefaultApplicationFactory.createApplication(DefaultApplicationFactory.java:80)
        at de.codecentric.boot.admin.client.registration.DefaultApplicationRegistrator.register(DefaultApplicationRegistrator.java:56)
        at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

 

์—ฌ๊ธฐ๊นŒ์ง€ ์„ค์ •์„ ๋งˆ์น˜๊ณ  Admin Server์™€ Client Server๋ฅผ ๊ตฌ๋™์‹œํ‚ค๊ณ  Admin Server์— ์ ‘์†ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ํ™”๋ฉด์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

 Spring Boot 2๋ถ€ํ„ฐ๋Š” ์ด๋ ‡๊ฒŒ ๊ธฐ๋ณธ์„ธํŒ…๋งŒ ํ•˜๊ฒŒ ๋˜๋ฉด health ๋ฐ info์ด์™ธ์˜ endPoint๊ฐ€ ๋…ธ์ถœ๋˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•œ๋‹ค. 


 

2. endPoints ๋…ธ์ถœ ์‹œํ‚ค๊ธฐ

2.1 Client

  • application.yml ํ˜น์€ application.properties์— ์„ค์ • ์ถ”๊ฐ€
management:
  endpoints:
    web:
      exposure:
        include: "*" #๋…ธ์ถœ์‹œํ‚ฌ endpoint, *๋Š” ์ „์ฒด ๋…ธ์ถœ
  endpoint:
    health:
      show-details: always

include์˜ ์•„์Šคํ…Œ๋ฆฌ์นด(*)์–‘ ์˜†์— ํฐ๋”ฐ์˜ดํ‘œ(")๋ฅผ ๊ผญ ๋„ฃ์–ด์ฃผ์–ด์•ผ ํ•œ๋‹ค! application.properties๋กœ๋Š” ์„ค์ •์„ ์•ˆํ•ด๋ดค๋Š”๋ฐ ์ด๊ฑด ๊ทธ๋ƒฅ ์•„์Šคํ…Œ๋ฆฌ์นด๋งŒ ์ ์–ด๋„ ๋˜๋Š”๋“ฏํ•˜๋‹ค.

management.endpoint.health.show-details ์†์„ฑ์˜ ๊ธฐ๋ณธ๊ฐ’์€ never ์ด๋ฏ€๋กœ ํ•ญ์ƒ ์ƒ์„ธ ๋‚ด์—ญ์„ ๋ณด๊ณ ์‹ถ์œผ๋ฉด always๋กœ ์„ค์ •ํ•˜๋ฉด ๋œ๋‹ค.

 

๋ชจ๋“  endPoints๊ฐ€ ๋…ธ์ถœ๋˜๋„๋ก ์„ค์ •ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์‹œ admin server์— ์ ‘์†ํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์„œ๋ฒ„์˜ ์ž์„ธํ•œ ์ •๋ณด๊ฐ€ ํ‘œ์‹œ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 


 

3. Admin Server์—์„œ Client Server์˜ ๋กœ๊ทธ ํ™•์ธํ•˜๊ธฐ

3.1 Client

  • application.yml ํ˜น์€ application.properties์— ์„ค์ • ์ถ”๊ฐ€
logging:
  config: classpath:logback-spring.xml
  file:
    name: D:\logs\logback.log

client server์—์„œ ์ƒ์„ฑ๋˜๋Š” logํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. pattern์œผ๋กœ๋„ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š”๋ฐ ๋‚˜๋Š” ์ผ๋‹จ ์ด๋ ‡๊ฒŒ ์„ธํŒ…ํ•ด์„œ ํ™•์ธํ–ˆ๋‹ค.๊ทธ๋Ÿผ admin server์˜ ์™ผ์ชฝ ๋ชฉ๋ก์— '๋กœ๊ทธ>๋กœ๊ทธํŒŒ์ผ' ์ด๋ผ๋Š” ํƒญ์ด ์ƒ๊ธฐ๊ณ , client server์˜ ๋กœ๊ทธ๋ฅผ ์‹ค์‹œ๊ฐ„์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 


 

4. Login ์ถ”๊ฐ€

Spring Boot Admin์„ ํ†ตํ•ด์„œ ํ”„๋กœ์„ธ์Šค์˜ ์ƒํƒœ ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๊ณ , ์„ค์ • ์ •๋ณด๋„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.
๋”ฐ๋ผ์„œ ํ—ˆ์šฉํ•˜์ง€ ์•Š์€ ์‚ฌ์šฉ์ž๊ฐ€ ์ด๋Ÿฌํ•œ ํ–‰์œ„๋ฅผ ํ•  ์ˆ˜ ์—†๋„๋ก Spring Boot Security๋ฅผ ์ ์šฉํ•˜์—ฌ ๋กœ๊ทธ์ธ์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•˜์˜€๋‹ค.

4.1 Admin Server

  • maven ํ˜น์€ gradle์— spring-boot-admin-starter-server ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
    <version>2.6.2</version>
</dependency>

 

// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security
implementation 'org.springframework.boot:spring-boot-starter-security:2.6.2'

 

  • application.yml ํ˜น์€ application.properties์— ์„ค์ • ์ถ”๊ฐ€
server.port=58080
spring.security.user.name=admin
spring.security.user.password=admin

 

  • SecuritySecureConfig.java  ์ถ”๊ฐ€
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private final AdminServerProperties adminServer;

    public SecurityConfig(AdminServerProperties adminServer) {
        this.adminServer = adminServer;
    }

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

        http.authorizeRequests()
                // ๋ชจ๋“  ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€์— ๋Œ€ํ•œ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•œ๋‹ค.
                .antMatchers(this.adminServer.path("/assets/**")).permitAll()
                .antMatchers(this.adminServer.path("/login")).permitAll()
                // ๊ถŒํ•œ์ด ๋ถ€์—ฌ๋˜์ง€ ์•Š์œผ๋ฉด ์ธ์ฆ์„ ์š”์ฒญ ํ•œ๋‹ค.
                .anyRequest().authenticated()
                .and()
                // ๋กœ๊ทธ์ธ ๋ฐ ๋กœ๊ทธ ์•„์›ƒ์„ ๊ตฌ์„ฑํ•œ๋‹ค.
                .formLogin().loginPage(this.adminServer.path("/login")).successHandler(successHandler).and()
                .logout().logoutUrl(this.adminServer.path("/logout")).and()
                // Spring Boot Admin ํด๋ผ์ด์–ธํŠธ๋ฅผ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•ด HTTP-Basic ์ง€์›์„ ์‚ฌ์šฉํ•œ๋‹ค.
                .httpBasic().and()
                .csrf()
                // ์ฟ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ CSRF ๋ณดํ˜ธ ๊ธฐ๋Šฅ ๊ตฌํ˜„
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringAntMatchers(
                        // CRSF๋ฅผ ๋น„ํ™œ์„ฑํ™”ํ•œ๋‹ค.
                        this.adminServer.path("/instances"),
                        // actuator EndPoint ๋Œ€ํ•œ CRSF ๋ณดํ˜ธ๋ฅผ ๋น„ํ™œ์„ฑํ™”ํ•œ๋‹ค.
                        this.adminServer.path("/actuator/**")
                );

    }
}

 

4.2 Client

  • application.yml ํ˜น์€ application.properties์— ์„ค์ • ์ถ”๊ฐ€

: 4.1(Admin Server์˜ ์„ค์ •ํŒŒ์ผ)์—์„œ ์„ค์ •ํ•œ username/password ์ •๋ณด์™€ ๊ฐ™์•„์•ผ ํ•œ๋‹ค.

spring:
  boot:
    admin:
      client:
        url: http://localhost:58080 #admin server url
        username: admin
        password: admin
        instance:
          name: Client Server

 

์—ฌ๊ธฐ๊นŒ์ง€ ์„ค์ •ํ•˜๊ณ  admin server์— ์ ‘์†ํ•˜๋ฉด ๊ธฐ์กด๊ณผ ๋‹ค๋ฅด๊ฒŒ ๋กœ๊ทธ์ธํ™”๋ฉด์ด ๋จผ์ € ๋ณด์ผ ๊ฒƒ์ด๋‹ค. ์„ค์ •ํ•œ ID/PW๋Œ€๋กœ ์ž…๋ ฅํ•˜๋ฉด ๋กœ๊ทธ์ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 

 

 


 

๋กœ์ปฌ์—์„  ์ž˜ ๋˜๋‹ค๊ฐ€ ์„œ๋ฒ„์— ์˜ฌ๋ฆฌ๋‹ˆ SSLHandshakeException ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. ์—๋Ÿฌ ๋‚ด์šฉ์€ SSL์ธ์ฆ์„œ์˜ DNS name๊ณผ ์‹ค์ œ domain์ด ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. (์„œ๋ฒ„์— ์˜ฌ๋ฆฐ client server๋Š” ssl์ธ์ฆ์„œ๊ฐ€ ์ ์šฉ๋œ https)

org.springframework.web.reactive.function.client.WebClientRequestException

No subject alternative DNS name matching ora19c found.; nested exception is javax.net.ssl.SSLHandshakeException: No subject alternative DNS name matching ora19c found.d.

Admin Server UI์—์„  ์‹ค์ œ client ์„œ๋ฒ„์˜ ๋„๋ฉ”์ธ์ด ์•„๋‹Œ ip ํ˜น์€ ์žฅ๋น„๋ช…์œผ๋กœ ๋ณด์—ฌ์ง€๊ณ  ์žˆ์—ˆ๋‹ค. 

์ด๋Ÿฐ ๊ฒฝ์šฐ์—” Client Server ์„ค์ •์— 'spring.boot.admin.client.instance.service-url'์„ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

ex) spring.boot.admin.client.instance.service-url = https://<client-domain>:<port>/

 

 

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€