当前位置:   article > 正文

Gateway实现动态路由_gateway动态路由

gateway动态路由

       本文主要聊聊Spring Cloud Gateway(以下简称gateway,使用的版本是2.2.1.RELEASE),结合Nacos的注册中心,实现根据微服务名,自动路由到对应的微服务。在gateway的官网上,可以看到如下描述:

 大致意思是,通过如下配置,可以实现自动根据服务发现为每一个服务创建了一个路由router,
这个router将以服务名开头的请求路径转发到对应的服务:

spring.cloud.gateway.discovery.locator.enabled=true

并且在很多博客中,也可以发现类似的说法:

spring.cloud.gateway.discovery.locator.enabled为true,表明gateway开启服务注册和发现的功能,并且spring cloud gateway自动根据服务发现为每一个服务创建了一个router,
这个router将以服务名开头的请求路径转发到对应的服务。

       但是,我在测试中发现该配置并不起作用。在GitHub中也找到相关问题:

 

      于是,找了很多资料去佐证,发现很少有人提及这个问题,但是并不是毫无进展,起码知道了spring.cloud.gateway.discovery.locator.enabled配置生效的地方是GatewayDiscoveryClientAutoConfiguration的静态内部类:

  1. @Configuration(
  2. proxyBeanMethods = false
  3. )
  4. @ConditionalOnProperty(
  5. value = {"spring.cloud.discovery.reactive.enabled"},
  6. matchIfMissing = true
  7. )
  8. public static class ReactiveDiscoveryClientRouteDefinitionLocatorConfiguration {
  9. public ReactiveDiscoveryClientRouteDefinitionLocatorConfiguration() {
  10. }
  11. @Bean
  12. @ConditionalOnProperty(
  13. name = {"spring.cloud.gateway.discovery.locator.enabled"}
  14. )
  15. public DiscoveryClientRouteDefinitionLocator discoveryClientRouteDefinitionLocator(ReactiveDiscoveryClient discoveryClient, DiscoveryLocatorProperties properties) {
  16. return new DiscoveryClientRouteDefinitionLocator(discoveryClient, properties);
  17. }
  18. }

在这里,创建了DiscoveryClientRouteDefinitionLocator对象,DiscoveryClientRouteDefinitionLocator实现了RouteDefinitionLocator接口,该接口只有获取路由定义的方法:

  1. public interface RouteDefinitionLocator {
  2. Flux<RouteDefinition> getRouteDefinitions();
  3. }

DiscoveryClientRouteDefinitionLocator的实现,主要是对serviceInstances的操作:

而serviceInstances来源于构造函数:

  1. public DiscoveryClientRouteDefinitionLocator(ReactiveDiscoveryClient discoveryClient, DiscoveryLocatorProperties properties) {
  2. this(discoveryClient.getClass().getSimpleName(), properties);
  3. this.serviceInstances = discoveryClient.getServices().flatMap((service) -> {
  4. return discoveryClient.getInstances(service).collectList();
  5. });
  6. }

 discoveryClient的实现类是ReactiveCompositeDiscoveryClient:

 该方法确实实现了基于服务的路由创建:

 但是ReactiveCompositeDiscoveryClient实现的接口是ReactiveDiscoveryClient,和NacosDiscoveryClient毫无关联,所谓它无法获取服务的注册列表,进而无法创建有效的路由。

既然配置不行,就只能自己实现RouteDefinitionLocator接口了:

  1. @Component
  2. public class MyRouteDefinitionLocator implements RouteDefinitionLocator {
  3. @Autowired
  4. private DiscoveryClient discoveryClient;
  5. @Value("${spring.application.name}")
  6. private String gatewayName;
  7. @Override
  8. public Flux<RouteDefinition> getRouteDefinitions() {
  9. // 服务发现
  10. List<String> serviceList = discoveryClient.getServices();
  11. System.out.println("serviceList = "+serviceList);
  12. // 只有网关服务,直接返回
  13. if (serviceList.size() < 2){
  14. return Flux.empty();
  15. }
  16. // 服务的个数
  17. int serviceNum = serviceList.size();
  18. if (serviceList.contains(gatewayName)){
  19. // 排除网关服务
  20. serviceNum--;
  21. }
  22. RouteDefinition[] routeDefinitions = new RouteDefinition[serviceNum];
  23. int count = 0;
  24. for (String service : serviceList) {
  25. if (service.equalsIgnoreCase(gatewayName)){
  26. continue;
  27. }
  28. RouteDefinition definition = new RouteDefinition();
  29. definition.setId(service);
  30. // 负载均衡
  31. definition.setUri(URI.create("lb://"+service));
  32. // 根据微服务名,进行路径匹配
  33. definition.setPredicates(Collections.singletonList(new PredicateDefinition("Path=/"+service+"/**")));
  34. // 去掉微服务名
  35. definition.setFilters(Collections.singletonList(new FilterDefinition("StripPrefix=1")));
  36. routeDefinitions[count++] = definition;
  37. }
  38. return Flux.just(routeDefinitions);
  39. }
  40. }

     通过RouteDefinitionLocator接口,实现全量更新路由信息,达到根据微服务名动态路由到对应的微服务的目的。比如调用:

http://localhost:8888/feign-consumer/feign/test?msg=test123

     这里的端口是网关的端口,feign-consumer是注册到nacos的微服务名,则会根据微服务名路由到feign-consumer微服务。

示例代码地址:GitHub - qiuxinfa/cloud-alibaba

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/317424
推荐阅读
相关标签
  

闽ICP备14008679号