1

我在 Dapr 中启动了一个具有 gRPC 服务的应用程序。使用 id MyGrpcApi001 启动 Dapr。HTTP 端口:55319。gRPC 端口:55320

我已经使用以下命令启动它:

dapr run --app-id MyGrpcApi001 --app-protocol grpc --app-port 5000 -- dotnet run

原型:

syntax = "proto3";

import "google/protobuf/empty.proto";

option csharp_namespace = "MyGrpcService";

package greet;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply);
  rpc Test (google.protobuf.Empty) returns (google.protobuf.Empty);
}

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

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

我有一个客户端,它有 2 种使用 gRPC 连接到服务的方法

  • 直接通过自动生成的客户端调用服务
  • 另一个通过 dapr sidecar gRPC 端口
    class Program
    {
        private static string LOCAL_ADDRESS = @"http://localhost:5000";
        private static string DAPR_ADDRESS = $"http://localhost:55320";

        static async Task Main(string[] args)
        {
            await CallToLocalHost().ConfigureAwait(false); // runs OK
            await CallWithClient().ConfigureAwait(false); // exception: Service is unimplemented
        }

        private static async Task CallToLocalHost()
        {
            using var channel = GrpcChannel.ForAddress(LOCAL_ADDRESS);
            var cl = new MyGrpcService.Greeter.GreeterClient(channel);
            MyGrpcService.HelloReply response = await cl.SayHelloAsync(new MyGrpcService.HelloRequest { Name = "ThinkPad" });
            Console.WriteLine(response.Message);
        }

        private static async Task CallWithClient()
        {
            using DaprClient client = new DaprClientBuilder()
                     .UseGrpcEndpoint(DAPR_ADDRESS)
                     .Build();

            var request = new MyGrpcService.HelloRequest { Name = "ThinkPad" };
            var result = await client.InvokeMethodGrpcAsync<MyGrpcService.HelloRequest, MyGrpcService.HelloReply>("MyGrpcApi001", "SayHello", request).ConfigureAwait(false);
        }
    }

第二种方法似乎不起作用,所以直接调用有效,但是sidecar似乎没有找到服务。以下是两者的日志:

正在运行的服务的日志:

第一种方法OK:

== APP == info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
== APP ==       Request starting HTTP/2 POST http://localhost:5000/greet.Greeter/SayHello application/grpc -
== APP == info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
== APP ==       Executing endpoint 'gRPC - /greet.Greeter/SayHello'
== APP == info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
== APP ==       Executed endpoint 'gRPC - /greet.Greeter/SayHello'
== APP == info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
== APP ==       Request finished HTTP/2 POST http://localhost:5000/greet.Greeter/SayHello application/grpc - - 200 - application/grpc 0.3074ms

第二种方法错误:

== APP == info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
== APP ==       Request starting HTTP/2 POST http://127.0.0.1:5000/dapr.proto.runtime.v1.AppCallback/OnInvoke application/grpc -
== APP == info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
== APP ==       Executing endpoint 'gRPC - Unimplemented service'
== APP == info: Grpc.AspNetCore.Server.Internal.ServerCallHandlerFactory[1]
== APP ==       Service 'dapr.proto.runtime.v1.AppCallback' is unimplemented.
== APP == info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
== APP ==       Executed endpoint 'gRPC - Unimplemented service'
== APP == info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
== APP ==       Request finished HTTP/2 POST http://127.0.0.1:5000/dapr.proto.runtime.v1.AppCallback/OnInvoke application/grpc - - 200 0 application/grpc 0.1474ms

第二个似乎有错误的 URL,其中包括/dapr.proto.runtime. ...

我在这里做错了什么?dapr run 命令是错误的,还是 InvokeMethodGrpcAsync 方法的参数错误?

由于我可以直接通过自动生成的客户端运行该方法,我认为服务器工作正常,是 dapr 没有找到服务和/或方法。

有任何想法吗?

谢谢!

4

0 回答 0