Spring Boot Admin Server, Client config 설정하기

이미지
 Spring Boot로 많은 프로젝트를 진행하게 됩니다. 많은 모니터링 도구가 있지만, Spring Boot 어플리케이션을 쉽게 모니터링 할 수 있는 방법을 소개하려고 합니다.   코드 중심으로 살펴보겠습니다. 1. 어드민 서버 구축 1-1. 디펜던시 추가 dependencies { // https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-server implementation 'de.codecentric:spring-boot-admin-starter-server:2.5.4' } 1-2. 어드민 서버 설정 활성화 @SpringBootApplication @EnableAdminServer public class ServerApplication { public static void main (String[] args) { SpringApplication. run (ServerApplication. class, args) ; } } EnableAdminServer를 하면 됩니다. 2. 클라이언트 서버 설정  예제는 book-client, member-client 2가지 클라이언트, member-client가 2개의 인스턴스 실행으로 작성했습니다.  2-1 디펜던시 추가 dependencies { // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web implementation 'org.springframework.boot:spring-boot-starter-web:2.5.4' // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-actuator implementation 'org.spring...

자바 스프링 설정값 사용하기

자바에서 스프링 설정 읽는 두가지 방법을 설명하겠습니다.


자바 설정을 통하여 읽어 올 수 있습니다.

@Configuration
@PropertySource("classpath:filename.properties")


파일에 include를 정의하여 읽어 올 수 있습니다.

스프링부트에서 기본적으로 applicaion.*를 설정 파일을 읽어오도록 되어 있습니다.

스프링부트 > 설정 파일 예시

스프링부트 설정 파일 예시




*.yml 파일에서 읽을 파일 목록 정의하기
spring:
profiles:
include:
- env
- datasource
- redis

application-env.yml 파일에 설정 정의

env:
api:
host: localhost.withccm


정의된 설정값 사용하기

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
@Value("${env.api.host}")
private String apiHost;

@Autowired
private Environment env;

public String getApiHost() {
return env.getProperty("env.api.host");
}

@Value 어노테이션을 통하여 정의된 사용할 값을 할당하여 사용합니다. 읽어야할 설정이 많은 경우 일일히 정의해야 하기 때문에 선언 부분이 길어질 수 있습니다.

또는 스프링 환경인 Environment 를 주입 받아, getProperty 함수를 호출하여 사용합니다.

다음 글 > 환경 설정 방법 


댓글

이 블로그의 인기 게시물

Spring boot redis cache config

Spring boot redis RedisTemplate

MySQL PK 중복 (duplicate key ) 해결 방법