2017-08-24 40 views
1

我發生了一些奇怪的事情。我不知道如何解決它,春天的雲配置不能加載本地或雲配置文件,除非它的文件名是'application.yml/application.properties'。春雲配置無法加載原生配置文件,除非文件名是應用程序

在下面的代碼是我的配置:

的pom.xml:

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.5.2.RELEASE</version> 
    <relativePath/> 
</parent> 
<modelVersion>4.0.0</modelVersion> 

<artifactId>micro-certification-config-center</artifactId> 
<version>1.0-SNAPSHOT</version> 
<packaging>jar</packaging> 

<dependencies> 
    <dependency> 
     <groupId>org.springframework.cloud</groupId> 
     <artifactId>spring-cloud-config-server</artifactId> 
    </dependency> 
</dependencies> 

<dependencyManagement> 
    <dependencies> 
     <dependency> 
      <groupId>org.springframework.cloud</groupId> 
      <artifactId>spring-cloud-dependencies</artifactId> 
      <version>Dalston.SR2</version> 
      <type>pom</type> 
      <scope>import</scope> 
     </dependency> 
    </dependencies> 
</dependencyManagement> 

ConfigServer:

@SpringBootApplication 
@EnableConfigServer 
public class ConfigServer { 

    public static void main(String[] args) { 
     SpringApplication.run(ConfigServer.class, args); 
    } 

} 

application.yml:

server: 
    port: 8000 

spring: 
    cloud: 
    config: 
     server: 
     native: 
      search-locations: classpath:/shared 
    profiles: 
    active: native 

共享文件夾structor :
個 資源
- 共享
- - 應用程序 - dev.yml
- - 短信dev.yml

它看起來不錯,並沒有錯誤運行良好,但是當我訪問http://127.0.0.1:8000/configCenter/dev/master,它只顯示應用程序屬性源。

響應:

{ 
    "name": "configCenter", 
    "profiles": [ 
     "dev" 
    ], 
    "label": "master", 
    "version": null, 
    "state": null, 
    "propertySources": [ 
     { 
      "name": "classpath:/shared/application-dev.yml", 
      "source": { 
       "logging.level.org.springframework.security": "INFO", 
       "eureka.instance.prefer-ip-address": true, 
       "eureka.client.serviceUrl.defaultZone": "http://registry:8761/eureka/", 
       "security.oauth2.resource.user-info-uri": "http://auth-service:5000/uaa/users/current", 
       "spring.rabbitmq.host": "rabbitmq" 
      } 
     } 
    ] 
} 

服務器控制檯:

2017-08-24 22:34:08.055 INFO 30010 --- [nio-8000-exec-4] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]4a931797: startup date [Thu Aug 24 22:34:08 CST 2017]; root of context hierarchy 
2017-08-24 22:34:08.062 INFO 30010 --- [nio-8000-exec-4] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring 
2017-08-24 22:34:08.075 INFO 30010 --- [nio-8000-exec-4] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: classpath:/shared/application-dev.yml 
2017-08-24 22:34:08.075 INFO 30010 --- [nio-8000-exec-4] s.c.a.AnnotationConfigApplicationContext : Closing org.spring[email protected]4a931797: startup date [Thu Aug 24 22:34:08 CST 2017]; root of context hierarchy 

上述一切都表明了 'application.yml' 文件只工作。

有人可以幫助我嗎?非常感謝!

+1

如果您希望加載'sms-dev.yml',則spring.application.name將需要sms。 – spencergibb

+0

哦,我得了,謝謝!@spencergibb – WhiteWater

回答

0

正如@spencergibb所說,如果我想加載sms-dev.yml,我應該將application.name重命名爲sms。

1

外部配置可以幫助你。細節是在這裏:

如果你不喜歡application.properties作爲配置文件 名:http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

您可以將此默認的應用程序配置的命令就像在指導線變更爲外部一個你可以通過指定一個spring.config.name 環境屬性來切換到另一個。您還可以使用spring.config.location環境屬性(目錄位置或文件路徑的逗號分隔列表 )引用顯式位置。

$ Java的罐子myproject.jar --spring.config.name = myproject的或

$ Java的罐子myproject.jar --spring.config.location =類路徑:/default.properties,classpath: /override.properties

或者在你的代碼,你可以把它想:

 new SpringApplicationBuilder(Application.class) 
        .properties("spring.config.name:YOUR_EXTERNAL_CONFIG_FILE") 
        .build() 
        .run(args); 
    } 

希望這有助於。

+0

對不起,我以爲spencergibb說的是對的。 – WhiteWater