Param ‘serviceName‘ is illegal, serviceName is blank,SpringCloudAlibaba踩坑记录

0 前言

Param ‘serviceName‘ is illegal, serviceName is blank,SpringCloudAlibaba

重新梳理一下nacos和gateway的配置流程,把之前配置好pom文件重新梳理一下,发现gateway服务报错了。

当前环境配置

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

官方推荐配置

在这里插入图片描述
地址链接,具体配置看官网文档
https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E

报错内容

java.lang.IllegalArgumentException: Param 'serviceName' is illegal, serviceName is blank

配置文件中配置的端口号为 88但是实际启动端口为8080!!!
在这里插入图片描述

问题分析

后来对比源码是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 配置程序参数

配置方法可以参考这个两个链接,不同IDEA版本配置方法不一样

https://blog.csdn.net/qq_42164368/article/details/108073538 IDEA参数配置方法
https://blog.csdn.net/weixin_43423377/article/details/118222124 2021新版idea为java程序添加启动参数

spring.cloud.bootstrap.enabled=true
在这里插入图片描述

2 增加POM文件配置

根据自己的版本指定version,查看文档看对应的spring boot版本!!!

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

参考

https://developer.aliyun.com/article/937388
SpringCloudAlibaba踩坑日记(一)nacos报错: Param ‘serviceName‘ is illegal, serviceName is blank

猜你喜欢

转载自blog.csdn.net/qq_41398619/article/details/133121332