我正在尝试为特定配置文件配置spring cloud consul,但当我尝试运行应用程序时,我得到以下错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Config data location 'consul:' does not exist
Action:
Check that the value 'consul:' at class path resource [application-local.properties] - 6:22 is correct, or prefix it with 'optional:'
Process finished with exit code 1
如果我在application-local.properties
中将spring.config.import
更新为spring.config.import=optional:consul:
,则忽略consul配置,并使用属性文件中的配置而不是consul。
application.properties
:
spring.cloud.consul.enabled=false
application-local.properties
:
spring.cloud.consul.enabled=true
spring.config.import=consul:
spring.cloud.consul.config.enabled=true
spring.cloud.consul.config.watch.enabled=true
spring.cloud.consul.config.watch.delay=6000
spring.cloud.consul.config.watch.wait-time=1
spring.cloud.consul.config.profile-separator=_
似乎Spring没有用application-local.properties
中指定的配置覆盖默认配置。我还尝试在bootstrap.properties
和bootstrap-local.properties
中配置consul,但我得到了以下错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
No spring.config.import property has been defined
Action:
Add a spring.config.import=consul: property to your configuration.
If configuration is not required add spring.config.import=optional:consul: instead.
To disable this check, set spring.cloud.consul.config.enabled=false or
spring.cloud.consul.config.import-check.enabled=false.
我使用org.springframework.cloud:spring-cloud-starter-consul-config:3.0.3
和spring boot 2.4.7
提前谢谢你。
尝试在您的pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
我以前就这样做过,所以首先你可以添加下面的依赖性:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
,其次,创建一个引导程序。yml文件。您可以在这里设置您的配置文件,并有单独的配置,如下所示:
spring:
profiles:
active: dev
include:
- consul
# - offline
cloud:
consul:
host: 10.20.8.67
port: 8500
config:
enabled: false
discovery:
heartbeat:
enabled: true
ttl: 5s
acl-token: 4319cf67-2690-62b3-a8bb-623d79b92737
service-registry:
enabled: true
retry:
enabled: true
max-attempts: 1
application:
name: servicename
server:
port: 8090
---
spring:
config:
activate:
on-profile: offline
cloud:
bus:
enabled: false
consul:
config:
enabled: false
application:
name: servicename
在这种情况下,如果您将include配置文件设置为脱机,则脱机部分属性将使用和禁用您的配置领事。与引导。您可以在consul中启用或禁用发现或配置特性。您也可以直接使用活动配置文件,而不是包含配置文件组。