简体   繁体   English

从.net Core中的webapi调用grpc服务的策略

[英]Strategies for calling grpc service from a webapi in .net Core

I have a rest/web API in .Net Core that I am using as an aggregation service to call some underlying gRPC services also in .Net core . 我在.Net Core中有一个rest / web API ,我正在使用它作为聚合服务来在.Net core调用某些基础gRPC services For the WEB API client, I am creating the channel once as a singleton on startup and injecting it where I need it using dependency injection. 对于WEB API客户端,我将在启动时作为一个单例创建一次通道,并使用依赖项注入将其注入到需要的位置。 I am also thinking of creating the clients to the grpc services on startup and then injecting them. 我也正在考虑在启动时为grpc services创建客户端,然后注入它们。 Is creating the channel and the clients on Startup the correct strategy or should I create and close the channel and clients on every requests of the web API ? 是在启动时创建通道和客户端的正确策略,还是应该针对web API每个请求创建并关闭通道和客户端? Furthermore, if creating the channel once is the correct strategy how do I ensure that the channel is closed on shutdown....Both the web API/rest service and the grpc services will be running on a Kubernetes Cluster as docker containers. 此外,如果一次创建通道是正确的策略,那么我如何确保通道在关闭时关闭.... web API/rest服务和grpc services都将作为Kubernetes Cluster容器在Kubernetes Cluster上运行。

You definitely shouldn't create a new channel for every call you make. 您绝对不应该为您拨打的每个电话创建一个新渠道。 gRPC channels are supposed to be long-lived and you'll generally get the best performance and resource utilization if you create a channel once and then keep invoking subsequent calls on that channel. gRPC通道被认为是长期存在的,并且如果您一次创建一个通道,然后继续在该通道上调用后续调用,通常将获得最佳性能和资源利用率。 The exact scope at which you create the channel depends on your application - but creating the channel only once at application startup is one of the possible approaches (also note that TCP/IP connections are created lazily - a new connection won't be created until you start a first call on that channel). 创建通道的确切范围取决于您的应用程序-但是在应用程序启动时仅创建一次通道是可能的方法之一(还要注意,TCP / IP连接是延迟创建的-在创建新连接之前,您在该频道上开始第一个通话)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM