spring boot不使用consul属性

时间:2016-09-19 20:58:19

标签: java spring spring-boot spring-cloud consul

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

@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数据库。它试图连接到localhost的mongo db,而不是我的属性中的那个

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

以下是领事的开发简介

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 boot应用程序指向同一个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,在运行配置中,我在启动应用程序时使用了依赖项目中的主类,如下所示,但我运行的项目是包含上面的应用程序类的项目。 / p>

@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,com / app和com / app,dev和com / app,im3中有3个配置文件。 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

1 个答案:

答案 0 :(得分:1)

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

-Dspring.profiles.active=dev,im3

如果删除

,它就不起作用
spring: 
  profiles:
    active: dev,im3
来自com / app的

,我需要两者似乎是多余的