Spring Cloud Gateway 未找到微服务(未找到 404 错误)

问题描述 投票:0回答:4

我有一个带有 Spring Boot 的简单微服务应用程序,我尝试添加 Spring Cloud Gateway 服务,但它不起作用。所有微服务,包括 Eureka 服务器,都运行良好,但是当我尝试使用网关路由访问某些微服务时,它找不到该微服务。

ApiGateway pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.futurex.microservices</groupId>
    <artifactId>APIGateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>APIGateway</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2020.0.3</spring-cloud.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

API网关应用程序.yml

spring:
  application:
    name: API-GATEWAY
  cloud:
    gateway:
      routes:
        - id: FX-SERVICE1
          uri: http://localhost:8005
          predicates:
            - Path=/fx-service1/**
        - id: FX-SERVICE2
          uri: http://localhost:8006
          predicates:
            - Path=/fx-service2/**
    discovery:
      enabled: true
    config:
      uri: http://localhost:8081
server:
  port: 8081
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
    register-with-eureka: true
    fetch-registry: true
  instance:
    hostname: localhost

API网关主

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ApiGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApiGatewayApplication.class, args);
    }


}

Api Gateway Not found 404 error

java spring spring-boot gateway spring-cloud-gateway
4个回答
3
投票

有时会发生服务名称问题edge-service上添加此属性:

对于 application.properties 文件:

    spring.cloud.gateway.discovery.locator.enabled=true
    spring.cloud.gateway.discovery.locator.lower-case-service-id=true

对于 application.yml 文件:

 spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lowerCaseServiceId: true

1
投票

我主要做了两件事,这对我有用。

  • 所有服务名称都必须大写,例如

    uri:lb://大写名称

  • 当您指定 URI 时,它必须以斜杠“/”结尾

    uri:lb://大写名称/

  • 通过路由 id 仅指定一个路径谓词我不知道为什么,但它的工作原理是这样的

    • id:会计服务 uri:lb://会计/ 谓词: - 路径=/api/v1/act/**

0
投票

如果它可以帮助某人尝试此配置。在 application.properties 中:

spring.application.name=service-gateway-server
server.port=8090

eureka.client.service-url.defaultZone=http://localhost:8761/eureka

我在 application.yml 中显示了我的其余配置:

spring:
  cloud:
    gateway:
      routes:
      - id: service-one
        uri: lb://service-one
        predicates:
          - Path=/api/service1/**
        filters:
          - StripPrefix=2
      - id: service-two
        uri: lb://service-two
        predicates:
          - Path=/api/service2/**
        filters:
          - StripPrefix=2

0
投票

就我而言,我最初使用依赖:

spring-cloud-starter-gateway-mvc

但是网关不起作用。

当我将其更改为:

 spring-cloud-starter-gateway

成功了。

© www.soinside.com 2019 - 2024. All rights reserved.