phpcms做视频网站,anew wordpress 下载,许昌做网站的公司,温州公司建设网站制作在单体应用中#xff0c;相互调用都是在一个进程内部调用#xff0c;也就是说调用发生在本机内部#xff0c;因此也被叫做本地方法调用#xff1b;在微服务中#xff0c;服务之间调用就变得比较复杂#xff0c;需要跨网络调用#xff0c;他们之间的调用相对于与本地方法… 在单体应用中相互调用都是在一个进程内部调用也就是说调用发生在本机内部因此也被叫做本地方法调用在微服务中服务之间调用就变得比较复杂需要跨网络调用他们之间的调用相对于与本地方法调用可称为远程过程调用简称RPCRemote procedure call。看过上篇API网关篇知道案例中包含商品、订单两个微服务本文将会演示如何采用开源的高性能rpc框架(grpc)通过订单微服务调用产品微服务内的接口。没有看过上篇文章不影响我先提供下项目代码结构图方便你阅读。下面将会一步一步分享如何使用Grpc进行服务之间调用。步骤1首先定义服务锲约-proto文件1.创建类库.NET Standard,作为服务契约项目命名为-AAStore.ProductCatalog.DataContracts如图2.安装三个nuget包Google.Protobuf
Grpc
Grpc.Tools
3.开始定义proto文件product.api.protosyntax span data-raw-text data-textnode-index11 data-index445 classcharacterproto3span data-raw-text data-textnode-index11 data-index452 classcharacter;optioncsharp_namespace span data-raw-text data-textnode-index15 data-index480 classcharacterAAStore.ProductCatalog.Api.V1span data-raw-text data-textnode-index15 data-index510 classcharacter;
packageAAStore;serviceProductApi{rpcGetProduct(GetProductRequest) returns(GetProductResponse);
}//请求消息体
messageGetProductRequest{
int32Id1;
}
//返回消息体
messageGetProductResponse{
stringproductName1;
}Grpc协议使用Protobuf简称proto文件来定义接口名、调用参数以及返回值类型。比如product.api.proto文件定义一个接口GetProduct方法它的请求结构体是GetProductRequest包含一个int类型的id属性它的返回结构体GetProductResponse包含一个输出string类型的产品名称属性。4.添加product.api.proto文件到项目中双击项目或者右键-》编辑项目文件添加一下代码ItemGroup
Protobuf Includespan data-raw-text data-textnode-index58 data-index972 classcharacter..\Schema\grpc\v1\product.api.protospan data-raw-text data-textnode-index58 data-index1008 classcharacterGrpcServicesspan data-raw-text data-textnode-index60 data-index1023 classcharacterBothspan data-raw-text data-textnode-index60 data-index1028 classcharacterLinkspan data-raw-text data-textnode-index62 data-index1035 classcharacterproduct.api.protospan data-raw-text data-textnode-index62 data-index1053 classcharacter/
/ItemGroup
然后build项目此时gprc代码已经生成了在obj文件项目如图步骤2Grpc服务端实现选择项目AAStore.ProductCatalog详情见项目代码结构图安装包Grpc.AspNetCore同时添加引用项目AAStore.ProductCatalog.DataContractsItemGroup
PackageReference Includespan data-raw-text data-textnode-index74 data-index1262 classcharacterGrpc.AspNetCorespan data-raw-text data-textnode-index74 data-index1278 classcharacterVersionspan data-raw-text data-textnode-index76 data-index1288 classcharacter2.29.0span data-raw-text data-textnode-index76 data-index1295 classcharacter/
/ItemGroupItemGroup
ProjectReference Includespan data-raw-text data-textnode-index85 data-index1356 classcharacter..\AAStore.ProductCatalog.DataContracts\AAStore.ProductCatalog.DataContracts.csprojspan data-raw-text data-textnode-index85 data-index1440 classcharacter/
/ItemGroup然后定义获取产品方法的逻辑和实现供产品api站点项目调用publicTaskGetProductResponse GetProduct(GetProductRequest request, ServerCallContext context)
{
returnTask.FromResult(newGetProductResponse
{
//todo 具体的逻辑下面代码仅为显示
ProductName span data-raw-text data-textnode-index110 data-index1730 classcharacter测试商品grpcspan data-raw-text data-textnode-index112 data-index1739 classcharacter
}) ;
}
选择AAStore.ProductCatalog.Api产品api项目添加引用项目AAStore.ProductCatalogItemGroup
ProjectReference Includespan data-raw-text data-textnode-index121 data-index1871 classcharacter..\AAStore.ProductCatalog\AAStore.ProductCatalog.csprojspan data-raw-text data-textnode-index121 data-index1927 classcharacter/
/ItemGroup
新增ProductServices.cs并继承ProductApiBase类实现grpc服务public classProductServices : ProductApi.ProductApiBase
{
public override TaskGetProductResponse GetProduct(GetProductRequest request, ServerCallContext context)
{
return newGrpcProductServices().GetProduct(request, context);
}
}
由于AAStore.ProductCatalog.Api项目不是通过grpc模板项目创建的所以在Startup类中手工添加gprc服务和中间件代码public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddGrpc();
}app.UseEndpoints(endpoints
{
endpoints.MapGrpcServiceProductServices();
endpoints.MapControllers();
});
最后在Program文件配置站点启动监听8081端口我们并运行它webBuilder.ConfigureKestrel(options
{
// Setup a HTTP/2 endpoint without TLS.
options.ListenLocalhost(8081, o o.Protocols
HttpProtocols.Http2);
});
步骤3Grpc客户端实现-调用Grpc服务选择AAStore.Orde项目具体见项目代码结构图安装Grpc.AspNetCore包并且添加项目引用AAStore.ProductCatalog.DataContractsItemGroup
ProjectReference Includespan data-raw-text data-textnode-index171 data-index3123 classcharacter..\AAStore.ProductCatalog.DataContracts\AAStore.ProductCatalog.DataContracts.csprojspan data-raw-text data-textnode-index171 data-index3207 classcharacter/
/ItemGroupItemGroup
PackageReference Includespan data-raw-text data-textnode-index180 data-index3268 classcharacterGrpc.AspNetCorespan data-raw-text data-textnode-index180 data-index3284 classcharacterVersionspan data-raw-text data-textnode-index182 data-index3294 classcharacter2.29.0span data-raw-text data-textnode-index182 data-index3301 classcharacter/
/ItemGroup新建ProductGateway.cs,封装产品微服务公开grpc服务的调用publicclassProductGateway
{
privatereadonlyProductApiClient _client;
publicProductGateway()
{
AppContext.SetSwitch(span data-raw-text data-textnode-index206 data-index3514 classcharacterSystem.Net.Http.SocketsHttpHandler.Http2UnencryptedSupportspan data-raw-text data-textnode-index206 data-index3573 classcharacter, true);
_client newProductApiClient(GrpcChannel.ForAddress(span data-raw-text data-textnode-index213 data-index3648 classcharacterhttp://localhost:8081span data-raw-text data-textnode-index213 data-index3670 classcharacter));//TODO 根据动态配置
}publicasyncTaskGetProductResponse GetProduct(GetProductRequest request)
{
returnawait_client.GetProductAsync(request);
}
}
新建OrderService.cs添加GetOrder方法在其方法内调用产品微服务GetProduct方法publicasyncTaskstring GetOrder()
{
varproductModel await_productGateway.GetProduct(newGetProductRequest() { Id 1});
return$span data-raw-text data-textnode-index256 data-index4081 classcharacterOrder Service从产品微服务获取产品信息{productModel.ProductName}span data-raw-text data-textnode-index259 data-index4136 classcharacter;
}
然后在订单控制器中调用[Route(span data-raw-text data-textnode-index264 data-index4167 classcharacterapi/[controller]span data-raw-text data-textnode-index264 data-index4184 classcharacter)]
[ApiController]
publicclassOrderController: ControllerBase
{
privatereadonlyRestOrderService _restOrderService;
publicOrderController()
{
_restOrderService newRestOrderService();
}
[HttpGet(template:span data-raw-text data-textnode-index294 data-index4451 classcharacterGetspan data-raw-text data-textnode-index294 data-index4455 classcharacter)]
publicasyncTaskstring GetOrder()
{
returnawait_restOrderService.GetOrder();
}
}
至此客户端代码已经完成我们运行来看看通过网关访问订单服务查看调用结果我们发现结果也是一样的以上演示了如何使用grpc进行服务间的调用最后使用一张图作结。