什么是Gateway
在微服務體系結構中,如果每個微服務通常都會公開一組精細終結點,這種情況可能會有以下問題
- 如果沒有 API 網關模式,客戶端應用將與內部微服務相耦合,
- 在客戶端應用中,單個頁面/螢屏可能需要多次呼叫多個服務,
- 如果沒有網關,所有微服務必定會暴露在“外部世界”中,
- 每個公開發布的微服務都必須處理授權和 SSL 等問題,
而Gateway可以為微服務組提供單一入口點,API 網關位于客戶端應用和微服務之間, 它充當反向代理,將請求從客戶端路由到服務, 它還可以提供其他跨領域功能,例如身份驗證、SSL 終止和快取,

什么是Envoy
Envoy 是專為大型現代 SOA(面向服務架構)架構設計的 L7 代理和通信總線,它有以下優勢
- C++11撰寫,原生代碼高性能
- L3/L4 filter架構,例如TCP代理
- HTTP L7 filter架構,快取,限速,路由/轉發
- 頂級HTTP2與GRPC支持
- 服務發現與動態配置
- 健康檢查
- 高級負載均衡
我們可以借助Envoy實作API Gateway,Envoy通過yaml組態檔來組織網關的資訊,下面來說說Envoy中的核心概念
Listener
一個命名的網路地址,可以被下游客戶端連接,它的配置樣式如下:
static_resources:
listeners:
- name: listener_0
address:
socket_address:
protocol: TCP
address: 0.0.0.0
port_value: 10000
此配置說明Envoy監聽在10000埠,下游客戶端可以通過此埠與Envoy互動
L3/L4過濾器Filter
L3/L4過濾器Filter可以幫我們實作如:HTTP連接管理,限速,TCP代理等功能,它的配置樣式如下:
filter_chains:
- filters:
- name: envoy.filters.network.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
scheme_header_transformation:
scheme_to_overwrite: http
stat_prefix: ingress_http
route_config:
name: local_route
virtual_hosts:
- name: local_service
domains: ["*"]
routes:
- match:
prefix: "/"
route:
host_rewrite_literal: 192.168.43.94
cluster: service_envoyproxy_io
http_filters:
- name: envoy.filters.http.router
此配置說明通過HttpConnectionManager這個過濾器來接受HTTP請求,并將請求通過router過濾器的配置轉發到service_envoyproxy_io這個上游集群
Upstream Cluster
Envoy 的集群管理器管理所有配置的上游集群,用來真正處理Envoy接受的請求,其配置樣式如下:
clusters:
- name: service_envoyproxy_io
connect_timeout: 30s
type: strict_dns
dns_lookup_family: V4_ONLY
lb_policy: ROUND_ROBIN
load_assignment:
cluster_name: service_envoyproxy_io
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: 192.168.43.94
port_value: 5000
此配置說明Envoy會將請求轉發到192.168.43.94:5000這個地址,
呼叫邏輯我們總結如下,Listener接受請求,將請求交給過濾器,過濾器處理完后,根據路由規則將請求轉發給上游集群,上游集群中的endpoint會真正處理請求,

運行Envoy
我們通過docker運行一個默認Envoy容器
docker run --rm -it -p 9901:9901 -p 10000:10000 envoyproxy/envoy-dev
訪問http://localhost:10000/,發現其跳轉到Envoy官網

我們進入容器查看其配置,發現其最侄訓將請求轉發到www.envoyproxy.io
cat /etc/envoy/envoy.yaml
- lb_endpoints:
- endpoint:
address:
socket_address:
address: www.envoyproxy.io
port_value: 443
靜態檔案配置
我們現在通過Envoy來實作我們自己的網關,靜態檔案配置是我們把配置資訊提前配置好,Envoy啟動后不可修改配置內容
準備服務
我們準備兩個.NET WebAPI,server1與server2,其中分別創建NameController,并新建Get方法
Server1
[HttpGet] public string Get() { _logger.LogInformation("call server1"); var req = Request; return "server1"; }
Server2
[HttpGet] public string Get() { _logger.LogInformation("call server2"); var req = Request; return "server2"; }
并將server1的啟動埠指定為5000,將server2的啟動埠指定為5001
Server1
webBuilder.UseUrls("http://*:5555").UseStartup<Startup>();
Server2
webBuilder.UseUrls("http://*:5001/").UseStartup<Startup>();
我們啟動Server1與Server2

準備Envoy配置
我們將上節課的默認Envoy組態檔從容器中取出,并作修改如下
admin:
address:
socket_address:
protocol: TCP
address: 0.0.0.0
port_value: 9901
static_resources:
listeners:
- name: listener_0
address:
socket_address:
protocol: TCP
address: 0.0.0.0
port_value: 10000
filter_chains:
- filters:
- name: envoy.filters.network.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
scheme_header_transformation:
scheme_to_overwrite: http
stat_prefix: ingress_http
route_config:
name: local_route
virtual_hosts:
- name: local_service
domains: ["*"]
routes:
- match:
prefix: "/"
route:
host_rewrite_literal: 192.168.43.94
cluster: service_envoyproxy_io
http_filters:
- name: envoy.filters.http.router
clusters:
- name: service_envoyproxy_io
connect_timeout: 30s
type: static
# Comment out the following line to test on v6 networks
dns_lookup_family: V4_ONLY
lb_policy: ROUND_ROBIN
load_assignment:
cluster_name: service_envoyproxy_io
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: 192.168.43.94
port_value: 5000
- endpoint:
address:
socket_address:
address: 192.168.43.94
port_value: 5001
我們啟動Envoy,驗證配置是否正確
docker run --rm -it -p 9901:9901 -p 10000:10000 -v D:/gateway/envoy/config/static/envoy.yaml:/etc/envoy/envoy.yaml -v D:/gateway/envoy/logs:/logs envoyproxy/envoy-dev -c /etc/envoy/envoy.yaml --log-path logs/custom.log
呼叫api,發現其實作了負載
http://localhost:10000/Name


動態檔案配置
動態檔案可以幫助我們實作當檔案發生更改時,Envoy 將自動更新其配置,
修改靜態檔案,將其中的cluster提取到cds.yaml檔案中
resources:
- "@type": type.googleapis.com/envoy.config.cluster.v3.Cluster
name: example_proxy_cluster
type: STRICT_DNS
typed_extension_protocol_options:
envoy.extensions.upstreams.http.v3.HttpProtocolOptions:
"@type": type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions
explicit_http_config:
http_protocol_options: {}
load_assignment:
cluster_name: example_proxy_cluster
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: 192.168.43.94
port_value: 5000
將listener提取到lds.yaml檔案中
resources:
- "@type": type.googleapis.com/envoy.config.listener.v3.Listener
name: listener_0
address:
socket_address:
address: 0.0.0.0
port_value: 10000
filter_chains:
- filters:
- name: envoy.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
stat_prefix: ingress_http
http_filters:
- name: envoy.filters.http.router
route_config:
name: local_route
virtual_hosts:
- name: local_service
domains:
- "*"
routes:
- match:
prefix: "/envoyapi/"
route:
prefix_rewrite: "/"
host_rewrite_literal: 192.168.43.94
cluster: example_proxy_cluster
修改envoy.yaml讓其參考lds.yaml與cds.yaml檔案
admin:
address:
socket_address:
protocol: TCP
address: 0.0.0.0
port_value: 9902
node:
cluster: test-cluster
id: test-id
dynamic_resources:
cds_config:
path: /etc/envoy/cds.yaml
lds_config:
path: /etc/envoy/lds.yaml
啟動Envoy
docker run --rm -it -p 9902:9902 -p 10000:10000 -v D:/gateway/envoy/config/dynamic/:/etc/envoy/ -v D:/gateway/envoy/logs:/logs envoyproxy/envoy-dev -c /etc/envoy/envoy.yaml --log-path logs/custom.log
呼叫api,發現呼叫成功
http://localhost:10000/envoyapi/Name

修改動態檔案配置
修改cds.yaml,將endpoint埠設定為5001
resources:
- "@type": type.googleapis.com/envoy.config.cluster.v3.Cluster
name: example_proxy_cluster
type: STRICT_DNS
typed_extension_protocol_options:
envoy.extensions.upstreams.http.v3.HttpProtocolOptions:
"@type": type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions
explicit_http_config:
http_protocol_options: {}
load_assignment:
cluster_name: example_proxy_cluster
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: 192.168.43.94
port_value: 5001
進入容器內部強制更新檔案
# cd /etc/envoy # mv cds.yaml tmp # mv tmp cds.yaml
呼叫api,發現在不重啟Envoy的情況下,實作了配置資訊的動態更新
![]()
至此,我們已經通過Envoy的靜態配置與檔案動態配置實作了一個網關來代理我們的.NET程式
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/340617.html
標籤:.NET技术
上一篇:如何使用批處理檔案從虛幻引擎.uproject檔案生成VisualStudio專案檔案?
下一篇:從MySQL結果創建PHP陣列
