当前位置: 首页 > 知识库问答 >
问题:

Spring Boot 2.5.6 API Gateway Eureka Server=404

辛盛
2023-03-14

我正在创建一个项目与Spring API网关,尤里卡服务器和WebFlux。

通过spring文档,我开始了3项服务:

  • localhost:3761-尤里卡
  • localhost:4001-Serivce
  • localhost:8080-Api网关

Eureka列出2项服务(网关和我的服务)我的服务从浏览器工作正常我的网关只响应如果我的路由urilocalhost,我真的不知道如何解决它。

server:
  port: 8080

eureka:
  instance:
    hostname: localhost
    non-secure-port: 8761
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${eureka.instance.non-secure-port}/eureka/

spring:
  application:
    name: GATEWAY-SERVER
  cloud:
    gateway:
      
      discovery:
        locator:
          enabled: true
      routes:
        - id: my-service-route
          uri: lb://MY-SERVICE
          predicates:
            - Path=/api/my/service/**

server:
  port: 8080

eureka:
  instance:
    hostname: localhost
    non-secure-port: 8761
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${eureka.instance.non-secure-port}/eureka/

spring:
  application:
    name: GATEWAY-SERVER
  cloud:
    gateway:
      
      discovery:
        locator:
          enabled: true
      routes:
        - id: my-service-route
          uri: http://localhost:4001
          predicates:
            - Path=/api/my/service/**

请注意,唯一的区别是URI不查找服务名称。

共有2个答案

东方高洁
2023-03-14

在网关服务上做一些测试。我注意到一些奇怪的事情:

我已经列出了网关的服务:

@EnableEurekaClient
@EnableDiscoveryClient
@SpringBootApplication
public class GatewayServerApplication implements CommandLineRunner {
    @Autowired
    private DiscoveryClient discoveryClient;

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

    @Override
    public void run(String... args) throws Exception {
        System.out.println("LIST INSTANCES");
        List<String> list = discoveryClient.getServices();

        list.forEach(item -> {
            System.out.println("Service: " + item);
            List<ServiceInstance> instances = this.discoveryClient.getInstances(item);
            instances.forEach(instance -> {
                System.out.println("URI: " + instance.getUri());
                System.out.println("HOST: " + instance.getHost());
                System.out.println("PORT: " + instance.getPort());
                System.out.println("INSTANCE ID: " + instance.getInstanceId());
                System.out.println("SERVICE ID: " + instance.getServiceId());
            });
        });
    }
}


2021-11-13 09:24:35.633  INFO 1547 --- [           main] b.c.f.gateway.GatewayServerApplication   : Started GatewayServerApplication in 3.576 seconds (JVM running for 4.419)
LIST INSTANCES
Service: my-service
URI: http://192.168.0.103:8761
HOST: 192.168.0.103
PORT: 8761
INSTANCE ID: mbp-of-rafael:MY-SERVICE:4001
SERVICE ID: MY-SERVICE

URI和端口定义为8761(eureka服务器端口)。但我觉得应该是4001(我的服务端口)。我错了吗?!

仲俊豪
2023-03-14

@斯宾塞·吉布

以下是依赖项:

<?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 http://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.6</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.site</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <packaging>pom</packaging>
    <name>Parent POM</name>

    <properties>
        <java.version>11</java.version>
        <encoding>UTF-8</encoding>
        <spring-cloud.version>2020.0.4</spring-cloud.version>
        <spring-native.version>0.10.3</spring-native.version>
        <feign.reactor.version>3.1.1</feign.reactor.version>

        <maven.release.version>3.0.0-M4</maven.release.version>
        <lombok.mapstruct.binding.version>0.2.0</lombok.mapstruct.binding.version>
        <org.mapstruct.version>1.4.2.Final</org.mapstruct.version>
        <org.springdoc.version>1.5.11</org.springdoc.version>

        <project.build.sourceEncoding>${encoding}</project.build.sourceEncoding>
        <project.reporting.outputEncoding>${encoding}</project.reporting.outputEncoding>
        <project.resources.sourceEncoding>${encoding}</project.resources.sourceEncoding>
        <archetype.encoding>${encoding}</archetype.encoding>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- Develop -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok-mapstruct-binding</artifactId>
                <version>${lombok.mapstruct.binding.version}</version>
            </dependency>

            <dependency>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct</artifactId>
                <version>${org.mapstruct.version}</version>
            </dependency>

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

            <dependency>
                <groupId>com.playtika.reactivefeign</groupId>
                <artifactId>feign-reactor-spring-cloud-starter</artifactId>
                <version>3.1.1</version>
                <type>pom</type>
            </dependency>

            <!-- Documentation -->
            <dependency>
                <groupId>org.springdoc</groupId>
                <artifactId>springdoc-openapi-webflux-ui</artifactId>
                <version>${org.springdoc.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- DevOps -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/application*.yml</include>
                </includes>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layers>
                        <enabled>true</enabled>
                        <includeLayerTools>true</includeLayerTools>
                    </layers>
                    <image>
                        <name>${project.artifactId}:${project.version}</name>
                    </image>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source> <!-- depending on your project -->
                    <target>${java.version}</target> <!-- depending on your project -->
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>

                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>

                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok-mapstruct-binding</artifactId>
                            <version>${lombok.mapstruct.binding.version}</version>
                        </path>
                    </annotationProcessorPaths>
                    <compilerArgs>
                        <compilerArg>-Amapstruct.defaultComponentModel=spring</compilerArg>
                    </compilerArgs>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>${maven.release.version}</version>
                <configuration>
                    <allowTimestampedSnapshots>true</allowTimestampedSnapshots>
                    <tagNameFormat>@{project.version}</tagNameFormat>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

<?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>com.site</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <artifactId>eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name>
    <description>eureka-server</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>
<?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>com.site</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <artifactId>gateway-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gateway-server</name>
    <description>gateway-server</description>

    <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-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

<?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>com.site</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <artifactId>service</artifactId>
    <version>0.1.1-SNAPSHOT</version>

    <name>my-service</name>
    <description>my-service</description>


    <dependencies>
        <!-- Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>com.playtika.reactivefeign</groupId>
            <artifactId>feign-reactor-spring-cloud-starter</artifactId>
            <type>pom</type>
        </dependency>

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

        <!-- Database -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
        </dependency>

        <!-- Developer -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-mapstruct-binding</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
        </dependency>

        <!-- Documentation -->
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-webflux-ui</artifactId>
        </dependency>

        <!-- Validation -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

        <!-- Tests -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>de.flapdoodle.embed</groupId>
            <artifactId>de.flapdoodle.embed.mongo</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

这里还有yaml服务。我想我在gateway或Eureka做错了什么。

server:
  port: 8761

spring:
  application:
    name: EUREKA-SERVER

eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
  client:
    register-with-eureka: false
    fetch-registry: false
    use-dns-for-fetching-service-urls: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

logging:
  level:
    com:
      netflix:
        eureka: off
        discovery: off

server:
  port: 8080

eureka:
  instance:
    hostname: localhost
    non-secure-port: 8761
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${eureka.instance.non-secure-port}/eureka/

spring:
  application:
    name: GATEWAY-SERVER
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        - id: my-service-route
          uri: lb://MY-SERVICE
          predicates:
            - Path=/api/v1/**
server:
  port: 4001

spring:
  application:
    name: MY-SERVICE
  data:
    mongodb:
      uri: mongodb://localhost:27017/service

eureka:
  instance:
    hostname: localhost
    non-secure-port: 8761
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${eureka.instance.non-secure-port}/eureka/

springdoc:
  api-docs:
    enabled: true
    path: /api-docs
  swagger-ui:
    path: /index.html

 类似资料:
  • 问题内容: 在Tomcat服务器上运行Eclipse项目时出现以下错误: HTTP状态404-请求的资源(/ ProjectName /)不可用。 浏览器地址栏中的URL为。 我真的不知道文件中缺少什么。我在文件夹中有一个,并且我的包含以下条目: 问题答案: 默认情况下,当您打开项目根文件夹而不是中的物理文件时,服务器将在中查找欢迎文件。如果未找到任何内容,则将出现此404“找不到页面”错误。 就

  • 问题内容: 我将在春季阅读有关宁静的Web服务的这本书。我决定放弃他们的工作,而使用Java配置文件。出于某种原因,切换到Java配置后,该服务将正确运行(在控制台窗口中),但是当我实际转到localhost上的端点时,我得到了: 白标错误页 此应用程序没有针对/ error的显式映射,因此您将其视为后备。 PDT 2016年4月23日星期六20:48:25发生意外错误(类型=未找到,状态= 40

  • 问题内容: 我正在构建一个Spring 4 MVC应用程序。并且它是使用Java注释完全配置的。没有。该应用是使用的实例进行配置的,就像这样, 和 我现在正在尝试为404页添加全局/全部捕获异常处理程序,但没有成功。以下是我尝试过的一些方法。 这些方法均未执行。我不知道如何处理这个问题。我不想使用,因为我必须为此创建一个。 问题答案: 默认情况下,不会抛出。您需要启用它。 本应该让你重写如何被创建

  • 问题内容: 如何使用Play为他们的404/505错误页面创建全局自定义外观? 问题答案: 在Play Framework 1中,您只需修改提供的和。

  • 问题内容: 在带有springboot的vaadin项目中使用spring security时遇到问题 。因此,我正在使用PdfViewer插件来显示PDF文件。但是我收到 以下错误消息: and my spring security configuration looks like this: So checking the loaded files in Chrome I see a fold

  • 问题内容: 这是我有史以来的第一个servlet。这是代码。 我使用此命令对其进行了编译, 然后将文件放入WEB-INF文件夹中的classes文件夹中。 这是我的web.xml Tomcat7一直给我一个404的说法 文件树: 我在这里做错了什么? 问题答案: 您应该将servlet类放在包中。无包servlet是否起作用取决于旧版Tomcat和JVM版本的特定组合。如果您在书/教程中看到此示例

  • 问题内容: 当我运行selenium WebDriver驱动程序= new FirefoxDriver()显示错误时,它将导入所需的所有库,但仍会重现错误 当我使用Firefox驱动程序()运行时,显示以下错误 你能帮我解决这个问题吗:D 问题答案: 此错误消息… …暗示 GeckoDriver 无法启动/产生新的 WebBrowsing会话, 即 Firefox浏览器 会话。 当您使用 硒3.x

  • 问题内容: 如何获得Spring 3.0控制器来触发404? 我有一个带有控制器的控制器,对于要访问该控制器的某些URL,我希望该容器带有404。 问题答案: 从Spring 3.0开始,您还可以抛出带有注释声明的Exception :