来自 Consul 的Spring Cloud 门户路线

我无法弄清楚spring-cloud-gateway是否支持从consul注册表中读取路由,就像Zuul一样 .

我添加了spring-cloud-starter-consul-discovery依赖项和@EnableDiscoveryClient,并在application.yml,hovewer,/ actuator / gateway / routes中配置了consul属性,不显示任何来自consul的路由

我还试图设置spring.cloud.gateway.discovery.locator.enabled:true但是没有改变任何东西 . 以下示例示例:

spring:
  cloud:
    consul:
      discovery:
        register: false
          locator:
            enabled: true
        acl-token: d3ee84e2-c99a-5d84-e4bf-b2cefd7671ba
        enabled: true

所以主要问题是,它甚至可以设想工作吗?

编辑:可能应该提到它是版本2.0.0.M5 . ,与Spring Boot 2.0.0.M7

我也用--debug推出了这一行:

GatewayDiscoveryClientAutoConfiguration#discoveryClientRouteDefinitionLocator:
  Did not match:
     - @ConditionalOnBean (types: org.springframework.cloud.client.discovery.DiscoveryClient; SearchStrategy: all) did not find any beans of type org.springframework.cloud.client.discovery.DiscoveryClient (OnBeanCondition)
  Matched:
     - @ConditionalOnProperty (spring.cloud.gateway.discovery.locator.enabled) matched (OnPropertyCondition)

回答(1)

3 years ago

我可以解决它声明以下bean: DiscoveryClientRouteDefinitionLocatorreference

@Configuration
@EnableDiscoveryClient
public class AutoRouting {
  @Bean
  public DiscoveryClientRouteDefinitionLocator discoveryClientRouteDefinitionLocator(DiscoveryClient discoveryClient, DiscoveryLocatorProperties properties) {
    return new DiscoveryClientRouteDefinitionLocator(discoveryClient, properties);
  }
}

P.S:你需要包括“spring-cloud-consul”