問題產生背景
在使用Spring Cloud Gateway過程中,希望配置多Routes映射不同的微服務,因為Gateway 和Zuul的訪問路徑不同(zuul 會帶有服務service Id),造成錯誤。
現象表現

問題定位
- 認為是配置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
- 認為是順序問題
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
- 以為自己寫錯了。
四處尋求幫助,無奈,Gateway的資料網上真的很少。還是自食其力吧,根據錯誤信息,查看Nacos中元數據,發現異常!
問題結論
- 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。
