即使配置正确,Spring Cloud gateway api 也无法工作

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

我正在尝试使用尤里卡发现服务器、API 网关和一项休息服务的微服务。如下图所示

  • [尤里卡发现][1]

对于服务发现:

  • 我已经添加了相关的pom依赖
  • 我添加了注释,使 Spring Boot 项目成为发现服务

application.yml:-


server:
  port: 8761
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false

对于 Spring boot 服务 1:-

  • 在主类添加@EnableEurekaClient

application.yml

server:
  port: 8761
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false

对于 Api 网关服务:-

  • 在主类添加@EnableEurekaClient

application.yml


server:
  port: 9009
spring:
  application:
    name: HEALTH-CARD
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/
  • 我也尝试过java类路由配置部分
server:
  port: 9009
spring:
  application:
    name: HEALTH-CARD
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/

即使我已经尝试了下面链接中的所有建议 Spring Cloud API 网关路由不起作用 但对我来说没有任何作用。请帮我处理这个有线外壳。 [1]:https://i.stack.imgur.com/pGwlB.jpg

java spring spring-boot microservices api-gateway
1个回答
0
投票

您可能想尝试在应用程序 yaml 中减少 Spring Cloud 行


server:
  port: 8080
spring:
  application:
    name: API-GATEWAY
    cloud:
      gateway:
        discovery:
          locator:
            enabled: true
        routes:
          - id: health-card
            uri: http://localhost:9009/health-card
            predicates:
              - Path=/**
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/

server:
  port: 8080
spring:
  application:
    name: API-GATEWAY
    cloud:
      gateway:
        discovery:
          locator:
            enabled: true
        routes:
          - id: health-card
            uri: http://localhost:9009/health-card
            predicates:
              - Path=/**
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/

package com.apigateway.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringApiGatewayConfig {
    public RouteLocator configRoute(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("healthCard", r->r.path("/health-card/**").uri("localhost:9009/")).build();
    }
}
的同级,您的示例在小时候就有它。

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