1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > springcloud v.0.1 导致bootstrap.yml文件配置不生效的问题

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

时间:2020-04-01 03:49:25

相关推荐

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

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

<spring.boot.version>2.6.3</spring.boot.version><spring.cloud.version>.0.1</spring.cloud.version><spring.cloud.alibaba.version>.0.1.0</spring.cloud.alibaba.version>

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

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

#bootstrap.ymlspring:cloud:nacos:discovery:server-addr: 192.168.111.131:8848namespace: devgroup: DEMOconfig:server-addr: 192.168.111.131:8848namespace: dev #命令空间按照环境制定group: DEMO #分组按照业务制定auto-refresh: truefile-extension: yaml

#application.ymlserver:port: 9000spring:application:name: gatewayprofiles:active: devconfig: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: .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 thespring-cloud-starter-bootstrapstarter. The property isspring.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 thespring.cloud.config.uribootstrap configuration property) and initializes SpringEnvironmentwith remote property sources.

The net result of this behavior is that all client applications that want to consume the Config Server need abootstrap.yml(or an environment variable) with the server address set inspring.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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。