项目环境

Spring Cloud Alibaba: 2.2.6.RELEASE
Spring Cloud: Hoxton.SR9
Spring Boot: 2.3.7.RELEASE
Nacos: 2.2.0


问题描述

今天在把Gateway注册到Nacos的过程中时,Gateway所在微服务的SpringApplication启动失败,并抛出如下所示的异常

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]
	at com.alibaba.nacos.client.naming.NacosNamingService.subscribe(NacosNamingService.java:457) ~[nacos-client-1.4.2.jar:na]
	at com.alibaba.cloud.nacos.discovery.NacosWatch.start(NacosWatch.java:131) ~[spring-cloud-starter-alibaba-nacos-discovery-2.2.6.RELEASE.jar:2.2.6.RELEASE]
	at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:182) [spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]
	at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:53) [spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]
	at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:360) [spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]
	at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:158) [spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]
	at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:122) [spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:895) [spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:554) [spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]
	at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:62) [spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:405) [spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE]
	at com.gn.GatewayApplication.main(GatewayApplication.java:9) [classes/:na]

原因分析

认真查找了报错原因发现这个错误的是由于在discovery过程中找不到serviceName造成的,需要在bootstrap/application配置文件中配置,但我的bootstrap.yaml如下所示,已经配置了相应的spring.application.name

# 微服务配置
spring:
  application:
    name: gateway
  cloud:
    nacos:
      server-addr: 192.168.88.66:8848
      discovery:
        namespace: ${spring.profiles.active}
        group: yanxin-class
      config:
        namespace: ${spring.profiles.active}
        group: yanxin-class
        file-extension: yaml
        refresh-enabled: true
  profiles:
    active: dev

这里我初步怀疑是没有成功加载bootstrap导致的,在网上找到一种说法,也是出现此错误的主流解释,SpringBoot 2.4.之后的版本不再默认加载bootstrap文件,需要手动引入下面的依赖

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

但这并没有解决我的问题,重启后还是报同样的错


解决方案

最后,正当我尝试重写了所有配置文件,重启所有软件服务甚至虚拟机依然没解决时,偶然的一次修改竟然成功启动了Gateway服务,仔细排查过后发现是在Gateway所在微服务的pom.xml文件中指定了打包方式造成的,把packaging标签去掉即可正常启动(我不理解…)

<packaging>pom</packaging>

这次重启服务后没有报错,在Nacos中也可查看到名为“gateway”的服务,证明已成功启动

在这里插入图片描述

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐