Spring Cloud Gateway 之請求坑位[微服務IP不同請求會失敗]


問題產生背景

在使用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。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM