解决 Spring Cloud 2021.0.5 版本,使用 nacos 做配置中心,报 No spring.config.import property has been defined 的问题

报错信息如下

Description:
No spring.config.import property has been defined

Spring 官方给出的解决方案如下

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

这里只尝试了第一种解决方案:
首先,2021.0.5版本的 Spring Cloud 默认不再启用 bootstrap 包,因此应该将配置文件写在 application.yml 中,或手动在 maven 中导入 bootstrap 包
然后,在 application.yml 中,按照以下方式配置

spring:
  application:
    name: XXXX
  cloud:
    nacos:
      server-addr: XX.XX.XX.XX:8848
      config:
        group: XXXX
        namespace: XXXX
# SpringCloud 2021版本之后,需要用以下方式导入nacos的配置文件
  config:
    import: nacos:XXXX.yml

问题解决。

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Cloud 2021.0.5.0 是 Spring Cloud 的一个版本,它可以和 Sentinel-datasource-nacos 模块一起使用。下面是使用 Spring Cloud 2021.0.5.0 和 Sentinel-datasource-nacos 的步骤: 1. 在 pom.xml 文件中引入以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2021.0.5.0</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId> <version>2.2.5.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-nacos-config</artifactId> <version>2.2.5.RELEASE</version> </dependency> ``` 2. 配置 Nacos 作为 Sentinel 的数据源,例如: ```yaml spring: cloud: sentinel: transport: dashboard: localhost:8080 port: 8719 nacos: server-addr: localhost:8848 groupId: DEFAULT_GROUP dataId: sentinel rule-type: flow ``` 3. 在 Nacos 中创建对应的命名空间和配置文件,例如: ```yaml spring: cloud: sentinel: transport: dashboard: localhost:8080 port: 8719 nacos: server-addr: localhost:8848 groupId: DEFAULT_GROUP dataId: sentinel rule-type: flow data-type: json namespace: 7e1d4f1f-8a31-4f0d-9f8d-0d4a563beba4 ``` 4. 在应用程序中添加 Sentinel 规则,例如: ```java @PostConstruct public void init() throws Exception { String appName = env.getProperty("spring.application.name"); ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(nacosServerAddr, groupId, appName + "-flow-rule", source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {})); FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); } ``` 以上是 Spring Cloud 2021.0.5.0 使用 Sentinel-datasource-nacos 的基本步骤,具体实现可以根据项目需要进行调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值