Envoy實現.NET架構的網關(一)靜態配置與文件動態配置

什麼是Gateway

在微服務體系結構中,如果每個微服務通常都會公開一組精細終結點,這種情況可能會有以下問題

  1. 如果沒有 API 網關模式,客戶端應用將與內部微服務相耦合
  2. 在客戶端應用中,單個頁面/屏幕可能需要多次調用多個服務。 
  3. 如果沒有網關,所有微服務必定會暴露在“外部世界”中。
  4. 每個公開發布的微服務都必須處理授權和 SSL 等問題。

而Gateway可以爲微服務組提供單一入口點,API 網關位於客戶端應用和微服務之間。 它充當反向代理,將請求從客戶端路由到服務。 它還可以提供其他跨領域功能,例如身份驗證、SSL 終止和緩存

什麼是Envoy

Envoy 是專爲大型現代 SOA(面向服務架構)架構設計的 L7 代理和通信總線,它有以下優勢

  1. C++11編寫,原生代碼高性能
  2. L3/L4 filter架構,例如TCP代理
  3. HTTP L7 filter架構,緩存,限速,路由/轉發
  4. 頂級HTTP2與GRPC支持
  5. 服務發現與動態配置
  6. 健康檢查
  7. 高級負載均衡

我們可以藉助Envoy實現API Gateway。Envoy通過yaml配置文件來組織網關的信息。下面來說說Envoy中的核心概念

.NET網關與Gateway實戰-Envoy與kong課程希望大家支持  https://ke.qq.com/course/4033027?tuin=1271860f

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程序

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章