springcloud v2021.0.1 导致bootstrap.yml文件配置不生效的问题

笔者最近使用以下组件版本搭建微服务,发现启动gateway服务时,报错:“failed to req API:/nacos/v1/ns/instance after all servers([127.0.0.1:8848]) tried: java.net.ConnectException: Connection refused: connect”,最终发现竟是springcloud版本的问题。

<spring.boot.version>2.6.3</spring.boot.version>
<spring.cloud.version>2021.0.1</spring.cloud.version>
<spring.cloud.alibaba.version>2021.0.1.0</spring.cloud.alibaba.version>

以上配置是官方比较推荐的稳定搭配版本,关于版本搭配想了解更多,可以参考《SpringBoot、SpringCloud、SpringCloudAlibaba的版本对应关系》

笔者项目的配置文件包含:bootstrap.yml和application.yml两个文件,内容如下:

#bootstrap.yml
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.111.131:8848
        namespace: dev
        group: DEMO
      config:
        server-addr: 192.168.111.131:8848
        namespace: dev  #命令空间按照环境制定
        group: DEMO #分组按照业务制定
        auto-refresh: true
        file-extension: yaml
#application.yml
server:
  port: 9000

spring:
  application:
    name: gateway
  profiles:
    active: dev
  config:
    use-legacy-processing: true

为了证明项目的bootstrap.yml文件未成功加载,笔者在bootstrap.yml中加入server.port=8000的配置信息,观察程序启动日志中的监听端口号具体是多少,发现仍然是9000,因此可以判定bootstrap.yml文件未成功加载,所以注册服务和配置服务都读取不到,所以默认只有本地(127.0.0.1)一个nacos服务实例,而实际情况,我本地并没有启动nacos服务,所以报错:failed to req API:/nacos/v1/ns/instance after all servers([127.0.0.1:8848]) tried: java.net.ConnectException: Connection refused: connect。

那上述问题是怎么来的呢?

从Spring Boot 2.4版本开始,配置文件加载方式进行了重构。另外也有配置的默认值变化,原来加载bootstrap.yml文件的默认开关由 true 变更为 false。 

// v2.4之前
package org.springframework.cloud.bootstrap;
public class BootstrapApplicationListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
        ConfigurableEnvironment environment = event.getEnvironment();
        if ((Boolean)environment.getProperty("spring.cloud.bootstrap.enabled", Boolean.class, true)) {
        }
    }
}

 // v2.4之后
package org.springframework.cloud.util;
public abstract class PropertyUtils {
    public static boolean bootstrapEnabled(Environment environment) {
        return (Boolean)environment.getProperty("spring.cloud.bootstrap.enabled", Boolean.class, false) || MARKER_CLASS_EXISTS;
    }
}

如何解决上述问题呢?

官网描述如下:

To use the legacy bootstrap way of connecting to Config Server, bootstrap must be enabled via a property or the spring-cloud-starter-bootstrap starter. The property is spring.cloud.bootstrap.enabled=true. It must be set as a System Property or environment variable. Once bootstrap has been enabled any application with Spring Cloud Config Client on the classpath will connect to Config Server as follows: When a config client starts, it binds to the Config Server (through the spring.cloud.config.uri bootstrap configuration property) and initializes Spring Environment with remote property sources.

The net result of this behavior is that all client applications that want to consume the Config Server need a bootstrap.yml (or an environment variable) with the server address set in spring.cloud.config.uri (it defaults to "http://localhost:8888").

根据官网的描述,有两种解决方案(推荐第一种)

(1)增加依赖:spring-cloud-starter-bootstrap,具体代码如下:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

(2)通过设置环境变量和系统变量的方式显示配置启动加载bootstrap.yml文件,具体配置参数和值如下:

spring.cloud.bootstrap.enabled=true

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萧十一郎君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值