Idea-SpringCloud-Nacos配置中心配置文件无法访问的问题

    技术2023-11-10  127

    **

    Idea-SpringCloud-Nacos配置中心配置文件无法访问的问题

    ** 在springcloud微服务体系中,注册中心是必不可少且非常重要的一大组件,从最初的Springcloud团队自提供的Eureka,到现在使用越来越多的阿里团队研发提供的Nacos。Nacos我相信大家肯定都有使用过,它不仅可以作为注册中使用,还可以作为配置中心使用,并且提供了相当丰富的功能。本文重点记录自己再使用nacos存放配置文件时,经常会遇到配置文件读取不到的问题的解决方案。如果对Nacos其他功能感兴趣的童鞋可前往Nacos官网学习。

    Nacos官网地址

    下面直接上干货: 在我们开发过程中使用Nacos存放配置文件时,经常会出现配置文件名称,组名,后缀名等都书写正确,但是无论如何项目就是访问不到Nacos服务上的配置文件,导致项目启动报错或者运行报错。

    *************************** APPLICATION FAILED TO START *************************** Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine suitable jdbc url Action: Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath. If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

    大家可以看到这是我项目启动时报错,提示很简单,就是因为配置文件中没有找到数据源的url配置信息导致项目启动报错。但是其实我的配置文件中已经配置了,如下是我的bootstrap.properties文件和Nacos配置中心的配置文件: bootstrap.properties:

    #注册中心地址 #发布的时候需要改为线上的注册中心地址 #spring.cloud.nacos.config.server-addr=77.1.24.51:8848 spring.cloud.nacos.config.server-addr=127.0.0.1:8848 #发布的时候需要改为线上的名称空间 #spring.cloud.nacos.config.namespace=cqzz-prod spring.cloud.nacos.config.namespace=cqzz-dev #配置组名子,需要在nacos的配置设置,不然找不到 spring.cloud.nacos.config.group=netsafe spring.cloud.nacos.config.prefix=service-netsafe #配置后缀,properties不需要 spring.cloud.nacos.config.file-extension=yml #刷新配置 spring.cloud.nacos.config.refreshable-dataids=service-netsafe.yml

    service-netsafe.yml:

    server: port: 10082 spring: application: name: service-netsafe cloud: nacos: discovery: server-addr: 127.0.0.1:8848 datasource: driver-class-name: oracle.jdbc.driver.OracleDriver username: XZXT password: XZXT url: jdbc:oracle:thin:@**.**.**.**:1600:orcl

    可以看到,我的配置基本没问题,但是项目启动时就报错。 其实问题解决方法很简单

    ps:本人开发使用的是idea

    遇到这中情况基本不是代码的问题,配置什么都正确的话,一般是idea的问题,项目重新强制打包clean package -U或者重启一下idea基本可解决。如果还是不行,那真的就是你配置的问题了。哈哈!!!!

    Processed: 0.010, SQL: 9