ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

重新整理 .net core 实践篇—————grpc工具[三十四]

2021-07-01 08:01:53  阅读:213  来源: 互联网

标签:core License proto grpc 重新整理 add dotnet message


前言

简单整理一下grpc工具。

正文

工具核心包:

  1. Grpc.Tools 这个是项目要引用的包,用来生成cs代码的。

  2. dotnet-grpc 这个就是cli,命令行工具

dotnet-grpc 核心工具:

  1. dotnet grpc add-file

  2. dotnet grpc add-url

  3. dotnet grpc remove

  4. dotnet grpc refresh

有几种方式来管理proto:

  1. 使用单独的git仓库管理proto文件

  2. 使用submodule 将proto文件集成到工程目录中

  3. 使用dotnet-grpc 命令行添加到proto文件及相关依赖包引用

备注:

由proto 生成的代码文件会存放在obj目录,不会迁入到git仓库中。

最好的方式是使用单独的git仓库管理proto文件。

安装dotnet-grpc 工具:

dotnet tool install dotnet-grpc -g

然后就可以使用了。

然后客户端进行引用:

dotnet grpc add-file ../remoteSite/Proto/order.proto  -o Protos/order.proto

上面这个其实不怎么用。

那么来一个add-url例子:

dotnet grpc add-url https://raw.githubusercontent.com/grpc/grpc/master/examples/protos/helloworld.proto -o Protos/helloword.proto

这样就引用了远程的。

生成:

// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";

package helloworld;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

然后我们就可以去添加一些服务地址:

services.AddGrpcClient<Helloworld.Greeter.GreeterClient>(options =>
{
	options.Address = new Uri("https://localhost:5001");
}).ConfigurePrimaryHttpMessageHandler(provider =>
{
	var handle = new SocketsHttpHandler();
	handle.SslOptions.RemoteCertificateValidationCallback = (a, b, c, d) => true;
	return handle;
});

这样就可以了。

如果远程更新了,那么我们使用命令行更新。

dot-net grpc reflesh https://raw.githubusercontent.com/grpc/grpc/master/examples/protos/helloworld.proto

如果要移除的话:

dot-net grpc remove https://raw.githubusercontent.com/grpc/grpc/master/examples/protos/helloworld.proto

然后需要手动删除生成的这些东西。

下一节poll。

标签:core,License,proto,grpc,重新整理,add,dotnet,message
来源: https://www.cnblogs.com/aoximin/p/14957169.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有