解決No spring.config.import property has been defined
在學(xué)習(xí)Nacos組件,加載多配置集時(shí)遇到問(wèn)題咪辱,希望微服務(wù)在讀取配置文件之前先去nacos中讀取配置文件優(yōu)先于本地配置猜惋。配置了bootstrap.properties
后啟動(dòng)項(xiàng)目報(bào)錯(cuò):
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
第一個(gè)辦法
后續(xù)找解決方案發(fā)現(xiàn)是因?yàn)樵趕pringcloud 2020.0.2版本中把bootstrap的相關(guān)依賴從spring-cloud-starter-config中移除了令花,所以現(xiàn)在在2020.02 以后需要單獨(dú)引入spring-cloud-starter-bootstrap 依賴中捆。版本對(duì)應(yīng)網(wǎng)站:https://mvnrepository.com/
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.3</version>
</dependency>
刷新依賴以后正常運(yùn)行遏餐,配置也讀取到了霹粥。
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
第二個(gè)辦法
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