Nacos-SpringBoot框架启动不加载bootstrap.yml的解决

 更新时间:2022年11月22日 11:26:20   作者:Eistert  
这篇文章主要介绍了Nacos-SpringBoot框架启动不加载bootstrap.yml的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

背景

nacos server 源码运行在本地自己创建的项目中。

正常启动的话,SpringBoot框架会自动加载resource目录下的bootstrap.yml文件中的配置,日志记录会打印相关日志:

在这里插入图片描述

Connected to the target VM, address: '127.0.0.1:54696', transport: 'socket'
2021-12-21 16:18:44.639  INFO 8342 --- [           main] c.a.n.c.l.StartingApplicationListener    : Nacos started successfully in stand alone mode. use external storage

         ,--.
       ,--.'|
   ,--,:  : |                                           Nacos 
,`--.'`|  ' :                       ,---.               Running in stand alone mode, All function modules
|   :  :  | |                      '   ,'\   .--.--.    Port: 8848
:   |   \ | :  ,--.--.     ,---.  /   /   | /  /    '   Pid: 8342
|   : '  '; | /       \   /     \.   ; ,. :|  :  /`./   Console: http://192.168.100.43:8848/nacos/index.html
'   ' ;.    ;.--.  .-. | /    / ''   | |: :|  :  ;_
|   | | \   | \__\/: . ..    ' / '   | .; : \  \    `.      https://nacos.io
'   : |  ; .' ," .--.; |'   ; :__|   :    |  `----.   \
|   | '`--'  /  /  ,.  |'   | '.'|\   \  /  /  /`--'  /
'   : |     ;  :   .'   \   :    : `----'  '--'.     /
;   |.'     |  ,     .-./\   \  /            `--'---'
'---'        `--`---'     `----'

2021-12-21 16:18:46.141  INFO 8342 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=71ca1145-0798-3a30-a497-d6b8d15b4f05
2021-12-21 16:18:46.504  INFO 8342 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8848 (http)
2021-12-21 16:18:46.704  INFO 8342 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1780 ms
2021-12-21 16:18:48.714  INFO 8342 --- [           main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: class path resource [static/index.html]
2021-12-21 16:18:49.190  INFO 8342 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Will secure Ant [pattern='/**'] with []
2021-12-21 16:18:49.222  INFO 8342 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Will secure any request with [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@20616795, org.springframework.security.web.context.SecurityContextPersistenceFilter@74de1e9d, org.springframework.security.web.header.HeaderWriterFilter@1a927db2, org.springframework.security.web.csrf.CsrfFilter@14abde3e, org.springframework.security.web.authentication.logout.LogoutFilter@7170da18, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@43b9ef41, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@191d69a, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@1b61c1, org.springframework.security.web.session.SessionManagementFilter@4e22fdc0, org.springframework.security.web.access.ExceptionTranslationFilter@281f2d6c]
2021-12-21 16:18:49.270  INFO 8342 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 16 endpoint(s) beneath base path '/actuator'
2021-12-21 16:18:49.374  INFO 8342 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8848 (http) with context path '/nacos'
2021-12-21 16:18:49.396  INFO 8342 --- [           main] c.a.n.c.l.StartingApplicationListener    : Nacos started successfully in stand alone mode. use external storage
2021-12-21 16:18:49.689  INFO 8342 --- [-192.168.100.43] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-12-21 16:18:49.691  INFO 8342 --- [-192.168.100.43] o.s.web.servlet.DispatcherServlet        : Completed initialization in 2 ms

但是我把这块的nocos代码原封不动的copy到的自己的一个项目中,修改了bootstrap.yml文件中的数据库连接。

然后启动日志打印却显示使用的是embedded storage。

日志如下:

Connected to the target VM, address: '127.0.0.1:54270', transport: 'socket'

         ,--.
       ,--.'|
   ,--,:  : |                                           Nacos 
,`--.'`|  ' :                       ,---.               Running in stand alone mode, All function modules
|   :  :  | |                      '   ,'\   .--.--.    Port: 8848
:   |   \ | :  ,--.--.     ,---.  /   /   | /  /    '   Pid: 8132
|   : '  '; | /       \   /     \.   ; ,. :|  :  /`./   Console: http://192.168.100.43:8848/nacos/index.html
'   ' ;.    ;.--.  .-. | /    / ''   | |: :|  :  ;_
|   | | \   | \__\/: . ..    ' / '   | .; : \  \    `.      https://nacos.io
'   : |  ; .' ," .--.; |'   ; :__|   :    |  `----.   \
|   | '`--'  /  /  ,.  |'   | '.'|\   \  /  /  /`--'  /
'   : |     ;  :   .'   \   :    : `----'  '--'.     /
;   |.'     |  ,     .-./\   \  /            `--'---'
'---'        `--`---'     `----'

2021-12-21 16:17:11.755  WARN 8132 --- [           main] o.s.boot.StartupInfoLogger               : InetAddress.getLocalHost().getHostName() took 5002 milliseconds to respond. Please verify your network configuration (macOS machines may need to add entries to /etc/hosts).
2021-12-21 16:17:20.760  INFO 8132 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8848 (http)
2021-12-21 16:17:21.339  INFO 8132 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 4410 ms
2021-12-21 16:17:31.806  INFO 8132 --- [           main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: class path resource [static/index.html]
2021-12-21 16:17:32.562  INFO 8132 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Will secure Ant [pattern='/**'] with []
2021-12-21 16:17:32.627  INFO 8132 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Will secure any request with [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@51a06fd5, org.springframework.security.web.context.SecurityContextPersistenceFilter@53c0424a, org.springframework.security.web.header.HeaderWriterFilter@360432d3, org.springframework.web.filter.CorsFilter@71458178, org.springframework.security.web.authentication.logout.LogoutFilter@18487f92, com.alibaba.nacos.filter.JwtAuthenticationTokenFilter@26edfa34, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@408a0e92, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@51a65f56, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@776b7fa2, org.springframework.security.web.session.SessionManagementFilter@6391d34f, org.springframework.security.web.access.ExceptionTranslationFilter@66978c15, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@2e7cd4bb]
2021-12-21 16:17:32.664  INFO 8132 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 1 endpoint(s) beneath base path '/actuator'
2021-12-21 16:17:32.783  INFO 8132 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8848 (http) with context path '/nacos'
2021-12-21 16:17:32.829  INFO 8132 --- [           main] c.a.n.c.l.StartingApplicationListener    : Nacos started successfully in stand alone mode. use embedded storage
2021-12-21 16:17:33.599  INFO 8132 --- [-192.168.100.43] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-12-21 16:17:33.602  INFO 8132 --- [-192.168.100.43] o.s.web.servlet.DispatcherServlet        : Completed initialization in 2 ms

原因

原因一

看日志应该发现是服务没有读取到bootstrap.yml文件中的配置。应该就是定位到是bootstrap.yml没有被加载。

只要全局引入bootstrap 启动器和配置文件处理器就可以解决这个问题

        <!--bootstrap 启动器-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
        <!--配置文件处理器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!--配置文件加解密-->
        <dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>${jasypt.version}</version>
        </dependency>

原因二

也有可能是自己pom文件排除掉了相关结尾的文件,删掉过滤器就行。

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • springboot集成RestTemplate及常见的用法说明

    springboot集成RestTemplate及常见的用法说明

    这篇文章主要介绍了springboot集成RestTemplate及常见的用法说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • springboot 去掉netflix 禁用Eureka的解决方法

    springboot 去掉netflix 禁用Eureka的解决方法

    这篇文章主要介绍了springboot 去掉netflix 禁用Eureka的解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-09-09
  • 浅析12306售票算法(java版)

    浅析12306售票算法(java版)

    这篇文章主要介绍了浅析12306售票算法(java版)的相关资料,需要的朋友可以参考下
    2016-02-02
  • SpringBoot读取resource目录下文件失败的原因及解决方案

    SpringBoot读取resource目录下文件失败的原因及解决方案

    在idea中运行时,有些resource下文件读取工具类能够正常获取读取到文件,但是通过java–jar的方式去运行jar包,此时resource下文件读取工具类读取文件就失效了,本文就给大家介绍一下SpringBoot读取resource目录下文件失败解决方案,需要的朋友可以参考下
    2023-08-08
  • Java对象转换的方案分享

    Java对象转换的方案分享

    这篇文章主要介绍了Java对象转换的方案分享,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的朋友可以参考一下
    2022-09-09
  • Spring Boot + Jpa(Hibernate) 架构基本配置详解

    Spring Boot + Jpa(Hibernate) 架构基本配置详解

    本篇文章主要介绍了Spring Boot + Jpa(Hibernate) 架构基本配置详解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • 详解spring boot 以jar的方式启动常用shell脚本

    详解spring boot 以jar的方式启动常用shell脚本

    本篇文章主要介绍了详解spring boot 以jar的方式启动常用shell脚本,具有一定的参考价值,有兴趣的可以了解一下
    2017-09-09
  • Java判断对象是否为空的四种方法小结

    Java判断对象是否为空的四种方法小结

    这篇文章主要介绍了Java判断对象是否为空的四种方法,判断对象是否为空有多种方法,包括使用==或!=运算符直接比较对象与null,使用Objects.isNull()方法,以及用instanceof运算符或Optional类进行更安全的空值处理,需要的朋友可以参考下
    2024-10-10
  • myeclipse创建servlet_动力节点Java学院整理

    myeclipse创建servlet_动力节点Java学院整理

    这篇文章主要为大家详细介绍了myeclipse创建servlet的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • 如何用Java的swing编写简单计算器

    如何用Java的swing编写简单计算器

    这篇文章主要给大家介绍了关于如何用Java的swing编写简单计算器的相关资料,通过本文可以设计一个图形界面的简易计算器,完成简单的算术运算符,可以完成加法、减法、乘法、除法和取余运算,需要的朋友可以参考下
    2023-12-12

最新评论