由于其本身的效率以及对众多编程语言的支持,gRPC 是微服务集成和客户端服务器通信的常见选择。gRPC 是一种高性能远程过程调用 (RPC) 框架,它使用 HTTP/2 进行传输,使用协议缓冲区描述接口。
为了更轻松地将 gRPC 用于应用程序,Application Load Balancer (ALB) 现在支持端到端 HTTP/2,从而使您能够通过单个负载均衡器将 gRPC 服务和非 gRPC 服务一起发布。您可以将 Amazon Elastic Compute Cloud (EC2) 实例或 IP 地址(例如,使用 AWS Fargate)用作 gRPC 目标,同时支持对目标组进行 gRPC 运行状况检查。通过这种方式,您可以使用 ALB 来终止、路由和负载均衡您的微服务之间或启用了 gRPC 的客户端与服务之间的 gRPC 流量。
ALB 提供丰富的基于内容的路由功能,用于检查 gRPC 调用并将其路由到适当的服务。更具体地说,ALB 提供运行状况检查,以此检查 gRPC 状态代码、gRPC 请求计数指标、区分 gRPC 请求的访问日志以及特定于 gRPC 的响应标头。此外,您还可以从粘性、不同的负载均衡算法和 TLS 终止等原生功能中受益。
如何将 gRPC 与 Application Load Balancer 结合使用
为了测试这项新功能,我首先准备 gRPC 服务器应用程序。我使用的是 Python 编程语言和 grpc
存储库中包含的 route_guide
演示。我使用这个应用程序是因为它快速引入了客户端和服务器可以通过 gRPC 进行交互的很多方法,例如:
- 简单的一元远程过程调用 (RPC) – 客户端发送请求至服务器,然后等待响应,工作原理与函数调用的原理类似。
- 服务器端流式处理 RPC – 客户端发送请求至服务器,收到返回的消息流,然后从消息流中进行读取,直到没有更多消息。
- 客户端流式传输 RPC – 客户端写入一系列消息,将消息发送到服务器,然后等待服务器读取所有消息并返回其响应。
- 双向流式传输 RPC – 客户端和服务器都独立发送消息,从而保持顺序。
首先,我准备一个 Dockerfile,以便在容器中运行 route_guide
应用程序。从技术上讲,这不是必要的,因为我可以使用普通的 EC2 实例,但将容器与 gRPC 结合使用非常常见,因为这使得示例更加具有相关性。
FROM python:3.7
RUN pip install protobuf grpcio
COPY ./grpc/examples/python/route_guide .
CMD python route_guide_server.py
EXPOSE 50051
我构建容器映像并将其上传到 Amazon Elastic Container Registry。
aws ecr get-login-password --region eu-north-1 | docker login --username AWS --password-stdin 123412341234.dkr.ecr.eu-north-1.amazonaws.com
docker build -t route-guide .
docker tag route-guide:latest 123412341234.dkr.ecr.eu-north-1.amazonaws.com/route-guide:latest
docker push 123412341234.dkr.ecr.eu-north-1.amazonaws.com/route-guide:latest
在 Amazon ECS 控制台中,我使用 Networking only 模板创建一个新集群。我将该集群命名为 demo
并为该集群创建一个新的 VPC,将其他所有值保留为默认值。ECS 控制台使用 AWS CloudFormation 为集群设置资源。几分钟后,所有资源都已成功创建,且集群准备就绪。
为了授予对 gRPC 流量的访问权限,我创建了一个安全组,以允许来自客户端笔记本电脑的端口 50051
上的入站 TCP 流量。我将此安全组命名为 gRPC
。
现在,创建负载均衡器。在 EC2 控制台中,我选择左侧的负载均衡器,然后选择创建负载均衡器。下一步,我创建一个 Application Load Balancer 并将其命名为 route-guide
。我保留默认的 internet-facing Scheme,因为我希望通过公共互联网连接到 gRPC 服务。在侦听器部分中,我选择 HTTPS 作为协议,选择 50051
作为端口。在下面,我选择新创建的 VPC 和 ECS 控制台创建的两个可用区。
接下来,在安全设置中,我选择之前创建的由 AWS Certificate Manager (ACM) 管理的证书。
然后,我选择默认
安全组和上面创建的 gRPC
安全组。每个 VPC 都自动带有一个默认安全组,该安全组将提供对使用相同安全组的其他资源进行网络访问的权限。
在路由部分,我创建一个名为 route-guide
的新目标组。由于我计划使用 AWS Fargate 运行 gRPC 服务器,我选择 IP 地址目标类型。ALB 支持将安全和不安全连接用于使用 gRPC 协议的目标组。在这里,我使用 HTTP(因为我正在负载均衡器和运行 gRPC 服务器的容器之间使用不安全连接)与端口 50051。然后,我选择 ECS 集群使用的 VPC。对于协议版本,我使用 gRPC。
在高级运行状况检查设置中,我可以指定在检查正确响应时要使用哪些 gRPC 成功代码。我保留默认值 12
,这意味着未实现。
如果找不到方法,则 gRPC 服务器将返回代码 12
。它在这种情况下可行,是因为我使用的路径并非由 route_guide
应用程序实施。更概括地说,检查代码 12
是验证您的 gRPC 服务器是否正确运行的快速方法。具体而言,在您的运行状况检查中,您可以使用一个代码、一个列表或一个范围,具体取决于您期望从实施中获得什么。
在下一步中,我不注册任何目标和完成目标组的创建。
回到 ECS 控制台,我创建与 Fargate 启动类型兼容的新任务定义。我将它命名为 route-guide
并为它提供最少量的资源:0.5 GB 内存和 0.25 个 CPU 单位。我使用上传到 ECR 的容器映像的映像 URI、容器端口 50051/tcp
和上面的 Dockerfile 中公开的网络端口来添加容器定义。
选择 route-guide
任务定义后,我在操作菜单中选择创建服务。我将 Fargate 启动类型、route-guide
用作服务名称,2 用作任务数量。在下一步中,我在 VPC 中添加两个子网,然后选择默认
安全组以允许负载均衡器访问任务。在负载均衡部分中,我选择 Application Load Balancer 和 route-guide
负载均衡器。
对于要均衡的容器,我选择 route-guide:50051:50051
和添加到负载均衡器。
然后,我选择 route-guide
目标组。
在下一步中,我选择不调整服务的所需计数,以便不将 Auto Scaling 用于此演示。
我完成了 ECS 服务的创建。几分钟后,两个任务在运行中
。通过查看 route-guide
目标组的目标选项卡,我发现两个目标在正常运行。负载均衡器现已准备好接受流量。
在 Amazon Route 53 控制台中,我创建 DNS A Record 作为负载均衡器的别名。我使用的域与创建负载均衡器时选择的证书的域名匹配。
我在客户端笔记本电脑中,编辑 gRPC 存储库的的 route_client.py
文件,以在连接到负载均衡器时使用安全通道。下面是我更改的代码的一部分:
# create credentials
credentials = grpc.ssl_channel_credentials()
# create secure channel using credentials
with grpc.secure_channel('<ALB-ALIAS-DOMAIN>:50051', credentials) as channel:
stub = route_guide_pb2_grpc.RouteGuideStub(channel)
print("-------------- GetFeature --------------")
guide_get_feature(stub)
print("-------------- ListFeatures --------------")
guide_list_features(stub)
print("-------------- RecordRoute --------------")
guide_record_route(stub)
print("-------------- RouteChat --------------")
guide_route_chat(stub)
现在,我启动客户端,以使用一些工作负载来测试 gRPC 通道。在输出中,我看到 route_guide
应用程序在请求和响应中使用一元、服务器端流式处理、客户端流式处理和双向流式处理。
$ python3 route_guide_client.py
-------------- GetFeature --------------
Feature called Berkshire Valley Management Area Trail, Jefferson, NJ, USA at latitude: 409146138
longitude: -746188906
Found no feature at
-------------- ListFeatures --------------
Looking for features between 40, -75 and 42, -73
Feature called Patriots Path, Mendham, NJ 07945, USA at latitude: 407838351
longitude: -746143763
Feature called 101 New Jersey 10, Whippany, NJ 07981, USA at latitude: 408122808
longitude: -743999179
Feature called U.S.6, Shohola, PA 18458, USA at latitude: 413628156
longitude: -749015468
Feature called 5 Conners Road, Kingston, NY 12401, USA at latitude: 419999544
longitude: -740371136
Feature called Mid Hudson Psychiatric Center, New Hampton, NY 10958, USA at latitude: 414008389
longitude: -743951297
Feature called 287 Flugertown Road, Livingston Manor, NY 12758, USA at latitude: 419611318
longitude: -746524769
Feature called 4001 Tremley Point Road, Linden, NJ 07036, USA at latitude: 406109563
longitude: -742186778
Feature called 352 South Mountain Road, Wallkill, NY 12589, USA at latitude: 416802456
longitude: -742370183
Feature called Bailey Turn Road, Harriman, NY 10926, USA at latitude: 412950425
longitude: -741077389
Feature called 193-199 Wawayanda Road, Hewitt, NJ 07421, USA at latitude: 412144655
longitude: -743949739
Feature called 406-496 Ward Avenue, Pine Bush, NY 12566, USA at latitude: 415736605
longitude: -742847522
Feature called 162 Merrill Road, Highland Mills, NY 10930, USA at latitude: 413843930
longitude: -740501726
Feature called Clinton Road, West Milford, NJ 07480, USA at latitude: 410873075
longitude: -744459023
Feature called 16 Old Brook Lane, Warwick, NY 10990, USA at latitude: 412346009
longitude: -744026814
Feature called 3 Drake Lane, Pennington, NJ 08534, USA at latitude: 402948455
longitude: -747903913
Feature called 6324 8th Avenue, Brooklyn, NY 11220, USA at latitude: 406337092
longitude: -740122226
Feature called 1 Merck Access Road, Whitehouse Station, NJ 08889, USA at latitude: 406421967
longitude: -747727624
Feature called 78-98 Schalck Road, Narrowsburg, NY 12764, USA at latitude: 416318082
longitude: -749677716
Feature called 282 Lakeview Drive Road, Highland Lake, NY 12743, USA at latitude: 415301720
longitude: -748416257
Feature called 330 Evelyn Avenue, Hamilton Township, NJ 08619, USA at latitude: 402647019
longitude: -747071791
Feature called New York State Reference Route 987E, Southfields, NY 10975, USA at latitude: 412567807
longitude: -741058078
Feature called 103-271 Tempaloni Road, Ellenville, NY 12428, USA at latitude: 416855156
longitude: -744420597
Feature called 1300 Airport Road, North Brunswick Township, NJ 08902, USA at latitude: 404663628
longitude: -744820157
Feature called at latitude: 407113723
longitude: -749746483
Feature called at latitude: 402133926
longitude: -743613249
Feature called at latitude: 400273442
longitude: -741220915
Feature called at latitude: 411236786
longitude: -744070769
Feature called 211-225 Plains Road, Augusta, NJ 07822, USA at latitude: 411633782
longitude: -746784970
Feature called at latitude: 415830701
longitude: -742952812
Feature called 165 Pedersen Ridge Road, Milford, PA 18337, USA at latitude: 413447164
longitude: -748712898
Feature called 100-122 Locktown Road, Frenchtown, NJ 08825, USA at latitude: 405047245
longitude: -749800722
Feature called at latitude: 418858923
longitude: -746156790
Feature called 650-652 Willi Hill Road, Swan Lake, NY 12783, USA at latitude: 417951888
longitude: -748484944
Feature called 26 East 3rd Street, New Providence, NJ 07974, USA at latitude: 407033786
longitude: -743977337
Feature called at latitude: 417548014
longitude: -740075041
Feature called at latitude: 410395868
longitude: -744972325
Feature called at latitude: 404615353
longitude: -745129803
Feature called 611 Lawrence Avenue, Westfield, NJ 07090, USA at latitude: 406589790
longitude: -743560121
Feature called 18 Lannis Avenue, New Windsor, NY 12553, USA at latitude: 414653148
longitude: -740477477
Feature called 82-104 Amherst Avenue, Colonia, NJ 07067, USA at latitude: 405957808
longitude: -743255336
Feature called 170 Seven Lakes Drive, Sloatsburg, NY 10974, USA at latitude: 411733589
longitude: -741648093
Feature called 1270 Lakes Road, Monroe, NY 10950, USA at latitude: 412676291
longitude: -742606606
Feature called 509-535 Alphano Road, Great Meadows, NJ 07838, USA at latitude: 409224445
longitude: -748286738
Feature called 652 Garden Street, Elizabeth, NJ 07202, USA at latitude: 406523420
longitude: -742135517
Feature called 349 Sea Spray Court, Neptune City, NJ 07753, USA at latitude: 401827388
longitude: -740294537
Feature called 13-17 Stanley Street, West Milford, NJ 07480, USA at latitude: 410564152
longitude: -743685054
Feature called 47 Industrial Avenue, Teterboro, NJ 07608, USA at latitude: 408472324
longitude: -740726046
Feature called 5 White Oak Lane, Stony Point, NY 10980, USA at latitude: 412452168
longitude: -740214052
Feature called Berkshire Valley Management Area Trail, Jefferson, NJ, USA at latitude: 409146138
longitude: -746188906
Feature called 1007 Jersey Avenue, New Brunswick, NJ 08901, USA at latitude: 404701380
longitude: -744781745
Feature called 6 East Emerald Isle Drive, Lake Hopatcong, NJ 07849, USA at latitude: 409642566
longitude: -746017679
Feature called 1358-1474 New Jersey 57, Port Murray, NJ 07865, USA at latitude: 408031728
longitude: -748645385
Feature called 367 Prospect Road, Chester, NY 10918, USA at latitude: 413700272
longitude: -742135189
Feature called 10 Simon Lake Drive, Atlantic Highlands, NJ 07716, USA at latitude: 404310607
longitude: -740282632
Feature called 11 Ward Street, Mount Arlington, NJ 07856, USA at latitude: 409319800
longitude: -746201391
Feature called 300-398 Jefferson Avenue, Elizabeth, NJ 07201, USA at latitude: 406685311
longitude: -742108603
Feature called 43 Dreher Road, Roscoe, NY 12776, USA at latitude: 419018117
longitude: -749142781
Feature called Swan Street, Pine Island, NY 10969, USA at latitude: 412856162
longitude: -745148837
Feature called 66 Pleasantview Avenue, Monticello, NY 12701, USA at latitude: 416560744
longitude: -746721964
Feature called at latitude: 405314270
longitude: -749836354
Feature called at latitude: 414219548
longitude: -743327440
Feature called 565 Winding Hills Road, Montgomery, NY 12549, USA at latitude: 415534177
longitude: -742900616
Feature called 231 Rocky Run Road, Glen Gardner, NJ 08826, USA at latitude: 406898530
longitude: -749127080
Feature called 100 Mount Pleasant Avenue, Newark, NJ 07104, USA at latitude: 407586880
longitude: -741670168
Feature called 517-521 Huntington Drive, Manchester Township, NJ 08759, USA at latitude: 400106455
longitude: -742870190
Feature called at latitude: 400066188
longitude: -746793294
Feature called 40 Mountain Road, Napanoch, NY 12458, USA at latitude: 418803880
longitude: -744102673
Feature called at latitude: 414204288
longitude: -747895140
Feature called at latitude: 414777405
longitude: -740615601
Feature called 48 North Road, Forestburgh, NY 12777, USA at latitude: 415464475
longitude: -747175374
Feature called at latitude: 404062378
longitude: -746376177
Feature called at latitude: 405688272
longitude: -749285130
Feature called at latitude: 400342070
longitude: -748788996
Feature called at latitude: 401809022
longitude: -744157964
Feature called 9 Thompson Avenue, Leonardo, NJ 07737, USA at latitude: 404226644
longitude: -740517141
Feature called at latitude: 410322033
longitude: -747871659
Feature called at latitude: 407100674
longitude: -747742727
Feature called 213 Bush Road, Stone Ridge, NY 12484, USA at latitude: 418811433
longitude: -741718005
Feature called at latitude: 415034302
longitude: -743850945
Feature called at latitude: 411349992
longitude: -743694161
Feature called 1-17 Bergen Court, New Brunswick, NJ 08901, USA at latitude: 404839914
longitude: -744759616
Feature called 35 Oakland Valley Road, Cuddebackville, NY 12729, USA at latitude: 414638017
longitude: -745957854
Feature called at latitude: 412127800
longitude: -740173578
Feature called at latitude: 401263460
longitude: -747964303
Feature called at latitude: 412843391
longitude: -749086026
Feature called at latitude: 418512773
longitude: -743067823
Feature called 42-102 Main Street, Belford, NJ 07718, USA at latitude: 404318328
longitude: -740835638
Feature called at latitude: 419020746
longitude: -741172328
Feature called at latitude: 404080723
longitude: -746119569
Feature called at latitude: 401012643
longitude: -744035134
Feature called at latitude: 404306372
longitude: -741079661
Feature called at latitude: 403966326
longitude: -748519297
Feature called at latitude: 405002031
longitude: -748407866
Feature called at latitude: 409532885
longitude: -742200683
Feature called at latitude: 416851321
longitude: -742674555
Feature called 3387 Richmond Terrace, Staten Island, NY 10303, USA at latitude: 406411633
longitude: -741722051
Feature called 261 Van Sickle Road, Goshen, NY 10924, USA at latitude: 413069058
longitude: -744597778
Feature called at latitude: 418465462
longitude: -746859398
Feature called at latitude: 411733222
longitude: -744228360
Feature called 3 Hasta Way, Newton, NJ 07860, USA at latitude: 410248224
longitude: -747127767
-------------- RecordRoute --------------
Visiting point latitude: 400066188
longitude: -746793294
Visiting point latitude: 412452168
longitude: -740214052
Visiting point latitude: 401827388
longitude: -740294537
Visiting point latitude: 414777405
longitude: -740615601
Visiting point latitude: 409642566
longitude: -746017679
Visiting point latitude: 406685311
longitude: -742108603
Visiting point latitude: 406523420
longitude: -742135517
Visiting point latitude: 405047245
longitude: -749800722
Visiting point latitude: 418858923
longitude: -746156790
Visiting point latitude: 409532885
longitude: -742200683
Finished trip with 10 points
Passed 10 features
Travelled 863981 meters
It took 0 seconds
-------------- RouteChat --------------
Sending First message at
Sending Second message at longitude: 1
Sending Third message at latitude: 1
Sending Fourth message at
Sending Fifth message at latitude: 1
Received message First message at
Received message Third message at latitude: 1
这个输出很长,但正如我在本博客文章开头所说的那样,route-guide
演示显示了客户端和服务器可以使用 gRPC 进行交互的很多不同方式,而不仅仅是基本的 RPC 调用。
现已推出
端对端 HTTP/2 和 gRPC 支持现在所有区域为新的和现有 Application Load Balancer 推出。您可以通过控制台、AWS 命令行界面 (CLI)、AWS 开发工具包使用此功能。我们正在努力尽快增加 AWS CloudFormation 支持。
将 gRPC 协议与 ALB 结合使用不会产生额外费用。有关详细信息,您可以参见 Elastic Load Balancing 定价页面。
有了这些新功能,更加容易使用 gRPC 来集成您的应用程序,或改进客户端/服务器通信。要了解更多信息,请参阅文档。
– Danilo