SpringCloud Gateway路由失败, 报错503的解决方法

SpringCloud Gateway路由失败, 报错503的解决方法

网关服务配置:

spring:
  cloud:
    gateway:
      routes:
        - id: xxx
          uri: lb://xxx
          predicates:
            - Path=/api/**
          filters:
            - RewritePath=/api/(?>/?.*), /xxx/$\{segment}

SpringBoot版本:

<parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>2.7.11version>
    <relativePath/> 
parent>

SpringCloud + SpringCloud Alibaba 版本:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-dependenciesartifactId>
            <version>2021.0.7version>
            <type>pomtype>
            <scope>importscope>
        dependency>

        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-alibaba-dependenciesartifactId>
            <version>2021.0.5.0version>
            <type>pomtype>
            <scope>importscope>
        dependency>
    dependencies>
dependencyManagement>

启动网关服务访问 /api/** 时得到 503 的响应:

Whitelabel Error Page
This application has no configured error view, so you are seeing this as a fallback.
[fd8c3a90-7] There was an unexpected error (type=Service Unavailable, status=503).

解决办法

在网关服务的 pom.xml 中添加以下依赖:


<dependency>
	<groupId>org.springframework.cloudgroupId>
	<artifactId>spring-cloud-starter-loadbalancerartifactId>
dependency>

网关服务的完整依赖:

<dependencies>
    
    <dependency>
        <groupId>com.alibaba.cloudgroupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    dependency>

    
    <dependency>
        <groupId>com.alibaba.cloudgroupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
    dependency>

    
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-bootstrapartifactId>
    dependency>

    
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-gatewayartifactId>
    dependency>

    
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-loadbalancerartifactId>
    dependency>

dependencies>

你可能感兴趣的:(Java,Spring,报错,spring,cloud,gateway,spring)