一, spring cloud 整合gateway后访问其他访问出现404

{     
    "timestamp": "2025-01-08T10:25:32.281+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/admin/vod/user/test" 
}

二, 可能的原因

        1. 启动类注解错误,把@EnableDiscoveryClient写成了@EnableFeignClients

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient  // 开启服务发现
public class ApiGatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(ApiGatewayApplication.class, args);

        System.out.println("123");
    }
}

        2.pom.xml文件引入冲突, 不要引入starter-web

    <dependencies>
        <!-- 网关 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <!-- 服务注册 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

        3. yml文件配置断言和服务发现

server:
  port: 8333
spring:
  application:
    name: service-gateway
  cloud:
    loadbalancer:
      ribbon:
        enabled: true # 开启负载均衡
    # nacos
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    gateway:
      discovery:
        locator:
          enabled: true # 开启服务发现
          lower-case-service-id: true
      routes[0]:
          id: service-vod
          uri: lb://service-vod
          predicates:
            - Path=/*/vod/**

 三, 版本

<spring-boot.version>2.2.1.RELEASE</spring-boot.version>
<springframework.version>5.2.1.RELEASE</springframework.version>
<alibaba.version>2.2.0.RELEASE</alibaba.version>

更多推荐