https://mvc.tw
歡迎參加我們的每週四固定聚會
1
YARP - Yet Another Reverse Proxy
Dino Wang
https://mvc.tw
Who am I
Dino Wang
▪ Microsoft – Cloud Solution Architect
▪ twMVC – Co-funder
2
https://mvc.tw
Agenda
▪ Reverse Proxy overview
▪ What is YARP
▪ Why YARP?
▪ How to use YARP
▪ Use Cases
3
https://mvc.tw
歡迎參加我們的每週四固定聚會
4
Reverse Proxy overview
https://mvc.tw
A Reverse Proxy
5
反向代理 - 維基百科,自由的百科全書 (wikipedia.org)
https://mvc.tw
A Reverse Proxy
▪ 對客戶端隱藏伺服器(叢集)的 IP 位址
▪ 安全:作為應用層防火牆 (Web Application Firewall),為網站提供對基於 Web 的攻擊行
爲(例如 DoS/DDoS)的防護,更容易排查惡意軟體等
▪ 為後端伺服器(叢集)統一提供加密和 SSL 加速(如 SSL 終端代理)
▪ 負載均衡,若伺服器叢集中有負荷較高者,反向代理通過 URL 重寫,根據連線請求從負荷較
低者獲取與所需相同的資源或備援
▪ 對於靜態內容及短時間內有大量存取請求的動態內容提供快取服務
▪ 對一些內容進行壓縮,以節約頻寬或為網路頻寬不佳的網路提供服務
6
反向代理 - 維基百科,自由的百科全書 (wikipedia.org)
https://mvc.tw
Reverse Proxy and (Forward) Proxy
7
https://mvc.tw
Reverse Proxy – Load Balancing
8
https://mvc.tw
Reverse Proxy – Layers
9
https://mvc.tw
Reverse Proxy – Path Routing
10
https://mvc.tw
Reverse Proxy – Path Routing
11
https://mvc.tw
Featured Proxy
▪ Proxy Based Web Application Firewall
▪ API Gateway pattern
12
https://mvc.tw
歡迎參加我們的每週四固定聚會
13
Why YARP
https://mvc.tw
So.. 什麼是 YARP 對我來說有什麼意義
▪ Yet Another Reverse Proxy
▪ YARP 是一個反向代理工具包,運用 .NET (.NET Core) 建置高
效能反向代理
▪ YARP 提供了兩個主要使用和自訂的方向,
▪ 全功能代理
▪ 高度自定義的環境,用戶可以直接呼叫 YARP 請求轉送程式,繞過路由和負
載平衡等模組。開發者甚至可以在同一個程序中混合兩種方式,根據路由切
換。
14
https://mvc.tw
Why YARP?
▪ 也認識一下 Reverse Proxy 的強者們
▪ NGINX
▪ Envoy
15
https://mvc.tw
Reverse Proxy market share
16
Reverse proxies market share, websites and contacts - Wappalyzer
https://mvc.tw
Reverse Proxy market share
17
Envoy vs. Nginx feature and pricing comparison - Wappalyzer
https://mvc.tw
Web Server market share
18
Web Server technologies Web Usage Distribution (builtwith.com)
https://mvc.tw
19
20
網路迷因,僅供參考
21
Bill …
22
網路迷因,僅供參考
https://mvc.tw
Reverse Proxy 的強者們
NGINX
▪ Reverse Proxy
▪ Web Server
▪ 客製化
▪ NGINX + Lua
▪ NGINX + JavaScript
Envoy
▪ Reverse Proxy
▪ 客製化
▪ Lua and HTTP filter
23
24
https://mvc.tw
YARP
▪ Web Server (Kestrel) and Reverse Proxy
▪ Fully customizable in C#
25
https://mvc.tw
How to use YARP
1. dotnet new web –n {your-project-name}
2. dotnet add package Yarp.ReverseProxy
26
https://mvc.tw
How to use YARP in .NET Core / .NET 5
27
Startup.cs appsetting.json
https://mvc.tw
How to use YARP in .NET 6
28
Startup.cs appsetting.json
https://mvc.tw
歡迎參加我們的每週四固定聚會
29
High Availability
https://mvc.tw
Use YARP for Load Balancing
Policies
▪ FirstAlphabetical
▪ Random
▪ PowerOfTwoChoices (default)
▪ RoundRobin
▪ LeastRequests
▪ Customized
ILoadBalancingPolicy
30
https://mvc.tw
Load Balancing
▪ Session Affinity
▪ Load Balance Policy
▪ Destinations Health Checks
31
https://mvc.tw
SPOF: Single Point of Failure
32
單點故障 - 維基百科,自由的百科全書 (wikipedia.org)
https://mvc.tw
歡迎參加我們的每週四固定聚會
33
Use Cases
Classic ASP 轉圈圈
https://mvc.tw
處理問題
▪ ASP/COM+ 舊系統,用戶數量龐大
▪ 使用者回報,有若干頁面操作會有長時間的後端查詢
▪ 因為使用了 session,這一頁查詢發起後,該用戶隨後的所有系統
操作都只會轉圈圈,而且永遠都不會出結果
▪ 能不能避免轉圈,讓用戶空等
34
https://mvc.tw
Startup.cs
ConfigureServices
35
https://mvc.tw
Startup.cs
Configure
36
https://mvc.tw
appsettings.json
37
https://mvc.tw
AvoidReentryMiddleware
38
https://mvc.tw
歡迎參加我們的每週四固定聚會
39
Use Cases
Realtime Visit Counter
Millisecond-level Scaling
https://mvc.tw
處理問題
▪ 解決電子商務微秒級
▪ 線上使用人數統計(特定部位)
▪ Cloud Scaling
40
https://mvc.tw
Architecture
41
https://mvc.tw
t1 t2 t3 t4 t5 t6 t7 t8
🐵 🐵
🐵
🐵
🐷
🐷
🐷 🐷
😈
😈
🐰
🐰
🐰
😈
🐷
😈
🐯
🐯
🐯
👺
👺
time
https://mvc.tw
Configuration
▪ 計算 20 秒內結帳的人數
▪ 計算 5 秒內首頁的訪問人數
▪ …
43
https://mvc.tw
一定要使用 Reverse Proxy 嗎?
▪ 評估功能切入點,Middleware or Proxy
Java Spring、PHP Laravel、Python Flash、ASP.NET 都有
middleware 的設計
44
https://mvc.tw
一定要使用 Reverse Proxy 嗎?
45
▪ Approach 1: Middleware ▪ Approach 2: Proxy
https://mvc.tw
Resources
▪ YARP Documentation (microsoft.github.io)
▪ microsoft/reverse-proxy (github.com)
▪ YARP: The .NET Reverse proxy - YouTube
46
Blog 是記錄知識的最佳平台
47
https://dotblogs.com.tw
48
SkillTree 為了確保內容與實務不會脫節,我們都是聘請企業顧問等級
並且目前依然在職場的業界講師,我們不把時間浪費在述說歷史與沿革,
我們並不是教您考取證照,而是教您如何上場殺敵,拳拳到肉的內容才
是您花錢想要聽到的,而這也剛好是我們擅長的。
https://skilltree.my
49
天瓏資訊圖書

twMVC#43 YARP