其他分享
首页 > 其他分享> > Envoy实现.NET架构的网关(二)基于控制平面的动态配置

Envoy实现.NET架构的网关(二)基于控制平面的动态配置

作者:互联网

什么是控制平面

上一篇我们讲了文件系统的动态配置,这次我们来看看通过Control Panel来配置Envoy。控制平面就是一个提供Envoy配置信息的单独服务,我们可以通过这个服务来修改Envoy的配置。官方为我们提供了两种已经实现好的控制面板。

 

go控制面板:https://github.com/envoyproxy/go-control-plane

java控制面板:https://github.com/envoyproxy/java-control-plane

配置控制面板

我们下载官方的go语言控制面板,并修改go-control-plane\internal\example\resource.go中的配置

首先修改upstream信息,指定我们的上游ip与两个上有服务的端口

const (
    ClusterName   = "example_proxy_cluster"
    RouteName     = "local_route"
    ListenerName  = "listener_0"
    ListenerPort  = 10000
    UpstreamHost  = "192.168.43.94"
    UpstreamPort  = 5000
    UpstreamPort2 = 5001
)

然后我们修改,修改dns类型,修改为静态dns解析

func makeCluster(clusterName string) *cluster.Cluster {
    return &cluster.Cluster{
        Name:                 clusterName,
        ConnectTimeout:       ptypes.DurationProto(5 * time.Second),
        ClusterDiscoveryType: &cluster.Cluster_Type{Type: cluster.Cluster_STATIC},
        LbPolicy:             cluster.Cluster_ROUND_ROBIN,
        LoadAssignment:       makeEndpoint(clusterName),
        DnsLookupFamily:      cluster.Cluster_V4_ONLY,
    }
}

修改makeEndpoint方法,指定两个Endpoint

func makeEndpoint(clusterName string) *endpoint.ClusterLoadAssignment {
    return &endpoint.ClusterLoadAssignment{
        ClusterName: clusterName,
        Endpoints: []*endpoint.LocalityLbEndpoints{{
            LbEndpoints: []*endpoint.LbEndpoint{{
                HostIdentifier: &endpoint.LbEndpoint_Endpoint{
                    Endpoint: &endpoint.Endpoint{
                        Address: &core.Address{
                            Address: &core.Address_SocketAddress{
                                SocketAddress: &core.SocketAddress{
                                    Protocol: core.SocketAddress_TCP,
                                    Address:  UpstreamHost,
                                    PortSpecifier: &core.SocketAddress_PortValue{
                                        PortValue: UpstreamPort,
                                    },
                                },
                            },
                        },
                    },
                },
            },
                {
                    HostIdentifier: &endpoint.LbEndpoint_Endpoint{
                        Endpoint: &endpoint.Endpoint{
                            Address: &core.Address{
                                Address: &core.Address_SocketAddress{
                                    SocketAddress: &core.SocketAddress{
                                        Protocol: core.SocketAddress_TCP,
                                        Address:  UpstreamHost,
                                        PortSpecifier: &core.SocketAddress_PortValue{
                                            PortValue: UpstreamPort2,
                                        },
                                    },
                                },
                            },
                        },
                    },
                },
            },
        }},
    }
}

启动控制平面

PS C:\demo\go-control-plane\internal\example\main> go run .\main.go
2021/10/29 15:55:14 management server listening on 18000

配置Envoy.yaml

我们需要配置Envoy.yaml让Envoy从控制平面中获取详细的配置信息,需要在dynamic_resources中配置ads_config,并让cds_config与lds_config从中读取;另外我们需要配置xds cluster,让Envoy知道控制平面的地址。具体配置信息如下:

admin:
  address:
    socket_address:
      address: 0.0.0.0
      port_value: 9902
      
node:
  cluster: test-cluster
  id: test-id

dynamic_resources:
  ads_config:
    api_type: GRPC
    transport_api_version: V3
    grpc_services:
    - envoy_grpc:
        cluster_name: xds_cluster
  cds_config:
    resource_api_version: V3
    ads: {}
  lds_config:
    resource_api_version: V3
    ads: {}

static_resources:
  clusters:
  - 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:
          http2_protocol_options: {}
    name: xds_cluster
    load_assignment:
      cluster_name: xds_cluster
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: 192.168.43.94
                port_value: 18000

运行Envoy

我们通过docker运行Envoy

docker run --rm -it -p 9902:9902 -p 10000:10000 -v D:/gateway/envoy/config/dynamic-plane/:/etc/envoy/ -v D:/gateway/envoy/logs:/logs envoyproxy/envoy-dev  -c /etc/envoy/envoy.yaml

启动我们上一节的server1与server2

 

 调用http://localhost:10000/Name,负载成功!!!

标签:core,网关,endpoint,Envoy,SocketAddress,cluster,Address,NET
来源: https://www.cnblogs.com/chenyishi/p/15480961.html