다음을 통해 공유


빠른 시작: Azure App Configuration으로 Java Spring 앱 만들기

이 빠른 시작에서는 Azure App Configuration을 Java Spring 앱에 통합하여 코드와 별도로 애플리케이션 설정의 스토리지 및 관리를 중앙 집중화합니다.

필수 구성 요소

키-값 추가

App Configuration 저장소에 다음 키-값을 추가하고 레이블콘텐츠 형식을 기본값으로 둡니다. Azure Portal 또는 CLI를 사용하여 저장소에 키-값을 추가하는 방법에 대한 자세한 내용은 키-값 만들기로 이동합니다.

/application/config.message 안녕하세요.

App Configuration 저장소에 연결

이제 App Configuration 저장소가 있으므로 Spring Cloud Azure Config 스타터를 사용하여 애플리케이션이 사용자가 만든 App Configuration 저장소와 통신하도록 할 수 있습니다.

Spring Cloud Azure Config 스타터 모듈을 설치하려면 pom.xml 파일에 다음 종속성을 추가합니다.

<dependencies>
    ...
    <dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>spring-cloud-azure-appconfiguration-config-web</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>spring-cloud-azure-dependencies</artifactId>
        <version>6.0.0</version>
        <type>pom</type>
        <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
  1. 기본적으로 라이브러리는 관리 ID를 통해 App Configuration 저장소에 연결됩니다. 지침에 따라 자격 증명에 App Configuration 데이터 읽기 권한자 역할을 할당합니다. 애플리케이션을 실행하기 전에 권한이 전파될 수 있는 충분한 시간을 허용해야 합니다. 그런 다음 AppConfigCredential.java 새 파일을 만들고 다음 줄을 추가합니다.

    import org.springframework.stereotype.Component;
    
    import com.azure.data.appconfiguration.ConfigurationClientBuilder;
    import com.azure.identity.DefaultAzureCredentialBuilder;
    import com.azure.spring.cloud.appconfiguration.config.ConfigurationClientCustomizer;
    
    @Component
    public class AppConfigCredential implements ConfigurationClientCustomizer {
    
        @Override
        public void customize(ConfigurationClientBuilder builder, String endpoint) {
            builder.credential(new DefaultAzureCredentialBuilder().build());
        }
    }
    

    비고

    또한 Spring Cloud Azure 인증 을 사용하여 인증 정보를 제공할 수 있습니다. Azure Spring 구성을 사용하여 인증하는 경우 모든 Azure Spring 라이브러리에 대해 동일한 인증을 사용할 수 있습니다.

  2. 그런 다음 spring.factories 디렉터리에 resources/META-INF 파일을 만들고 다음 줄을 추가하고 com.example.MyApplication을 애플리케이션 이름과 패키지로 업데이트하여 구성 부트스트랩 구성을 만듭니다.

    org.springframework.cloud.bootstrap.BootstrapConfiguration=\
    com.example.MyApplication
    
  3. 앱의 리소스 디렉터리 아래에 application.properties 라는 새 파일을 만들고 파일에 다음 줄을 추가합니다.

    spring.config.import=azureAppConfiguration
    spring.cloud.azure.appconfiguration.stores[0].endpoint= ${APP_CONFIGURATION_ENDPOINT}
    

App Configuration 저장소에서 읽기

Spring Cloud Azure Config 스타터를 사용하여 애플리케이션이 사용자가 만든 App Configuration 저장소와 통신하도록 하려면 다음 단계를 사용하여 애플리케이션을 구성합니다.

  1. MyProperties.java라는 새 Java 파일을 만들고 다음 줄을 추가합니다.

    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    @Component
    @ConfigurationProperties(prefix = "config")
    public class MyProperties {
        private String message;
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    }
    
  2. HelloController.java라는 새 Java 파일을 만들고 다음 줄을 추가합니다.

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
    
        @Autowired
        private MyProperties properties; 
    
        @GetMapping
        public String getMessage() {
            return "Message: " + properties.getMessage();
        }
    }
    
  3. 자동 생성된 단위 테스트를 열고 Azure App Configuration을 사용하지 않도록 업데이트합니다. 이렇게 하지 않으면 단위 테스트를 실행할 때 Azure App Configuration이 서비스에서 로드하려고 시도합니다.

    import org.junit.jupiter.api.Test;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest(properties = "spring.cloud.azure.appconfiguration.enabled=false")
    class DemoApplicationTests {
    
        @Test
        void contextLoads() {
        }
    
    }
    

로컬로 앱 빌드 및 실행

  1. APP_CONFIGURATION_ENDPOINT라는 환경 변수를 설정하고 이를 App Configuration 저장소의 액세스 키로 설정합니다. 명령줄에서 다음 명령을 실행하고 명령 프롬프트를 다시 시작하여 변경 내용을 적용합니다.

    setx APP_CONFIGURATION_ENDPOINT "<endpoint-of-your-app-configuration-store>"
    

    Windows PowerShell을 사용하는 경우 다음 명령을 실행합니다.

    $Env:APP_CONFIGURATION_ENDPOINT = "<endpoint-of-your-app-configuration-store>"
    

    macOS 또는 Linux를 사용하는 경우 다음 명령을 실행합니다.

    export APP_CONFIGURATION_ENDPOINT='<endpoint-of-your-app-configuration-store>'
    
  2. 루트 디렉터리에 대한 명령 프롬프트를 열고 다음 명령을 실행하여 Maven으로 Spring Boot 애플리케이션을 빌드하고 실행합니다.

    mvn clean package
    mvn spring-boot:run
    
  3. 애플리케이션이 실행되면 curl을 사용하여 애플리케이션을 테스트합니다. 예를 들어 다음과 같습니다.

    curl -X GET http://localhost:8080/
    

    App Configuration 저장소에 입력한 메시지가 표시됩니다.

리소스 정리

이 문서에서 만든 리소스를 계속 사용하지 않으려면 여기서 만든 리소스 그룹을 삭제하여 요금이 부과되지 않도록 합니다.

중요합니다

리소스 그룹을 삭제하면 다시 되돌릴 수 없습니다. 리소스 그룹 및 포함된 모든 리소스가 영구적으로 삭제됩니다. 잘못된 리소스 그룹 또는 리소스를 자동으로 삭제하지 않도록 합니다. 유지하려는 다른 리소스가 포함된 리소스 그룹 내에서 이 문서에 대한 리소스를 만든 경우 리소스 그룹을 삭제하는 대신 해당 창에서 각 리소스를 개별적으로 삭제합니다.

  1. Azure Portal에 로그인하고 리소스 그룹을 선택합니다.
  2. 이름으로 필터링 상자에서 리소스 그룹의 이름을 입력합니다.
  3. 결과 목록에서 리소스 그룹 이름을 선택하여 개요를 확인합니다.
  4. 리소스 그룹 삭제를 선택합니다.
  5. 리소스 그룹 삭제를 확인하는 메시지가 표시됩니다. 리소스 그룹의 이름을 입력하여 확인하고 삭제를 선택합니다.

잠시 후, 리소스 그룹 및 모든 해당 리소스가 삭제됩니다.

다음 단계

이 빠른 시작에서는 새 App Configuration 저장소를 만들고, Java Spring 앱에서 사용했습니다. 자세한 내용은 Azure의 Spring을 참조하세요. 자세한 내용은 참조 설명서를 확인하세요. Spring Cloud Azure App Configuration 라이브러리의 작동 방식에 대한 모든 정보가 포함되어 있습니다. Java Spring 앱에서 구성 설정을 동적으로 새로 고치도록 설정하는 방법을 알아보려면 다음 자습서를 계속 진행하세요.