解决No spring.config.import property has been defined
在学习Nacos组件,加载多配置集时遇到问题,希望微服务在读取配置文件之前先去nacos中读取配置文件优先于本地配置。配置了bootstrap.properties
后启动项目报错:
Description:
No spring.config.import property has been defined
Action:
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.
Process finished with exit code 1
第一个办法
后续找解决方案发现是因为在springcloud 2020.0.2版本中把bootstrap的相关依赖从spring-cloud-starter-config中移除了,所以现在在2020.02 以后需要单独引入spring-cloud-starter-bootstrap 依赖。版本对应网站:https://mvnrepository.com/
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.3</version>
</dependency>
刷新依赖以后正常运行,配置也读取到了。
WebApplicationContext: initialization completed in 681 ms
_ _ |_ _ _|_. ___ _ | _
| | |\/|_)(_| | |_\ |_)||_|_\
/ |
3.3.1
2023-08-15 10:37:50.258 WARN 28016 --- [ main] iguration$LoadBalancerCaffeineWarnLogger : Spring Cloud LoadBalancer is currently working with the default cache. While this cache implementation is useful for development and tests, it's recommended to use Caffeine cache in production.You can switch to using Caffeine cache, by adding it and org.springframework.cache.caffeine.CaffeineCacheManager to the classpath.
2023-08-15 10:37:50.310 INFO 28016 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 100 (http) with context path ''
2023-08-15 10:37:50.317 INFO 28016 --- [ main] c.a.n.p.a.s.c.ClientAuthPluginManager : [ClientAuthPluginManager] Load ClientAuthService com.alibaba.nacos.client.auth.impl.NacosClientAuthServiceImpl success.
2023-08-15 10:37:50.318 INFO 28016 --- [ main] c.a.n.p.a.s.c.ClientAuthPluginManager : [ClientAuthPluginManager] Load ClientAuthService com.alibaba.nacos.client.auth.ram.RamClientAuthServiceImpl success.
2023-08-15 10:37:50.445 INFO 28016 --- [ main] c.a.c.n.registry.NacosServiceRegistry : nacos registry, DEFAULT_GROUP gulimall-coupon 192.168.79.1:100 register finished
2023-08-15 10:37:50.633 INFO 28016 --- [ main] c.a.g.coupon.GulimailCouponApplication : Started GulimailCouponApplication in 4.133 seconds (JVM running for 4.614)
2023-08-15 10:37:50.640 INFO 28016 --- [ main] c.a.c.n.refresh.NacosContextRefresher : [Nacos Config] Listening config: dataId=gulimall-coupon, group=dev
2023-08-15 10:37:50.642 INFO 28016 --- [ main] c.a.c.n.refresh.NacosContextRefresher : [Nacos Config] Listening config: dataId=gulimall-coupon.properties, group=dev
2023-08-15 10:37:50.642 INFO 28016 --- [ main] c.a.c.n.refresh.NacosContextRefresher : [Nacos Config] Listening config: dataId=other.yml, group=dev
2023-08-15 10:37:50.643 INFO 28016 --- [ main] c.a.c.n.refresh.NacosContextRefresher : [Nacos Config] Listening config: dataId=datasource.yml, group=dev
2023-08-15 10:37:50.643 INFO 28016 --- [ main] c.a.c.n.refresh.NacosContextRefresher : [Nacos Config] Listening config: dataId=mybatis-plus.yml, group=dev
第二个办法
spring:
application:
name: @artifactId@
cloud:
nacos:
username: @nacos.username@
password: @nacos.password@
discovery:
server-addr: ${NACOS_HOST:omp-register}:${NACOS_PORT:8848}
config:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
config:
import:
- optional:nacos:application-@profiles.active@.yml
- optional:nacos:${spring.application.name}-@profiles.active@.yml