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>

参考

  • Nacos踩坑:SpringBoot启动不加载bootstrap.yml

https://blog.csdn.net/weixin_63133634/article/details/120855779?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_title~default-0.no_search_link&spm=1001.2101.3001.4242.1

  • 记一个使用Nacos的问题:bootstrap.yml和application.yml配置不生效

https://blog.csdn.net/qq_36369267/article/details/119035688

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 中,可以使用 `bootstrap.yml` 文件来配置应用程序的属性。在使用 Nacos 作为配置中心时,我们可以将应用程序的配置文件存储在 Nacos 中,然后通过 `bootstrap.yml` 文件来加载这些配置文件。 下面是一个示例 `bootstrap.yml` 文件的编写和解释: ```yaml spring: cloud: nacos: config: server-addr: ${NACOS_SERVER_ADDR:127.0.0.1:8848} # Nacos 服务器地址 namespace: ${NACOS_NAMESPACE:default} # Nacos 命名空间 group: ${NACOS_GROUP:DEFAULT_GROUP} # 配置组 file-extension: ${NACOS_FILE_EXTENSION:yml} # 配置文件扩展名 prefix: ${NACOS_PREFIX:} # 配置文件前缀 suffix: ${NACOS_SUFFIX:} # 配置文件后缀 timeout: ${NACOS_TIMEOUT:5000} # Nacos 连接超时时间 discovery: server-addr: ${NACOS_SERVER_ADDR:127.0.0.1:8848} # Nacos 服务器地址 namespace: ${NACOS_NAMESPACE:default} # Nacos 命名空间 group: ${NACOS_GROUP:DEFAULT_GROUP} # 服务组 port: ${NACOS_PORT:8080} # 服务端口 ``` 这个示例中,我们使用了 Spring Cloud AlibabaNacos 配置中心,通过 `spring.cloud.nacos.config` 和 `spring.cloud.nacos.discovery` 属性来配置 Nacos 的相关参数。 对于加载指定的配置文件,我们可以在 Nacos 中创建对应的配置文件,例如 `redisson.yml` 和 `redisson2.yml`,然后在 `bootstrap.yml` 文件中通过 `spring.cloud.nacos.config.prefix` 属性来指定前缀。如果前缀为 `redisson`,那么 Nacos 中以 `redisson` 开头的配置文件都会被加载。 示例: ```yaml spring: cloud: nacos: config: prefix: redisson # 配置文件前缀 ``` 以上为示例代码,将会加载 Nacos 中以 `redisson` 开头的所有配置文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值