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

前言

简单整理一下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。

原文地址:https://www.cnblogs.com/aoximin/p/14957169.html