SpringCloudAlibaba踩坑日记(一)nacos报错: Param ‘serviceName‘ is illegal, serviceName is blank

前言

这俩天闲来无事想搭建一套最新版本的微服务,顺便写博客记录一下,我用的是当前时间(2022-04-14)最新版本

在这里插入图片描述
在这里插入图片描述

报错内容

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.2.jar:na]
后来对比源码是SpringBoot2.4之后不会默认加载bootstrap.yaml
  • 2.4之前版本 spring.cloud.bootstrap.enabled = true
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
    
    
        ConfigurableEnvironment environment = event.getEnvironment();
        if ((Boolean)environment.getProperty("spring.cloud.bootstrap.enabled", Boolean.class, true)) {
    
    
            if (!environment.getPropertySources().contains("bootstrap")) {
    
    
                ConfigurableApplicationContext context = null;
                String configName = environment.resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}");
                Iterator var5 = event.getSpringApplication().getInitializers().iterator();

                while(var5.hasNext()) {
    
    
                    ApplicationContextInitializer<?> initializer = (ApplicationContextInitializer)var5.next();
                    if (initializer instanceof ParentContextApplicationContextInitializer) {
    
    
                        context = this.findBootstrapContext((ParentContextApplicationContextInitializer)initializer, configName);
                    }
                }

                if (context == null) {
    
    
                    context = this.bootstrapServiceContext(environment, event.getSpringApplication(), configName);
                    event.getSpringApplication().addListeners(new ApplicationListener[]{
    
    new BootstrapApplicationListener.CloseContextOnFailureApplicationListener(context)});
                }

                this.apply(context, event.getSpringApplication(), environment);
            }
        }
    }
  • 2.4之后版本 spring.cloud.bootstrap.enabled = false
public static boolean bootstrapEnabled(Environment environment) {
    
    
        return (Boolean)environment.getProperty("spring.cloud.bootstrap.enabled", Boolean.class, false) || MARKER_CLASS_EXISTS;
    }

解决方案1 配置程序参数

spring.cloud.bootstrap.enabled=true 

在这里插入图片描述

解决方案2 添加bootstrap依赖

<!--SpringBoot2.4.x之后默认不加载bootstrap.yml文件,需要在pom里加上依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

猜你喜欢

转载自blog.csdn.net/weixin_43627706/article/details/124170850