简体   繁体   English

Spring Cloud Gateway 找不到微服务(Not found 404 错误)

[英]Spring Cloud Gateway doesn't find the microservices (Not found 404 error)

I have this simple microservices application with Spring Boot and I try to add the Spring Cloud Gateway service, but it doesn't work.我有这个带有 Spring Boot 的简单微服务应用程序,我尝试添加 Spring 云网关服务,但它不起作用。 All microservices, including the Eureka server, work well, but when I try to access to some microservice using the Gateway route, it doesn't find the microservice.所有微服务,包括 Eureka 服务器,都运行良好,但是当我尝试使用 Gateway 路由访问某些微服务时,它找不到微服务。

ApiGateway pom.xml Api网关 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 Gateway application.yml 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 Gateway main 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 Api 找不到网关 404 错误

Sometimes it's happened case of the service name issue add this property on the edge-service :有时会发生服务名称问题,在边缘服务上添加此属性:

For application.properties file:对于application.properties文件:

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

For application.yml file:对于application.yml文件:

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

i've principaly do 2 things and it works for me.我主要做了两件事,它对我有用。

  • all services name have to be in UPERCASE like所有服务名称都必须大写

    uri: lb://UPERCASE-NAME uri: lb://大写名称

  • when you specify URI your it will have to end by slash "/"当您指定 URI 时,它必须以斜杠“/”结尾

    uri: lb://UPERCASE-NAME/ uri: lb://大写名称/

  • specify only one path predicates by route id i don't know why but it's work like that通过路由 ID 仅指定一个路径谓词我不知道为什么,但它是这样工作的

    • id: ACCOUNTING-SERVICE uri: lb://ACCOUNTING/ predicates: - Path=/api/v1/act/** id: 会计服务 uri: lb://ACCOUNTING/ predicates: - Path=/api/v1/act/**

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM