【发布时间】:2017-01-27 15:56:48
【问题描述】:
我有一个这样注释的应用程序
@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 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
这是来自 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 boot 应用程序指向同一个 consul 实例并使用相同的配置文件并且那个正在工作。我的类路径中也有 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,并且在运行配置中,我在启动如下所示的应用程序时使用了来自依赖项目的主类,但我正在运行的项目是包含上述应用程序类的项目。
@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
【问题讨论】:
-
一个可以工作而另一个不能工作没有多大意义。没有什么明显的跳出来。也许您有一个重现问题的小样本?
-
配置文件似乎处于活动状态,但没有被使用
-
/env说什么? -
我无法启动应用程序,因为它无法连接到数据库或解析我需要注入的属性
-
没什么好猜的了。能否提供一个示例项目?
标签: java spring spring-boot spring-cloud consul