spring 不使用 consul 属性启动

spring boot not using consul properties

我有一个这样注释的应用程序

@SpringBootApplication(exclude = {DefaultNotificationServiceConfig.class})
@EnableDiscoveryClient
@EnableSwagger2
@EnableSpringDataWebSupport
@EnableJpaRepositories(basePackages = {"com.repositories.jpa"})
@EnableMongoRepositories(basePackages = {"com.repositories.mongo"})
public class DemoApplication {

    private static ApplicationContext ctx;

    public static void main(String[] args) {
        ctx = SpringApplication.run(new Object[]{DemoApplication.class}, args);
        System.out.println(ctx.getEnvironment());
    }

}

我希望它使用 consul,它正在从 consul 获取活动配置文件,因为我在启动它时在日志中看到了这一行

2016-09-19 20:38:29.947  INFO 9556 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='consul', propertySources=[ConsulPropertySource [name='com/app/'], ConsulPropertySource [name='com/application/']]]
2016-09-19 20:38:29.975  INFO 9556 --- [           main] com.Application           : The following profiles are active: dev,im3

但是它没有使用我在开发配置文件中指定的 mongo 和 mysql 数据库。它试图连接到本地主机上的 mongo 数据库,这不是我的属性

中的数据库
Caused by: com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting for a server that matches AnyServerSelector{}. Client view of cluster state is {type=Unknown, servers=[{address=localhost:27017

liquibase 正在尝试使用我的属性中未列出的默认数据库

INFO 9/19/16 8:38 PM: liquibase: Creating database history table with name: PUBLIC.DATABASECHANGELOG

这是来自 consul 的开发配置文件

server:
  port: 8090
spring:
  redis:
    host: ubuntu
    port: 6379
  data:
    mongodb:
      uri: mongodb://ubuntu:27017/test
  datasource:
    url: jdbc:mysql://ubuntu:3309/test
    username: uesr
    password: password
    driverClassName: com.mysql.jdbc.Driver
    testOnBorrow: true
    validationQuery: select 1
multipart:
  maxFileSize: 250MB
  maxRequestSize: 250MB

我有另一个 spring 引导应用程序指向同一个 consul 实例并使用相同的配置文件,并且那个正在运行。我的 classpath

中也有 bootstrap.yml
spring:
  application:
    name: ${APPLICATION_NAME:app}
  cloud:
    consul:
      host: ${CONSUL_HOST:localhost}
      port: ${CONSUL_PORT:8500}
      config:
        format: YAML
        watch:
          delay: 10
        prefix: app

我正在使用 eclipse,在 运行 配置中,当启动如下所示的应用程序时,我正在使用来自依赖项目的主要 class,但我的项目是 运行ning from 是包含上述应用程序 class 的项目。

@SpringBootApplication(exclude = {DefaultNotificationServiceConfig.class})
@EnableSpringDataWebSupport
@EnableDiscoveryClient
@EnableSwagger2
@EnableJpaRepositories(basePackages = {"com.repositories.jpa"})
@EnableMongoRepositories(basePackages = {"com.repositories.mongo"})
public class Application {

    private static ApplicationContext ctx;

    public static void main(String[] args) throws Exception {
        TimeZone.setDefault(TimeZone.getTimeZone("Etc/UTC"));
        ctx = SpringApplication.run(new Object[]{Application.class}, args);
        System.out.println(ctx);
    }
}

更新 正在运行的应用程序说明了日志中正在使用的配置文件

2016-09-19 23:53:55.265  INFO 9744 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='consul', propertySources=[ConsulPropertySource [name='com/app,im3/'], ConsulPropertySource [name='com/app,dev/'], ConsulPropertySource [name='com/app/'], ConsulPropertySource [name='com/application,im3/'], ConsulPropertySource [name='com/application,dev/'], ConsulPropertySource [name='com/application/']]]

我在 consul 中有 3 个配置文件,com/app 和 com/app,dev 和 com/app,im3。 com/app 看起来像这样

server:
  session:
    timeout: 600
spring: 
  profiles:
    active: dev,im3
  jpa:  
    hibernate: 
      ddl-auto: none
      naming-strategy: com.config.OracleNamingStrategy
    show-sql: false
  thymeleaf:
    mode: LEGACYHTML5
logging:
  level:
    org:
      springframework: ERROR
      thymeleaf: ERROR
      hibernate: ERROR
    springfox: ERROR
    com:
      bosch:
        mercurio: DEBUG

愚蠢的错误,我定义了环境变量但没有定义 VM 参数

-Dspring.profiles.active=dev,im3

如果我删除它不起作用

spring: 
  profiles:
    active: dev,im3

来自com/app,我需要两者似乎是多余的