gary69
gary69

Reputation: 4230

spring boot not using consul properties

I have an application annotated like this

@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());
    }

}

I want it to use consul, and it is getting the active profiles from consul because I see this line in the logs when starting it

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

However its not using the mongo and mysql databases I've specified in the dev profile. Its trying to connect to a mongo db at localhost which is not the one in my properties

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

and liquibase is trying to use a default database not listed in my properties

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

Here is the dev profile from 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

I have another spring boot application pointing to the same consul instance and using the same profiles and that one is working. I also have bootstrap.yml in my classpath

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

I'm using eclipse and in the run configurations I'm using a main class from a dependent project when starting the app that looks like below, but the project I am running from is the project containing the application class above.

@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);
    }
}

Update The app that's working states the profiles being used in the log

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/']]]

I have 3 config files in consul, com/app and com/app,dev and com/app,im3. com/app looks like this

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

Upvotes: 1

Views: 2569

Answers (1)

gary69
gary69

Reputation: 4230

Stupid mistake, I defined the environment variables but not the VM argument

-Dspring.profiles.active=dev,im3

it doesn't work if I remove

spring: 
  profiles:
    active: dev,im3

from com/app, it seems redundant that I need both

Upvotes: 1

Related Questions