Zuul的主要功能是路由和過濾器。路由功能是微服務的一部分,比如/api/user映射到user服務,/api/shop映射到shop服務。zuul實現了負載均衡。
zuul有以下功能:
- Authentication
- Insights
- Stress Testing
- Canary Testing
- Dynamic Routing
- Service Migration
- Load Shedding
- Security
- Static Response handling
- Active/Active traffic management
filterType:返回一個字符串代表過濾器的類型,在zuul中定義了四種不同生命周期的過濾器類型,具體如下:
- pre:路由之前
- routing:路由之時
- post: 路由之后
- error:發送錯誤調用
其他概念:
- filterOrder:過濾的順序
- shouldFilter:這里可以寫邏輯判斷,是否要過濾,本文true,永遠過濾。
- run:過濾器的具體邏輯。可用很復雜,包括查sql,nosql去判斷該請求到底有沒有權限訪問。
1、pom.xml

2、application.yml

3、app

4、filter

5、運行驗證

http://localhost:8401/app-a/HelloA?name=liangshengqi&token=xxxx

發現:已經使用了網關的ip,使用了spring.application.name使用了映射的app-a, 訪問路徑HelloA?name=liangshengqi&token=xxxx和原來一樣
