티스토리 뷰

반응형

1. Spring Cloud Config Server

// build.gradle에 아래 내용을 추가
dependencies {
    implementation 'org.springframework.cloud:spring-cloud-config-server'
}
  • Spring Cloud Config Server로 동작하기 위한 디펜던시를 추가해둔다.
# application.yml
spring:
  application:
    name: my_first_config_server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/injuk/spring_cloud_config_demo.git
          default-label: main

server:
  # 포트는 자유롭게 설정하되, 추후 실행할 Client App.과 겹치지 않게 설정한다.
  port: 8840
  • 해당 실습 내용은 Git Repository에 저장된 설정 파일을 Spring Cloud Config Server를 통해 Fetch하는 예제이다.
  • Local File System을 사용하고 싶은 경우라면 spring.cloud.config.server.native 설정을 사용할 수 있다.
    • 그 외에도 jdbc, vault, s3 등을 지원하는 듯.
  • default-label은 git에서 사용하는 branch 명을 작성하자.
// MainClass
@SpringBootApplication
@EnableConfigServer
public class ConfigServer {

  public static void main(String[] args) {

    SpringApplication.run(ConfigServer.class, args);
  }
}
  • 이제 EnableConfigServer 어노테이션만 달아주면 준비는 끝난다.
  • App.을 실행한 후 설정 정보를 잘 받아오는지 다음의 URI를 통해 확인하자.
  • http://[CONFIG_SERVER_IP]:[PORT]/[CONFIG_FILE_NAME]/[PROFILE]
    • 예시 1. http://localhost:8840/ingnoh-configs/dev
    • 예시 2. http://localhost:8840/ingnoh-configs-dev.yml
[~] curl --silent localhost:8840/ingnoh-configs/dev | jq
{
  "name": "ingnoh-configs",
  "profiles": [
    "dev"
  ],
  "label": null,
  "version": "2781d45670dd70289de24680ad8452448e8a92b1",
  "state": null,
  "propertySources": [
    {
      "name": "https://github.com/injuk/spring_cloud_config_demo.git/ingnoh-configs-dev.yml",
      "source": {
        "hello.ingnoh": "developer"
      }
    }
  ]
}
[~] curl --silent localhost:8840/ingnoh-configs-dev.yml | yq e
hello:
  ingnoh: developer

 

2. Spring Cloud Config Client

  • 코드 전문
  • Spring Cloud Config Client App.은 기본적인 설정만을 내장 설정 파일에 포함하고, 외부의 설정 파일을 혼용하는 시나리오를
    가정하자.
// build.gradle에 아래 내용을 추가
dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-config'
}
  • Spring Cloud Config를 활용하기 위한 디펜던시를 추가해둔다.
# application.yml
server:
  port: 8850

spring:
  profiles:
    active: test
  application:
    name: ingnoh-configs
  config:
    import: configserver:http://localhost:8840
  • spring.application.name의 이름과 같은 설정 정보 중, spring.profiles.active에 명시된 설정 정보를 Fetch하게 된다.
  • 우리 프로젝트는 application.yml보다 먼저 로드되는 특징을 갖는 bootstrap.yml에 config uri 등을 설정하였다.
  • 그러나 구글링 중 bootstrap.yml 대신 application.properties(또는 .yml)을 사용할 수 있도록 변경되었다는 글을 본 후,
    이를 위와같이 적용해보게 되었다.
 

GitHub - spring-cloud/spring-cloud-release: Spring Cloud Release Train - dependency management across a wide range of Spring Clo

Spring Cloud Release Train - dependency management across a wide range of Spring Cloud projects. - GitHub - spring-cloud/spring-cloud-release: Spring Cloud Release Train - dependency management acr...

github.com

 

[Spring Cloud Config] Client 의 bootstrap.yml 지원 만료

스프링 클라우드 config client 에서 bootstrap.yml을 더이상 지원하지 않는다. 예전 자료들이 전부 bootstrap.yml 기반이라서 많이 해맸다. 하루는 해맨거 같다. stackoverflow : java - Config Client is not wo..

hongdori2.tistory.com

// Main은 그대로 둔 후 별도의 Controller Class를 추가한다.
@RestController
public class IngnohController {

  @Value("${hello.ingnoh}")
  private String ingnoh;

  @GetMapping("")
  public String hello() {
    return ingnoh;
  }
}
  • 받아온 정보를 화면에 표시하는 컨트롤러 클래스를 만들어보자.
  • hello.ingnoh라는 속성은 현재 실행 중인 Application의 application.yml에 포함되지 않은 값이므로, Spring Cloud Config
    가 관리하는 설정 정보를 Fetch하여 적용하게 된다.
  • App.을 실행한 후 http://localhost:8850을 통해 설정 적용 여부를 확인한다.
[~] curl http://localhost:8850
monkey%
  • app profile을 dev로 바꾼 후 다시 실행하면 다음과 같이 변경되어 적용되는 것을 확인할 수 있다.
[~] curl http://localhost:8850
developer%

 

주의사항

  • Config Server
    • default-label을 적용하지 않아 애를 먹었다. 자신의 Git Repository branch 명과 맞춰주자.
    • Repository가 Public인 경우 계정 정보를 입력하지 않아도 좋다.
    • 그러나 Config 특성 상 외부로 노출되지 않아야 하므로, 운영 환경에서는 Private한 Repository를 사용하자.
      이 경우, user 정보 역시 Config Server의 설정에 포함되어야 한다.
  • Config Client
    • spring.application.name과 spring.profiles.active 값에 주의하자.
    • Config Client가 Fetch해오는 설정은 http://[CONFIG_SERVER_IP]:[PORT]/[spring.application.name]/[spring.profiles.active]의 값이다.
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함