【问题标题】:Spring cloud gateway unable to resolve service id from eureka serverSpring Cloud Gateway 无法从 eureka 服务器解析服务 ID
【发布时间】:2019-02-11 07:04:04
【问题描述】:

跟着@​​987654321@搭建了eureka server、spring cloud gateway和一个示例rest service。

但是,网关无法使用来自 eureka 服务器的服务名称检索 url。 网关和服务的注册似乎没问题。 当提供实际端点而不是服务 ID 时,它可以正常工作。 我无法理解为什么网关没有从尤里卡解析服务 ID。我是否缺少任何配置?

错误信息:

2018-09-05 23:20:17.751  INFO 47037 --- [ctor-http-nio-2] c.netflix.loadbalancer.BaseLoadBalancer  : Client: localhost instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=localhost,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2018-09-05 23:20:17.756  INFO 47037 --- [ctor-http-nio-2] c.n.l.DynamicServerListLoadBalancer      : Using serverListUpdater PollingServerListUpdater
2018-09-05 23:20:17.760  INFO 47037 --- [ctor-http-nio-2] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client localhost initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=localhost,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@107dc063
2018-09-05 23:20:17.822 ERROR 47037 --- [ctor-http-nio-2] .a.w.r.e.DefaultErrorWebExceptionHandler : Failed to handle request [GET http://localhost:8080/rest-service/hello]

org.springframework.cloud.gateway.support.NotFoundException: Unable to find instance for localhost
    at org.springframework.cloud.gateway.filter.LoadBalancerClientFilter.filter(LoadBalancerClientFilter.java:72) ~[spring-cloud-gateway-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
    at org.springframework.cloud.gateway.handler.FilteringWebHandler$GatewayFilterAdapter.filter(FilteringWebHandler.java:133) ~[spring-cloud-gateway-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
    at org.springframework.cloud.gateway.filter.OrderedGatewayFilter.filter(OrderedGatewayFilter.java:44) ~[spring-cloud-gateway-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
    at org.springframework.cloud.gateway.handler.FilteringWebHandler$DefaultGatewayFilterChain.lambda$filter$0(FilteringWebHandler.java:115) ~[spring-cloud-gateway-core-2.0.1.RELEASE.jar:2.0.1.RELEASE]
    at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:45) ~[reactor-core-3.1.8.RELEASE.jar:3.1.8.RELEASE]

服务注册:

附上代码链接:

【问题讨论】:

  • 您找到解决方案了吗?遇到类似问题
  • @roberttrudel 该问题已通过在网关代码中添加属性 spring.cloud.gateway.discovery.locator.lower-case-service-id= true 得到解决

标签: spring-boot spring-cloud netflix-eureka spring-cloud-gateway


【解决方案1】:

问题在于 Spring Cloud Gateway 从 eureka 访问服务名称的方式,它区分大小写。解决方法是在 application.yml 中添加以下属性

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

I have created a sample project to show all of them working together.

【讨论】:

  • 在调试了大约 3 个小时后,您的回答对我有用,我怀疑尤里卡名称无论出于何种原因都需要强制小写。让这个工作很痛苦......谢谢!
【解决方案2】:

除了接受的答案外,请确保添加 DiscoveryClient 依赖项:

10.2 DiscoveryClient 路由定义定位器

网关可以配置为基于在与 DiscoveryClient 兼容的服务注册表中注册的服务创建路由。

要启用此功能,请设置 spring.cloud.gateway.discovery.locator.enabled=true 并确保 DiscoveryClient 实现位于类路径上并已启用(例如 Netflix Eureka、Consul 或 Zookeeper)。

10.2 DiscoveryClient Route Definition Locator

【讨论】:

    【解决方案3】:

    如果您使用 WebClient 从 spring cloud gateway-service 对其他微服务进行 API 调用,请确保您在配置类中注释 WebClient.Builder

    @Configuration
    @EnableConfigurationProperties
    @ConfigurationProperties
    @Component
    public class ContentConfig {
    
        @Bean
        @LoadBalanced
        public WebClient.Builder loadBalancedWebClientBuilder() {
            return WebClient.builder();
        }
    }
    

    然后在您的提供者或服务类@AutoWired WebClient bean 中,

    @Autowired
    public WebClient.Builder webClientBuilder;
    
    

    【讨论】:

    • 解决了我的问题
    猜你喜欢
    • 2018-04-29
    • 1970-01-01
    • 2020-05-16
    • 2017-11-29
    • 2015-04-11
    • 2021-10-23
    • 2021-12-09
    • 2016-02-15
    • 2018-10-18
    相关资源
    最近更新 更多