尚未定义 spring.config.import 属性



在创建 Spring 启动云配置应用程序时出现以下错误。 对此有任何帮助吗?

No spring.config.import property has been defined

Action:
Add a spring.config.import=configserver: property to your configuration.   
If configuration is not required add spring.config.import=optional:configserver: instead.
To disable this check, set spring.cloud.config.enabled=false or 
spring.cloud.config.import-check.enabled=false.

Maven的解决方案

pom.xml文件中添加以下依赖项:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

格拉德尔解决方案

build.gradle文件中添加:

implementation('org.springframework.cloud:spring-cloud-starter-bootstrap')

这解决了我的问题。

您收到此错误是因为您使用的是新版本的 Spring Boot 和 Spring Cloud,但您尝试以旧方式配置它。

原因

Spring 云配置客户端已更改,技术上bootstrap.propertiesbootstrap.yml文件已弃用

正确的解决方案

  1. 将所有属性从boostrap.properties移动到application.properties(也可以.yml)
  2. 删除bootstrap.properties文件
  3. spring.cloud.config.uri=http://localhost:8888替换为spring.config.import=configserver:http://localhost:8888

这是告诉您 Spring 启动应用程序要从localhost:8888上运行的Spring 云配置服务加载属性的正确方法。

传统解决方案

如果要使用旧版bootstrap.properties文件,只需添加以下依赖项:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

请注意,这是一种已弃用的机制,因此如果您要创建新项目,请继续使用正确的解决方案

根本原因是Spring Boot 2.4更改了其默认功能。新的spring.config.import属性是强制性的。

若要修复,请添加新的spring.config.import属性。这是在application.yml中对我有用的一个例子。

spring:
config:
import: "optional:configserver:"

以下是需要设置其他值时的文档:

Spring 引导配置数据导入

Spring Boot 2.4引入了一种通过spring.config.import属性导入配置数据的新方法。现在,这是绑定到配置服务器的默认方式。

从 Spring Boot 2.4 开始导入配置是通过spring.config.import功能完成的。

添加以下内容以application.properties连接到默认配置服务器 URLhttp://localhost:8888

spring.config.import=optional:configserver:

或 YML:

spring.config.import: "optional:configserver:"

参考文档中介绍了更多配置选项。

如果将org.springframework.cloud:spring-cloud-starter-bootstrap依赖项添加到项目中,则旧版引导功能仍然可用。

添加bootstrap.yml文件:

spring:
cloud:
config:
enabled: true
uri: http://localhost:9296

其中 9296 是云配置服务器端口

并添加以下依赖项:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

如果上述属性不起作用,只需将以下属性添加到application.yml文件中即可停止端口检查,因为您已经在bootstrap.yml中定义了端口检查

spring:
cloud:
config:
import-check:
enabled: false

我在实现客户端时遇到了这个问题spring-cloud-config。 我添加了bootstrap.yml来指定配置服务器地址。

稍后,在application.yml本身中添加了以下代码,从而解决了该问题。

spring:
application:
name: user-service
config:
import: optional:configserver:http://localhost:9004

错误消息本身具有针对解决方案的建议/操作:

Add a spring.config.import=configserver: property to your configuration.
If configuration is not required add spring.config.import= optional:configserver: instead.
To disable this check, set spring.cloud.config.enabled=false 
or
spring.cloud.config.import-check.enabled=false.

只需在pom中导入此依赖项.xml

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

注意:快照、M1、M2、M3 和 M4 版本通常正在进行中。Spring团队仍在研究它们,建议不要使用它们。

我在本地主机上的端口 8191、Spring 版本 3.1.0 和 Java 17 上使用 Spring Cloud Config Server

我对此有 2 个答案

  1. 像波纹管一样改变application.yml
spring:config:import: configserver:http://localhost:8191

在这种情况下,我没有bootsrap.yml文件

  1. 向 pom 添加新依赖项
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

并添加bootstrap.yml文件,在这种情况下是我的引导程序文件,例如

spring:cloud:config:uri: http://localhost:8191

在 SpringBoot v2.7.1 中,这个解决方案对我有用:

我的微服务bootstrap.properties文件:

spring.application.name=microservice-name
spring.cloud.config.uri=http://localhost:8888 # the URL of your server configuration

并添加以下依赖项:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

如果有人在测试阶段在 spring-boot 2.7.0 版本上传递此错误,请将以下内容放在 src/test/application.properties 上

spring.cloud.config.enabled=false

如果不想使用配置服务器进行测试。

添加此依赖项也解决了我的问题: 弹簧-云-启动器-引导

我在本地主机上的端口 8888、Spring 版本 2.5.4 和 Java 16 上使用 Spring 云配置服务器

我的引导属性:

spring.application.name=hr-worker
# Server Config
spring.cloud.config.enabled=true
spring.cloud.config.uri= http://localhost:8888

为了停止出现错误,我只是把这个配置放在我的应用程序属性中:

spring.config.import=optional:configserver:http://localhost:8888

我通过删除依赖项spring-cloud-starter-config克服了错误。 如果您保留它,那么它将继续提示。我认为使用较新的 Spring Boot 我们不需要依赖关系。

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

bootstrap.properties

spring.application.name=config-client-app
eureka.client.server-url.defaultZone=http://localhost:8761/eureka
spring.config.import=optional:configserver:

我正在使用的春季版本:

<spring-cloud.version>2020.0.3</spring-cloud.version>

我使用 spring 2.4.4 的版本克服了错误,那一刻我正在使用 2.5.1

应用程序属性:

`
spring.application.name=testserv
spring.profiles.active=prodtest
spring.config.import=optional:configserver:http://localhost:8071
spring.cloud.config.enabled=true
spring.cloud.config.uri=http://localhost:8071
management.endpoints.web.exposure.include=*
eureka.instance.preferIpAddress = true 
eureka.client.registerWithEureka = true
eureka.client.fetchRegistry = true
eureka.client.serviceUrl.defaultZone = http://localhost:8070/eureka/
`

2020.0.3

1- for maven add:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>

2- 对于 gradle 添加:

dependencies{ implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'}

3- 在应用程序中,属性替换(旧配置):

spring.cloud.config.uri=http://localhost:8888

通过这个:

spring.config.import=configserver:http://localhost:8888

在应用此问题提到的任何解决方案后。你可以试一试:

步骤1:如果在IntelliJ IDE上,右键单击项目的"pom.xml">文件。

步骤2:在上下文菜单上找到"Maven"。然后点击"重新加载项目"。

步骤3:接下来,尝试"mvn全新安装">并再次尝试运行。

PS:我仍在寻找它对我起作用的原因。(也许与maven有关)

我使用的是 Spring Boot 3.0.3 版本,要解决此问题,只需在 application.properties 文件中添加即可

spring.cloud.config.enabled=false

它将从本地属性文件中读取配置,而不是从 GIT 存储库(如果已配置)读取配置。

云: 配置: URI:http://localhost:9296(您的端口号)

这对我来说很好用。

对于春季版本> 2.6.6 在 application.property 中添加以下属性。

spring.application.name=...//应用程序名称

spring.profiles.active=prodtest

spring.config.import=optional:configserver:http://localhost:8071

spring.cloud.config.enabled=true

spring.cloud.config.uri=http://localhost:8071

management.endpoints.web.exposure.include=*

eureka.instance.preferIpAddress = true

eureka.client.registerWithEureka = true

eureka.client.fetchRegistry = true

eureka.client.serviceUrl.defaultZone = http://localhost:8070/eureka/

相关内容

最新更新