Spring Cloud Gateway无法从Eureka服务器解析服务ID

时间:2018-09-06 05:12:20

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

遵循spring guides来构建eureka服务器,spring cloud网关和示例Rest服务。

但是,网关无法使用eureka服务器中的服务名称来检索URL。 网关和服务的注册似乎很好。 当提供实际端点而不是服务ID时,它可以正常工作。 我无法理解为什么网关无法从eureka解析服务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]

服务注册表:enter image description here

附加代码链接:

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

10.2 DiscoveryClient路由定义定位器

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

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

请参见10.2 DiscoveryClient Route Definition Locator

答案 2 :(得分:0)

如果您使用 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 by,

@Autowired
public WebClient.Builder webClientBuilder;