当前位置: 移动技术网 > IT编程>开发语言>Java > Spring Cloud Gateway 之请求坑位[微服务IP不同请求会失败]

Spring Cloud Gateway 之请求坑位[微服务IP不同请求会失败]

2019年09月26日  | 移动技术网IT编程  | 我要评论

问题产生背景

在使用spring cloud gateway过程中,希望配置多routes映射不同的微服务,因为gateway 和zuul的访问路径不同(zuul 会带有服务service id),造成错误。

现象表现

file

问题定位

  1. 认为是配置predicate问题。
      routes:
        - id: after_route
          uri: lb://user-center
          predicates:
            # 当当前时间晚于设置时间之后,才能访问
            # 否则得到404错误
            #- after=2010-01-01t18:00:00.789-07:00[america/denver]
            # 当host属于**.geekplus.com.cn或**.life-runner.com时
            # http://localhost:9999/** -> user-center/**
            # eg. http://localhost:9999/users/1 -> user-center/users/1
            #- host=**.geekplus.com.cn,**.life-runner.com
            - timebetween=上午6:00,下午11:00
            - path=/users/**
          filters:
            - addrequestheader=companykey,123456
            - addresponseheader=success,isaac
            - prelog=customlogkey,customlogvalue
        - id: content_route
          uri: lb://shared-center
            - after=2010-01-01t18:00:00.789-07:00[america/denver]
            - path=/share/**
          filters:
            - addrequestheader=companykey,123456
            - addresponseheader=success,isaac
            - prelog=customlogkey,customlogvalue
  1. 认为是顺序问题
      routes:
        - id: content_route
          uri: lb://shared-center
          predicates:
            - path=/share/**
        - id: after_route
          uri: lb://user-center
          predicates:
            # 当当前时间晚于设置时间之后,才能访问
            - timebetween=上午6:00,下午11:00
            - path=/users/**
          filters:
            - addrequestheader=companykey,123456
            - addresponseheader=success,isaac
            - prelog=customlogkey,customlogvalue
  1. 以为自己写错了。

    四处寻求帮助,无奈,gateway的资料网上真的很少。还是自食其力吧,根据错误信息,查看nacos中元数据,发现异常!

问题结论

  1. gateway 和 user-center 都进行过重启,因为重启后,服务ip发生了变更,在服务注册中心这两个ip相同,因此可以访问。
    2,shared-center 我长时间没有重启,注册在发现中心的ip 是老的ip,和gateway/user-center的ip不同,造成请求失败。
    具体如下:
    shared-center: 172.16.33.167
    user-center & gateway : 172.16.29.0

解决方法,重启shared-center,重新获取实例ip,结果恢复正常!

tips

我使用的是spring cloud alibaba nacos作为服务发现中心,在重启内容服务之后,发现中心的失败ipservice并没有被刷新,需要手动处理一下,否则依旧会调用到老的ip。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网