복붙노트

[SPRING] Spring Cloud Configuration Server가 로컬 속성 파일로 작동하지 않습니다.

SPRING

Spring Cloud Configuration Server가 로컬 속성 파일로 작동하지 않습니다.

나는 여기에있는 github의 Spring Cloud 프로젝트로 놀고있다 : https://github.com/spring-cloud/spring-cloud-config

그러나 github에서 속성을 가져 오는 대신 로컬 속성 파일을 읽는 데 문제가 발생했습니다. github에 대한 모든 참조를 제거하더라도 봄이 로컬 파일을 무시하는 것 같습니다. 비슷한 질문이 여기에 있습니다 : Spring-Cloud 설정 서버는 설정 속성 파일을 무시합니다.

그러나 나는 아직 좋은 해답을 보지 못했습니다. 나는 누군가가 이것의 예를 가르쳐 줄 수 있는지 궁금하네요. 모든 종류의 git repo를 사용하는 대신 로컬로 속성을 설정하고 싶습니다. 나는 누군가가 전에 이것을 겪었다 고 가정하고, 어딘가에 그것의 예가 있다면, 나는 그것을 올바른 방향으로 움직일 수 있도록 정말로보고 싶다.

해결법

  1. ==============================

    1.내 모든 코드는 https://github.com/spencergibb/communityanswers/tree/so27131143 여기에 있습니다.

    내 모든 코드는 https://github.com/spencergibb/communityanswers/tree/so27131143 여기에 있습니다.

    src / main / java / Application.java

    @Configuration
    @EnableAutoConfiguration
    @EnableConfigServer
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    src / main / resources / application.yml

    spring:
      application:
         name: myconfigserver
      profiles:
         active: native
    
    my:
      property: myvalue
    

    src / main / resources / myapp.yml

    my:
      otherprop: myotherval
    

    myapp라는 app의 속성을 가져 오려면 다음을 수행하십시오.

    curl http : // localhost : 8080 / myapp / default

    {
         "name": "default",
         "label": "master",
         "propertySources": [
              {
                    "name": "applicationConfig: [classpath:/myapp.yml]",
                    "source": {
                         "my.otherprop": "myotherval"
                    }
              },
              {
                    "name": "applicationConfig: [classpath:/application.yml]",
                    "source": {
                         "spring.application.name": "myconfigserver",
                         "spring.profiles.active": "native",
                         "my.property": "myvalue"
                    }
              }
         ]
    }
    
  2. ==============================

    2.Spring config 서버를 사용하여 apple-service (Test Micro Service)에 대한 구성을 읽을 수 있습니다.

    Spring config 서버를 사용하여 apple-service (Test Micro Service)에 대한 구성을 읽을 수 있습니다.

    Spring 설정 어플리케이션의 예제 application.yml

    spring:
        profiles:
            active: native
        cloud:
            config:
                server:
                    native:
                        searchLocations: classpath:config/
    server:
      port: 8888
    
    
    endpoints:
        restart:
          enabled: true
    

    .properties 또는 .yml 파일을 src \ main \ resources \ config 폴더에 넣으십시오. 이 파일의 이름이 마이크로 서비스의 spring.application.name과 일치해야합니다.

    예를 들어 spring.application.name = apple-service 인 경우 속성 파일은 src \ main \ resources \ config 폴더의 apple-service.properties 여야합니다.

    apple-service의 bootstrap.yml 예제 :

    spring:
      application:
        name: apple-service
    
    cloud:
      config:
        uri: http://localhost:8888
    
  3. ==============================

    3.spring.profiles.active = native를 사용하는 것은 스프링 문서가 제안하는 것처럼 보이지만, 나는 그것을 작동시킬 수 없다. 내 application.properties 파일은

    spring.profiles.active = native를 사용하는 것은 스프링 문서가 제안하는 것처럼 보이지만, 나는 그것을 작동시킬 수 없다. 내 application.properties 파일은

    server.port=8888
    spring.cloud.config.profiles=native 
    

    그러나 URL로부터의 응답

    http://localhost:8888/config-server/env
    

    ~이다.

    {"name":"env","label":"master","propertySources":[{"name":"https://github.com/spring-cloud-samples/config-repo/application.yml","source":{"info.url":"https://github.com/spring-cloud-samples","info.description":"Spring Cloud Samples"}}]}
    

    이는 네이티브 프로필이 무시되었으며 서버가 여전히 github를 속성 소스로 간주 함을 나타냅니다.

    내가 만난 작은 추가 문제는 구성 서비스 기본 포트입니다. Sprin Cloud Config 문서에 따르면 8888이어야합니다. 내 application.properties에서 server.port = 8888을 제거하면 구성 서버는 기본 스프링 부트 포트 인 8080 포트에서 시작하지만 하나의 구성 서버가 사용해야하는 것은 아닙니다.

  4. ==============================

    4.Mac OS 환경에서 Configuration Server를 실행할 때도 동일한 문제가있었습니다. 이것은 Linux 나 Windows에서 발생하지 않았습니다.

    Mac OS 환경에서 Configuration Server를 실행할 때도 동일한 문제가있었습니다. 이것은 Linux 나 Windows에서 발생하지 않았습니다.

    bootstrap.yml 파일에서 네이티브 속성을 다음과 같이 설정했습니다.

    spring:
      profiles:
        active: native
    

    마침내 그것은 Mac에서 나를 위해 일하는 방식은 jar 파일에 활성 프로파일을 전달하는 것이 었습니다.

    java -jar config-server.jar --spring.profiles.active=native
    

    Mac OS에서 왜 다르게 동작하는지 아직도 알 수 없습니다.

  5. ==============================

    5.로컬 repo 및 부트 스트랩 구성으로 작동시킬 수있었습니다.

    로컬 repo 및 부트 스트랩 구성으로 작동시킬 수있었습니다.

    java -jar spring-cloud-config-server-1.0.0.M3-exec.jar --spring.config.name=bootstrap
    

    bootstrap.yml 파일은 ./config/ 폴더에 있습니다.

    server:
      port: 8080
    spring:
      config:
        name: cfg_server
      cloud:
        config:
          server:
           git:
            uri: /home/us/config_repo
            searchPaths: pmsvc,shpsvc
    
  6. ==============================

    6.구성 서버의 application.properties에 다음이 포함 된 경우 config 서버는 로컬 특성 파일을 읽습니다.

    구성 서버의 application.properties에 다음이 포함 된 경우 config 서버는 로컬 특성 파일을 읽습니다.

    spring.profiles.active=native
    **spring.cloud.config.server.native.searchLocations=file:/source/tmp**
    

    / source / tmp 디렉토리에서 클라이언트에 대한 로컬 특성 파일을 저장합니다 (예 :

    http://localhost:8888/a-bootiful-client/default
    

    당신은 얻을 것이다 :

    {"name":"a-bootiful-client","profiles":["default"],"label":null,"version":null,"state":null,"propertySources":[{"name":"file:/source/tmp/a-bootiful-client.properties","source":{"message":"Kim"}}]}
    
  7. ==============================

    7.내 로컬 컴퓨터에서 동일한 문제가 있었지만 원격 서버에서 정상적으로 작동합니다.

    내 로컬 컴퓨터에서 동일한 문제가 있었지만 원격 서버에서 정상적으로 작동합니다.

    @ Oreste의 답변이 저에게 효과적입니다. 그래서 나는 오류 로그를 다시 확인했다. 그리고 나는 발견했다.

     2018-06-25 16:09:49.789  INFO 4397 --- [           main] t.p.a.s.api.user.UserServiceApplication  : The following profiles are active:  someProfileISetBefore
    

    그래서, 제 경우의 근본적인 이유는 전에 환경 변수를 설정하는 것입니다,하지만 그것을 제거하는 것을 잊지 마십시오. 그리고 그것은 응용 프로그램 속성 파일 설정을 덮어 씁니다.

    너희들이 나 같은 어리석은 실수를하지 않기를 바란다. 수정 한 사람 :

     unset spring_profiles_active 
    
  8. from https://stackoverflow.com/questions/27131143/spring-cloud-configuration-server-not-working-with-local-properties-file by cc-by-sa and MIT license