记录一次新版本的spring cloud加载bootstrap.yml失效的问题现象:
报错信息(版本<spring-cloud.version>2020.0.4</spring-cloud.version>):
java.lang.IllegalArgumentException: Param 'serviceName' is illegal, serviceName is blank
at com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(NamingUtils.java:47) ~[nacos-api-1.4.1.jar:na]
at com.alibaba.nacos.client.naming.NacosNamingService.subscribe(NacosNamingService.java:457) ~[nacos-client-1.4.1.jar:na]
at com.alibaba.cloud.nacos.discovery.NacosWatch.start(NacosWatch.java:123) ~[spring-cloud-starter-alibaba-nacos-discovery-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) [spring-context-5.3.10.jar:5.3.10]
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) [spring-context-5.3.10.jar:5.3.10]
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) [spring-context-5.3.10.jar:5.3.10]
at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_151]
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) [spring-context-5.3.10.jar:5.3.10]
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) [spring-context-5.3.10.jar:5.3.10]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~[spring-context-5.3.10.jar:5.3.10]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.10.jar:5.3.10]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.5.jar:2.5.5]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-2.5.5.jar:2.5.5]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-2.5.5.jar:2.5.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) ~[spring-boot-2.5.5.jar:2.5.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.5.jar:2.5.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.5.jar:2.5.5]
at org.usersv.UserSVApp.main(UserSVApp.java:16) ~[classes/:na]
看了半天没看出哪里错了,查阅官网发现新版本的配置加载发生了变化
官网链接:Spring Cloud Config
官网描述:
Config First Bootstrap
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.
解决方案一:
按照官网的说法给出的解决方案是,在系统变量中增加spring.cloud.bootstrap.enabled=true
@SpringBootApplication
public class UserSVApp
{
public static void main( String[] args )
{
System.setProperty("spring.cloud.bootstrap.enabled", "true");
SpringApplication.run(UserSVApp.class, args);
}
}
解决方案二:
结合网上的其他方案,可以引入一个依赖包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>