728x90
반응형
개요
Spring Cloud Config는 마이크로서비스 아키텍처에서 설정 정보를 중앙 집중식으로 관리하기 위한 서비스입니다. 이를 통해 설정 정보를 수정하고 배포하는 과정을 단순화하고, 마이크로서비스 간에 설정 정보를 공유할 수 있습니다.
특징
1. 중앙 집중식 설정 관리
Spring Cloud Config는 설정 정보를 중앙 집중식으로 관리합니다. 이를 통해 모든 마이크로서비스에서 동일한 설정 정보를 사용할 수 있습니다. 또한 설정 정보를 수정하면 모든 마이크로서비스에서 변경된 설정 정보를 사용할 수 있습니다.
2. 다양한 데이터 소스 지원
Spring Cloud Config는 다양한 데이터 소스를 지원합니다. Git, Subversion, JDBC 등 다양한 데이터 소스를 이용하여 설정 정보를 가져올 수 있으므로, 기존에 사용하던 데이터 소스를 계속해서 사용할 수 있습니다.
3. 설정 정보의 보안성 강화
Spring Cloud Config는 다양한 데이터 소스를 지원합니다. Git, Subversion, JDBC 등 다양한 데이터 소스를 이용하여 설정 정보를 가져올 수 있으므로, 기존에 사용하던 데이터 소스를 계속해서 사용할 수 있습니다.
4. 환경별 설정 정보 관리
Spring Cloud Config를 이용하면 여러 개의 프로파일을 이용하여 환경별 설정 정보를 관리할 수 있습니다. 예를 들어, dev, test, prod 등의 프로파일을 만들어 각각의 환경에서 다른 설정 정보를 사용할 수 있습니다.
이러한 이유들로 인해 Spring Cloud Config를 사용하면 마이크로서비스 아키텍처에서 설정 관리를 용이하게 할 수 있으며, 안정적이고 신뢰성 높은 서비스를 제공할 수 있습니다.
구현
spring boot프로젝트를 생성(필자는 maven을 사용)
Config Server
1. pom.xml 설정
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
...
<properties>
<java.version>11</java.version>
<spring-cloud.version>3.1.5</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>${spring-cloud.version}</version>
</dependency>
</dependencies>
...
2. @EnableConfigServer 추가
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
3. application.yml 설정
server:
port: 8888 # Config Server의 포트 설정
spring:
application:
name: config-server # Config Server의 이름 설정
cloud:
config:
server:
git:
uri: https://github.com/{username}/{repository}.git # Git 저장소 URI
search-paths: '{path/to/properties}' # 검색할 설정 파일 경로
username: {username} # Git 계정 정보
password: {password} # Git 계정 정보
clone-on-start: true # Config Server 시작시 Git 저장소 클론 여부
force-pull: true # Config Server 시작시 Git 저장소 강제 업데이트 여부
label: master # Git 브랜치 또는 태그 이름 설정
만약 Git SSH 연결을 사용하려면 Git 저장소 URI에 ssh:// 프로토콜을 사용하여 연결해야 한다
server:
port: 8888 # Config Server의 포트 설정
spring:
application:
name: config-server # Config Server의 이름 설정
cloud:
config:
server:
git:
uri: ssh://{git-host}:{git-port}/{username}/{repository}.git
ignore-local-ssh-settings: true # 로컬 SSH 설정 무시 여부
host-key-algorithm: ssh-rsa # 호스트 키 알고리즘
strict-host-key-checking: no # 호스트 키 체크 여부
private-key: |
-----BEGIN OPENSSH PRIVATE KEY-----
{private-key}
-----END OPENSSH PRIVATE KEY-----
passphrase: {passphrase} # SSH 암호
Config Client
1. pom.xml 설정
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
...
<properties>
<java.version>11</java.version>
<spring-cloud.version>3.1.5</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>${spring-cloud.version}</version>
</dependency>
</dependencies>
...
2. yml 설정
spring:
application:
name: example-app
cloud:
config:
uri: http://config-server:8888
profile: dev # 프로파일
label: main # Git 브랜치
fail-fast: true # Config 서버 연결 실패 시 어플리케이션 로드 여부
3. 호출정보
서버를 실행하면 설정 파일 저장소를 클론하고 설정 정보를 읽어오는데, spring cloud config server가 갖는 endpoint는 다음과 같다.
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
설정 파일 저장소에 있는 해당 파일을 불러오게 되는 것이다.
실제로 config server를 호출하면
정상적으로 호출이 된다.
Github
https://github.com/victory940209/spring-cloud
728x90
반응형
'Spring > Spring Cloud' 카테고리의 다른 글
Spring Cloud Gateway (0) | 2023.03.08 |
---|---|
Spring Cloud Eureka (0) | 2023.03.08 |
Spring Cloud MSA (0) | 2022.01.25 |