簡體   English   中英

Spring Cloud Gateway 找不到微服務(Not found 404 錯誤)

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

我有這個帶有 Spring Boot 的簡單微服務應用程序,我嘗試添加 Spring 雲網關服務,但它不起作用。 所有微服務,包括 Eureka 服務器,都運行良好,但是當我嘗試使用 Gateway 路由訪問某些微服務時,它找不到微服務。

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 網關應用.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 找不到網關 404 錯誤

有時會發生服務名稱問題,在邊緣服務上添加此屬性:

對於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

我主要做了兩件事,它對我有用。

  • 所有服務名稱都必須大寫

    uri: lb://大寫名稱

  • 當您指定 URI 時,它必須以斜杠“/”結尾

    uri: lb://大寫名稱/

  • 通過路由 ID 僅指定一個路徑謂詞我不知道為什么,但它是這樣工作的

    • id: 會計服務 uri: lb://ACCOUNTING/ predicates: - Path=/api/v1/act/**

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM